-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathEDSMResponder.cs
269 lines (241 loc) · 10 KB
/
EDSMResponder.cs
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
using EddiConfigService;
using EddiCore;
using EddiDataDefinitions;
using EddiEvents;
using EddiStarMapService;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Windows.Controls;
using Utilities;
namespace EddiEdsmResponder
{
public class EDSMResponder : IEddiResponder
{
private Thread updateThread;
private List<string> ignoredEvents = new List<string>();
private readonly StarMapService edsmService;
// This responder currently requires game version 4.0 or later.
private static readonly System.Version minGameVersion = new System.Version(4, 0);
public string ResponderName()
{
return Properties.EDSMResources.ResourceManager.GetString( "name", CultureInfo.InvariantCulture );
}
public string LocalizedResponderName()
{
return Properties.EDSMResources.name;
}
public string ResponderDescription()
{
return Properties.EDSMResources.desc;
}
public EDSMResponder() : this(new StarMapService(null, true))
{ }
public EDSMResponder(StarMapService edsmService)
{
this.edsmService = edsmService;
Logging.Info($"Initialized {ResponderName()}");
}
public bool Start()
{
Reload();
edsmService?.StartJournalSync();
return edsmService != null;
}
public void Stop()
{
edsmService?.StopJournalSync();
// Stop flight log synchronization
updateThread?.Abort();
updateThread = null;
}
public void Reload()
{
// Set up the star map service
if (ignoredEvents == null)
{
ignoredEvents = edsmService?.getIgnoredEvents();
}
if (edsmService != null)
{
// Renew our credentials for the EDSM API
StarMapService.inGameCommanderName = EDDI.Instance.Cmdr?.name;
edsmService.SetEdsmCredentials();
if (updateThread == null && edsmService.EdsmCredentialsSet())
{
// Spin off a thread to download & sync flight logs & system comments from EDSM in the background
updateThread = new Thread(() => EDDI.Instance.DataProvider.syncFromStarMapService(ConfigService.Instance.edsmConfiguration?.lastFlightLogSync))
{
IsBackground = true,
Name = "EDSM updater"
};
updateThread.Start();
}
}
}
public void Handle(Event theEvent)
{
if (EDDI.Instance.inTelepresence)
{
// We don't do anything whilst in CQC
return;
}
if (EDDI.Instance.gameIsBeta)
{
// We don't send data whilst in beta
return;
}
if (EDDI.Instance.GameVersion is null || EDDI.Instance.GameVersion < minGameVersion)
{
// We don't sent data whilst running a lower game version than the minimum required by EDSM
return;
}
if (edsmService != null)
{
// Retrieve applicable transient game state info (metadata)
// for the event and send the event with transient info to EDSM
IDictionary<string, object> eventObject = null;
try
{
eventObject = prepareEventData(theEvent);
}
catch (System.Exception ex)
{
Logging.Error("Failed to prepare event meta-data for submittal to EDSM", ex);
}
if (eventObject != null && !EDDI.Instance.gameIsBeta)
{
edsmService.EnqueueEvent(eventObject);
}
}
}
private IDictionary<string, object> prepareEventData(Event theEvent)
{
// Prep transient game state info (metadata) per https://www.edsm.net/en/api-journal-v1.
// Unpackage the event, add transient game state info as applicable, then repackage and send the event
var eventObject = Deserializtion.DeserializeData(theEvent.raw);
var eventType = JsonParsing.getString(eventObject, "event");
if (ignoredEvents.Contains(eventType) || theEvent.raw == null)
{
return null;
}
// Add metadata from events
switch (eventType)
{
case "LoadGame":
{
eventObject.Add("_systemAddress", null);
eventObject.Add("_systemName", null);
eventObject.Add("_systemCoordinates", null);
eventObject.Add("_marketId", null);
eventObject.Add("_stationName", null);
break;
}
case "ShipyardBuy":
{
eventObject.Add("_shipId", null);
break;
}
case "SetUserShipName":
case "ShipyardSwap":
case "Loadout":
{
eventObject.TryGetValue("ShipID", out object shipIdVal);
if (shipIdVal != null)
{
eventObject.Add("_shipId", (int)(long)shipIdVal);
}
break;
}
case "Undocked":
{
eventObject.Add("_marketId", null);
eventObject.Add("_stationName", null);
break;
}
case "Location":
case "FSDJump":
case "Docked":
{
if (eventObject.ContainsKey("StarSystem"))
{
eventObject.Add("_systemName", JsonParsing.getString(eventObject, "StarSystem"));
}
if (eventObject.ContainsKey("SystemAddress"))
{
long? systemAddress = JsonParsing.getOptionalLong(eventObject, "SystemAddress");
// Some events are bugged and return a SystemAddress of 1, regardles of the system we are in.
// We need to ignore data that matches this pattern.
systemAddress = (systemAddress > 1 ? systemAddress : null);
if (systemAddress != null)
{
eventObject.Add("_systemAddress", systemAddress);
}
}
if (eventObject.ContainsKey("StarPos"))
{
eventObject.TryGetValue("StarPos", out object starpos);
if (starpos != null)
{
eventObject.Add("_systemCoordinates", starpos);
}
}
if (eventObject.ContainsKey("MarketID"))
{
eventObject.Add("_marketId", JsonParsing.getOptionalLong(eventObject, "MarketID"));
}
if (eventObject.ContainsKey("StationName"))
{
eventObject.Add("_stationName", JsonParsing.getString(eventObject, "StationName"));
}
break;
}
}
// Supplement with metadata from the tracked game state, as applicable
var currentStarSystemAddress = EDDI.Instance.CurrentStarSystem?.systemAddress;
if ( currentStarSystemAddress != null && !eventObject.ContainsKey( "_systemAddress" ) )
{
eventObject.Add( "_systemAddress", currentStarSystemAddress );
}
var currentStarSystemName = EDDI.Instance.CurrentStarSystem?.systemname;
if ( !string.IsNullOrEmpty( currentStarSystemName ) && !eventObject.ContainsKey( "_systemName" ) )
{
eventObject.Add( "_systemName", currentStarSystemName );
}
var currentStarSystemCoordinates = new []
{
EDDI.Instance.CurrentStarSystem?.x,
EDDI.Instance.CurrentStarSystem?.y,
EDDI.Instance.CurrentStarSystem?.z
};
if ( currentStarSystemCoordinates.All( c => c != null ) &&
!eventObject.ContainsKey( "_systemCoordinates" ) )
{
eventObject.Add( "_systemCoordinates", currentStarSystemCoordinates );
}
var currentStationMarketID = EDDI.Instance.CurrentStation?.marketId;
if ( currentStationMarketID != null && !eventObject.ContainsKey( "_marketId" ) )
{
eventObject.Add( "_marketId", currentStationMarketID );
}
var currentStationName = EDDI.Instance.CurrentStation?.name;
if ( !string.IsNullOrEmpty( currentStationName ) && !eventObject.ContainsKey( "_stationName" ) )
{
eventObject.Add( "_stationName", currentStationName );
}
var currentShipID = EDDI.Instance.CurrentShip?.LocalId;
if (currentShipID != null && !eventObject.ContainsKey("_shipId"))
{
eventObject.Add("_shipId", currentShipID);
}
return eventObject;
}
public UserControl ConfigurationTabItem()
{
return new ConfigurationWindow(this);
}
public void HandleStatus ( Status status )
{ }
}
}