-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvent.php
More file actions
619 lines (532 loc) · 21.2 KB
/
Event.php
File metadata and controls
619 lines (532 loc) · 21.2 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
<?php
/**
* Elite Dangerous Star Map
* @link https://www.edsm.net/
*/
namespace Journal;
class Event
{
protected static $isOK = false;
// Default game state
protected static $gameState = [
'systemName' => null,
'systemCoordinates' => null,
'stationName' => null,
'shipId' => null,
'isGuestCrew' => false,
];
// Default return message
protected static $return = [
'msgnum' => 100,
'msg' => 'OK',
];
// Default description
protected static $description = '<ul>
<li>Stored in a temporary table.</li>
</ul>';
// Exclude ships that don''t belong to main list
protected static $notShipTypes = [
'testbuggy',
'empire_fighter',
'federation_fighter',
'independent_fighter',
'gdn_hybrid_fighter_v1',
'gdn_hybrid_fighter_v2',
'gdn_hybrid_fighter_v3',
];
// Exclude some outfitting that don't belong in main list
protected static $excludedOutfitting = [
'$enginecustomisation_blue_name;',
'$modularcargobaydoor_name;',
'$modularcargobaydoorfdl_name;',
'$decal_powerplay_aislingduval_name;',
'$decal_explorer_starblazer_name;',
'$paintjob_testbuggy_luminous_purple_name;',
'$paintjob_testbuggy_tactical_grey_name;',
'$paintjob_testbuggy_vibrant_red_name;',
'$paintjob_federation_fighter_gladiator_blueblack_name;',
'$paintjob_indfighter_vibrant_yellow_name;',
'$adder_cockpit_name;',
'$anaconda_cockpit_name;',
'$asp_cockpit_name;',
'$asp_scout_cockpit_name;',
'$belugaliner_cockpit_name;',
'$cobramkiii_cockpit_name;',
'$cobramkiv_cockpit_name;',
'$cutter_cockpit_name;',
'$diamondback_cockpit_name;',
'$diamondbackxl_cockpit_name;',
'$dolphin_cockpit_name;',
'$eagle_cockpit_name;',
'$empire_courier_cockpit_name;',
'$empire_trader_cockpit_name;',
'$empire_eagle_cockpit_name;',
'$federation_corvette_cockpit_name;',
'$federation_dropship_cockpit_name;',
'$federation_dropship_mkii_cockpit_name;',
'$federation_gunship_cockpit_name;',
'$ferdelance_cockpit_name;',
'$hauler_cockpit_name;',
'$independant_trader_cockpit_name;',
'$krait_mkii_cockpit_name;',
'$krait_light_cockpit_name;',
'$mamba_cockpit_name;',
'$orca_cockpit_name;',
'$python_cockpit_name;',
'$sidewinder_cockpit_name;',
'$type6_cockpit_name;',
'$type9_cockpit_name;',
'$type7_cockpit_name;',
'$type9_military_cockpit_name;',
'$typex_cockpit_name;',
'$typex_2_cockpit_name;',
'$typex_3_cockpit_name;',
'$viper_cockpit_name;',
'$viper_mkiv_cockpit_name;',
'$vulture_cockpit_name;',
];
// Store current user/software
protected static $user = null;
protected static $softwareId = null;
// Default method for unknown events
public static function run($json)
{
if(!is_null(static::$user) && !is_null(static::$softwareId))
{
try
{
$insert = array();
$insert['refUser'] = static::$user->getId();
$insert['refSoftware'] = static::$softwareId;
$insert['event'] = $json['event'];
$insert['gameState'] = \Zend_Json::encode(static::$gameState);
$insert['dateEvent'] = $json['timestamp'];
// We do not need event and timestamps in the final JSON
unset($json['event'], $json['timestamp']);
// If the message came from an erroneous events, store it for further processing
if(array_key_exists('isError', $json))
{
$insert['isError'] = 1;
unset($json['isError']);
}
$insert['message'] = \Zend_Json::encode($json);
if(in_array($insert['event'], ['Scan', 'SAAScanComplete']))
{
$journalScannerModel = new \Models_Journal_Scanner;
$journalScannerModel->insert($insert);
}
else
{
$journalModel = new \Models_Journal;
$journalModel->insert($insert);
}
}
catch(\Zend_Db_Exception $e)
{
if(static::$return['msgnum'] != 402)
{
// Based on unique index, this journal entry was already saved.
if(strpos($e->getMessage(), '1062 Duplicate') !== false)
{
// If it's an error, update the already existing journal
// Throw a 500 to avoid our reparser deleting the message yet
if(array_key_exists('isError', $insert) && $insert['isError'] == 1)
{
static::$return['msgnum'] = 500;
static::$return['msg'] = 'Message needs to be checked';
}
else
{
static::$return['msgnum'] = 101;
static::$return['msg'] = 'Message already stored';
}
}
else
{
static::$return['msgnum'] = 500;
static::$return['msg'] = 'Exception: ' . $e->getMessage();
if(defined('APPLICATION_SENTRY') && APPLICATION_SENTRY === true)
{
\Sentry\captureException($e);
}
}
}
}
}
return static::$return;
}
/**
* Init methods
*/
public static function setUser($user)
{
if($user instanceof \Component\User)
{
static::$user = $user;
}
}
public static function setSoftware($softwareId)
{
static::$softwareId = $softwareId;
}
public static function setGameState($gameState)
{
static::$gameState = $gameState;
}
public static function getDescription($raw = false)
{
if($raw === false)
{
if(is_array(static::$description))
{
return '<ul><li>' . implode('</li></li>', static::$description) . '</li></ul>';
}
}
return static::$description;
}
public static function isOK()
{
return static::$isOK;
}
public static function resetReturnMessage()
{
static::$return['msgnum'] = 100;
static::$return['msg'] = 'OK';
}
/**
* COMMON METHODS
*/
protected static function updateCurrentGameShipId($newShipId, $newShipTimestamp)
{
$currentShipId = static::$user->getCurrentGameShipId();
$lastCurrentShipUpdate = static::$user->getCurrentGameShipIdLastUpdate();
if($currentShipId != $newShipId || is_null($lastCurrentShipUpdate))
{
// If newer or null, update the ship ID
if(is_null($lastCurrentShipUpdate) || strtotime($lastCurrentShipUpdate) < strtotime($newShipTimestamp))
{
if($newShipId == 4294967295)
{
$newShipId = new \Zend_Db_Expr('NULL');
}
else
{
$newShipId = (int) $newShipId;
}
$usersModel = new \Models_Users;
$usersModel->updateById(
static::$user->getId(),
[
'currentShipId' => $newShipId,
'lastCurrentShipUpdate' => $newShipTimestamp,
]
);
}
}
}
/**
* EVENTS DETAILS
*/
protected static function findSystemId($json, $preventRenamedSystems = false, $insertSystemIfNeeded = false)
{
$systemsModel = new \Models_Systems;
$systemName = null;
$systemAddress = null;
$systemCoordinates = null;
// If event contains SystemAddress
if(array_key_exists('SystemAddress', $json))
{
$system = $systemsModel->getById64($json['SystemAddress']);
$systemAddress = $json['SystemAddress'];
if(!is_null($system))
{
return $system['id'];
}
}
// If transient state, take the transient state
if(array_key_exists('_systemAddress', $json) && !is_null($json['_systemAddress']) && !empty($json['_systemAddress']))
{
$system = $systemsModel->getById64($json['_systemAddress']);
if(!is_null($system))
{
return $system['id'];
}
}
// Some events have the StarSystem
if(array_key_exists('event', $json) && in_array($json['event'], ['Docked', 'Location', 'Scan']) && array_key_exists('StarSystem', $json))
{
$systemName = $json['StarSystem'];
if(array_key_exists('StarPos', $json)) // Not in Docked
{
$systemCoordinates = $json['StarPos'];
}
}
// If transient state, take the transient state
elseif(array_key_exists('_systemName', $json) && !is_null($json['_systemName']))
{
$systemName = $json['_systemName'];
if(array_key_exists('_systemCoordinates', $json))
{
$systemCoordinates = $json['_systemCoordinates'];
}
}
// If multiple event have fed the transient state
elseif(!is_null(static::$gameState['systemName']))
{
$systemName = static::$gameState['systemName'];
if(!is_null(static::$gameState['systemCoordinates']))
{
$systemCoordinates = static::$gameState['systemCoordinates'];
}
}
// We have a system name, proceed
if(!is_null($systemName))
{
// Convert coordinates to EDSM format
if(!is_null($systemCoordinates))
{
if($systemCoordinates[0] == 'NaN' || is_nan((float) $systemCoordinates[0]))
{
// Reset old converted netLog
$systemCoordinates = null;
}
else
{
$systemCoordinates = array(
'x' => round($systemCoordinates[0] * 32),
'y' => round($systemCoordinates[1] * 32),
'z' => round($systemCoordinates[2] * 32),
);
}
}
$systemName = trim($systemName);
$system = $systemsModel->getByName($systemName);
// System creation
if(is_null($system) && !is_null($systemName))
{
$insertSystem = array();
$insertSystem['name'] = $systemName;
if(!is_null($systemAddress))
{
$insertSystem['id64'] = $systemAddress;
}
if(!is_null($systemCoordinates))
{
$insertSystem = array_merge($insertSystem, $systemCoordinates);
}
if($insertSystemIfNeeded === true)
{
try
{
$systemId = $systemsModel->insert($insertSystem);
}
catch(\Zend_Db_Exception $e)
{
if(strpos($e->getMessage(), '1062 Duplicate') !== false) // Can happen when the same system is submitted twice during the process
{
if(!is_null($systemAddress))
{
$system = $systemsModel->getById64($json['SystemAddress']);
if(!is_null($system))
{
return $system['id'];
}
}
else
{
$system = $systemsModel->getByName($systemName);
if(!is_null($system))
{
return $system['id'];
}
}
}
}
if(!is_null($systemId))
{
return $systemId;
}
}
}
if(!is_null($system))
{
$currentSystem = \Component\System::getInstance($system['id']);
// Check system renamed/merged to another
if($currentSystem->isHidden() === true)
{
$mergedTo = $currentSystem->getMergedTo();
if(!is_null($mergedTo) && $preventRenamedSystems === false)
{
// Switch systems when they have been renamed
$currentSystem = \Component\System::getInstance($mergedTo);
}
else
{
return null;
}
}
// Check if system have duplicates
$duplicates = $currentSystem->getDuplicates();
if(!is_null($duplicates) && is_array($duplicates) && count($duplicates) > 0)
{
// We don't have coordinates to check duplicates...
if(is_null($systemCoordinates))
{
return null;
}
else
{
if($systemCoordinates['x'] != $currentSystem->getX() || $systemCoordinates['y'] != $currentSystem->getY() || $systemCoordinates['z'] != $currentSystem->getZ())
{
foreach($duplicates AS $duplicate)
{
$duplicateSystem = \Component\System::getInstance($duplicate);
if($systemCoordinates['x'] == $duplicateSystem->getX() && $systemCoordinates['y'] == $duplicateSystem->getY() && $systemCoordinates['z'] == $duplicateSystem->getZ())
{
if(array_key_exists('SystemAddress', $json) && is_null($duplicateSystem->getId64()))
{
$systemsModel->updateById(
$duplicateSystem->getId(),
[
'id64' => $json['SystemAddress'],
]
);
}
return $duplicateSystem->getId();
}
unset($duplicateSystem);
}
}
else
{
if(array_key_exists('SystemAddress', $json) && is_null($currentSystem->getId64()))
{
$systemsModel->updateById(
$currentSystem->getId(),
[
'id64' => $json['SystemAddress'],
]
);
}
return $currentSystem->getId();
}
}
}
else
{
if(array_key_exists('SystemAddress', $json) && is_null($currentSystem->getId64()))
{
$systemsModel->updateById(
$currentSystem->getId(),
[
'id64' => $json['SystemAddress'],
]
);
}
return $currentSystem->getId();
}
}
}
// If timestamp is after last location
return null;
}
protected static function findStationId($json)
{
$systemId = null;
$stationName = null;
// If event contain MarketID
if(array_key_exists('MarketID', $json))
{
$stationsModel = new \Models_Stations;
$station = $stationsModel->getByMarketId($json['MarketID']);
if(!is_null($station))
{
return $station['id'];
}
}
// If transient state, take the transient state
if(array_key_exists('_marketId', $json) && !is_null($json['_marketId']) && !empty($json['_marketId']))
{
$stationsModel = new \Models_Stations;
$station = $stationsModel->getByMarketId($json['_marketId']);
if(!is_null($station))
{
return $station['id'];
}
}
// Some events have the StationName
if(in_array($json['event'], ['Docked', 'Location']) && array_key_exists('StationName', $json))
{
$stationName = $json['StationName'];
}
// If transient state, take the transient state
elseif(array_key_exists('_stationName', $json) && !is_null($json['_stationName']))
{
$stationName = $json['_stationName'];
}
// If multiple event have fed the transient state
elseif(!is_null(static::$gameState['stationName']))
{
$stationName = static::$gameState['stationName'];
}
if(!is_null($stationName))
{
$systemId = static::findSystemId($json);
if(!is_null($systemId))
{
$currentSystem = \Component\System::getInstance($systemId);
$stations = $currentSystem->getStations(true);
foreach($stations AS $station)
{
$currentStation = \EDSM_System_Station::getInstance($station['id']);
if($currentStation->getName() == $stationName)
{
if(array_key_exists('MarketID', $json) && is_null($currentStation->getMarketId()))
{
$stationsModel = new \Models_Stations;
$stationsModel->updateById(
$currentStation->getId(),
[
'marketId' => $json['MarketID'],
]
);
}
return $currentStation->getId();
}
}
}
else
{
// Find if it's a unique station
$stationsModel = new \Models_Stations;
$stations = $stationsModel->fetchAll(
$stationsModel->select()->where('name = ?', $stationName)
);
if(!is_null($stations) && count($stations) == 1)
{
$stations = $stations->toArray();
return $stations[0]['id'];
}
}
}
//TODO: If timestamp is after last docking, take the docked station
return null;
}
protected static function findShipId($json)
{
// If transient state, take the transient state
if(array_key_exists('_shipId', $json) && !is_null($json['_shipId']))
{
if((int) $json['_shipId'] >= 0)
{
return $json['_shipId'];
}
}
// If multiple event have fed the transient state
if(!is_null(static::$gameState['shipId']))
{
return static::$gameState['shipId'];
}
return null;
}
}