Skip to content

Commit

Permalink
some cleanup and API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
EliphasNUIT committed Oct 5, 2023
1 parent 074e0e7 commit 6e57888
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 103 deletions.
41 changes: 21 additions & 20 deletions GW2EIDPSReport/DPSReportController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
Expand All @@ -18,6 +17,8 @@ namespace GW2EIDPSReport
public static class DPSReportController
{

public delegate void TraceHandler(string trace);

private static bool IsUserTokenValid(string userToken)
{
return userToken != null && userToken.Length > 0;
Expand Down Expand Up @@ -95,7 +96,7 @@ private static string GetGetUploadsURL(GetUploadsParameters parameters, string u
return GetURL(url, userToken);
}
///////////////// APIs
public static DPSReportUploadObject UploadUsingEI(FileInfo fi, List<string> traces, string userToken, bool anonymous = false, bool detailedWvW = false)
public static DPSReportUploadObject UploadUsingEI(FileInfo fi, TraceHandler traceHandler, string userToken, bool anonymous = false, bool detailedWvW = false)
{
string fileName = fi.Name;
byte[] fileContents = File.ReadAllBytes(fi.FullName);
Expand All @@ -108,68 +109,68 @@ public static DPSReportUploadObject UploadUsingEI(FileInfo fi, List<string> trac
return multiPartContent;
};

DPSReportUploadObject response = GetDPSReportResponse<DPSReportUploadObject>("UploadUsingEI", GetUploadContentURL(BaseUploadContentURL, userToken, anonymous, detailedWvW) + " & generator=ei", traces, contentCreator);
DPSReportUploadObject response = GetDPSReportResponse<DPSReportUploadObject>("UploadUsingEI", GetUploadContentURL(BaseUploadContentURL, userToken, anonymous, detailedWvW) + " & generator=ei", traceHandler, contentCreator);
if (response != null && response.Error != null)
{
traces.Add("DPSReport: UploadUsingEI failed - " + response.Error);
traceHandler("DPSReport: UploadUsingEI failed - " + response.Error);
return null;
}
return response;
}

public static DPSReportGetUploadsObject GetUploads(List<string> traces, string userToken, GetUploadsParameters parameters)
public static DPSReportGetUploadsObject GetUploads(TraceHandler traceHandler, string userToken, GetUploadsParameters parameters)
{
return GetDPSReportResponse<DPSReportGetUploadsObject>("GetUploads", GetGetUploadsURL(parameters, userToken), traces);
return GetDPSReportResponse<DPSReportGetUploadsObject>("GetUploads", GetGetUploadsURL(parameters, userToken), traceHandler);
}
public static string GenerateUserToken(List<string> traces)
public static string GenerateUserToken(TraceHandler traceHandler)
{
DPSReportUserTokenResponse responseItem = GetDPSReportResponse<DPSReportUserTokenResponse>("GenerateUserToken", BaseGetUserTokenURL, traces);
DPSReportUserTokenResponse responseItem = GetDPSReportResponse<DPSReportUserTokenResponse>("GenerateUserToken", BaseGetUserTokenURL, traceHandler);
if (responseItem != null)
{
return responseItem.UserToken;
}
return "";
}
public static DPSReportUploadObject GetUploadMetaDataWithID(string id, List<string> traces)
public static DPSReportUploadObject GetUploadMetaDataWithID(string id, TraceHandler traceHandler)
{
if (id == null || id.Length == 0)
{
throw new InvalidDataException("Missing ID for GetUploadMetaData end point");
}
return GetDPSReportResponse<DPSReportUploadObject>("GetUploadMetaDataWithID", BaseGetUploadMetadataURL + "id=" + id, traces);
return GetDPSReportResponse<DPSReportUploadObject>("GetUploadMetaDataWithID", BaseGetUploadMetadataURL + "id=" + id, traceHandler);
}
public static DPSReportUploadObject GetUploadMetaDataWithPermalink(string permalink, List<string> traces)
public static DPSReportUploadObject GetUploadMetaDataWithPermalink(string permalink, TraceHandler traceHandler)
{
if (permalink == null || permalink.Length == 0)
{
throw new InvalidDataException("Missing Permalink for GetUploadMetaData end point");
}
return GetDPSReportResponse<DPSReportUploadObject>("GetUploadMetaDataWithPermalink", BaseGetUploadMetadataURL + "permalink=" + permalink, traces);
return GetDPSReportResponse<DPSReportUploadObject>("GetUploadMetaDataWithPermalink", BaseGetUploadMetadataURL + "permalink=" + permalink, traceHandler);
}

public static T GetJsonWithID<T>(string id, List<string> traces)
public static T GetJsonWithID<T>(string id, TraceHandler traceHandler)
{
if (id == null || id.Length == 0)
{
throw new InvalidDataException("Missing ID for GetJson end point");
}
return GetDPSReportResponse<T>("GetJsonWithID", BaseGetJsonURL + "id=" + id, traces);
return GetDPSReportResponse<T>("GetJsonWithID", BaseGetJsonURL + "id=" + id, traceHandler);
}
public static T GetJsonWithPermalink<T>(string permalink, List<string> traces)
public static T GetJsonWithPermalink<T>(string permalink, TraceHandler traceHandler)
{
if (permalink == null || permalink.Length == 0)
{
throw new InvalidDataException("Missing Permalink for GetJson end point");
}
return GetDPSReportResponse<T>("GetJsonWithPermalink", BaseGetJsonURL + "permalink=" + permalink, traces);
return GetDPSReportResponse<T>("GetJsonWithPermalink", BaseGetJsonURL + "permalink=" + permalink, traceHandler);
}
///////////////// Response Utilities
private static T GetDPSReportResponse<T>(string requestName, string URI, List<string> traces, Func<HttpContent> content = null)
private static T GetDPSReportResponse<T>(string requestName, string URI, TraceHandler traceHandler, Func<HttpContent> content = null)
{
const int tentatives = 5;
for (int i = 0; i < tentatives; i++)
{
traces.Add("DPSReport: " + requestName + " tentative");
traceHandler("DPSReport: " + requestName + " tentative");
var webService = new Uri(@URI);
var requestMessage = new HttpRequestMessage(HttpMethod.Post, webService);
requestMessage.Headers.ExpectContinue = false;
Expand Down Expand Up @@ -200,13 +201,13 @@ private static T GetDPSReportResponse<T>(string requestName, string URI, List<st
ContractResolver = DefaultJsonContractResolver,
StringEscapeHandling = StringEscapeHandling.EscapeHtml
});
traces.Add("DPSReport: " + requestName + " tentative successful");
traceHandler("DPSReport: " + requestName + " tentative successful");
return item;
}
}
catch (Exception e)
{
traces.Add("DPSReport: " + requestName + " tentative failed - " + e.Message);
traceHandler("DPSReport: " + requestName + " tentative failed - " + e.Message);
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion GW2EIDPSReport/GW2EIDPSReport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<Version>4.1.0</Version>
<Version>5.0.0</Version>
<Authors>EliphasNUIT</Authors>
<Company>GW2 Elite Insights</Company>
<Product>GW2 Elite Insights</Product>
Expand Down
16 changes: 14 additions & 2 deletions GW2EIParser/OperationControllers/OperationController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using GW2EIEvtcParser;
using GW2EIEvtcParser.EncounterLogic;
using GW2EIEvtcParser.ParsedData;

namespace GW2EIParser
{
Expand Down Expand Up @@ -63,7 +63,10 @@ public OperationBasicMetaData(ParsedEvtcLog log)
/// <summary>
/// Time elapsed parsing
/// </summary>
public string Elapsed { get; set; } = "";
public string Elapsed { get; private set; } = "";


private readonly Stopwatch _stopWatch = new Stopwatch();

public OperationController(string location, string status)
{
Expand All @@ -84,6 +87,15 @@ public override void Reset()
OpenableFiles.Clear();
}

public void Start() {
_stopWatch.Start();
}

public void Stop() {
_stopWatch.Stop();
Elapsed = ("Elapsed " + _stopWatch.ElapsedMilliseconds + " ms");
}

public void FinalizeStatus(string prefix)
{
StatusList.Insert(0, Elapsed);
Expand Down
69 changes: 32 additions & 37 deletions GW2EIParser/ProgramHelper.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Discord;
using GW2EIBuilders;
using GW2EIDiscord;
using GW2EIDPSReport;
using GW2EIDPSReport.DPSReportJsons;
using GW2EIWingman;
using GW2EIWingman.WingmanUploadJsons;
using GW2EIEvtcParser;
using GW2EIEvtcParser.EIData;
using GW2EIGW2API;
using GW2EIParser.Exceptions;
using static GW2EIEvtcParser.ParserHelper;

namespace GW2EIParser
{
Expand Down Expand Up @@ -103,7 +98,7 @@ public static bool ParseMultipleLogs()
{
if (Properties.Settings.Default.ParseMultipleLogs)
{
if (!HasFormat() && Properties.Settings.Default.UploadToDPSReports)
if (!HasFormat() && (Properties.Settings.Default.UploadToDPSReports || (false && Properties.Settings.Default.UploadToWingman)))
{
return false;
}
Expand All @@ -112,24 +107,24 @@ public static bool ParseMultipleLogs()
return false;
}

private static string[] UploadOperation(List<string> traces, FileInfo fInfo, ParsedEvtcLog log)
private static string[] UploadOperation(FileInfo fInfo, ParsedEvtcLog originalLog, OperationController originalController)
{
// Only upload supported 5 men, 10 men and golem logs, without anonymous players
var isWingmanCompatible = !log.ParserSettings.AnonymousPlayers && (
log.FightData.Logic.Mode == GW2EIEvtcParser.EncounterLogic.FightLogic.ParseMode.Instanced10 ||
log.FightData.Logic.Mode == GW2EIEvtcParser.EncounterLogic.FightLogic.ParseMode.Instanced5 ||
log.FightData.Logic.Mode == GW2EIEvtcParser.EncounterLogic.FightLogic.ParseMode.Benchmark
var isWingmanCompatible = !originalLog.ParserSettings.AnonymousPlayers && (
originalLog.FightData.Logic.Mode == GW2EIEvtcParser.EncounterLogic.FightLogic.ParseMode.Instanced10 ||
originalLog.FightData.Logic.Mode == GW2EIEvtcParser.EncounterLogic.FightLogic.ParseMode.Instanced5 ||
originalLog.FightData.Logic.Mode == GW2EIEvtcParser.EncounterLogic.FightLogic.ParseMode.Benchmark
);
//Upload Process
string[] uploadresult = new string[2] { "", "" };
if (Properties.Settings.Default.UploadToDPSReports)
{
traces.Add("Uploading to DPSReport using EI");
DPSReportUploadObject response = DPSReportController.UploadUsingEI(fInfo, traces, Properties.Settings.Default.DPSReportUserToken,
log.ParserSettings.AnonymousPlayers,
log.ParserSettings.DetailedWvWParse);
originalController.UpdateProgressWithCancellationCheck("Uploading to DPSReport using EI");
DPSReportUploadObject response = DPSReportController.UploadUsingEI(fInfo, str => originalController.UpdateProgress(str), Properties.Settings.Default.DPSReportUserToken,
originalLog.ParserSettings.AnonymousPlayers,
originalLog.ParserSettings.DetailedWvWParse);
uploadresult[0] = response != null ? response.Permalink : "Upload process failed";
traces.Add("DPSReports using EI: " + uploadresult[0]);
originalController.UpdateProgressWithCancellationCheck("DPSReports using EI: " + uploadresult[0]);
/*
if (Properties.Settings.Default.UploadToWingman)
{
Expand All @@ -150,12 +145,13 @@ private static string[] UploadOperation(List<string> traces, FileInfo fInfo, Par
#if !DEBUG
if (!isWingmanCompatible)
{
traces.Add("Can not upload to Wingman: unsupported log");
originalController.UpdateProgressWithCancellationCheck("Can not upload to Wingman: unsupported log");
}
else
{
string accName = log.LogData.PoV != null ? log.LogData.PoVAccount : null;
if (WingmanController.CheckUploadPossible(fInfo, accName, traces, ParserVersion))
string accName = originalLog.LogData.PoV != null ? originalLog.LogData.PoVAccount : null;

if (WingmanController.CheckUploadPossible(fInfo, accName, str => originalController.UpdateProgress(str), ParserVersion))
{
try
{
Expand All @@ -166,14 +162,15 @@ private static string[] UploadOperation(List<string> traces, FileInfo fInfo, Par
true,
Properties.Settings.Default.CustomTooShort,
Properties.Settings.Default.DetailledWvW);
ParsedEvtcLog logToUse = log;
if (log.ParserSettings.ComputeDamageModifiers != expectedSettings.ComputeDamageModifiers ||
log.ParserSettings.ParsePhases != expectedSettings.ParsePhases ||
log.ParserSettings.ParseCombatReplay != expectedSettings.ParseCombatReplay)
ParsedEvtcLog logToUse = originalLog;
if (originalLog.ParserSettings.ComputeDamageModifiers != expectedSettings.ComputeDamageModifiers ||
originalLog.ParserSettings.ParsePhases != expectedSettings.ParsePhases ||
originalLog.ParserSettings.ParseCombatReplay != expectedSettings.ParseCombatReplay)
{
// We need to create a parser that matches Wingman's expected settings
var parser = new EvtcParser(expectedSettings, APIController);
logToUse = parser.ParseLog(new ConsoleOperationController(fInfo.FullName), fInfo, out GW2EIEvtcParser.ParserHelpers.ParsingFailureReason failureReason, Properties.Settings.Default.MultiThreaded);
originalController.UpdateProgressWithCancellationCheck("Wingman: Setting mismatch, creating a new ParsedEvtcLog");
logToUse = parser.ParseLog(originalController, fInfo, out GW2EIEvtcParser.ParserHelpers.ParsingFailureReason failureReason, Properties.Settings.Default.MultiThreaded);
}
byte[] jsonFile, htmlFile;
var uploadResult = new UploadResults();
Expand All @@ -196,18 +193,23 @@ private static string[] UploadOperation(List<string> traces, FileInfo fInfo, Par
sw.Close();
htmlFile = ms.ToArray();
}
WingmanController.UploadProcessed(fInfo, accName, jsonFile, htmlFile, traces, ParserVersion);
if (logToUse != originalLog)
{
originalController.UpdateProgressWithCancellationCheck("Wingman: new ParsedEvtcLog processing completed");
}
WingmanController.UploadProcessed(fInfo, accName, jsonFile, htmlFile, str => originalController.UpdateProgress(str), ParserVersion);
}
catch (Exception e)
{
traces.Add("Can not upload to Wingman: " + e.Message);
originalController.UpdateProgressWithCancellationCheck("Can not upload to Wingman: " + e.Message);
}
}
else
{
traces.Add("Can not upload to Wingman: log already uploaded");
originalController.UpdateProgressWithCancellationCheck("Can not upload to Wingman: log already uploaded");
}
}
originalController.UpdateProgressWithCancellationCheck("Wingman: operation completed");
#endif

}
Expand All @@ -220,10 +222,9 @@ public static void DoWork(OperationController operation)
Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo("en-US");
operation.Reset();
var sw = new Stopwatch();
try
{
sw.Start();
operation.Start();
var fInfo = new FileInfo(operation.InputFile);

var parser = new EvtcParser(new EvtcParserSettings(Properties.Settings.Default.Anonymous,
Expand All @@ -242,12 +243,7 @@ public static void DoWork(OperationController operation)
failureReason.Throw();
}
operation.BasicMetaData = new OperationController.OperationBasicMetaData(log);
var externalTraces = new List<string>();
string[] uploadStrings = UploadOperation(externalTraces, fInfo, log);
foreach (string trace in externalTraces)
{
operation.UpdateProgressWithCancellationCheck(trace);
}
string[] uploadStrings = UploadOperation(fInfo, log, operation);
if (Properties.Settings.Default.SendEmbedToWebhook && Properties.Settings.Default.UploadToDPSReports)
{
if (Properties.Settings.Default.SendSimpleMessageToWebhook)
Expand All @@ -274,10 +270,9 @@ public static void DoWork(OperationController operation)
}
finally
{
sw.Stop();
operation.Stop();
GC.Collect();
Thread.CurrentThread.CurrentCulture = before;
operation.Elapsed = ("Elapsed " + sw.ElapsedMilliseconds + " ms");
}
}

Expand Down
2 changes: 1 addition & 1 deletion GW2EIWingman/GW2EIWingman.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<Version>0.1.0</Version>
<Version>0.2.0</Version>
<Authors>EliphasNUIT</Authors>
<Company>GW2 Elite Insights</Company>
<Product>GW2 Elite Insights</Product>
Expand Down
Loading

0 comments on commit 6e57888

Please sign in to comment.