start working on a CommanderContext

This commit is contained in:
Florian Stinglmayr 2025-07-31 15:30:16 +02:00
parent f14c841e51
commit f21bf5ea5e
12 changed files with 273 additions and 0 deletions

View File

@ -0,0 +1,56 @@
using EDPlayerJournal.Entries;
namespace EDPlayerJournal.CommanderContext;
/// <summary>
/// A class that uses various journal entries to provide a (hopefully)
/// up to date summary of the current commander in game.
///
/// Data in this context may be null, in which case the current status of
/// that information is not yet known. For example if the player died, but
/// they have't respawned yet, their location might be null.
/// </summary>
public class CommanderContext {
private static Dictionary<string, ICommanderParser> parsers = new() {
{ Events.Commander, new CommanderParser() },
{ Events.Docked, new DockedParser() },
{ Events.FSDJump, new FSDJumpParser() },
{ Events.Location, new LocationParser() },
{ Events.Undocked, new UndockedParser() },
};
/// <summary>
/// Name of the commander.
/// </summary>
public string? Name { get; set; } = null;
/// <summary>
/// Whether the commander is currently docked.
/// </summary>
public bool IsDocked { get; set; } = false;
/// <summary>
/// Current station
/// </summary>
public Station? Station { get; set; } = null;
/// <summary>
/// Current star system
/// </summary>
public StarSystem? System { get; set; } = null;
public void Update(Entry entry) {
if (string.IsNullOrEmpty(entry.Event)) {
throw new CommanderParserException("invalid or empty event given");
}
string ev = entry.Event;
if (!parsers.ContainsKey(ev)) {
return;
}
ICommanderParser parser = parsers[ev];
parser.Parse(entry, this);
}
}

View File

@ -0,0 +1,15 @@
using EDPlayerJournal.Entries;
namespace EDPlayerJournal.CommanderContext;
internal class CommanderParser : ICommanderParser {
public void Parse(Entry entry, CommanderContext ctx) {
CommanderEntry? e = entry as CommanderEntry;
if (e == null) {
throw new CommanderParserException("invalid entry");
}
ctx.Name = e.Name;
}
}

View File

@ -0,0 +1,16 @@
using EDPlayerJournal.Entries;
namespace EDPlayerJournal.CommanderContext;
internal class DockedParser : ICommanderParser {
public void Parse(Entry entry, CommanderContext ctx) {
DockedEntry? e = entry as DockedEntry;
if (e == null) {
throw new CommanderParserException("invalid entry");
}
ctx.Station = Station.ParseEntry(e);
ctx.IsDocked = true;
}
}

View File

@ -0,0 +1,15 @@
using EDPlayerJournal.Entries;
namespace EDPlayerJournal.CommanderContext;
internal class FSDJumpParser : ICommanderParser {
public void Parse(Entry entry, CommanderContext ctx) {
FSDJumpEntry? e = entry as FSDJumpEntry;
if (e == null) {
throw new CommanderParserException("invalid entry");
}
ctx.System = StarSystem.ParseEntry(e);
}
}

View File

@ -0,0 +1,19 @@
using EDPlayerJournal;
using EDPlayerJournal.Entries;
namespace EDPlayerJournal.CommanderContext;
public class CommanderParserException : ApplicationException {
public CommanderParserException() {
}
public CommanderParserException(string message) : base(message) {
}
public CommanderParserException(string message, params object?[] args) : base(string.Format(message, args)) {
}
}
internal interface ICommanderParser {
void Parse(Entry entry, CommanderContext ctx);
}

View File

@ -0,0 +1,17 @@
using EDPlayerJournal.Entries;
namespace EDPlayerJournal.CommanderContext;
internal class LocationParser : ICommanderParser {
public void Parse(Entry entry, CommanderContext ctx) {
LocationEntry? e = entry as LocationEntry;
if (e == null) {
throw new CommanderParserException("invalid entry");
}
ctx.Station = Station.ParseEntry(e);
ctx.System = StarSystem.ParseEntry(e);
ctx.IsDocked = e.Docked;
}
}

View File

@ -0,0 +1,16 @@
using EDPlayerJournal.Entries;
namespace EDPlayerJournal.CommanderContext;
internal class UndockedParser : ICommanderParser {
public void Parse(Entry entry, CommanderContext ctx) {
UndockedEntry? e = entry as UndockedEntry;
if (e == null) {
throw new CommanderParserException("invalid entry");
}
ctx.Station = null;
ctx.IsDocked = false;
}
}

