From 69f76ba3fbd7ac23cb5711543e584477076a6fa3 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Sat, 12 Feb 2022 19:49:11 +0100 Subject: [PATCH] provide detailed faction information from Location and FSDJump --- FSDJumpEntry.cs | 15 ++++++- Faction.cs | 102 +++++++++++++++++++++++++++++++++++++++++++++++ LocationEntry.cs | 12 ++++++ edjournal.csproj | 1 + 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 Faction.cs diff --git a/FSDJumpEntry.cs b/FSDJumpEntry.cs index 389b6a2..f045c2b 100644 --- a/FSDJumpEntry.cs +++ b/FSDJumpEntry.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json.Linq; +using System.Collections.Generic; +using Newtonsoft.Json.Linq; namespace EDJournal { public class FSDJumpEntry : Entry { @@ -9,11 +10,23 @@ namespace EDJournal { if (faction != null) { SystemFaction = faction.Value("Name"); } + + JArray factions = JSON.Value("Factions"); + if (factions != null) { + foreach (JObject system_faction in factions) { + Faction f = Faction.FromJSON(system_faction); + if (f != null) { + SystemFactions.Add(f); + } + } + } } public string StarSystem { get; set; } public string SystemFaction { get; set; } public ulong SystemAddress { get; set; } + + public List SystemFactions { get; set; } = new List(); } } diff --git a/Faction.cs b/Faction.cs new file mode 100644 index 0000000..844bcf0 --- /dev/null +++ b/Faction.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; + +namespace EDJournal { + public class FactionState { + public string State { get; set; } + public long Trend { get; set; } + + public static FactionState FromJSON(JObject j) { + if (j == null) { + return null; + } + + FactionState s = new FactionState() { + State = j.Value("State") ?? "", + Trend = j.Value("Trend") ?? 0, + }; + + if (string.IsNullOrEmpty(s.State)) { + return null; + } + + return s; + } + } + + public class Faction { + public string Name { get; set; } + public string FactionState { get; set; } + public string Government { get; set; } + public double Influence { get; set; } + public string Allegiance { get; set; } + public string Happiness { get; set; } + public string HappinessLocalised { get; set; } + public double MyReputation { get; set; } + public bool SquadronFaction { get; set; } = false; + public bool HomeSystem { get; set; } = false; + + public List RecoveringStates { get; set; } = new List(); + public List ActiveStates { get; set; } = new List(); + public List PendingStates { get; set; } = new List(); + + public static Faction FromJSON(JObject j) { + if (j == null) { + return null; + } + + Faction f = new Faction() { + Name = j.Value("Name") ?? "", + FactionState = j.Value("FactionState") ?? "", + Government = j.Value("Government") ?? "", + Influence = j.Value("Influence") ?? 1.0, + Allegiance = j.Value("Allegiance") ?? "", + Happiness = j.Value("Happiness") ?? "", + HappinessLocalised = j.Value("Happiness_Localised") ?? "", + MyReputation = j.Value("MyReputation") ?? 1.0, + SquadronFaction = j.Value("SquadronFaction") ?? false, + HomeSystem = j.Value("HomeSystem") ?? false, + }; + + if (string.IsNullOrEmpty(f.Name)) { + return null; + } + + JArray recovering_states = j.Value("RecoveringStates"); + if (recovering_states != null) { + foreach (JObject s in recovering_states) { + FactionState state = EDJournal.FactionState.FromJSON(s); + if (state != null) { + f.RecoveringStates.Add(state); + } + } + } + + JArray active_states = j.Value("ActiveStates"); + if (active_states != null) { + foreach (JObject s in active_states) { + FactionState state = EDJournal.FactionState.FromJSON(s); + if (state != null) { + f.ActiveStates.Add(state); + } + } + } + + JArray pending_states = j.Value("PendingStates"); + if (pending_states != null) { + foreach (JObject s in pending_states) { + FactionState state = EDJournal.FactionState.FromJSON(s); + if (state != null) { + f.PendingStates.Add(state); + } + } + } + + return f; + } + } +} diff --git a/LocationEntry.cs b/LocationEntry.cs index 81e93c2..897ae96 100644 --- a/LocationEntry.cs +++ b/LocationEntry.cs @@ -14,6 +14,8 @@ namespace EDJournal { public string Body { get; set; } public bool Docked { get; set; } + public List SystemFactions { get; set; } = new List(); + protected override void Initialise() { StarSystem = JSON.Value("StarSystem") ?? ""; SystemAddress = JSON.Value("SystemAddress") ?? 0; @@ -24,6 +26,16 @@ namespace EDJournal { if (systemfaction != null) { SystemFaction = systemfaction.Value("Name") ?? ""; } + + JArray factions = JSON.Value("Factions"); + if (factions != null) { + foreach (JObject system_faction in factions) { + Faction f = Faction.FromJSON(system_faction); + if (f != null) { + SystemFactions.Add(f); + } + } + } } } } diff --git a/edjournal.csproj b/edjournal.csproj index eb9f46f..b7b476b 100644 --- a/edjournal.csproj +++ b/edjournal.csproj @@ -50,6 +50,7 @@ +