160 lines
5.2 KiB
C#
160 lines
5.2 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using System.Text;
|
|||
|
using System.Linq;
|
|||
|
using Newtonsoft.Json.Linq;
|
|||
|
|
|||
|
namespace EDPlayerJournal.Entries;
|
|||
|
public class MissionCompletedEntry : Entry {
|
|||
|
public Dictionary<string, Dictionary<ulong, string>> Influences { get; set; } = new Dictionary<string, Dictionary<ulong, string>>();
|
|||
|
private List<string> Affected { get; set; } = new List<string>();
|
|||
|
|
|||
|
private string? readable_name = null;
|
|||
|
private bool readable_name_generated = false;
|
|||
|
|
|||
|
protected override void Initialise() {
|
|||
|
MissionID = JSON.Value<ulong?>("MissionID") ?? 0;
|
|||
|
Name = JSON.Value<string>("Name");
|
|||
|
Faction = JSON.Value<string>("Faction") ?? "";
|
|||
|
TargetFaction = JSON.Value<string>("TargetFaction") ?? "";
|
|||
|
if (JSON.ContainsKey("Commodity_Localised")) {
|
|||
|
Commodity = JSON.Value<string>("Commodity_Localised");
|
|||
|
}
|
|||
|
if (JSON.ContainsKey("Count")) {
|
|||
|
Count = JSON.Value<int>("Count");
|
|||
|
}
|
|||
|
if (JSON.ContainsKey("Donated")) {
|
|||
|
Donated = JSON.Value<int>("Donated");
|
|||
|
}
|
|||
|
|
|||
|
MakeHumanReadableName();
|
|||
|
BuildInfluenceList();
|
|||
|
}
|
|||
|
|
|||
|
private void BuildInfluenceList() {
|
|||
|
Influences.Clear();
|
|||
|
Affected.Clear();
|
|||
|
|
|||
|
var effects = JSON.Value<JArray>("FactionEffects");
|
|||
|
if (effects == null) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
foreach (var effect in effects.Children<JObject>()) {
|
|||
|
string? faction = effect.Value<string>("Faction");
|
|||
|
|
|||
|
if (faction == null) {
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
Affected.Add(faction);
|
|||
|
|
|||
|
var influence = effect.Value<JArray>("Influence");
|
|||
|
if (influence == null || influence.Count == 0) {
|
|||
|
// No influence reward, happens sometimes, but we have to accept it
|
|||
|
Influences.Add(faction, new Dictionary<ulong, string>());
|
|||
|
} else {
|
|||
|
foreach (var infl in influence.Children<JObject>()) {
|
|||
|
infl.TryGetValue("Influence", out JToken? result);
|
|||
|
infl.TryGetValue("SystemAddress", out JToken? systemaddr);
|
|||
|
|
|||
|
if (result != null && result.Type == JTokenType.String &&
|
|||
|
systemaddr != null && systemaddr.Type == JTokenType.Integer) {
|
|||
|
ulong system = systemaddr.ToObject<ulong?>() ?? 0;
|
|||
|
string inf = result.ToString();
|
|||
|
|
|||
|
if (!Influences.ContainsKey(faction)) {
|
|||
|
Influences.Add(faction, new Dictionary<ulong, string>());
|
|||
|
}
|
|||
|
|
|||
|
if (!Influences[faction].ContainsKey(system)) {
|
|||
|
Influences[faction].Add(system, inf);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public string? Name { get; set; }
|
|||
|
public string? Commodity { get; set; }
|
|||
|
public int? Count { get; set; }
|
|||
|
public int? Donated { get; set; }
|
|||
|
public ulong? MissionID { get; set; }
|
|||
|
public string? Faction { get; set; }
|
|||
|
public string? TargetFaction { get; set; }
|
|||
|
|
|||
|
private void MakeHumanReadableName() {
|
|||
|
if (readable_name != null || Name == null) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
string? readable = HumanReadableMissionName.MakeHumanReadableName(Name);
|
|||
|
StringBuilder builder = new StringBuilder();
|
|||
|
|
|||
|
if (readable == null) {
|
|||
|
builder = new StringBuilder(Name);
|
|||
|
builder.Replace("Mission_", "");
|
|||
|
builder.Replace("_name", "");
|
|||
|
builder.Replace("_MB", "");
|
|||
|
builder.Replace('_', ' ');
|
|||
|
builder.Replace("Illegal", " (Illegal)");
|
|||
|
builder.Replace("OnFoot", "On Foot");
|
|||
|
builder.Replace(" BS", "");
|
|||
|
builder.Replace("HackMegaship", "Hack Megaship");
|
|||
|
builder.Replace("Boom", "");
|
|||
|
builder.Replace("RebootRestore", "Reboot/Restore");
|
|||
|
builder.Replace("CivilLiberty", "");
|
|||
|
readable_name_generated = true;
|
|||
|
} else {
|
|||
|
builder.Append(readable);
|
|||
|
}
|
|||
|
|
|||
|
if (Count > 0 && Commodity != null) {
|
|||
|
builder.AppendFormat(" ({0} {1})", Count, Commodity);
|
|||
|
}
|
|||
|
|
|||
|
if (Donated != null || Donated > 0) {
|
|||
|
builder.AppendFormat(" ({0})", Credits.FormatCredits(Donated ?? 0));
|
|||
|
}
|
|||
|
|
|||
|
readable_name = builder.ToString().Trim();
|
|||
|
}
|
|||
|
|
|||
|
public string? HumanReadableName {
|
|||
|
get {
|
|||
|
MakeHumanReadableName();
|
|||
|
return readable_name;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool HumanReadableNameWasGenerated {
|
|||
|
get {
|
|||
|
MakeHumanReadableName();
|
|||
|
return readable_name_generated;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public string[] AffectedFactions => Affected.ToArray();
|
|||
|
|
|||
|
public string? GetInfluenceForFaction(string faction) {
|
|||
|
if (Influences == null || !Influences.ContainsKey(faction)) {
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
var inf = Influences[faction];
|
|||
|
return string.Join("", inf.Values);
|
|||
|
}
|
|||
|
|
|||
|
public string? GetInfluenceForFaction(string faction, ulong systemaddr) {
|
|||
|
if (!Influences.ContainsKey(faction)) {
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
if (!Influences[faction].ContainsKey(systemaddr)) {
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
return Influences[faction][systemaddr];
|
|||
|
}
|
|||
|
}
|