From a9ce5be2669fb9c04e42c1cd98ed20b4c3dd151b Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Tue, 20 Dec 2022 18:33:09 +0100 Subject: [PATCH] fix mission fails --- EDPlayerJournal/BGS/InfluenceSupport.cs | 4 +- EDPlayerJournal/BGS/MissionCompleted.cs | 8 +-- EDPlayerJournal/BGS/MissionFailed.cs | 15 ++-- EDPlayerJournal/BGS/Report.cs | 2 +- EDPlayerJournal/BGS/TransactionParser.cs | 72 +++++++++++++++----- EliteBGS/LogGenerator/FailedMissionFormat.cs | 22 ++++-- 6 files changed, 84 insertions(+), 39 deletions(-) diff --git a/EDPlayerJournal/BGS/InfluenceSupport.cs b/EDPlayerJournal/BGS/InfluenceSupport.cs index 90c3bd8..3e9e249 100644 --- a/EDPlayerJournal/BGS/InfluenceSupport.cs +++ b/EDPlayerJournal/BGS/InfluenceSupport.cs @@ -17,9 +17,9 @@ public class InfluenceSupport : Transaction { public MissionCompletedEntry? RelevantMission { get; set; } /// - /// Mission accepted entry + /// Mission information /// - public MissionAcceptedEntry? AcceptedEntry { get; set; } + public Mission? Mission { get; set; } public override DateTime? CompletedAtDateTime { get { diff --git a/EDPlayerJournal/BGS/MissionCompleted.cs b/EDPlayerJournal/BGS/MissionCompleted.cs index 67ab66f..e5b87ae 100644 --- a/EDPlayerJournal/BGS/MissionCompleted.cs +++ b/EDPlayerJournal/BGS/MissionCompleted.cs @@ -9,6 +9,8 @@ public class MissionCompleted : Transaction { public MissionAcceptedEntry? AcceptedEntry { get; set; } + public Mission? Mission { get; set; } + public MissionCompleted() { } public override DateTime? CompletedAtDateTime { @@ -45,15 +47,13 @@ public class MissionCompleted : Transaction { /// public override bool SystemContribution { get { - if (AcceptedEntry == null || - AcceptedEntry.Mission == null || - AcceptedEntry.Mission.Name == null) { + if (Mission == null || Mission.Name == null) { return false; } // If the mission starts with the name for thargoid war mission // names, we assume its a system contribution - if (AcceptedEntry.Mission.Name.Contains("Mission_TW_")) { + if (Mission.Name.Contains("Mission_TW_")) { return true; } diff --git a/EDPlayerJournal/BGS/MissionFailed.cs b/EDPlayerJournal/BGS/MissionFailed.cs index 56abb37..b642ead 100644 --- a/EDPlayerJournal/BGS/MissionFailed.cs +++ b/EDPlayerJournal/BGS/MissionFailed.cs @@ -4,16 +4,13 @@ using EDPlayerJournal.Entries; namespace EDPlayerJournal.BGS; public class MissionFailed : Transaction { public MissionFailedEntry? Failed { get; set; } - public MissionAcceptedEntry? Accepted { get; set; } + public Mission? Mission { get; set; } public MissionFailed() { } - public MissionFailed(MissionAcceptedEntry accepted) { - if (accepted.Mission == null) { - throw new Exception("Mission cannot be null"); - } - Accepted = accepted; - Faction = accepted.Mission.Faction; + public MissionFailed(MissionFailedEntry failed) { + Entries.Add(failed); + Failed = failed; } public override int CompareTo(Transaction? other) { @@ -46,13 +43,13 @@ public class MissionFailed : Transaction { public override string ToString() { StringBuilder builder = new StringBuilder(); - if (Failed == null || Accepted == null) { + if (Failed == null || Mission == null) { return ""; } builder.AppendFormat("{0}x Mission failed: \"{1}\"", Amount, - Failed?.Mission?.FriendlyName + Mission?.FriendlyName ); return builder.ToString(); diff --git a/EDPlayerJournal/BGS/Report.cs b/EDPlayerJournal/BGS/Report.cs index 61f0305..6db8376 100644 --- a/EDPlayerJournal/BGS/Report.cs +++ b/EDPlayerJournal/BGS/Report.cs @@ -450,7 +450,7 @@ public class Report { continue; } - results.Add(new MissionFailed(accepted) { + results.Add(new MissionFailed(failed) { Failed = failed, System = accepted_system, Station = accepted_station, diff --git a/EDPlayerJournal/BGS/TransactionParser.cs b/EDPlayerJournal/BGS/TransactionParser.cs index e0d147e..bf1a6dc 100644 --- a/EDPlayerJournal/BGS/TransactionParser.cs +++ b/EDPlayerJournal/BGS/TransactionParser.cs @@ -1,4 +1,5 @@ -using EDPlayerJournal.Entries; +using EDPlayerJournal; +using EDPlayerJournal.Entries; namespace EDPlayerJournal.BGS; @@ -52,7 +53,7 @@ internal class TransactionParserContext { /// /// A list of accepted missions index by their mission ID /// - public Dictionary AcceptedMissions { get; } = new(); + public Dictionary AcceptedMissions { get; } = new(); public Dictionary AcceptedMissionLocation { get; } = new(); /// /// A way to lookup a system by its system id @@ -187,16 +188,24 @@ internal class TransactionParserContext { return SystemFactions[system]; } - public void MissionAccepted(MissionAcceptedEntry accepted) { + public void MissionAccepted(MissionAcceptedEntry? entry) { + if (entry == null) { + return; + } + + MissionAccepted(entry.Mission); + } + + public void MissionAccepted(Mission? mission) { if (CurrentSystem == null || CurrentSystemAddress == null) { throw new Exception("Mission accepted without knowing where."); } - if (accepted.Mission == null) { + if (mission == null) { throw new Exception("Mission is null"); } - AcceptedMissions.TryAdd(accepted.Mission.MissionID, accepted); + AcceptedMissions.TryAdd(mission.MissionID, mission); Location location = new() { StarSystem = CurrentSystem, @@ -204,7 +213,7 @@ internal class TransactionParserContext { Station = (CurrentStation ?? ""), }; - AcceptedMissionLocation.TryAdd(accepted.Mission.MissionID, location); + AcceptedMissionLocation.TryAdd(mission.MissionID, location); } } @@ -420,6 +429,35 @@ internal class CommitCrimeParser : TransactionParserPart { } } +internal class MissionsParser : TransactionParserPart { + public void Parse(Entry entry, TransactionParserContext context, TransactionList transactions) { + MissionsEntry? missions = entry as MissionsEntry; + + if (missions == null) { + return; + } + + if (context.CurrentSystem == null || context.CurrentSystemAddress == null) { + transactions.AddIncomplete(new MissionCompleted(), + "Could not determine current location on Missions event.", + entry + ); + return; + } + + foreach (Mission mission in missions.Active) { + try { + context.MissionAccepted(mission); + } catch (Exception exception) { + transactions.AddIncomplete(new MissionCompleted(), + exception.Message, + entry + ); + } + } + } +} + internal class MissionAcceptedParser : TransactionParserPart { public void Parse(Entry e, TransactionParserContext context, TransactionList transactions) { MissionAcceptedEntry? entry = e as MissionAcceptedEntry; @@ -453,13 +491,13 @@ internal class MissionCompletedParser : TransactionParserPart { throw new NotImplementedException(); } - MissionAcceptedEntry? accepted = null; + Mission? mission = null; Location? accepted_location = null; string? target_faction_name = entry.Mission.TargetFaction; string? source_faction_name = entry.Mission.Faction; // We did not find when the mission was accepted. - if (!context.AcceptedMissions.TryGetValue(entry.Mission.MissionID, out accepted)) { + if (!context.AcceptedMissions.TryGetValue(entry.Mission.MissionID, out mission)) { transactions.AddIncomplete(new MissionCompleted(), String.Format("Mission acceptance for mission id {0} was not found", entry.Mission.MissionID), e); @@ -529,7 +567,7 @@ internal class MissionCompletedParser : TransactionParserPart { // for the source system. So we make a full mission completed entry. transactions.Add(new MissionCompleted() { CompletedEntry = entry, - AcceptedEntry = accepted, + Mission = mission, System = accepted_location.StarSystem, Faction = source_faction_name, SystemAddress = accepted_location.SystemAddress, @@ -543,7 +581,7 @@ internal class MissionCompletedParser : TransactionParserPart { // differs. Sometimes missions go to different systems but to // the same faction. transactions.Add(new InfluenceSupport() { - AcceptedEntry = accepted, + Mission = mission, Faction = faction, Influence = influences.Value, System = system, @@ -559,7 +597,7 @@ internal class MissionCompletedParser : TransactionParserPart { internal class MissionFailedParser : TransactionParserPart { public void Parse(Entry e, TransactionParserContext context, TransactionList transactions) { - MissionAcceptedEntry? accepted = null; + Mission? mission = null; Location? accepted_location = null; string? accepted_system = null; @@ -572,14 +610,14 @@ internal class MissionFailedParser : TransactionParserPart { throw new InvalidJournalEntryException("No mission specified in mission failure"); } - if (!context.AcceptedMissions.TryGetValue(entry.Mission.MissionID, out accepted)) { + if (!context.AcceptedMissions.TryGetValue(entry.Mission.MissionID, out mission)) { transactions.AddIncomplete(new MissionFailed(), "Mission acceptance was not found", e ); return; } - if (!context.AcceptedMissionLocation.TryGetValue(entry.Mission.MissionID, out accepted_location)) { + if (!context.AcceptedMissionLocation.TryGetValue(mission.MissionID, out accepted_location)) { transactions.AddIncomplete(new MissionFailed(), "Unable to figure out where failed mission was accepted", e ); @@ -593,10 +631,9 @@ internal class MissionFailedParser : TransactionParserPart { return; } - transactions.Add(new MissionFailed() { - Accepted = accepted, - Faction = accepted.Mission?.Faction, - Failed = entry, + transactions.Add(new MissionFailed(entry) { + Faction = mission?.Faction, + Mission = mission, Station = accepted_location.Station, System = accepted_location.StarSystem, SystemAddress = accepted_location.SystemAddress, @@ -931,6 +968,7 @@ public class TransactionParser { { Events.MissionAccepted, new MissionAcceptedParser() }, { Events.MissionCompleted, new MissionCompletedParser() }, { Events.MissionFailed, new MissionFailedParser() }, + { Events.Missions, new MissionsParser() }, { Events.MultiSellExplorationData, new MultiSellExplorationDataParser() }, { Events.ReceiveText, new ReceiveTextParser() }, { Events.RedeemVoucher, new RedeemVoucherParser() }, diff --git a/EliteBGS/LogGenerator/FailedMissionFormat.cs b/EliteBGS/LogGenerator/FailedMissionFormat.cs index 810a17c..9ac6c79 100644 --- a/EliteBGS/LogGenerator/FailedMissionFormat.cs +++ b/EliteBGS/LogGenerator/FailedMissionFormat.cs @@ -14,13 +14,23 @@ public class FailedMissionFormat : LogFormatter { return ""; } - foreach (MissionFailed failed in missions) { + foreach (var failed in missions) { MissionFailedEntry f = failed.Failed; - builder.AppendFormat("Failed {0} mission(s) \"{1}\" targeting {2}\n", - failed.Amount, - string.IsNullOrEmpty(f.Mission.LocalisedName) ? f.Mission.Name : f.Mission.LocalisedName, - failed.Faction - ); + string name; + + if (!string.IsNullOrEmpty(f.Mission.FriendlyName)) { + name = f.Mission.FriendlyName; + } else if (!string.IsNullOrEmpty(f.Mission.LocalisedName)) { + name = f.Mission.LocalisedName; + } else { + name = f.Mission.Name; + } + + if (!string.IsNullOrEmpty(failed.Faction)) { + builder.AppendFormat("Failed mission \"{0}\" targeting {1}\n", name, failed.Faction); + } else { + builder.AppendFormat("Failed mission \"{0}\"\n", name); + } } return builder.ToString().Trim();