View File

@ -59,6 +59,7 @@ public class Entry {
{ Events.SupercruiseEntry, typeof(SupercruiseEntryEntry) }, { Events.SupercruiseEntry, typeof(SupercruiseEntryEntry) },
{ Events.SupercruiseExit, typeof(SupercruiseExitEntry) }, { Events.SupercruiseExit, typeof(SupercruiseExitEntry) },
{ Events.UnderAttack, typeof(UnderAttackEntry) }, { Events.UnderAttack, typeof(UnderAttackEntry) },
{ Events.Undocked, typeof(UndockedEntry) },
}; };
private string? eventtype = null; private string? eventtype = null;

View File

@ -50,4 +50,5 @@ public class Events {
public static readonly string SupercruiseEntry = "SupercruiseEntry"; public static readonly string SupercruiseEntry = "SupercruiseEntry";
public static readonly string SupercruiseExit = "SupercruiseExit"; public static readonly string SupercruiseExit = "SupercruiseExit";
public static readonly string UnderAttack = "UnderAttack"; public static readonly string UnderAttack = "UnderAttack";
public static readonly string Undocked = "Undocked";
} }

View File

@ -0,0 +1,12 @@
namespace EDPlayerJournal.Entries;
public class UndockedEntry : Entry {
/// <summary>
/// Name of the station
/// </summary>
public string? StationName { get; set; }
protected override void Initialise() {
StationName = JSON.Value<string>("StationName");
}
}

View File

@ -0,0 +1,40 @@
using EDPlayerJournal.Entries;
using Newtonsoft.Json.Linq;
namespace EDPlayerJournal;
public class StarSystem {
public string Name { get; set; } = string.Empty;
public double[] Position { get; set; } = new double[3] { 0.0, 0.0, 0.0 };
public ulong Address { get; set; } = 0;
public ulong Population { get; set; } = 0;
public string Allegiance { get; set; } = string.Empty;
public static StarSystem? ParseEntry(Entry e) {
if (!(e.Is(Events.Location) || e.Is(Events.FSDJump))) {
return null;
}
return ParseJSON(e.JSON);
}
public static StarSystem? ParseJSON(JObject obj) {
StarSystem s = new();
s.Name = obj.Value<string?>("StarSystem") ?? string.Empty;
var pos = obj.Value<JArray?>("StarPos");
if (pos != null) {
s.Position = pos.Select(x => (double)x).ToArray();
}
s.Address = obj.Value<ulong?>("SystemAddress") ?? 0;
s.Population = obj.Value<ulong?>("Population") ?? 0;
// TODO: more info
return s;
}
}

View File

@ -0,0 +1,65 @@
using EDPlayerJournal.Entries;
using Newtonsoft.Json.Linq;
namespace EDPlayerJournal;
/// <summary>
/// Represents a station ingame
/// </summary>
public class Station {
public string SystemName { get; set; } = string.Empty;
public ulong SystemAddress { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public string Type { get; set; } = string.Empty;
public ulong MarketID { get; set; } = 0;
public string Faction { get; set; } = string.Empty;
public string Government { get; set; } = string.Empty;
public string GovernmentLocalised { get; set; } = string.Empty;
public List<string> Services { get; set; } = new();
public static Station? ParseEntry(Entry e) {
if (!(e.Is(Events.Docked) || e.Is(Events.Location))) {
return null;
}
var s = ParseJSON(e.JSON);
return s;
}
public static Station? ParseJSON(JObject obj) {
Station s = new();
s.Name = obj.Value<string?>("StationName") ?? string.Empty;
s.Type = obj.Value<string?>("StationType") ?? string.Empty;
s.MarketID = obj.Value<ulong?>("MarketID") ?? 0;
var faction = obj.Value<JObject?>("StationFaction");
if (faction != null) {
s.Faction = faction.Value<string?>("Name") ?? string.Empty;
}
s.Government = obj.Value<string?>("StationGovernment") ?? string.Empty;
s.GovernmentLocalised = obj.Value<string?>("StationGovernment_Localised") ?? string.Empty;
var services = obj.Value<JArray?>("StationServices");
if (services != null) {
s.Services = services
.Select(x => x.ToString())
.ToList()
;
}
// TODO: more info
return s;
}
}