/
StatisticsTracker.js
595 lines (512 loc) · 15.8 KB
/
StatisticsTracker.js
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
function StatisticsTracker() {}
const g_UpdateSequenceInterval = 30 * 1000;
StatisticsTracker.prototype.Schema =
"<empty/>";
StatisticsTracker.prototype.Init = function()
{
this.unitsClasses = [
"Infantry",
"Worker",
"FemaleCitizen",
"Cavalry",
"Champion",
"Hero",
"Siege",
"Ship",
"Domestic",
"Trader"
];
this.unitsTrained = {
"Infantry": 0,
"Worker": 0,
"FemaleCitizen": 0,
"Cavalry": 0,
"Champion": 0,
"Hero": 0,
"Siege": 0,
"Ship": 0,
"Trader": 0,
"Domestic": 0,
"total": 0
};
this.unitsLost = {
"Infantry": 0,
"Worker": 0,
"FemaleCitizen": 0,
"Cavalry": 0,
"Champion": 0,
"Hero": 0,
"Siege": 0,
"Ship": 0,
"Trader": 0,
"total": 0
};
this.unitsLostValue = 0;
this.enemyUnitsKilled = {
"Infantry": 0,
"Worker": 0,
"FemaleCitizen": 0,
"Cavalry": 0,
"Champion": 0,
"Hero": 0,
"Siege": 0,
"Ship": 0,
"Trader": 0,
"total": 0
};
this.enemyUnitsKilledValue = 0;
this.unitsCaptured = {
"Infantry": 0,
"Worker": 0,
"FemaleCitizen": 0,
"Cavalry": 0,
"Champion": 0,
"Hero": 0,
"Siege": 0,
"Ship": 0,
"Trader": 0,
"total": 0
};
this.unitsCapturedValue = 0;
this.buildingsClasses = [
"House",
"Economic",
"Outpost",
"Military",
"Fortress",
"CivCentre",
"Wonder"
];
this.buildingsConstructed = {
"House": 0,
"Economic": 0,
"Outpost": 0,
"Military": 0,
"Fortress": 0,
"CivCentre": 0,
"Wonder": 0,
"total": 0
};
this.buildingsLost = {
"House": 0,
"Economic": 0,
"Outpost": 0,
"Military": 0,
"Fortress": 0,
"CivCentre": 0,
"Wonder": 0,
"total": 0
};
this.buildingsLostValue = 0;
this.enemyBuildingsDestroyed = {
"House": 0,
"Economic": 0,
"Outpost": 0,
"Military": 0,
"Fortress": 0,
"CivCentre": 0,
"Wonder": 0,
"total": 0
};
this.enemyBuildingsDestroyedValue = 0;
this.buildingsCaptured = {
"House": 0,
"Economic": 0,
"Outpost": 0,
"Military": 0,
"Fortress": 0,
"CivCentre": 0,
"Wonder": 0,
"total": 0
};
this.buildingsCapturedValue = 0;
this.resourcesGathered = {
"vegetarianFood": 0
};
this.resourcesUsed = {};
this.resourcesSold = {};
this.resourcesBought = {};
for (let res of Resources.GetCodes())
{
this.resourcesGathered[res] = 0;
this.resourcesUsed[res] = 0;
this.resourcesSold[res] = 0;
this.resourcesBought[res] = 0;
}
this.tributesSent = 0;
this.tributesReceived = 0;
this.tradeIncome = 0;
this.treasuresCollected = 0;
this.lootCollected = 0;
this.peakPercentMapControlled = 0;
this.teamPeakPercentMapControlled = 0;
this.successfulBribes = 0;
this.failedBribes = 0;
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
this.updateTimer = cmpTimer.SetInterval(
this.entity, IID_StatisticsTracker, "UpdateSequences", 0, g_UpdateSequenceInterval);
};
StatisticsTracker.prototype.OnGlobalInitGame = function()
{
this.sequences = clone(this.GetStatistics());
this.sequences.time = [];
};
/**
* Returns a subset of statistics that will be added to the simulation state,
* thus called each turn. Basic statistics should not contain data that would
* be expensive to compute.
*
* Note: as of now, nothing in the game needs that, but some AIs developed by
* modders need it in the API.
*/
StatisticsTracker.prototype.GetBasicStatistics = function()
{
return {
"resourcesGathered": this.resourcesGathered,
"percentMapExplored": this.GetPercentMapExplored()
};
};
StatisticsTracker.prototype.GetStatistics = function()
{
return {
"unitsTrained": this.unitsTrained,
"unitsLost": this.unitsLost,
"unitsLostValue": this.unitsLostValue,
"enemyUnitsKilled": this.enemyUnitsKilled,
"enemyUnitsKilledValue": this.enemyUnitsKilledValue,
"unitsCaptured": this.unitsCaptured,
"unitsCapturedValue": this.unitsCapturedValue,
"buildingsConstructed": this.buildingsConstructed,
"buildingsLost": this.buildingsLost,
"buildingsLostValue": this.buildingsLostValue,
"enemyBuildingsDestroyed": this.enemyBuildingsDestroyed,
"enemyBuildingsDestroyedValue": this.enemyBuildingsDestroyedValue,
"buildingsCaptured": this.buildingsCaptured,
"buildingsCapturedValue": this.buildingsCapturedValue,
"resourcesGathered": this.resourcesGathered,
"resourcesUsed": this.resourcesUsed,
"resourcesSold": this.resourcesSold,
"resourcesBought": this.resourcesBought,
"tributesSent": this.tributesSent,
"tributesReceived": this.tributesReceived,
"tradeIncome": this.tradeIncome,
"treasuresCollected": this.treasuresCollected,
"lootCollected": this.lootCollected,
"percentMapExplored": this.GetPercentMapExplored(),
"teamPercentMapExplored": this.GetTeamPercentMapExplored(),
"percentMapControlled": this.GetPercentMapControlled(),
"teamPercentMapControlled": this.GetTeamPercentMapControlled(),
"peakPercentMapControlled": this.peakPercentMapControlled,
"teamPeakPercentMapControlled": this.teamPeakPercentMapControlled,
"successfulBribes": this.successfulBribes,
"failedBribes": this.failedBribes
};
};
StatisticsTracker.prototype.GetSequences = function()
{
let ret = clone(this.sequences);
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
ret.time.push(cmpTimer.GetTime() / 1000);
this.PushValue(this.GetStatistics(), ret);
return ret;
};
/**
* Used to print statistics for non-visual autostart games.
* @return The player's statistics as a JSON string beautified with some indentations.
*/
StatisticsTracker.prototype.GetStatisticsJSON = function()
{
let cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
let playerStatistics = {
"playerID": cmpPlayer.GetPlayerID(),
"playerState": cmpPlayer.GetState(),
"statistics": this.GetStatistics()
};
return JSON.stringify(playerStatistics, null, "\t");
};
/**
* Increments counter associated with certain entity/counter and type of given entity.
* @param cmpIdentity - the entity identity component.
* @param counter - the name of the counter to increment (e.g. "unitsTrained").
* @param type - the type of the counter (e.g. "workers").
*/
StatisticsTracker.prototype.CounterIncrement = function(cmpIdentity, counter, type)
{
var classes = cmpIdentity.GetClassesList();
if (!classes)
return;
if (classes.indexOf(type) != -1)
++this[counter][type];
};
/**
* Counts the total number of units trained as well as an individual count for
* each unit type. Based on templates.
*/
StatisticsTracker.prototype.IncreaseTrainedUnitsCounter = function(trainedUnit)
{
let cmpUnitEntityIdentity = Engine.QueryInterface(trainedUnit, IID_Identity);
if (!cmpUnitEntityIdentity)
return;
let cmpCost = Engine.QueryInterface(trainedUnit, IID_Cost);
let costs = cmpCost && cmpCost.GetResourceCosts();
for (let type of this.unitsClasses)
this.CounterIncrement(cmpUnitEntityIdentity, "unitsTrained", type);
if (!cmpUnitEntityIdentity.HasClass("Domestic"))
++this.unitsTrained.total;
if (cmpUnitEntityIdentity.HasClass("Domestic") && costs)
{
// Subtract costs for sheep/goats/pigs to get the net food gain/use for corralling
this.resourcesUsed.food -= costs.food;
this.resourcesGathered.food -= costs.food;
}
};
/**
* Counts the total number of buildings constructed as well as an individual count for
* each building type. Based on templates.
*/
StatisticsTracker.prototype.IncreaseConstructedBuildingsCounter = function(constructedBuilding)
{
var cmpBuildingEntityIdentity = Engine.QueryInterface(constructedBuilding, IID_Identity);
if (!cmpBuildingEntityIdentity)
return;
for (let type of this.buildingsClasses)
this.CounterIncrement(cmpBuildingEntityIdentity, "buildingsConstructed", type);
++this.buildingsConstructed.total;
};
StatisticsTracker.prototype.KilledEntity = function(targetEntity)
{
var cmpTargetEntityIdentity = Engine.QueryInterface(targetEntity, IID_Identity);
if (!cmpTargetEntityIdentity)
return;
var cmpCost = Engine.QueryInterface(targetEntity, IID_Cost);
var costs = cmpCost && cmpCost.GetResourceCosts();
if (cmpTargetEntityIdentity.HasClass("Unit") && !cmpTargetEntityIdentity.HasClass("Animal"))
{
for (let type of this.unitsClasses)
this.CounterIncrement(cmpTargetEntityIdentity, "enemyUnitsKilled", type);
++this.enemyUnitsKilled.total;
if (costs)
for (let type in costs)
this.enemyUnitsKilledValue += costs[type];
}
let cmpFoundation = Engine.QueryInterface(targetEntity, IID_Foundation);
if (cmpTargetEntityIdentity.HasClass("Structure") && !cmpFoundation)
{
for (let type of this.buildingsClasses)
this.CounterIncrement(cmpTargetEntityIdentity, "enemyBuildingsDestroyed", type);
++this.enemyBuildingsDestroyed.total;
if (costs)
for (let type in costs)
this.enemyBuildingsDestroyedValue += costs[type];
}
};
StatisticsTracker.prototype.LostEntity = function(lostEntity)
{
var cmpLostEntityIdentity = Engine.QueryInterface(lostEntity, IID_Identity);
if (!cmpLostEntityIdentity)
return;
var cmpCost = Engine.QueryInterface(lostEntity, IID_Cost);
var costs = cmpCost && cmpCost.GetResourceCosts();
if (cmpLostEntityIdentity.HasClass("Unit") && !cmpLostEntityIdentity.HasClass("Domestic"))
{
for (let type of this.unitsClasses)
this.CounterIncrement(cmpLostEntityIdentity, "unitsLost", type);
++this.unitsLost.total;
if (costs)
for (let type in costs)
this.unitsLostValue += costs[type];
}
let cmpFoundation = Engine.QueryInterface(lostEntity, IID_Foundation);
if (cmpLostEntityIdentity.HasClass("Structure") && !cmpFoundation)
{
for (let type of this.buildingsClasses)
this.CounterIncrement(cmpLostEntityIdentity, "buildingsLost", type);
++this.buildingsLost.total;
if (costs)
for (let type in costs)
this.buildingsLostValue += costs[type];
}
};
StatisticsTracker.prototype.CapturedEntity = function(capturedEntity)
{
let cmpCapturedEntityIdentity = Engine.QueryInterface(capturedEntity, IID_Identity);
if (!cmpCapturedEntityIdentity)
return;
let cmpCost = Engine.QueryInterface(capturedEntity, IID_Cost);
let costs = cmpCost && cmpCost.GetResourceCosts();
if (cmpCapturedEntityIdentity.HasClass("Unit"))
{
for (let type of this.unitsClasses)
this.CounterIncrement(cmpCapturedEntityIdentity, "unitsCaptured", type);
++this.unitsCaptured.total;
if (costs)
for (let type in costs)
this.unitsCapturedValue += costs[type];
}
if (cmpCapturedEntityIdentity.HasClass("Structure"))
{
for (let type of this.buildingsClasses)
this.CounterIncrement(cmpCapturedEntityIdentity, "buildingsCaptured", type);
++this.buildingsCaptured.total;
if (costs)
for (let type in costs)
this.buildingsCapturedValue += costs[type];
}
};
/**
* @param {string} type - generic type of resource.
* @param {number} amount - amount of resource, whick should be added.
* @param {string} specificType - specific type of resource.
*/
StatisticsTracker.prototype.IncreaseResourceGatheredCounter = function(type, amount, specificType)
{
this.resourcesGathered[type] += amount;
if (type == "food" && (specificType == "fruit" || specificType == "grain"))
this.resourcesGathered.vegetarianFood += amount;
};
/**
* @param {string} type - generic type of resource.
* @param {number} amount - amount of resource, which should be added.
*/
StatisticsTracker.prototype.IncreaseResourceUsedCounter = function(type, amount)
{
this.resourcesUsed[type] += amount;
};
StatisticsTracker.prototype.IncreaseTreasuresCollectedCounter = function()
{
++this.treasuresCollected;
};
StatisticsTracker.prototype.IncreaseLootCollectedCounter = function(amount)
{
for (let type in amount)
this.lootCollected += amount[type];
};
StatisticsTracker.prototype.IncreaseResourcesSoldCounter = function(type, amount)
{
this.resourcesSold[type] += amount;
};
StatisticsTracker.prototype.IncreaseResourcesBoughtCounter = function(type, amount)
{
this.resourcesBought[type] += amount;
};
StatisticsTracker.prototype.IncreaseTributesSentCounter = function(amount)
{
this.tributesSent += amount;
};
StatisticsTracker.prototype.IncreaseTributesReceivedCounter = function(amount)
{
this.tributesReceived += amount;
};
StatisticsTracker.prototype.IncreaseTradeIncomeCounter = function(amount)
{
this.tradeIncome += amount;
};
StatisticsTracker.prototype.IncreaseSuccessfulBribesCounter = function()
{
++this.successfulBribes;
};
StatisticsTracker.prototype.IncreaseFailedBribesCounter = function()
{
++this.failedBribes;
};
StatisticsTracker.prototype.GetPercentMapExplored = function()
{
let cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
if (!cmpPlayer)
return 0;
return Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager).GetPercentMapExplored(cmpPlayer.GetPlayerID());
};
/**
* Note: cmpRangeManager.GetUnionPercentMapExplored computes statistics from scratch!
* As a consequence, this function should not be called too often.
*/
StatisticsTracker.prototype.GetTeamPercentMapExplored = function()
{
let cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
if (!cmpPlayer)
return 0;
let team = cmpPlayer.GetTeam();
let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
// If teams are not locked, this statistic won't be displayed, so don't bother computing
if (team == -1 || !cmpPlayer.GetLockTeams())
return cmpRangeManager.GetPercentMapExplored(cmpPlayer.GetPlayerID());
let teamPlayers = [];
let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers();
for (let i = 1; i < numPlayers; ++i)
{
let cmpOtherPlayer = QueryPlayerIDInterface(i);
if (cmpOtherPlayer && cmpOtherPlayer.GetTeam() == team)
teamPlayers.push(i);
}
return cmpRangeManager.GetUnionPercentMapExplored(teamPlayers);
};
StatisticsTracker.prototype.GetPercentMapControlled = function()
{
let cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
if (!cmpPlayer)
return 0;
return Engine.QueryInterface(SYSTEM_ENTITY, IID_TerritoryManager).GetTerritoryPercentage(cmpPlayer.GetPlayerID());
};
StatisticsTracker.prototype.GetTeamPercentMapControlled = function()
{
let cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
if (!cmpPlayer)
return 0;
let team = cmpPlayer.GetTeam();
let cmpTerritoryManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TerritoryManager);
if (team == -1 || !cmpPlayer.GetLockTeams())
return cmpTerritoryManager.GetTerritoryPercentage(cmpPlayer.GetPlayerID());
let teamPercent = 0;
let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers();
for (let i = 1; i < numPlayers; ++i)
{
let cmpOtherPlayer = QueryPlayerIDInterface(i);
if (cmpOtherPlayer && cmpOtherPlayer.GetTeam() == team)
teamPercent += cmpTerritoryManager.GetTerritoryPercentage(i);
}
return teamPercent;
};
StatisticsTracker.prototype.OnTerritoriesChanged = function(msg)
{
this.UpdatePeakPercentages();
};
StatisticsTracker.prototype.OnGlobalPlayerDefeated = function(msg)
{
this.UpdatePeakPercentages();
};
StatisticsTracker.prototype.OnGlobalPlayerWon = function(msg)
{
this.UpdatePeakPercentages();
};
StatisticsTracker.prototype.UpdatePeakPercentages = function()
{
this.peakPercentMapControlled = Math.max(this.peakPercentMapControlled, this.GetPercentMapControlled());
this.teamPeakPercentMapControlled = Math.max(this.teamPeakPercentMapControlled, this.GetTeamPercentMapControlled());
};
/**
* Adds the values of fromData to the end of the arrays of toData.
* If toData misses the needed array, one will be created.
*
* @param fromData - an object of values or a value.
* @param toData - an object of arrays or an array.
**/
StatisticsTracker.prototype.PushValue = function(fromData, toData)
{
if (typeof fromData == "object")
for (let prop in fromData)
{
if (typeof toData[prop] != "object")
toData[prop] = [fromData[prop]];
else
this.PushValue(fromData[prop], toData[prop]);
}
else
toData.push(fromData);
};
StatisticsTracker.prototype.UpdateSequences = function()
{
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
this.sequences.time.push(cmpTimer.GetTime() / 1000);
this.PushValue(this.GetStatistics(), this.sequences);
};
Engine.RegisterComponentType(IID_StatisticsTracker, "StatisticsTracker", StatisticsTracker);