edjournal/Entry.cs

107 lines
4.0 KiB
C#
Raw Normal View History

2021-09-02 16:18:02 +02:00
using Newtonsoft.Json;
2021-08-25 18:24:44 +02:00
using Newtonsoft.Json.Linq;
2021-09-02 16:18:02 +02:00
using System;
using System.Collections.Generic;
2021-08-25 18:24:44 +02:00
namespace EDJournal {
/// <summary>
/// Base class for a single entry within the player journal. If no specific sub class is available
/// this class gives basic information, such as EventType or date when it happened. It also allows
/// base classes access to the underlying JSON object. Base classes should be named after the event
/// type that they map + Entry. So "FSDJump" event is handled by FSDJumpEntry.
/// </summary>
public class Entry {
private static readonly Dictionary<string, Type> classes = new Dictionary<string, Type> {
2021-09-02 16:18:02 +02:00
{ Events.Bounty, typeof(BountyEntry) },
{ Events.CommitCrime, typeof(CommitCrimeEntry) },
{ Events.Died, typeof(DiedEntry) },
2021-08-25 18:24:44 +02:00
{ Events.Docked, typeof(DockedEntry) },
2021-09-28 14:14:37 +02:00
{ Events.FactionKillBond, typeof(FactionKillBondEntry) },
2021-08-25 18:24:44 +02:00
{ Events.FSDJump, typeof(FSDJumpEntry) },
2021-09-02 16:18:02 +02:00
{ Events.HullDamage, typeof(HullDamageEntry) },
2021-11-15 18:39:12 +01:00
{ Events.Location, typeof(LocationEntry) },
2022-01-12 16:19:26 +01:00
{ Events.MarketBuy, typeof(MarketBuyEntry) },
2021-09-02 16:18:02 +02:00
{ Events.MarketSell, typeof(MarketSellEntry) },
2021-08-27 20:05:31 +02:00
{ Events.MissionAbandoned, typeof(MissionAbandonedEntry) },
2021-08-26 11:29:11 +02:00
{ Events.MissionAccepted, typeof(MissionAcceptedEntry) },
2021-08-25 18:24:44 +02:00
{ Events.MissionCompleted, typeof(MissionCompletedEntry) },
2021-11-12 21:49:37 +01:00
{ Events.MissionFailed, typeof(MissionFailedEntry) },
2021-08-26 11:29:11 +02:00
{ Events.MissionRedirected, typeof(MissionRedirectedEntry) },
2021-08-25 18:24:44 +02:00
{ Events.MultiSellExplorationData, typeof(MultiSellExplorationDataEntry) },
{ Events.RedeemVoucher, typeof(RedeemVoucherEntry) },
2022-02-09 08:56:25 +01:00
{ Events.SearchAndRescue, typeof(SearchAndRescueEntry) },
{ Events.SellExplorationData, typeof(SellExplorationDataEntry) },
2021-09-02 16:18:02 +02:00
{ Events.SellMicroResources, typeof(SellMicroResourcesEntry) },
2022-02-07 16:21:44 +01:00
{ Events.SellOrganicData, typeof(SellOrganicDataEntry) },
2021-09-02 16:18:02 +02:00
{ Events.ShieldState, typeof(ShieldStateEntry) },
2021-08-26 11:29:11 +02:00
{ Events.ShipTargeted, typeof(ShipTargetedEntry) },
{ Events.UnderAttack, typeof(UnderAttackEntry) },
2021-08-25 18:24:44 +02:00
};
private string eventtype = null;
private string datetime = null;
private DateTime timestamp;
private string jsonstr = null;
protected JObject json = null;
public Entry() {
}
public static Entry Parse(string journalline) {
var json = JObject.Parse(journalline);
return Parse(json);
}
public static Entry Parse(JObject json) {
string event_name = json.GetValue("event").ToString();
classes.TryGetValue(event_name, out Type classhandler);
if (classhandler == null) {
classhandler = typeof(Entry);
}
var obj = (Entry)Activator.CreateInstance(classhandler);
obj.InternalInitialise(json);
obj.Initialise();
return obj;
}
private void InternalInitialise(JObject jobject) {
this.json = jobject;
this.jsonstr = json.ToString(formatting: Formatting.None);
this.eventtype = json.GetValue("event").ToString();
this.datetime = json.GetValue("timestamp").ToString();
this.timestamp = DateTime.Parse(this.datetime);
}
protected virtual void Initialise() {
}
public bool Is(string eventtype) {
if (eventtype == null || this.eventtype == null) {
return false;
}
return String.Equals(this.eventtype, eventtype, StringComparison.OrdinalIgnoreCase);
}
public string Event {
get { return eventtype; }
}
public DateTime Timestamp {
get { return timestamp; }
}
public JObject JSON {
get { return this.json; }
}
public override string ToString() {
2021-08-26 11:29:11 +02:00
return jsonstr ?? "";
2021-08-25 18:24:44 +02:00
}
}
}