105 lines
3.9 KiB
C#
105 lines
3.9 KiB
C#
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
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> {
|
|
{ Events.Bounty, typeof(BountyEntry) },
|
|
{ Events.CommitCrime, typeof(CommitCrimeEntry) },
|
|
{ Events.Died, typeof(DiedEntry) },
|
|
{ Events.Docked, typeof(DockedEntry) },
|
|
{ Events.FactionKillBond, typeof(FactionKillBondEntry) },
|
|
{ Events.FSDJump, typeof(FSDJumpEntry) },
|
|
{ Events.HullDamage, typeof(HullDamageEntry) },
|
|
{ Events.Location, typeof(LocationEntry) },
|
|
{ Events.MarketBuy, typeof(MarketBuyEntry) },
|
|
{ Events.MarketSell, typeof(MarketSellEntry) },
|
|
{ Events.MissionAbandoned, typeof(MissionAbandonedEntry) },
|
|
{ Events.MissionAccepted, typeof(MissionAcceptedEntry) },
|
|
{ Events.MissionCompleted, typeof(MissionCompletedEntry) },
|
|
{ Events.MissionFailed, typeof(MissionFailedEntry) },
|
|
{ Events.MissionRedirected, typeof(MissionRedirectedEntry) },
|
|
{ Events.MultiSellExplorationData, typeof(MultiSellExplorationDataEntry) },
|
|
{ Events.RedeemVoucher, typeof(RedeemVoucherEntry) },
|
|
{ Events.SellExplorationData, typeof(SellExplorationDataEntry) },
|
|
{ Events.SellMicroResources, typeof(SellMicroResourcesEntry) },
|
|
{ Events.ShieldState, typeof(ShieldStateEntry) },
|
|
{ Events.ShipTargeted, typeof(ShipTargetedEntry) },
|
|
{ Events.UnderAttack, typeof(UnderAttackEntry) },
|
|
};
|
|
|
|
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() {
|
|
return jsonstr ?? "";
|
|
}
|
|
}
|
|
}
|