From 3846f0c8efdea1987902f3009f8fcac60ae62346 Mon Sep 17 00:00:00 2001 From: Ben Parsons <9parsonsb@gmail.com> Date: Tue, 11 Feb 2025 12:41:59 +1100 Subject: [PATCH] Update Faction handling some database stuff in progress, to allow for complex querying of the journal --- .../Files/Journal/Startup/Loadout.cs | 18 +++--- .../Files/Journal/Travel/FSDJump.cs | 6 +- .../Files/Journal/Travel/Location.cs | 62 ++++++++++--------- .../Files/ParameterTypes/DetailedFaction.cs | 15 ----- .../Files/ParameterTypes/Faction.cs | 13 +++- .../Files/ParameterTypes/Material.cs | 2 +- .../Files/ParameterTypes/Modifiers.cs | 6 ++ .../Files/ParameterTypes/SystemFaction.cs | 28 --------- Pulsar/Features/Journal/JournalProcessor.cs | 34 +++++++--- 9 files changed, 90 insertions(+), 94 deletions(-) delete mode 100644 ObservatoryFramework/Files/ParameterTypes/DetailedFaction.cs delete mode 100644 ObservatoryFramework/Files/ParameterTypes/SystemFaction.cs diff --git a/ObservatoryFramework/Files/Journal/Startup/Loadout.cs b/ObservatoryFramework/Files/Journal/Startup/Loadout.cs index 8c6eac8..3f87a8d 100644 --- a/ObservatoryFramework/Files/Journal/Startup/Loadout.cs +++ b/ObservatoryFramework/Files/Journal/Startup/Loadout.cs @@ -10,14 +10,14 @@ public class Loadout : JournalBase public ulong ShipID { get; init; } public string ShipName { get; init; } public string ShipIdent { get; init; } - public int CargoCapacity { get; init; } - public ulong HullValue { get; init; } - public ulong ModulesValue { get; init; } - public double HullHealth { get; init; } - public double UnladenMass { get; init; } - public FuelCapacity FuelCapacity { get; init; } - public double MaxJumpRange { get; init; } - public ulong Rebuy { get; init; } - public bool Hot { get; init; } + public int? CargoCapacity { get; init; } + public ulong? HullValue { get; init; } + public ulong? ModulesValue { get; init; } + public double? HullHealth { get; init; } + public double? UnladenMass { get; init; } + public FuelCapacity? FuelCapacity { get; init; } + public double? MaxJumpRange { get; init; } + public ulong? Rebuy { get; init; } + public bool? Hot { get; init; } public List Modules { get; init; } } \ No newline at end of file diff --git a/ObservatoryFramework/Files/Journal/Travel/FSDJump.cs b/ObservatoryFramework/Files/Journal/Travel/FSDJump.cs index 4a71dc9..24edd7e 100644 --- a/ObservatoryFramework/Files/Journal/Travel/FSDJump.cs +++ b/ObservatoryFramework/Files/Journal/Travel/FSDJump.cs @@ -19,8 +19,8 @@ public class FSDJump : JournalBase public double FuelUsed { get; init; } public double FuelLevel { get; init; } public int BoostUsed { get; init; } - [JsonConverter(typeof(LegacyFactionConverter))] - public SystemFaction SystemFaction { get; init; } + [JsonConverter(typeof(LegacyFactionConverter))] + public Faction SystemFaction { get; init; } public string SystemAllegiance { get; init; } public string SystemEconomy { get; init; } @@ -33,7 +33,7 @@ public class FSDJump : JournalBase public string SystemSecurity_Localised { get; init; } public long Population { get; init; } public bool Wanted { get; init; } - public List Factions { get; init; } + public List Factions { get; init; } public List Conflicts { get; init; } public IList Powers { get; init; } public string PowerplayState { get; init; } diff --git a/ObservatoryFramework/Files/Journal/Travel/Location.cs b/ObservatoryFramework/Files/Journal/Travel/Location.cs index 0599a7e..7f722e6 100644 --- a/ObservatoryFramework/Files/Journal/Travel/Location.cs +++ b/ObservatoryFramework/Files/Journal/Travel/Location.cs @@ -1,4 +1,5 @@ -using System.Collections.Immutable; +using System.ComponentModel.DataAnnotations.Schema; +using System.Runtime.Serialization; using System.Text.Json.Serialization; using Observatory.Framework.Files.Converters; using Observatory.Framework.Files.ParameterTypes; @@ -12,30 +13,30 @@ public class Location : JournalBase [JsonConverter(typeof(IntBoolFlexConverter))] public bool Docked { get; init; } - public double DistFromStarLS { get; init; } + public double? DistFromStarLS { get; init; } /// /// Name of the station at which this event occurred. /// - public string StationName { get; init; } + public string? StationName { get; init; } - public string StationType { get; init; } - public float Longitude { get; init; } - public float Latitude { get; init; } - public ulong MarketID { get; init; } + public string? StationType { get; init; } + public float? Longitude { get; init; } + public float? Latitude { get; init; } + public ulong? MarketID { get; init; } [JsonConverter(typeof(LegacyFactionConverter))] - public Faction StationFaction { get; init; } + public Faction? StationFaction { get; init; } - public string StationGovernment { get; init; } - public string StationGovernment_Localised { get; init; } - public string StationAllegiance { get; init; } - public IList StationServices { get; init; } - public string StationEconomy { get; init; } - public string StationEconomy_Localised { get; init; } + public string? StationGovernment { get; init; } + public string? StationGovernment_Localised { get; init; } + public string? StationAllegiance { get; init; } + public List? StationServices { get; init; } + public string? StationEconomy { get; init; } + public string? StationEconomy_Localised { get; init; } public List StationEconomies { get; init; } public string StarSystem { get; init; } - public ulong SystemAddress { get; init; } + public ulong? SystemAddress { get; init; } [JsonConverter(typeof(StarPosConverter))] public StarPos StarPos { get; init; } @@ -43,29 +44,30 @@ public class Location : JournalBase public string SystemAllegiance { get; init; } public string SystemEconomy { get; init; } public string SystemEconomy_Localised { get; init; } - public string SystemSecondEconomy { get; init; } - public string SystemSecondEconomy_Localised { get; init; } + public string? SystemSecondEconomy { get; init; } + public string? SystemSecondEconomy_Localised { get; init; } public string SystemGovernment { get; init; } public string SystemGovernment_Localised { get; init; } public string SystemSecurity { get; init; } public string SystemSecurity_Localised { get; init; } - public long Population { get; init; } + public long? Population { get; init; } public string Body { get; init; } - public int BodyID { get; init; } + public int? BodyID { get; init; } public string BodyType { get; init; } - public List Factions { get; init; } + public List? Factions { get; init; } - [JsonConverter(typeof(LegacyFactionConverter))] - public DetailedFaction SystemFaction { get; init; } + [JsonConverter(typeof(LegacyFactionConverter))] + [NotMapped] + public Faction? SystemFaction { get; init; } - public List Conflicts { get; init; } - public IList Powers { get; init; } - public string PowerplayState { get; init; } - public bool Taxi { get; init; } - public bool Multicrew { get; init; } - public bool OnFoot { get; init; } - public bool InSRV { get; init; } - public ThargoidWar ThargoidWar { get; init; } + public List? Conflicts { get; init; } + public List? Powers { get; init; } + public string? PowerplayState { get; init; } + public bool? Taxi { get; init; } + public bool? Multicrew { get; init; } + public bool? OnFoot { get; init; } + public bool? InSRV { get; init; } + public ThargoidWar? ThargoidWar { get; init; } } public class StarPos diff --git a/ObservatoryFramework/Files/ParameterTypes/DetailedFaction.cs b/ObservatoryFramework/Files/ParameterTypes/DetailedFaction.cs deleted file mode 100644 index 03d4e75..0000000 --- a/ObservatoryFramework/Files/ParameterTypes/DetailedFaction.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Collections.Immutable; - -namespace Observatory.Framework.Files.ParameterTypes; - -public class DetailedFaction : Faction -{ - public string Government { get; init; } - public float Influence { get; init; } - public string Allegiance { get; init; } - public string Happiness { get; init; } - public string Happiness_Localised { get; init; } - public float MyReputation { get; init; } - public List RecoveringStates { get; init; } - public List ActiveStates { get; init; } -} \ No newline at end of file diff --git a/ObservatoryFramework/Files/ParameterTypes/Faction.cs b/ObservatoryFramework/Files/ParameterTypes/Faction.cs index 9855632..2f3a721 100644 --- a/ObservatoryFramework/Files/ParameterTypes/Faction.cs +++ b/ObservatoryFramework/Files/ParameterTypes/Faction.cs @@ -3,6 +3,17 @@ public class Faction { public string Name { get; init; } - public string FactionState { get; init; } + public string? Government { get; init; } + public float? Influence { get; init; } + public string? Allegiance { get; init; } + public string? Happiness { get; init; } + public string? Happiness_Localised { get; init; } + public float? MyReputation { get; init; } + public List? RecoveringStates { get; init; } + public List? ActiveStates { get; init; } + public List? PendingStates { get; init; } + public bool? SquadronFaction { get; init; } + public bool? HappiestSystem { get; init; } + public bool? HomeSystem { get; init; } } \ No newline at end of file diff --git a/ObservatoryFramework/Files/ParameterTypes/Material.cs b/ObservatoryFramework/Files/ParameterTypes/Material.cs index 694378c..baf0115 100644 --- a/ObservatoryFramework/Files/ParameterTypes/Material.cs +++ b/ObservatoryFramework/Files/ParameterTypes/Material.cs @@ -3,6 +3,6 @@ public class Material { public string Name { get; init; } - public string Name_Localised { get; init; } + public string? Name_Localised { get; init; } public int Count { get; init; } } \ No newline at end of file diff --git a/ObservatoryFramework/Files/ParameterTypes/Modifiers.cs b/ObservatoryFramework/Files/ParameterTypes/Modifiers.cs index 1d69cd9..56f04df 100644 --- a/ObservatoryFramework/Files/ParameterTypes/Modifiers.cs +++ b/ObservatoryFramework/Files/ParameterTypes/Modifiers.cs @@ -32,6 +32,12 @@ public class NumberOrString DoubleValue = value; IsDouble = true; } + + public override string ToString() + { + return IsString ? StringValue : DoubleValue.ToString(); + } + public string? StringValue { get; init; } public bool IsString { get; init; } public double? DoubleValue { get; init; } diff --git a/ObservatoryFramework/Files/ParameterTypes/SystemFaction.cs b/ObservatoryFramework/Files/ParameterTypes/SystemFaction.cs deleted file mode 100644 index 24970ab..0000000 --- a/ObservatoryFramework/Files/ParameterTypes/SystemFaction.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System.Collections.Immutable; - -namespace Observatory.Framework.Files.ParameterTypes; - -public class SystemFaction : Faction -{ - public string Government { get; init; } - - public double Influence { get; init; } - - public string Happiness { get; init; } - - public string Happiness_Localised { get; init; } - - public double MyReputation { get; init; } - - public List PendingStates { get; init; } - - public List RecoveringStates { get; init; } - - public List ActiveStates { get; init; } - - public bool? SquadronFaction { get; init; } - - public bool? HappiestSystem { get; init; } - - public bool? HomeSystem { get; init; } -} \ No newline at end of file diff --git a/Pulsar/Features/Journal/JournalProcessor.cs b/Pulsar/Features/Journal/JournalProcessor.cs index 8db6b41..d91026c 100644 --- a/Pulsar/Features/Journal/JournalProcessor.cs +++ b/Pulsar/Features/Journal/JournalProcessor.cs @@ -2,6 +2,7 @@ namespace Pulsar.Features.Journal; using Observatory.Framework; using Observatory.Framework.Files.Journal; +using Observatory.Framework.Files.Journal.Travel; using Observatory.Framework.Files.Journal.Startup; using Observatory.Framework.Files.Journal.StationServices; @@ -43,9 +44,10 @@ public class JournalProcessor( line = newLine; } + JournalBase? journal = null; try { - var journal = JsonSerializer.Deserialize(new ReadOnlySpan(line.ToArray()), options); + journal = JsonSerializer.Deserialize(new ReadOnlySpan(line.ToArray()), options); if (journal == null) { //return ValueTask.CompletedTask; @@ -84,7 +86,6 @@ public class JournalProcessor( await context.Reputation.AddAsync(reputation, token); await context.SaveChangesAsync(token); break; - case EngineerProgress engineerProgress when context.EngineerProgress.Any(e => e.Timestamp == engineerProgress.Timestamp): continue; @@ -98,15 +99,30 @@ public class JournalProcessor( await context.LoadGames.AddAsync(loadGame, token); await context.SaveChangesAsync(token); break; - - case Statistics statistics when context.Statistics.Any(s => s.Timestamp == statistics.Timestamp): continue; case Statistics statistics: await context.Statistics.AddAsync(statistics, token); await context.SaveChangesAsync(token); break; - + case Missions missions when context.Missions.Any(m => m.Timestamp == missions.Timestamp): + continue; + case Missions missions: + await context.Missions.AddAsync(missions, token); + await context.SaveChangesAsync(token); + break; + case Location location when context.Locations.Any(l => l.Timestamp == location.Timestamp): + continue; + case Location location: + await context.Locations.AddAsync(location, token); + await context.SaveChangesAsync(token); + break; + case Loadout loadout when context.Loadout.Any(l => l.Timestamp == loadout.Timestamp): + continue; + case Loadout loadout: + await context.Loadout.AddAsync(loadout, token); + await context.SaveChangesAsync(token); + break; } newJournals.Add(journal); @@ -115,10 +131,14 @@ public class JournalProcessor( { logger.LogError(ex, "Error deserializing journal file: '{File}', line: {Line}", filePath, line); } + catch (DbUpdateException ex) + { + logger.LogError(ex, "Error updating database with journal file: '{File}', line: {Line}", filePath, line); + } catch (Exception ex) { - logger.LogError(ex, "Error processing journal file: '{File}', line# {LineNumber}, line: {Line}", - filePath, index, Encoding.UTF8.GetString(line.ToArray())); + logger.LogError(ex, "Error processing journal file: '{File}', line# {LineNumber}, line: {Line}, type?: {Type}", + filePath, index, Encoding.UTF8.GetString(line.ToArray()), journal?.GetType()); } //return ValueTask.CompletedTask;