-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathFrontierApiStation.cs
More file actions
279 lines (250 loc) · 12.8 KB
/
FrontierApiStation.cs
File metadata and controls
279 lines (250 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using Utilities;
namespace EddiDataDefinitions
{
public class FrontierApiStation
{
/// <summary>Unique 64 bit id value for station</summary>
public long? marketId { get; set; }
/// <summary>The name</summary>
public string name { get; set; }
/// <summary>A list of the services offered by this station</summary>
public List<KeyValuePair<string, string>> stationServices { get; set; } = [ ];
/// <summary>What are the economies at the station, with proportions for each</summary>
public List<FrontierApiEconomyShare> economyShares { get; set; } = [ ];
/// <summary>Commodity market quotes as-received from the profile</summary>
public List<MarketInfoItem> eddnCommodityMarketQuotes { get; set; } = [ ];
/// <summary>Prohibited commodities as-received from the profile</summary>
public List<KeyValuePair<long, string>> prohibitedCommodities { get; set; } = [ ];
/// <summary>Outfitting modules as-received from the profile</summary>
public List<OutfittingInfoItem> outfitting { get; set; } = [ ];
/// <summary>Ship models as-received from the profile</summary>
public List<ShipyardInfoItem> ships { get; set; } = [ ];
/// <summary>The market JSON object</summary>
public JObject marketJson { get; set; }
/// <summary>The shipyard JSON object</summary>
public JObject shipyardJson { get; set; }
// Admin - the last time the market information present changed
public DateTime commoditiesupdatedat;
// Admin - the last time the outfitting information present changed
public DateTime outfittingupdatedat;
// Admin - the last time the shipyard information present changed
public DateTime shipyardupdatedat;
public static FrontierApiStation FromJson(JObject marketJson, JObject shipyardJson)
{
if (marketJson != null && shipyardJson != null && marketJson["id"]?.ToObject<ulong>() != shipyardJson["id"]?.ToObject<ulong>())
{
throw new ArgumentException("Frontier API market and shipyard endpoint data are not synchronized.");
}
// Obtain the list of station economies
List<FrontierApiEconomyShare> EconomiesFromProfile(JObject json)
{
var economyShares = new List<FrontierApiEconomyShare>();
if (json != null && json["economies"] != null)
{
foreach (dynamic economyJson in json["economies"])
{
var economy = economyJson.Value;
var name = ((string)economy["name"]).Replace("Agri", "Agriculture");
var proportion = (decimal)economy["proportion"];
economyShares.Add(new FrontierApiEconomyShare(name, proportion));
}
}
economyShares = economyShares.OrderByDescending(x => x.proportion).ToList();
Logging.Debug("Economies are: ", economyShares);
return economyShares;
}
// Obtain the list of commodities
List<MarketInfoItem> CommodityQuotesFromProfile(JObject json)
{
var eddnCommodityMarketQuotes = new List<MarketInfoItem>();
if (json?["commodities"] != null)
{
eddnCommodityMarketQuotes = json["commodities"]
.Select(c => JsonConvert.DeserializeObject<MarketInfoItem>(c.ToString())).ToList();
}
return eddnCommodityMarketQuotes;
}
// Obtain the list of prohibited commodities
List<KeyValuePair<long, string>> ProhibitedCommoditiesFromProfile(JObject json)
{
var edProhibitedCommodities = new List<KeyValuePair<long, string>>();
if (json != null && json["prohibited"] != null)
{
foreach (var jToken in json["prohibited"])
{
var prohibitedJSON = (JProperty)jToken;
var prohibitedCommodity = new KeyValuePair<long, string>(long.Parse(prohibitedJSON.Name), prohibitedJSON.Value.ToString());
edProhibitedCommodities.Add(prohibitedCommodity);
}
}
Logging.Debug("Prohibited Commodities are: ", edProhibitedCommodities.Select(c => c.Value).ToList());
return edProhibitedCommodities;
}
// Obtain the list of outfitting modules
List<OutfittingInfoItem> OutfittingFromProfile(JObject json)
{
var edModules = new List<OutfittingInfoItem>();
if (json != null && json["modules"] != null)
{
foreach (var jToken in json["modules"])
{
var moduleJsonProperty = (JProperty)jToken;
var moduleJson = (JObject)moduleJsonProperty.Value;
// Not interested in paintjobs, decals, ...
var moduleCategory =
(string)moduleJson[
"category"]; // need to convert from LINQ to string
switch (moduleCategory)
{
case "weapon":
case "module":
case "utility":
{
edModules.Add(
JsonConvert.DeserializeObject<OutfittingInfoItem>(
moduleJson.ToString()));
}
break;
}
}
}
return edModules;
}
// Obtain the list of ships available at the station from the profile
List<ShipyardInfoItem> ShipyardFromProfile(JObject json)
{
List<ShipyardInfoItem> edShipyardShips = [ ];
if (json?[nameof(ships)] != null)
{
edShipyardShips = json[nameof(ships)]?["shipyard_list"]?.Children().Values()
.Select(s => JsonConvert.DeserializeObject<ShipyardInfoItem>(s.ToString()))
.ToList() ?? [ ];
if (json[nameof( ships ) ]["unavailable_list"] != null)
{
edShipyardShips.AddRange( ( json[ nameof(ships) ][ "unavailable_list" ] ?? new JArray() )
.Select( s => JsonConvert.DeserializeObject<ShipyardInfoItem>( s.ToString() ) ).ToList() );
}
}
return edShipyardShips;
}
FrontierApiStation lastStation = null;
try
{
var lastStarport = ((string)marketJson?[nameof(name)])?.ReplaceEnd('+') ?? ((string)shipyardJson?[nameof(name)])?.ReplaceEnd('+');
var marketId = (long?)marketJson?["id"] ?? (long?)shipyardJson?["id"];
lastStation = new FrontierApiStation
{
name = lastStarport,
marketId = marketId,
};
if (marketJson != null)
{
lastStation.economyShares = EconomiesFromProfile(marketJson);
lastStation.eddnCommodityMarketQuotes = CommodityQuotesFromProfile(marketJson);
lastStation.prohibitedCommodities = ProhibitedCommoditiesFromProfile(marketJson);
lastStation.commoditiesupdatedat = marketJson["timestamp"]?.ToObject<DateTime?>() ?? DateTime.MinValue;
lastStation.marketJson = marketJson;
var stationServices = new List<KeyValuePair<string, string>>();
foreach (var jToken in marketJson["services"])
{
// These are key value pairs. The Key is the name of the service, the Value is its state.
var serviceJSON = (JProperty)jToken;
var service = new KeyValuePair<string, string>(serviceJSON.Name, serviceJSON.Value.ToString());
stationServices.Add(service);
}
lastStation.stationServices = stationServices;
}
if (shipyardJson != null)
{
lastStation.outfitting = OutfittingFromProfile(shipyardJson);
lastStation.ships = ShipyardFromProfile(shipyardJson);
lastStation.shipyardJson = shipyardJson;
lastStation.outfittingupdatedat = shipyardJson["timestamp"]?.ToObject<DateTime?>() ?? DateTime.MinValue;
lastStation.shipyardupdatedat = shipyardJson["timestamp"]?.ToObject<DateTime?>() ?? DateTime.MinValue;
}
}
catch (JsonException ex)
{
Logging.Error("Failed to parse companion station data", ex);
}
Logging.Debug("Station is: ", lastStation);
return lastStation;
}
public Station UpdateStation(DateTime profileTimeStamp, Station stationToUpdate)
{
Logging.Debug($"Updating station from Frontier API.", new Dictionary<string, object>()
{
{ "Station To Update", stationToUpdate },
{ "Frontier API Data", this }
});
if (stationToUpdate is null)
{
return null;
}
if (stationToUpdate.updatedat > Dates.fromDateTimeToSeconds(profileTimeStamp))
{
// The current station is already more up to date
return stationToUpdate;
}
if (stationToUpdate.marketId != marketId)
{
// The market IDs do not match, the stations are not the same
return stationToUpdate;
}
try
{
stationToUpdate.economyShares = economyShares.Select(e => e.ToEconomyShare()).ToList();
stationToUpdate.stationServices = stationServices.Select(s => StationService.FromEDName(s.Key)).ToList();
}
catch (Exception e)
{
Logging.Error("Failed to update station economy and services from profile.", e);
}
if (commoditiesupdatedat != DateTime.MinValue &&
(stationToUpdate.commoditiesupdatedat ?? 0) < Dates.fromDateTimeToSeconds(commoditiesupdatedat))
{
try
{
stationToUpdate.commodities = eddnCommodityMarketQuotes.Select(c => c.ToCommodityMarketQuote()).Where(c => c != null).ToList();
stationToUpdate.prohibited = prohibitedCommodities.Select(p => CommodityDefinition.CommodityDefinitionFromEliteID(p.Key, p.Value) ?? CommodityDefinition.FromEDName(p.Value)).ToList();
stationToUpdate.commoditiesupdatedat = Dates.fromDateTimeToSeconds(commoditiesupdatedat);
}
catch (Exception e)
{
Logging.Error("Failed to update station market from profile.", e);
}
}
if (outfittingupdatedat != DateTime.MinValue && (stationToUpdate.outfittingupdatedat ?? 0) < Dates.fromDateTimeToSeconds(outfittingupdatedat))
{
try
{
stationToUpdate.outfitting = outfitting.Select(m => m.ToModule()).Where(m => m != null).ToList();
stationToUpdate.outfittingupdatedat = Dates.fromDateTimeToSeconds(outfittingupdatedat);
}
catch (Exception e)
{
Logging.Error("Failed to update station outfitting from profile.", e);
}
}
if (shipyardupdatedat != DateTime.MinValue && (stationToUpdate.shipyardupdatedat ?? 0) < Dates.fromDateTimeToSeconds(shipyardupdatedat))
{
try
{
stationToUpdate.shipyard = ships.Select(s => s.ToShip()).Where(s => s != null).ToList();
stationToUpdate.shipyardupdatedat = Dates.fromDateTimeToSeconds(shipyardupdatedat);
}
catch (Exception e)
{
Logging.Error("Failed to update station shipyard from profile.", e);
}
}
stationToUpdate.updatedat = Dates.fromDateTimeToSeconds(profileTimeStamp);
return stationToUpdate;
}
}
}