edjournal/MissionCompletedEntry.cs

119 lines
4.1 KiB
C#
Raw Normal View History

2021-08-25 18:24:44 +02:00
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
namespace EDJournal {
public class MissionCompletedEntry : Entry {
private Dictionary<string, string> influences = new Dictionary<string, string>();
private string readable_name = null;
private bool readable_name_generated = false;
private string name = null;
private string commodity = null;
private int count = 0;
private int donated = 0;
2021-08-27 19:58:22 +02:00
private int id = 0;
2021-08-25 18:24:44 +02:00
protected override void Initialise() {
2021-08-27 19:58:22 +02:00
id = JSON.Value<int?>("MissionID") ?? 0;
2021-08-25 18:24:44 +02:00
name = JSON.Value<string>("Name");
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");
}
2021-11-16 18:43:43 +01:00
MakeHumanReadableName();
2021-08-25 18:24:44 +02:00
}
public string Name => name;
public string Commodity => commodity;
public int Count => count;
2021-08-27 19:58:22 +02:00
public int MissionID => id;
2021-08-25 18:24:44 +02:00
private void MakeHumanReadableName() {
2021-11-16 18:43:43 +01:00
if (readable_name != null || Name == null) {
2021-08-25 18:24:44 +02:00
return;
}
2021-11-12 22:23:12 +01:00
string readable = HumanReadableMissionName.MakeHumanReadableName(Name);
StringBuilder builder = new StringBuilder();
2021-08-25 18:24:44 +02:00
2021-11-12 22:23:12 +01:00
if (readable == null) {
2021-08-25 18:24:44 +02:00
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;
2021-11-12 22:23:12 +01:00
} else {
builder.Append(readable);
2021-08-25 18:24:44 +02:00
}
if (count > 0 && commodity != null) {
builder.AppendFormat(" ({0} {1})", count, commodity);
}
if (donated > 0) {
builder.AppendFormat(" ({0})", Credits.FormatCredits(donated));
}
readable_name = builder.ToString().Trim();
}
public string HumanReadableName {
get {
MakeHumanReadableName();
return readable_name;
}
}
public bool HumanReadableNameWasGenerated {
get {
MakeHumanReadableName();
return readable_name_generated;
}
}
public string GetInfluenceForFaction(string faction) {
if (influences.ContainsKey(faction)) {
return influences[faction];
}
var effects = JSON.Value<JArray>("FactionEffects");
foreach (var effect in effects.Children<JObject>()) {
if (effect.GetValue("Faction").ToString() != faction) {
continue;
}
var influence = effect.Value<JArray>("Influence");
if (influence == null || influence.Count == 0) {
// No influence reward, happens on courier missions sometimes.
// There is always one point of rep, even if the mission won't state it
influences.Add(faction, "+");
}
foreach (var infl in influence.Children<JObject>()) {
infl.TryGetValue("Influence", out JToken result);
if (result != null && result.Type == JTokenType.String) {
influences.Add(faction, result.ToString());
return result.ToString();
}
}
}
return "";
}
}
}