diff --git a/EDPlayerJournal/BGS/ThargoidKill.cs b/EDPlayerJournal/BGS/ThargoidKill.cs index b2986ea..21cc888 100644 --- a/EDPlayerJournal/BGS/ThargoidKill.cs +++ b/EDPlayerJournal/BGS/ThargoidKill.cs @@ -28,7 +28,7 @@ public class ThargoidKill : Transaction { public ThargoidKill() { } public ThargoidKill(FactionKillBondEntry entry) { - if (string.Compare(entry.VictimFaction, Thargoid.ThargoidFaction) != 0) { + if (!Factions.IsThargoid(entry.VictimFaction)) { throw new Exception("Not a valid thargoid kill"); } diff --git a/EDPlayerJournal/BGS/TransactionParser.cs b/EDPlayerJournal/BGS/TransactionParser.cs index ab11efe..3efd67c 100644 --- a/EDPlayerJournal/BGS/TransactionParser.cs +++ b/EDPlayerJournal/BGS/TransactionParser.cs @@ -488,9 +488,12 @@ internal class RedeemVoucherParser : TransactionParserPart { foreach (string faction in entry.Factions) { bool relevantBond = false; + string relevantFaction = faction; if (string.Compare(faction, Factions.PilotsFederationVouchers) == 0) { // Target faction is pilots' federation, so we assume thargoid bonks + // Also assign this combat bond to the Pilots Federation + relevantFaction = Factions.PilotsFederation; relevantBond = true; } @@ -511,7 +514,7 @@ internal class RedeemVoucherParser : TransactionParserPart { transactions.Add(new Vouchers(entry) { System = context.CurrentSystem, Station = context.CurrentStation, - Faction = faction, + Faction = relevantFaction, ControllingFaction = context.ControllingFaction, }); } @@ -600,9 +603,13 @@ internal class FactionKillBondParser : TransactionParserPart { throw new NotImplementedException(); } - if (string.Compare(entry.VictimFaction, Thargoid.ThargoidFaction) == 0) { + if (Factions.IsThargoid(entry.VictimFaction)) { // Thargoid bonk - transactions.Add(new ThargoidKill(entry)); + transactions.Add(new ThargoidKill(entry) { + System = context.CurrentSystem, + Faction = Factions.PilotsFederation, + Station = context.CurrentStation, + }); } } } diff --git a/EDPlayerJournal/BGS/Vouchers.cs b/EDPlayerJournal/BGS/Vouchers.cs index 392b3f7..a01501a 100644 --- a/EDPlayerJournal/BGS/Vouchers.cs +++ b/EDPlayerJournal/BGS/Vouchers.cs @@ -20,8 +20,8 @@ public class Vouchers : Transaction { } return Entries .OfType() - .Where(x => x.FactionBounties.ContainsKey(Faction)) - .Sum(x => x.FactionBounties[Faction]) + .ToList() + .Sum(x => (long)x.GetBountyForFaction(Faction)) ; } } diff --git a/EDPlayerJournal/Entries/RedeemVoucherEntry.cs b/EDPlayerJournal/Entries/RedeemVoucherEntry.cs index 72890ca..4023d59 100644 --- a/EDPlayerJournal/Entries/RedeemVoucherEntry.cs +++ b/EDPlayerJournal/Entries/RedeemVoucherEntry.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace EDPlayerJournal.Entries; public class RedeemVoucherEntry : Entry { protected override void Initialise() { - Amount = (JSON.Value("Amount") ?? 0); + Amount = (JSON.Value("Amount") ?? 0); Type = JSON.Value("Type"); /* according to API, there should be a Factions structure */ @@ -12,7 +12,7 @@ public class RedeemVoucherEntry : Entry { if (factions != null) { foreach (JObject faction in factions.Children()) { string? faction_name = faction.Value("Faction"); - long? faction_bounty = faction.Value("Amount"); + ulong? faction_bounty = faction.Value("Amount"); if (faction_name == null || faction_name.Length <= 0 || faction_bounty == null) { continue; } @@ -35,8 +35,29 @@ public class RedeemVoucherEntry : Entry { } } - public int Amount { get; set; } = 0; + public ulong GetBountyForFaction(string? a) { + if (a == null) { + return 0; + } + + var relevant = FactionBounties.Where(x => EDPlayerJournal.Factions.CompareFactions(x.Key, a) == 0).ToList(); + if (relevant == null || relevant.Count() == 0) { + return 0; + } + + return (ulong)relevant.Sum(x => (decimal)x.Value); + } + + public ulong Amount { get; set; } = 0; public string? Type { get; set; } = "Bounty"; - public List Factions { get; set; } = new List(); - public Dictionary FactionBounties { get; set; } = new Dictionary(); + + /// + /// List of factions + /// + public List Factions { get; set; } = new(); + + /// + /// Bounties awarded by faction + /// + public Dictionary FactionBounties { get; set; } = new(); } diff --git a/EDPlayerJournal/Faction.cs b/EDPlayerJournal/Faction.cs index 94f9f42..181b8cb 100644 --- a/EDPlayerJournal/Faction.cs +++ b/EDPlayerJournal/Faction.cs @@ -40,10 +40,71 @@ public class Factions { /// public static string PilotsFederationVouchers = "PilotsFederation"; + /// + /// Friendly name of the Pilots Federation + /// + public static string PilotsFederation = "Pilots' Federation"; + /// /// Internal name for the Thargoid faction /// - public static string Thargoid = "$faction_Thargoid;"; + public static string ThargoidInternal = "$faction_Thargoid;"; + + /// + /// Localised name of the Thargoids + /// + public static string Thargoid = "Thargoids"; + + public static bool IsPilotsFederation(string? name) { + if (name == null) { + return false; + } + + if (string.Compare(name, PilotsFederationInternal) == 0 || + string.Compare(name, PilotsFederationVouchers) == 0 || + string.Compare(name, PilotsFederation) == 0) { + return true; + } + + return false; + } + + public static bool IsThargoid(string? name) { + if (name == null) { + return false; + } + + if (string.Compare(name, ThargoidInternal) == 0 || + string.Compare(name, Thargoid) == 0) { + return true; + } + + return false; + } + + /// + /// Compares two factions names and sees if they are the same faction. Since + /// factions can have an internal name, and a public name, this function takes + /// these into account. + /// + /// + /// + /// + public static int CompareFactions(string? a, string? b) { + if (a == null || b == null) { + return -1; + } + + if (IsPilotsFederation(a) && IsPilotsFederation(b)) { + return 0; + } + + if (IsThargoid(a) && IsThargoid(b)) { + return 0; + } + + return string.Compare(a, b); + } } public class Faction {