using Newtonsoft.Json.Linq; using EDPlayerJournal.Entries; using System.Runtime.CompilerServices; namespace EDPlayerJournal; public class FactionEffect { public string? Effect { get; set; } public string? EffectLocalised { get; set; } public string? Trend { get; set; } public static FactionEffect FromJSON(JObject obj) { FactionEffect effect = new FactionEffect(); effect.Effect = obj.Value("Effect"); effect.EffectLocalised = obj.Value("Effect_Localised"); effect.Trend = obj.Value("Trend"); return effect; } } public class MissionInfluence { /// /// System Address this influence happened in. /// public ulong? SystemAddress { get; set; } /// /// Generic description of the trend (up or down) /// public string? Trend { get; set; } /// /// Influence gained in plusses /// public string Influence { get; set; } = string.Empty; public long InfluenceAmount { get { string trend = TrendAdjustedInfluence; return (long) (trend.Count(x => x == '-') * -1) + trend.Count(x => x == '+') ; } } /// /// Returns how much influence was made, represented in pluses for positive influence, /// and minuses with negative influence. This takes Trend (up, bad etc.) into account. /// public string TrendAdjustedInfluence { get { if (!string.IsNullOrEmpty(Trend) && Trend.Contains("bad", StringComparison.OrdinalIgnoreCase)) { return new string('-', Influence.Length); } else { return new string('+', Influence.Length); } } } public static MissionInfluence FromJSON(JObject obj) { MissionInfluence missionInfluence = new MissionInfluence(); missionInfluence.SystemAddress = obj.Value("SystemAddress"); missionInfluence.Trend = obj.Value("Trend"); missionInfluence.Influence = obj.Value("Influence") ?? ""; return missionInfluence; } } public class MissionFactionEffects { /// /// Faction in question /// public string? Faction { get; set; } /// /// Reputation trend /// public string? ReputationTrend { get; set; } /// /// Reputation in pluses. /// public string Reputation { get; set; } = string.Empty; /// /// List of economic and security affects. /// public List Effects { get; set; } = new List(); /// /// Influence gained. /// public List Influences { get; set; } = new List(); /// /// Whether this affect is for an empty, or non-existant faction. /// public bool IsEmptyFaction { get { return string.IsNullOrEmpty(Faction); } } public static MissionFactionEffects FromJSON(JObject j) { MissionFactionEffects o = new MissionFactionEffects(); o.Faction = j.Value("Faction"); o.ReputationTrend = j.Value("ReputationTrend"); o.Reputation = j.Value("Reputation") ?? string.Empty; JArray? effects = j.Value("Effects"); if (effects != null) { foreach (JObject effect in effects.Children().OfType()) { FactionEffect e = FactionEffect.FromJSON(effect); o.Effects.Add(e); } } JArray? influences = j.Value("Influence"); if (influences != null) { foreach (JObject influence in influences.Children().OfType()) { MissionInfluence missionInfluence = MissionInfluence.FromJSON(influence); o.Influences.Add(missionInfluence); } } return o; } } public class Mission : IComparable { /// /// Passenger type for refugees /// public static readonly string PassengerTypeRefugee = "Refugee"; public ulong MissionID { get; set; } = 0; /// /// Name of the mission, in machine readable format. /// public string? Name { get; set; } /// /// Localised, human readable mission name. /// public string? LocalisedName { get; set; } /// /// Target of the mission. Optional. /// public string? Target { get; set; } /// /// Target of the mission as a localised human readable string. /// public string? TargetLocalised { get; set; } /// /// Target system of the mission /// public string? DestinationSystem { get; set; } /// /// Destination station. /// public string? DestinationStation { get; set; } /// /// Destination settlement /// public string? DestinationSettlement { get; set; } /// /// In case of redirection, these values denote the new system. /// public string? NewDestinationSystem { get; set; } /// /// In case of redirection, these values denote the new station. /// public string? NewDestinationStation { get; set; } /// /// Faction offering the mission. /// public string? Faction { get; set; } /// /// Target faction (for example for courier missions). /// public string? TargetFaction { get; set; } /// /// Whether the mission is a wing message. /// public bool Wing { get; set; } = false; /// /// Expiry date for the mission. /// public string? Expiry { get; set; } /// /// Certain missions have an expires number. No one knows what that is. /// public ulong? Expires { get; set; } /// /// Influence reward offered. This is for accepting missions only, see the /// mission effects for actual effects once the mission is complete. /// public string? Influence { get; set; } /// /// Reputation reward offered. /// public string? Reputation { get; set; } /// /// Number of kills required for massacre missions. /// public ulong? KillCount { get; set; } /// /// Monatery reward offered for the mission. Optional, as donate missions don't give /// a monatery reward. /// public ulong? Reward { get; set; } /// /// Amount donated for donation missions, optional. If this is null, then /// the mission was not a donation mission. /// public ulong? Donation { get; set; } /// /// Actual amount donated. /// public ulong? Donated { get; set; } /// /// Commodity delivered, or donated. Optional, if this is null, then no /// commodity was donated or delivered. /// public string? Commodity { get; set; } /// /// Amount of the commodity donated or delivered. Optional. /// public ulong? Count { get; set; } /// /// How many passengers are being transported. /// public ulong? PassengerCount { get; set; } /// /// If the passengers are VIPs. /// public bool? PassengerVIPs { get; set; } /// /// If the passengers are wanted. /// public bool? PassengerWanted { get; set; } /// /// What sort of passengers are being transported. /// public string? PassengerType { get; set; } /// /// Affect this mission had on factions. /// public List FactionEffects { get; set; } = new List(); /// /// Returns true if the name is an on foot mission. /// public bool IsOnFoot { get { if (string.IsNullOrEmpty(Name)) { return false; } return Name.Contains("OnFoot"); } } public bool IsPassengerMission { get { if (PassengerCount == null || PassengerCount == 0) { return false; } return true; } } public bool IsRescueMission { get { if (!IsPassengerMission) { return false; } if (string.Compare(PassengerType, PassengerTypeRefugee) == 0) { return true; } return false; } } /// /// Returns a friendly human-readable name for the mission. If a localised name is available /// it will use that, baring that it will check EnglishMissionNames for a translation, and /// if that ain't available either, it will just use the internal Name. /// public string FriendlyName { get { if (!string.IsNullOrEmpty(LocalisedName)) { return LocalisedName; } if (string.IsNullOrEmpty(Name)) { return "Unknown Mission"; } string? translate = EnglishMissionNames.Translate(Name); if (!string.IsNullOrEmpty(translate)) { return translate; } return Name; } } public int CompareTo(Mission? other) { if (other == null) { return 1; } return MissionID.CompareTo(other.MissionID); } private static Mission FromJSON(JObject o) { Mission mission = new Mission(); mission.MissionID = o.Value("MissionID") ?? 0; mission.Reputation = o.Value("Reputation"); mission.Influence = o.Value("Influence"); mission.DestinationSystem = o.Value("DestinationSystem"); mission.DestinationSettlement = o.Value("DestinationSettlement"); mission.DestinationStation = o.Value("DestinationStation"); mission.NewDestinationSystem = o.Value("NewDestinationSystem"); mission.NewDestinationStation = o.Value("NewDestinationSystem"); mission.Reward = o.Value("Reward"); mission.Target = o.Value("Target"); mission.TargetLocalised = o.Value("Target_Localised"); mission.Expiry = o.Value("Expiry"); mission.Expires = o.Value("Expires"); mission.Wing = o.Value("Wing") ?? false; mission.Name = o.Value("Name"); mission.LocalisedName = o.Value("LocalisedName"); mission.Faction = o.Value("Faction"); mission.TargetFaction = o.Value("TargetFaction"); mission.Donation = o.Value("Donation"); mission.Donated = o.Value("Donated"); mission.PassengerCount = o.Value("PassengerCount"); mission.PassengerVIPs = o.Value("PassengerVIPs"); mission.PassengerWanted = o.Value("PassengerWanted"); mission.PassengerType = o.Value("PassengerType"); mission.KillCount = o.Value("KillCount"); JArray? factionEffects = o.Value("FactionEffects"); if (factionEffects != null) { foreach (JObject effect in factionEffects.Children().OfType()) { MissionFactionEffects factionEffect = MissionFactionEffects.FromJSON(effect); mission.FactionEffects.Add(factionEffect); } } return mission; } /// /// Returns a list of all affected factions. /// public string[] AffectedFactions { get { return FactionEffects .Where(x => !string.IsNullOrEmpty(x.Faction)) .Select(x => (x.Faction ?? string.Empty)) .ToArray() ; } } /// /// Returns the influence for a given faction in a given star system. /// /// Faction name in question. /// Star System address /// null if no entry was found, or a string denoting pluses for the amount influence gained. public MissionInfluence[]? GetInfluenceForFaction(string faction, ulong systemaddr) { var results = FactionEffects .Where(x => string.Compare(x.Faction, faction) == 0) .SelectMany(x => x.Influences) .Where(x => (x.SystemAddress != null && x.SystemAddress == systemaddr)) .Select(x => x) .ToArray() ; if (results == null || results.Length == 0) { return new MissionInfluence[0]; } return results; } /// /// A convenient Dictionary containing all influences given out by faction, /// then by system address and then by influence handed out. Influence can /// be either a series of "+" for positive influence, or "-" for negative /// influence. /// public Dictionary> Influences { get { return FactionEffects .Where(x => x.Faction != null) .ToDictionary( x => (x.Faction ?? string.Empty), x => x.Influences .Where(x => x.SystemAddress != null) .ToDictionary( x => (x.SystemAddress ?? 0), x => x ) ); } } public static Mission FromMissionAccepted(JObject o) { return FromJSON(o); } public static Mission FromMissionAbandoned(JObject o) { return FromJSON(o); } public static Mission FromMissionFailed(JObject o) { return FromJSON(o); } public static Mission FromMissionCompleted(JObject o) { return FromJSON(o); } }