forked from durzel/wormhol.es
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_killboard.php
More file actions
1454 lines (1288 loc) · 65.9 KB
/
Copy pathclass_killboard.php
File metadata and controls
1454 lines (1288 loc) · 65.9 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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?
ini_set('set_time_limit',120);
ini_set('max_execution_time',120);
ini_set('memory_limit','256M');
class Killboard {
private $isValidKB = false;
private $uniqID = null;
public $kill = array();
// We keep track of various stats on each corporation that has been active in some capacity in the system.
// We don't keep track (outside of the kill data) of individual pilot or alliance activity simply because
// this information is not important to us for diagnosing intel.
public $corp = array();
// Values that keep track of various metrics that we use elsewhere
public $res_metrics = array(
"totalScore" => 0,
"minScore" => null,
"maxScore" => 0,
"topCorp" => null,
"bottomCorp" => null,
"newestKill" => null,
"oldestKill" => null,
"battleCount" => 0,
"alliance" => array()
);
public $ctx;
// The constructor takes as input the JSON output of an Eve-Kill EPIC API scrape
public function __construct($uniqID, $kbCacheID, $kbCacheTime, $kbURL, $useCache = CACHE_USE_CACHE_UPDATE_NEW_DATA, $doCalcRes = true) {
$this->uniqID = $uniqID;
// Stream context for file_get_contents
$this->ctx = stream_context_create(array('http' => array('timeout' => EVEKILL_SOCKET_TIMEOUT_SECONDS, 'ignore_errors' => true)));
$jsonEKData = self::scrapeEveKill($uniqID, $kbCacheID, $kbCacheTime, $kbURL, $useCache);
// If this fails, try and forcibly use the cache
if (!is_array($jsonEKData)) $jsonEKData = self::scrapeEveKill($uniqID, $kbCacheID, $kbCacheTime, $kbURL, CACHE_FORCE_USE_CACHE);
if (is_array($jsonEKData)) {
// We have a valid killboard
$this->isValidKB = true;
$cacheFile = sprintf("%s/%s_kbdata.dat", realpath(CACHE_DIRECTORY), str_replace(" ","_",strtoupper($uniqID)));
if ($jsonEKData["used_cache"] && is_readable($cacheFile) && CACHE_KILLBOARD_DATA_CACHE) {
dprintf("[Killboard] Re-initialising killboard with cached data from %s (created: %s)", $cacheFile, date("Y-m-d H:i:s", filemtime($cacheFile)));
// Initialise this object with the data from the cache
$cacheKB = unserialize(gzinflate(file_get_contents($cacheFile)));
$this->kill = $cacheKB->kill;
$this->corp = $cacheKB->corp;
$this->res_metrics = $cacheKB->res_metrics;
unset($cacheKB);
} else {
dprintf("[Killboard] Killboard being created from scratch, cache not used or cache file does not exist.");
foreach ($jsonEKData["ekdata"] as $aKill) {
$this->kill[] = new Kill($aKill, $this);
}
// -------------------------------------------------------------------------------------------
// We now carry out some corp sub-database tidyup, since this is the database that we will use
// to provide intel
// Populate the corp catalogue with missing IDs by polling the database cache, and if
// that fails getting it from the EVE API.
$aryMissingIDName = array();
foreach ($this->corp as $aCorp) {
// Sanitise corp/alliance name if it is invalid
$this->fixBrokenEKData($aCorp);
if (is_null($aCorp->corporationID) && !in_array($aCorp->corporationName,$aryMissingIDName)) $aryMissingIDName[] = $aCorp->corporationName;
if (is_null($aCorp->allianceID) && !in_array($aCorp->allianceName,$aryMissingIDName)) $aryMissingIDName[] = $aCorp->allianceName;
}
if (sizeof($aryMissingIDName) > 0) {
$idArray = $this->getIDOrNameFromEveAPI($aryMissingIDName);
if (!is_null($idArray)) {
// Update our corporation database with our newly found IDs
// We have to create a temporary array to uppercase the entries because the database
// writes the EVE API result as the names are in game, which is not necessarily what
// they will be on the kills (strange but true)
$ucArray = $idArray;
for ($n = 0; $n < sizeof($ucArray); $n++) { $ucArray[$n] = strtoupper($ucArray[$n]["name"]); };
// Loop through the corps we have stored, and update the IDs as we find a match
foreach ($this->corp as $aCorp) {
if (($nKey = array_search(strtoupper($aCorp->corporationName), $ucArray)) !== false) $aCorp->corporationID = $idArray[$nKey]["id"];
if (($nKey = array_search(strtoupper($aCorp->allianceName), $ucArray)) !== false) $aCorp->allianceID = $idArray[$nKey]["id"];
}
unset($ucArray);
}
}
// -------------------------------------------------------------------------------------------
// Update metrics (these are required for calcResidency)
$this->res_metrics["newestKill"] = &$this->getSysNewestKill();
$this->res_metrics["oldestKill"] = &$this->getSysOldestKill();
$this->res_metrics["battleCount"] = $this->getTotalBattleCount();
if ($doCalcRes) {
$aTempWH = new Wormhole($this->uniqID);
if ($aTempWH->isValidLocus() && $aTempWH->isWHLocus()) {
// Calculate residency scores
$this->calcResidency();
// Update score metrics
// Note: Total residency score is populated by calcResidency(), because it uses it
$this->res_metrics["maxScore"] = $this->getHighestResidencyScore();
$this->res_metrics["minScore"] = $this->getLowestResidencyScore();
$this->res_metrics["topCorp"] = &$this->getTopResident();
$this->res_metrics["bottomCorp"] = &$this->getBottomResident();
}
// Write killboard cache to disk
if (file_exists($cacheFile)) unlink($cacheFile);
file_put_contents($cacheFile, gzdeflate(serialize($this)));
}
}
}
}
// This function scrapes Eve-kill until we have the last EVEKILL_KILL_COUNT_FOR_INTEL kills with which
// to do analysis. Because this scraping is time and resource intensive, and at risk of causing
// blacklisting, we save the results to a cache file. If this cache file exists and has not expired
// we use that instead of polling eve-kill at all.
private function scrapeEveKill($locusID, $kbCacheID, $kbCacheTime, $kbURL, $cacheUse) {
$ekData = null;
// Check to see whether cache file exists
$haveCache = false;
$addedNewRecords = false;
/* If we're not using EveKill, we force using the cache (if it doesn't exist we won't have intel) */
if (DISABLE_EVEKILL_COMPLETELY) $cacheUse = CACHE_FORCE_USE_CACHE;
dprintf('scrapeEveKill(): cache time: %d, cur time: %d, cacheUse: %d, kbURL: %s', $kbCacheTime, time(), $cacheUse, $kbURL);
if ($cacheUse != CACHE_FORCE_FULL_REFRESH) {
$cacheFile = sprintf("%s/%s_%s.json", realpath(CACHE_DIRECTORY), str_replace(" ","_",strtoupper($locusID)), strtolower($kbCacheID));
// Get mail limit boundary (used in loops)
$foundMailLimit = preg_match('/mailLimit:(\d+)/', $kbURL, $sMailLimit);
$sMailLimit = ($foundMailLimit === 0) ? EVEKILL_KILL_COUNT_FOR_INTEL : $sMailLimit[1];
if (is_readable($cacheFile)) {
dprintf('scrapeEveKill(): cache file exists');
$ekData = json_decode(gzinflate(file_get_contents($cacheFile, false, $this->ctx)));
if (!is_array($ekData) || empty($ekData)) {
// Cache is completely empty, so disregard it
$cacheUse = CACHE_FORCE_FULL_REFRESH;
} elseif ($cacheUse == CACHE_FORCE_USE_CACHE || (strtotime("+".$kbCacheTime." seconds", filemtime($cacheFile)) - time()) > 0) {
// Found a valid cache file
$haveCache = true;
dprintf('scrapeEveKill(): cache file is fresh (%d secs old) or force cache use on (%d?)', (time() - filemtime($cacheFile)), $cacheUse == CACHE_FORCE_USE_CACHE);
} else {
dprintf('scrapeEveKill(): cache file is expired');
if ($cacheUse == CACHE_USE_CACHE_REFRESH_IF_EXPIRED) {
dprintf('scrapeEveKill(): forcing complete refresh of cache (CACHE_USE_CACHE_REFRESH_IF_EXPIRED is on)');
unlink($cacheFile);
$haveCache = false;
} elseif ($cacheUse == CACHE_USE_CACHE_UPDATE_NEW_DATA) {
// Get the most recent kill timestamp in the cache, and built up the data
// beyond that to refresh it
$firstKill = current($ekData);
dprintf('scrapeEveKill(): doing partial refresh of cache, starting from %s', $firstKill->timestamp);
// Sanity check - only refresh if the last kill is actually older than the refrest time
if (strtotime("+".$kbCacheTime." seconds", strtotime($firstKill->timestamp)) < time()) {
// Replace the timestamps in the URL to begin at the oldest kill
// NOTE: THIS WILL MEAN WE WILL GET AT LEAST ONE DUPE WE HAVE TO REMOVE
$foundSDate = preg_match('/startDate:(\d{4}-\d{2}-\d{2}_\d{2}.\d{2}.\d{2})/', $kbURL, $sDate);
$foundEDate = preg_match('/endDate:(\d{4}-\d{2}-\d{2}_\d{2}.\d{2}.\d{2})/', $kbURL, $eDate);
if ($foundSDate !== 1 || $foundEDate !== 1) return false;
$kbURL = str_ireplace('/startDate:' . $sDate[1], '/startDate:' . date("Y-m-d_H.i.s", strtotime($firstKill->timestamp)), $kbURL);
$kbURL = str_ireplace('/endDate:' . $eDate[1], '/endDate:' . date("Y-m-t_23:59:59", strtotime($firstKill->timestamp)), $kbURL);
// Update temporary cache
set_error_handler("eve_api_warning_catcher", E_WARNING);
try {
$mChkCnt = 0;
$ekNewData = array();
dprintf('scrapeEveKill(): starting partial refresh loop');
do {
dprintf("[m: %d] EVE API URL: %s", $mChkCnt, $kbURL);
$ncScrape = file_get_contents($kbURL, false, $this->ctx);
// Note: we update in reverse order to the way we want to store
if ($ncScrape !== false) {
$ekNewData = array_merge(json_decode($ncScrape),$ekNewData);
} else {
// Eve-Kill API/website returned something we can't use, assume
// it's broken
restore_error_handler();
return null;
}
// Increment failsafe loop breaker
$mChkCnt++;
$foundSDate = preg_match('/startDate:(\d{4}-\d{2}-\d{2}_\d{2}.\d{2}.\d{2})/', $kbURL, $sDate);
$foundEDate = preg_match('/endDate:(\d{4}-\d{2}-\d{2}_\d{2}.\d{2}.\d{2})/', $kbURL, $eDate);
if ($foundSDate !== 1 || $foundEDate !== 1) break;
$kbURL = str_ireplace('/startDate:' . $sDate[1], '/startDate:' . date("Y-m-01_00.00.00", strtotime("+1 month", convEKD2Pts($sDate[1]))), $kbURL);
$kbURL = str_ireplace('/endDate:' . $eDate[1], '/endDate:' . date("Y-m-t_23:59:59", strtotime("+1 month", convEKD2Pts($sDate[1]))), $kbURL);
} while (count($ncScrape) > 0 && convEKD2Pts($eDate[1]) < time() && $mChkCnt <= EVEKILL_ANALYSIS_MAX_MONTH_HISTORY);
// Re-order database by timestamp (since Eve-Kill seems to be useless at it)
usort($ekNewData, 'tcmp');
// At this point we will have a new temporary cache, and the original kill db cache.
// Before we join the two we chop off any kills off the top that match the original
// database.
$aCnt = 0;
foreach($ekNewData as $newKill) {
if ($newKill->timestamp === $firstKill->timestamp && $newKill->internalID === $firstKill->internalID) {
array_splice($ekNewData, $aCnt, 1);
}
$aCnt++;
}
dprintf('scrapeEveKill(): completed partial refresh loop, we have %d new records to append.', sizeof($ekNewData));
if (sizeof($ekNewData) > 0) {
// If we still have data to merge, then merge the new data with the old data
$ekData = array_merge($ekNewData,$ekData);
file_put_contents($cacheFile, gzdeflate(json_encode($ekData)), LOCK_EX);
// If we added new records, we need to update the KB cache
$addedNewRecords = true;
} else {
// If we have nothing to add, at least mark that we tried to stop constant cache refresh checks with no new data
dprintf('scrapeEveKill(): nothing new to add, but touching file to current time.', sizeof($ekNewData));
touch($cacheFile, time());
}
// Whether or not we had any new data to append, the cache we now have is valid
$haveCache = true;
} catch (Exception $e) {
// Problem occured querying Eve Kill, so bomb out
printf('<p><span class="advisoryBad">Error: Unable to update kill data from EVE KILL - error reported %s.</span></p>', $e->getMessage());
error_log("EVE API error: " . $e->getMessage(), 0);
}
restore_error_handler();
}
}
}
}
}
if (!$haveCache) {
if ($cacheUse != CACHE_FORCE_USE_CACHE) {
// We don't have a cache file, so we have to poll Eve-Kill then create one
set_error_handler("eve_api_warning_catcher", E_WARNING);
try {
$mChkCnt = 0;
$ekData = array();
do {
dprintf("[m: %d] EVE API URL: %s", $mChkCnt, $kbURL);
//dprintf("scrapeEveKill(): Fetching from URL: %s", $kbURL);
$ekScrape = file_get_contents($kbURL, false, $this->ctx);
if ($ekScrape !== false) {
$ekData = array_merge($ekData,json_decode($ekScrape));
} else {
// Eve-Kill API/website returned something we can't use, assume
// it's broken
restore_error_handler();
return null;
}
// Increment failsafe loop breaker
$mChkCnt++;
$foundSDate = preg_match('/startDate:(\d{4}-\d{2}-\d{2}_\d{2}.\d{2}.\d{2})/', $kbURL, $sDate);
$foundEDate = preg_match('/endDate:(\d{4}-\d{2}-\d{2}_\d{2}.\d{2}.\d{2})/', $kbURL, $eDate);
if ($foundSDate !== 1 || $foundEDate !== 1) break;
$kbURL = str_ireplace('/startDate:' . $sDate[1], '/startDate:' . date("Y-m-01_00.00.00", strtotime("-1 month", convEKD2Pts($sDate[1]))), $kbURL);
$kbURL = str_ireplace('/endDate:' . $eDate[1], '/endDate:' . date("Y-m-t_23:59:59", strtotime("-1 month", convEKD2Pts($sDate[1]))), $kbURL);
} while (sizeof($ekData) < intval($sMailLimit) && $mChkCnt <= EVEKILL_ANALYSIS_MAX_MONTH_HISTORY);
} catch (Exception $e) {
// Problem occured querying Eve Kill, so bomb out
printf('<p><span class="advisoryBad">Error: Unable to update kill data from EVE KILL - error reported %s.</span></p>', $e->getMessage());
error_log("EVE API error: " . $e->getMessage(), 0);
}
restore_error_handler();
// Write cache file to disk for next run
dprintf("scrapeEveKill(): Scraping finished, total kills: %d, total months checked: %d", sizeof($ekData), $mChkCnt);
if (is_array($ekData) && !empty($ekData)) {
// Re-order database by timestamp (since Eve-Kill seems to be useless at it)
usort($ekData, 'tcmp');
file_put_contents($cacheFile, gzdeflate(json_encode($ekData)), LOCK_EX);
} else {
// Whatever data we have.. is invalid
return null;
}
} else {
// We are trying to force using the cache, but we don't have one!
return null;
}
}
dprintf('scrapeEveKill(): ALL DONE - Used cache? %s, kill count: %d', ($haveCache) ? "yes" : "no", sizeof($ekData));;
//print_r($ekData);
return array("ekdata" => $ekData, "used_cache" => ($addedNewRecords) ? false : $haveCache);
}
public function getKillCount() { return sizeof($kill); }
public function isValidKB() { return $this->isValidKB; }
public function getUniqID() { return $this->uniqID; }
// Return the most recent kill in the database
// Note: We can't assume the killboard will be in date order, so we'll do it the hard way
private function &getSysNewestKill() {
$newTS = 0;
$newKill = null;
if (sizeof($this->kill) > 0) {
foreach ($this->kill as $aKill) {
if (strtotime($aKill->timestamp) >= $newTS) {
$newTS = strtotime($aKill->timestamp);
$newKill = $aKill;
}
}
}
return $newKill;
}
// Return the oldest kill in the database
// Note: We can't assume the killboard will be in date order, so we'll do it the hard way
private function &getSysOldestKill() {
$oldTS = time();
$oldKill = null;
if (sizeof($this->kill) > 0) {
foreach ($this->kill as $aKill) {
if (strtotime($aKill->timestamp) <= $oldTS) {
$oldTS = strtotime($aKill->timestamp);
$oldKill = $aKill;
}
}
}
return $oldKill;
}
// This function returns a pointer to an instantiated corp for kill updating
public function &getCorporation($corpName) {
if (sizeof($this->corp) > 0) {
foreach($this->corp as $aCorp) {
if (strcasecmp(trim($aCorp->corporationName), trim($corpName)) == 0) {
return $aCorp;
}
}
}
// Return null pointer reference
$null = null;
return $null;
}
// Returns the total number of corps that are in the alliance that the specified corp is in.
// Zero is always returned if the corp is not in an alliance.
public function getAllianceCorpCount($aCorp) {
if ($aCorp->allianceID == 0 || $aCorp->allianceID == NO_ALLIANCE_ALLIANCE_ID) return 0;
else return is_null($aAlliance = &$this->getAllianceRM($aCorp->allianceName)) ? 0 : $aAlliance["corp_count"];
}
// This function returns a pointer to an instantiated corp for kill updating
private function &getAllianceRM($allianceName) {
if (sizeof($this->res_metrics["alliance"]) > 0) {
foreach($this->res_metrics["alliance"] as &$aAlliance) {
if (strcasecmp(trim($aAlliance["alliance_name"]), trim($allianceName)) == 0) {
return $aAlliance;
}
}
}
// Return null pointer reference
$null = null;
return $null;
}
// This function returns true if the specified corp looks like they have been or are being
// evicted (structure losses including a tower within the past X months)
public function calcEviction($aCorp, $numMonthsToChk = EVEKILL_RECENT_EVICTION_IN_MONTHS) {
if (sizeof($this->kill) == 0) return null;
// Get the typeIDs for towers
//$rsTower = mysql_query("SELECT it.typeID FROM ".EVEDB_NAME.".invTypes it WHERE groupID IN (SELECT groupID FROM ".EVEDB_NAME.".invGroups WHERE categoryID = 23)");
$rsTower = mysql_query_cache("SELECT it.typeID FROM ".EVEDB_NAME.".invTypes it WHERE groupID = 365");
if (is_array($rsTower)) {
if (!empty($rsTower)) {
// Build array of tower IDs
$towerIDAry = array();
for ($t = 0; $t < count($rsTower); $t++) {
$towerIDAry[] = $rsTower[$t]["typeID"];
}
$posKill = array();
$lastPosKill = null;
foreach($this->kill as $aKill) {
//dprintf("Checking for tower kill, victim %s = corp %s? (%d), kill TS: %s, is Tower? %d, is Old? %d (months to check: %d)", $aKill->victimCorpName, $aCorp->corporationName, (strcasecmp($aCorp->corporationName, $aKill->victimCorpName) == 0 || (($aCorp->allianceID != 0 && $aCorp->allianceID != NO_ALLIANCE_ALLIANCE_ID) && strcasecmp($aCorp->allianceName, $aKill->victimAllianceName) == 0)), $aKill->timestamp, in_array($aKill->victimShipID, $towerIDAry), (strtotime("+".$numMonthsToChk." months", strtotime($aKill->timestamp)) < time()), $numMonthsToChk);
if (strtotime("+".($numMonthsToChk+1)." months", strtotime($aKill->timestamp)) < time()) continue;
if (in_array($aKill->victimShipID, $towerIDAry) && (strcasecmp($aCorp->corporationName, $aKill->victimCorpName) == 0)) {
$posKill[] = $aKill;
if (is_null($lastPosKill) || strtotime($aKill->timestamp) > strtotime($lastPosKill->timestamp)) $lastPosKill = $aKill;
}
}
// At this point we know that $aCorp has lost a POS to someone else, but we don't know if it was spurious and they
// recovered or not. To check this we carry on looking through the kills to see if $aCorp still features a lot, if they
// do chances are it was spurious.
if (sizeof($posKill) > 0 && !is_null($lastPosKill)) {
// We only consider activity outside of that which is probably related to the eviction, as
// defined by SCORE_RES_IGNORE_RELATED_ACTIVITY_PERIOD_DAYS
if (strtotime("+".SCORE_RES_IGNORE_RELATED_ACTIVITY_PERIOD_DAYS." days", strtotime($lastPosKill->timestamp)) < time()) {
$stillHereWeight = 0;
$latestKill = $this->res_metrics["newestKill"];
$sysKillTimeSpan = strtotime($latestKill->timestamp) - strtotime($lastPosKill->timestamp);
// We need to exclude POS modules, since people cleaning those up after a corp has been evicted shouldn't qualify them for residency
$towerAssetsAry = array();
$rsTowerAssets = mysql_query_cache(sprintf("SELECT it.typeID FROM ".EVEDB_NAME.".invTypes it WHERE groupID IN (%s,%s)", implode(unserialize(POS_MODULES_STRUCTURES_GROUPIDS),","), implode(unserialize(POS_GUNS_GROUPIDS),",")));
if (is_array($rsTowerAssets)) {
if (!empty($rsTowerAssets)) {
// Build array of tower asset IDs
$towerAssetsAry = array();
for ($t = 0; $t < count($rsTower); $t++) {
$towerAssetsAry[] = $rsTowerAssets[$t]["typeID"];
}
}
}
// We use the same exponential decay calculation to apportion weight to a battle that involves this corp. The more
// recent it is (further away from the POS loss) the more weight it has.
foreach ($aCorp->battle as $aBattle) {
// Ignore any kill that occurs before the POS died, or for a period (SCORE_RES_IGNORE_RELATED_ACTIVITY_PERIOD_DAYS) afterwards
if ($aBattle->timestamp <= $lastPosKill->timestamp || strtotime($aBattle->timestamp) <= strtotime("+".SCORE_RES_IGNORE_RELATED_ACTIVITY_PERIOD_DAYS." days", strtotime($lastPosKill->timestamp))) continue;
dprintf("eviction check: pos kill on: %s, battle on: %s, is POS module and victim? %d, would add score: %f", $lastPosKill->timestamp, $aBattle->timestamp, (strcasecmp($aCorp->corporationName, $aBattle->victimCorpName) == 0 && in_array($aBattle->victimShipID, $towerAssetsAry)), $this->ed($sysKillTimeSpan, SCORE_EXPONENTIAL_DECAY_RATE, (strtotime($latestKill->timestamp) - strtotime($aBattle->timestamp)), SCORE_RES_STILL_RES_BATTLE_SCORE));
if (!(strcasecmp($aCorp->corporationName, $aBattle->victimCorpName) == 0 && in_array($aBattle->victimShipID, $towerAssetsAry))) {
$stillHereWeight += $this->ed($sysKillTimeSpan, SCORE_EXPONENTIAL_DECAY_RATE, (strtotime($latestKill->timestamp) - strtotime($aBattle->timestamp)), SCORE_RES_STILL_RES_BATTLE_SCORE);
}
}
}
}
return sizeof($posKill) == 0 ? null : array("losses" => $posKill, "ppl_score" => $stillHereWeight); // ppl = post pos loss
}
}
}
// A simple function that returns an array of corporations
public function getInvolvedCorpsOnKill($aKill) {
$aryKillers = null;
$corpNameAry = array();
foreach($aKill->involved as $aInvolved) {
if (!in_array($aInvolved->corporationName, $corpNameAry)) {
$aryKillers[] = &$this->getCorporation($aInvolved->corporationName);
$corpNameAry[] = $aInvolved->corporationName;
}
}
return $aryKillers;
}
// This function iterates over the kill database and returns an array of kills where the specified
// corporation was involved (as involved party or victim)
public function getKillsInvolvingCorporation($aCorp) {
$corpName = $aCorp->corporationName;
if (sizeof($this->kill) == 0) return null;
return $aCorp->killDB;
}
// This function returns an array of activity (kills and losses) that involved capital ships
// Optionally if corporationID is supplied it will return the kills that this corporation has
// used capitals on
public function getActivityInvolvingCaps($corporationName = null, $includeFighters = true) {
if (sizeof($this->kill) == 0) return null;
//if (!is_null($corporationID)) $corpName = $this->getNameFromEVEID($corporationID, GET_CORPORATION_ID);
$capKillDB = array();
foreach($this->kill as $aKill) {
// Check to see whether or not the victim was a capital
// If corp is specified, we verify that it was theirs
if (in_array($aKill->victimShipID, unserialize(CAPITAL_SHIP_TYPEIDS)) && (!is_null($corporationName) ? strcasecmp($corporationName, $aKill->victimName) == 0 : in_array($aKill->victimShipID, unserialize(CAPITAL_SHIP_TYPEIDS)))) {
$capKillDB[] = array("kill" => $aKill, "typeID" => $aKill->victimShipID, "isShip" => true);
}
foreach($aKill->involved as $aInvolved) {
if (!is_null($corporationName)) {
if (strcasecmp($aInvolved->corporationName, $corporationName) == 0) {
if (in_array($aInvolved->shipTypeID, unserialize(CAPITAL_SHIP_TYPEIDS))) {
$capKillDB[] = array("kill" => $aKill, "typeID" => $aInvolved->shipTypeID, "isShip" => true);
} elseif ($includeFighters && in_array($aInvolved->weaponTypeID, unserialize(FIGHTER_WEAPON_TYPEIDS))) {
$capKillDB[] = array("kill" => $aKill, "typeID" => $aInvolved->weaponTypeID, "isShip" => false);
}
}
} else {
if (in_array($aInvolved->shipTypeID, unserialize(CAPITAL_SHIP_TYPEIDS))) {
$capKillDB[] = array("kill" => $aKill, "typeID" => $aInvolved->shipTypeID, "isShip" => true);
} elseif ($includeFighters && in_array($aInvolved->weaponTypeID, unserialize(FIGHTER_WEAPON_TYPEIDS))) {
$capKillDB[] = array("kill" => $aKill, "typeID" => $aInvolved->weaponTypeID, "isShip" => false);
}
}
}
}
return $capKillDB;
}
public function getShipNameForTypeID($typeID) {
$rsShipType = mysql_query_cache(sprintf("SELECT it.typeName FROM ".EVEDB_NAME.".invTypes it LEFT JOIN ".EVEDB_NAME.".invGroups ig USING (groupID) WHERE it.typeID = '%d'", $typeID));
if (is_array($rsShipType)) {
if (!empty($rsShipType)) {
return $rsShipType[0]["typeName"];
}
}
return null;
}
private function getTotalBattleCount() {
$battleCount = 0;
foreach ($this->corp as $aCorp) {
$battleCount += sizeof($aCorp->battle);
}
return $battleCount;
}
// This function returns an array of kills where the specified corp has killed an InterBus C.O
public function getInterBusCOKills($aCorp) {
if (!$aCorp) return array();
$IBKillAry = array();
$corpKills = $this->getKillsInvolvingCorporation($aCorp);
if (sizeof($corpKills) > 0) {
foreach ($corpKills as $aCorpKill) {
if ($aCorpKill["kill"]->victimShipID == INTERBUS_CUSTOMS_OFFICE_TYPEID && $aCorpKill["isVictim"] == false) {
$IBKillAry[] = $aCorpKill["kill"];
}
}
}
return $IBKillAry;
}
// This function returns an array of POS kills where the specified corp was involved
public function getPOSCombatActivity($aCorp) {
if (!$aCorp) return array();
$posKillAry = array();
// If corp has lost POS or
$rsTower = mysql_query_cache(sprintf("SELECT it.typeID FROM ".EVEDB_NAME.".invTypes it WHERE groupID IN (%s)", implode(unserialize(POS_TOWER_GROUPID),",")));
$rsTowerGuns = mysql_query_cache(sprintf("SELECT it.typeID FROM ".EVEDB_NAME.".invTypes it WHERE groupID IN (%s)", implode(unserialize(POS_GUNS_GROUPIDS),",")));
if (!empty($rsTower) && !empty($rsTowerGuns)) {
// Build array of tower IDs
$towerIDAry = array();
$towerGunsAry = array();
for ($t = 0; $t < count($rsTower); $t++) { $towerIDAry[] = $rsTower[$t]["typeID"]; }
for ($t = 0; $t < count($rsTower); $t++) { $towerGunsAry[] = $rsTower[$t]["typeID"]; }
// Note: Although a character cannot wield a POS, a POS can shoot people (dur!). If a POS is
// involved in killing someone then we can be certain that someone is resident. Equally if the
// "victim" is a POS, they were definitely at one point resident.
foreach($this->kill as $aKill) {
if (strcasecmp($aKill->victimCorpName, $aCorp->corporationName) == 0 && in_array($aKill->victimShipID, $towerIDAry)) {
$posKillAry[] = array("kill" => $aKill, "isVictim" => 1);
} else {
// It is impossible for the victim to be a POS and the involved party to be a POS as well
foreach($aKill->involved as $aInvolved) {
if (strcasecmp($aCorp->corporationName, $aInvolved->corporationName) == 0 && (in_array($aInvolved->shipTypeID, $towerIDAry) || in_array($aInvolved->weaponTypeID,$towerGunsAry))) {
// aCorp owned POS killed someone (or was involved in it)
$posKillAry[] = array("kill" => $aKill, "isVictim" => 0);
}
}
}
}
}
return $posKillAry;
}
// This function returns true if the specific corporation ID is within the array
public function isNPCCorporation($corporationID) { return (($corporationID >= NPC_CORPORATION_START_ID && $corporationID <= NPC_CORPORATION_END_ID) || $corporationID == SLEEPER_CORPORATION_ID); }
// This function returns TRUE if the specified corp is in a player alliance
public function isCorpInAlliance($aCorp) { return (strtolower($aCorp->allianceName) == 'none' || $aCorp->allianceID == NO_ALLIANCE_ALLIANCE_ID || $aCorp->allianceID == 0) ? false : true; }
public function getNameFromEVEID($intID, $queryType = null) {
global $whConn;
// If a Kill object is supplied then we can scan through the involved parties to see
// if we can get the ID that way, instead of doing a costly EVE API lookup.
if (sizeof($this->kill) > 0) {
switch ($queryType) {
case GET_CHARACTER_ID :
foreach($this->kill as $aKill) {
if ($intID == $aKill->victimExternalID) {
return $aKill->victimName;
}
foreach($aKill->involved as $aInvolved) {
if ($intID == $aInvolved->characterID) {
return $aInvolved->characterName;
}
}
}
break;
case GET_CORPORATION_ID :
foreach($this->kill as $aKill) {
foreach($aKill->involved as $aInvolved) {
if ($intID == $aInvolved->corporationID) {
return $aInvolved->corporationName;
}
}
}
break;
case GET_ALLIANCE_ID :
foreach($this->kill as $aKill) {
foreach($aKill->involved as $aInvolved) {
if ($intID == $aInvolved->allianceID) {
return $aInvolved->allianceName;
}
}
}
break;
}
}
// Try and get the result from the database
$nameArray = self::getIDOrNameFromEveAPI($intID, EVE_API_NAME_SEARCH);
if (is_array($nameArray)) { return $nameArray[0]["name"]; }
return null;
}
// Gets the name corresponding to a typeID
public function getDBNameFromEVEID($iID) {
if (!is_numeric($iID)) return '';
$iID = trim(mysql_real_escape_string($iID));
$rsItem = mysql_query_cache(sprintf("SELECT it.typeName FROM ".EVEDB_NAME.".invTypes it WHERE it.typeID = '%d'", $iID));
return (!empty($rsItem)) ? $rsItem[0]["typeName"] : '';
}
// Gets the ID corresponding to a typeName
public function getDBIDFromEVEName($sName) {
if (strlen($sName) < 1) return -1;
$sName = trim(mysql_real_escape_string($sName));
$rsItem = mysql_query_cache(sprintf("SELECT it.typeID FROM ".EVEDB_NAME.".invTypes it WHERE it.typeName = '%s'", $sName));
return (!empty($rsItem)) ? $rsItem[0]["typeID"] : '';
}
public function getEVEIDFromName($strName, $queryType = null) {
global $whConn;
// If a Kill object is supplied then we can scan through the involved parties to see
// if we can get the ID that way, instead of doing a costly EVE API lookup.
if (sizeof($this->kill) > 0) {
switch ($queryType) {
case GET_CHARACTER_ID :
foreach($this->kill as $aKill) {
if (strcasecmp(trim($strName), trim($aKill->victimName)) == 0) {
return $aKill->victimExternalID;
}
foreach($aKill->involved as $aInvolved) {
if (strcasecmp(trim($strName), trim($aInvolved->characterName)) == 0) {
return $aInvolved->characterID;
}
}
}
break;
case GET_CORPORATION_ID :
foreach($this->kill as $aKill) {
foreach($aKill->involved as $aInvolved) {
if (strcasecmp(trim($strName), trim($aInvolved->corporationName)) == 0) {
return $aInvolved->corporationID;
}
}
}
break;
case GET_ALLIANCE_ID :
foreach($this->kill as $aKill) {
foreach($aKill->involved as $aInvolved) {
if (strcasecmp(trim($strName), trim($aInvolved->allianceName)) == 0) {
return $aInvolved->allianceID;
}
}
}
break;
}
}
// Try and get the result from the database
$idArray = self::getIDOrNameFromEveAPI($strName, EVE_API_ID_SEARCH);
if (is_array($idArray)) { return $idArray[0]["id"]; }
return -1;
}
// Sometimes Eve-Kill throws us some spurious data in the corporation/alliance names, which if we send to the API wlil bomb it out.
// This function addresses this by sanitising the corp names. It uses the EVE API to get the current corp/alliance name - which may
// not have been the one at the time of the kill - and replaces the information with that instead.
public function fixBrokenEKData($aCorp) {
//dprintf("fixBrokenEKData() called on (%d) '%s', (%d) '%s'", $aCorp->corporationID, $aCorp->corporationName, $aCorp->allianceID, $aCorp->allianceName);
if (preg_match(NAMING_POLICY_REGEXP, $aCorp->corporationName) === 0
|| preg_match(NAMING_POLICY_REGEXP, $aCorp->allianceName) === 0) {
//dprintf("fixBrokenEKData() - found a bad name (corp bad? %d, alliance bad? %d), trying to sanitise.", (preg_match(NAMING_POLICY_REGEXP, $aCorp->corporationName) === 0), (preg_match(NAMING_POLICY_REGEXP, $aCorp->allianceName) === 0), $aCorp->allianceName);
if (is_null($aCorp->corporationID) && preg_match(NAMING_POLICY_REGEXP, $aCorp->corporationName) === 1) {
//dprintf("fixBrokenEKData() - corporation ID is missing, but corporation name is valid - getting ID via getIDOrNameFromEveAPI()");
// If we don't have a valid corporationID then we can't get any of the rest of the data, since the EVE API for
// getting an alliance ID/name requires the corporationID as input.
$idArray = $this->getIDOrNameFromEveAPI($aCorp->corporationName);
if (is_null($idArray)) {
// If we still haven't got a corporationID by this point, we're screwed - so bomb out.
//printf("<p>Argh! We haven't got a corporationID even now! Exiting</p>");
return false;
}
$aCorp->corporationID = $idArray[0]["id"];
}
if (($aCorp->allianceID == 0 || $aCorp->allianceID == NO_ALLIANCE_ALLIANCE_ID) && strlen(trim($aCorp->allianceName)) == 0) {
// If we have an alliance ID of 0 or one that corresponds to "None", we don't need to
// query the EVE API. Technically we could - but this would get their current alliance, not
// the one they were in at that point in time.
//dprintf("Alliance name missing, ID is 0, assuming 'None'");
$aCorp->allianceName = "None";
} else {
// Get alliance info from the API
//dprintf("<strong>Querying EVE API URL: %s</strong>", sprintf(EVE_API_CORP_LOOKUP_URL, $aCorp->corporationID));
set_error_handler("eve_api_warning_catcher", E_WARNING);
try {
libxml_use_internal_errors(true);
$apiXml = simplexml_load_file(sprintf(EVE_API_CORP_LOOKUP_URL, $aCorp->corporationID));
// If the allianceID is not already set, set it with the data we now have
//dprintf("apiXml->allianceID = %d", $apiXml->result->allianceID);
if (isset($apiXml->result->allianceID)) {
$aCorp->allianceID = (int)$apiXml->result->allianceID;
// If alliance name is invalid, get the name from the newly found ID
if ($aCorp->allianceID == 0) {
// If alliance ID returned is 0 we already know they have no alliance, so just set that
$aCorp->allianceName = "None";
} elseif (preg_match(NAMING_POLICY_REGEXP, $aCorp->allianceName) === 0) {
//dprintf("fixBrokenEKData() - alliance name is invalid, fixing using allianceID %d", $aCorp->allianceID);
//dprintf("<strong>Querying EVE API URL: %s</strong>", sprintf(EVE_API_NAME_LOOKUP_URL, $aCorp->allianceID));
libxml_use_internal_errors(true);
$apiXml = simplexml_load_file(sprintf(EVE_API_NAME_LOOKUP_URL, $aCorp->allianceID));
if (isset($apiXml->result->rowset->row)) {
foreach($apiXml->result->rowset->row as $aChar) {
// Add this ID to the database cache
$idLookupSQL = sprintf("INSERT IGNORE INTO ".WHDB_NAME.".idMap (eve_id, eve_name) VALUES (%d, '%s')",
$aChar["characterID"], mysql_real_escape_string($aChar["name"]));
mysql_query($idLookupSQL);
// Update alliance name in corp table now that we have it
$aCorp->allianceName = (string)$aChar["name"];
}
}
}
}
} catch (Exception $e) {
printf('<p><span class="advisoryBad">Error: Unable to update kill data from EVE KILL - error reported %s.</span></p>', $e->getMessage());
error_log("EVE API error: " . $e->getMessage(), 0);
}
restore_error_handler();
}
}
}
private function getIDOrNameFromEveAPI($nameArray, $searchType = EVE_API_ID_SEARCH) {
global $whConn;
// Convert nameArray to array if it is a string
if (!is_array($nameArray)) {
$tmpAry = array();
$tmpAry[] = $nameArray;
$nameArray = $tmpAry;
unset($tmpAry);
}
$idArray = array();
// First interrogate the database cache, any entries we find in there we can exclude from the API run
$dbNameArray = $nameArray;
for ($n = 0; $n < sizeof($dbNameArray); $n++) { $dbNameArray[$n] = strtoupper(mysql_real_escape_string($dbNameArray[$n])); };
if ($searchType == EVE_API_ID_SEARCH) {
$idLookupSQL = sprintf("SELECT eve_id, eve_name FROM ".WHDB_NAME.".idMap WHERE UPPER(eve_name) IN ('%s')", implode($dbNameArray,"','"));
} else {
$idLookupSQL = sprintf("SELECT eve_id, eve_name FROM ".WHDB_NAME.".idMap WHERE eve_id IN (%s)", implode($dbNameArray,","));
}
//dprintf("getIDOrNameFromEveAPI(): SQL query: %s", $idLookupSQL);
$rsLookup = mysql_query_cache($idLookupSQL,$whConn);
if (is_array($rsLookup)) {
if (!empty($rsLookup)) {
//printf("<p>Found %d/%d results in cache.", mysql_num_rows($rsLookup), sizeof($nameArray));
// We have some cached IDs in the database, so add them to the "final" array, and remove them
// from the list of names we have to poll the EVE API for.
// We have to create a temporary array to uppercase the entries because the database writes
// the EVE API result as the names are in game, which is not necessarily what they will be on
// the kills (strange but true)
$ucArray = $nameArray;
for ($n = 0; $n < sizeof($ucArray); $n++) { $ucArray[$n] = strtoupper($ucArray[$n]); };
// For each database row we have, we remove the result from the nameArray table
for ($s = 0; $s < count($rsLookup); $s++) {
//dprintf("result: %d, %s", $sRow->eve_id, $sRow->eve_name);
$idArray[] = array("id" => $rsLookup[$s]["eve_id"], "name" => $rsLookup[$s]["eve_name"]);
if (($nKey = array_search(strtoupper(($searchType == EVE_API_ID_SEARCH) ? $rsLookup[$s]["eve_name"] : $rsLookup[$s]["eve_id"]), $ucArray)) !== false) { unset($nameArray[$nKey]); }
}
unset($ucArray);
// Re-index the nameArray
$nameArray = array_values($nameArray);
}
}
unset($dbNameArray);
//print_r($nameArray);
// We might not have any names left to search for..
if (sizeof($nameArray) > 0) {
//dprintf("%d results were missing from cache, need to query EVE API to complete.", sizeof($nameArray));
// We urlencode the individual names, but not the whole query
for ($n = 0; $n < sizeof($nameArray); $n++) { $nameArray[$n] = urlencode($nameArray[$n]); };
set_error_handler("eve_api_warning_catcher", E_WARNING);
try {
libxml_use_internal_errors(true);
$apiXml = simplexml_load_file(sprintf(($searchType == EVE_API_ID_SEARCH) ? EVE_API_ID_LOOKUP_URL : EVE_API_NAME_LOOKUP_URL, implode($nameArray,",")));
//dprintf("API: %s", sprintf(($searchType == EVE_API_ID_SEARCH) ? EVE_API_ID_LOOKUP_URL : EVE_API_NAME_LOOKUP_URL, implode($nameArray,",")));
if (isset($apiXml->result->rowset->row)) {
foreach($apiXml->result->rowset->row as $aChar) {
// Add this ID to the database cache
$idLookupSQL = sprintf("INSERT IGNORE INTO ".WHDB_NAME.".idMap (eve_id, eve_name) VALUES (%d, '%s')",
$aChar["characterID"], mysql_real_escape_string($aChar["name"]));
//dprintf("SQL insert: %s", $idLookupSQL);
mysql_query($idLookupSQL);
$idArray[] = array("id" => intval($aChar["characterID"]), "name" => strval($aChar["name"]));
}
}
} catch (Exception $e) {
printf('<p><span class="advisoryBad">Error: Unable to update kill data from EVE KILL - error reported %s.</span></p>', $e->getMessage());
error_log("EVE API error: " . $e->getMessage(), 0);
}
restore_error_handler();
}
// Returns the populated array to the calling function, or null if we have no IDs
return sizeof($idArray) > 0 ? $idArray : null;
}
// Exponential decay
private function ed($ti, $k, $t, $N0) {
$k = log($k) / $ti;
$k = -$k * $t;
$N = pow(2.718281828, -$k) * $N0;
return $N;
}
// This function attempts to work out who is resident in a given system based on the corp database.
// Note: This should only be called once the corp database has been properly sanitised.
private function calcResidency() {
$oldestKill = $this->res_metrics["oldestKill"];
$latestKill = $this->res_metrics["newestKill"];
$rsAry = array();
if ($oldestKill && $latestKill && sizeof($this->corp) > 0) {
$sysKillTimeSpan = strtotime($latestKill->timestamp) - strtotime($oldestKill->timestamp);
// Create new wormhole object, for checking capital use in lower class
$aTempWH = new Wormhole($this->uniqID);
foreach ($this->corp as $aCorp) {
// NPC corporations can't be resident (obviously Sleepers are :) )
if ($aCorp->isNPCCorporation()) continue;
// Create/update alliance object
if (!($aCorp->allianceID == 0 || $aCorp->allianceID == NO_ALLIANCE_ALLIANCE_ID)) {
if (is_null($aAlliance = &$this->getAllianceRM($aCorp->allianceName))) {
$this->res_metrics["alliance"][] = array(
"alliance_name" => &$aCorp->allianceName,
"corp_count" => 1
);
} else { $aAlliance["corp_count"]++; }
}
// Weigh each battle according to how long ago it occured
$battleCount = 0;
foreach ($aCorp->battle as $aBattle) {
//dprintf("[%s] battle time: %s, <i>it</i>: %d, <i>k</i>: %f, <i>t</i>: %d, <i>N<sub>0</sub></i>: %d, expo. decay: %f", $aCorp->corporationName, $aBattle->timestamp, $sysKillTimeSpan, SCORE_EXPONENTIAL_DECAY_RATE, (strtotime($latestKill->timestamp) - strtotime($aBattle->timestamp)), SCORE_MAX_SCORE_FOR_BATTLE, $this->ed($sysKillTimeSpan, SCORE_EXPONENTIAL_DECAY_RATE, (strtotime($latestKill->timestamp) - strtotime($aBattle->timestamp)), SCORE_MAX_SCORE_FOR_BATTLE));
$aCorp->residency["score"] += $this->ed($sysKillTimeSpan, SCORE_EXPONENTIAL_DECAY_RATE, (strtotime($latestKill->timestamp) - strtotime($aBattle->timestamp)), (SCORE_MAX_SCORE_FOR_BATTLE + (++$battleCount * SCORE_MULTIPLE_BATTLE_INCREMENT)));
}
// Add weight to residency score for kills where corp's POS was either a killer or a victim
$posKills = $this->getPOSCombatActivity($aCorp);
if (sizeof($posKills) > 0) {
foreach($posKills as $aPOSKill) {
dprintf('[%s] POS was involved in a %s on %s - <a href="%s" target="_blank">here</a>', $aCorp->corporationName, ($aPOSKill["isVictim"] == 1 ? "LOSS" : "KILL"), $aPOSKill["kill"]->timestamp, $aPOSKill["kill"]->url);
$aCorp->residency["score"] += $this->ed($sysKillTimeSpan, SCORE_EXPONENTIAL_DECAY_RATE, (strtotime($latestKill->timestamp) - strtotime($aPOSKill["kill"]->timestamp)), SCORE_SKEW_POS_COMBAT_ACTIVITY);
$aCorp->residency["posactvy"][] = $aPOSKill;
}
}
// If wormhole is class 4 or lower, and this corp has used a capital, it's a good
// assumption that they are or have been resident at some point.
if ($aTempWH->isValidLocus() && $aTempWH->isWHLocus()) {
if ($aTempWH->getSysClass() <= 4) {
$capUsageDB = $this->getActivityInvolvingCaps($aCorp->corporationName, true);
if (sizeof($capUsageDB) > 0) {
dprintf("[%s] Corp has used a cap %d times in a <= C4 wormhole", $aCorp->corporationName, sizeof($capUsageDB));
foreach($capUsageDB as $aKillWithCap) {
$aCorp->residency["score"] += $this->ed($sysKillTimeSpan, SCORE_EXPONENTIAL_DECAY_RATE, (strtotime($latestKill->timestamp) - strtotime($aKillWithCap["kill"]->timestamp)), SCORE_SKEW_CAPITAL_USE_IN_LOWCLASS_WH);
$aCorp->residency["caplcuse"][] = $aKillWithCap["kill"];
}
}
}
// Skew for InterBus Customs Office activity
$IBCOKillDB = $this->getInterBusCOKills($aCorp);
if (sizeof($IBCOKillDB) > 0) {
dprintf("[%s] Corp has killed %d InterBus C.Os in this system", $aCorp->corporationName, sizeof($IBCOKillDB));
foreach($IBCOKillDB as $aIBCOKill) {
$aCorp->residency["score"] += $this->ed($sysKillTimeSpan, SCORE_EXPONENTIAL_DECAY_RATE, (strtotime($latestKill->timestamp) - strtotime($aIBCOKill->timestamp)), SCORE_SKEW_INTERBUS_CO_KILL);
$aCorp->residency["ibcokills"][] = $aIBCOKill;
}
}
}
// Check eviction status for each corp
if (!is_null($aCorp->residency["evicted"] = $this->calcEviction($aCorp, EVEKILL_ANALYSIS_MAX_MONTH_HISTORY))) {
if ($aCorp->residency["evicted"]["ppl_score"] >= SCORE_RES_STILL_RES_THRESHOLD_SCORE) {
dprintf("[%s] Corp lost tower(s), but appeared to rally and remain resident? (still res weight: %f)", $aCorp->corporationName, $aCorp->residency["evicted"]["ppl_score"]);
// Include this corps score in array for stddev calc
$rsAry[] = $aCorp->residency["score"];
} else {
// If evicted zero score to eliminate them from standard deviation checks to allow us to find the current resident
dprintf("[%s] Corp is evicted. (%f)", $aCorp->corporationName, $aCorp->residency["evicted"]["ppl_score"]);
//$aCorp->residency["score"] = 0;
}
} else {
// Include this corps score in array for stddev calc
$rsAry[] = $aCorp->residency["score"];
}
}
// Populate total residency score metric in killboard
$this->res_metrics["totalScore"] = $this->getTotalResidencyScore();
// Calculate stddev, z-score and percentile
$resStdDev = sd($rsAry);
foreach ($this->corp as $aCorp) {
$aCorp->residency["stddev"] = $resStdDev;
$aCorp->residency["z-score"] = ($aCorp->residency["score"] - ($this->res_metrics["totalScore"] / sizeof($this->corp))) / $aCorp->residency["stddev"];
$aCorp->residency["z-perc"] = cdf($aCorp->residency["z-score"]) * 100;
}
// Sort the corp table by residency score descending order
usort($this->corp, 'rcmp');
}
}
public function getTimezoneActivity($corpArray) {
if (!is_array($corpArray)) return false;
$tzActivityArray = array();
foreach($corpArray as $aCorp) {
// Add this corps TZ activity. Note: Kills and losses are treated the same
// - we just want to know when they are active
if (is_array($corpActivityDB = $this->getKillsInvolvingCorporation($aCorp))) {
foreach($corpActivityDB as $aCorpA) {
$tzActivityArray[] = $aCorpA["kill"]->timestamp;
}
}
}
// Calculate TZ spread. Note: This is based on people working during the day, and
// is weighted towards the mid to upper end (prime time). Note also that EVE
// timestamps are constant - adjustments have to be made for timezones. The
// calculations are not binary, a kill at midnight EVE time would be prime time
// US, and still within EU.
$tzArray = array("eu" => array("name" => "Euro", "score" => 0, "z-score" => 0, "r-perc" => 0, "z-perc" => 0, "shift" => 1),
"us" => array("name" => "US", "score" => 0, "z-score" => 0, "r-perc" => 0, "z-perc" => 0, "shift" => -5),
"au_nz" => array("name" => "AU/NZ", "score" => 0, "z-score" => 0, "r-perc" => 0, "z-perc" => 0, "shift" => 10));
$tzWeight = array( 0 => 0.35,
1 => 0.1,
2 => 0.05,
3 => 0,
4 => 0,
5 => 0,
6 => 0,
7 => 0,
8 => 0,
9 => 0,
10 => 0,
11 => 0.05,
12 => 0.1,
13 => 0.1,
14 => 0.1,
15 => 0.1,
16 => 0.1,
17 => 0.1,
18 => 0.2,
19 => 0.45,