Differential D144 Diff 1667 ps/trunk/binaries/data/mods/public/simulation/components/StatisticsTracker.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/StatisticsTracker.js
function StatisticsTracker() {} | function StatisticsTracker() {} | ||||
const g_UpdateSequenceInterval = 30 * 1000; | |||||
StatisticsTracker.prototype.Schema = | StatisticsTracker.prototype.Schema = | ||||
"<empty/>"; | "<empty/>"; | ||||
StatisticsTracker.prototype.Init = function() | StatisticsTracker.prototype.Init = function() | ||||
{ | { | ||||
this.unitsClasses = [ | this.unitsClasses = [ | ||||
"Infantry", | "Infantry", | ||||
"Worker", | "Worker", | ||||
▲ Show 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | StatisticsTracker.prototype.Init = function() | ||||
this.tributesSent = 0; | this.tributesSent = 0; | ||||
this.tributesReceived = 0; | this.tributesReceived = 0; | ||||
this.tradeIncome = 0; | this.tradeIncome = 0; | ||||
this.treasuresCollected = 0; | this.treasuresCollected = 0; | ||||
this.lootCollected = 0; | this.lootCollected = 0; | ||||
this.peakPercentMapControlled = 0; | this.peakPercentMapControlled = 0; | ||||
this.teamPeakPercentMapControlled = 0; | this.teamPeakPercentMapControlled = 0; | ||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | |||||
this.updateTimer = cmpTimer.SetInterval( | |||||
this.entity, IID_StatisticsTracker, "updateSequences", 0, g_UpdateSequenceInterval); | |||||
}; | }; | ||||
/** | /** | ||||
* Returns a subset of statistics that will be added to the simulation state, | * 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 | * thus called each turn. Basic statistics should not contain data that would | ||||
* be expensive to compute. | * be expensive to compute. | ||||
* | * | ||||
* Note: as of now, nothing in the game needs that, but some AIs developed by | * Note: as of now, nothing in the game needs that, but some AIs developed by | ||||
Show All 37 Lines | return { | ||||
"teamPercentMapExplored": this.GetTeamPercentMapExplored(), | "teamPercentMapExplored": this.GetTeamPercentMapExplored(), | ||||
"percentMapControlled": this.GetPercentMapControlled(), | "percentMapControlled": this.GetPercentMapControlled(), | ||||
"teamPercentMapControlled": this.GetTeamPercentMapControlled(), | "teamPercentMapControlled": this.GetTeamPercentMapControlled(), | ||||
"peakPercentMapControlled": this.peakPercentMapControlled, | "peakPercentMapControlled": this.peakPercentMapControlled, | ||||
"teamPeakPercentMapControlled": this.teamPeakPercentMapControlled | "teamPeakPercentMapControlled": this.teamPeakPercentMapControlled | ||||
}; | }; | ||||
}; | }; | ||||
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; | |||||
} | |||||
/** | /** | ||||
* Increments counter associated with certain entity/counter and type of given entity. | * Increments counter associated with certain entity/counter and type of given entity. | ||||
* @param cmpIdentity The entity identity component | * @param cmpIdentity - the entity identity component. | ||||
* @param counter The name of the counter to increment (e.g. "unitsTrained") | * @param counter - the name of the counter to increment (e.g. "unitsTrained"). | ||||
* @param type The type of the counter (e.g. "workers") | * @param type - the type of the counter (e.g. "workers"). | ||||
*/ | */ | ||||
StatisticsTracker.prototype.CounterIncrement = function(cmpIdentity, counter, type) | StatisticsTracker.prototype.CounterIncrement = function(cmpIdentity, counter, type) | ||||
{ | { | ||||
var classes = cmpIdentity.GetClassesList(); | var classes = cmpIdentity.GetClassesList(); | ||||
if (!classes) | if (!classes) | ||||
return; | return; | ||||
if (classes.indexOf(type) != -1) | if (classes.indexOf(type) != -1) | ||||
▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | if (cmpCapturedEntityIdentity.HasClass("Structure")) | ||||
if (costs) | if (costs) | ||||
for (let type in costs) | for (let type in costs) | ||||
this.buildingsCapturedValue += costs[type]; | this.buildingsCapturedValue += costs[type]; | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* @param type Generic type of resource (string) | * @param {string} type - generic type of resource. | ||||
* @param amount Amount of resource, whick should be added (integer) | * @param {number} amount - amount of resource, whick should be added. | ||||
* @param specificType Specific type of resource (string, optional) | * @param {string} specificType - specific type of resource. | ||||
*/ | */ | ||||
StatisticsTracker.prototype.IncreaseResourceGatheredCounter = function(type, amount, specificType) | StatisticsTracker.prototype.IncreaseResourceGatheredCounter = function(type, amount, specificType) | ||||
{ | { | ||||
this.resourcesGathered[type] += amount; | this.resourcesGathered[type] += amount; | ||||
if (type == "food" && (specificType == "fruit" || specificType == "grain")) | if (type == "food" && (specificType == "fruit" || specificType == "grain")) | ||||
this.resourcesGathered.vegetarianFood += amount; | this.resourcesGathered.vegetarianFood += amount; | ||||
}; | }; | ||||
/** | /** | ||||
* @param type Generic type of resource (string) | * @param {string} type - generic type of resource. | ||||
* @param amount Amount of resource, which should be added (integer) | * @param {number} amount - amount of resource, which should be added. | ||||
*/ | */ | ||||
StatisticsTracker.prototype.IncreaseResourceUsedCounter = function(type, amount) | StatisticsTracker.prototype.IncreaseResourceUsedCounter = function(type, amount) | ||||
{ | { | ||||
this.resourcesUsed[type] += amount; | this.resourcesUsed[type] += amount; | ||||
}; | }; | ||||
StatisticsTracker.prototype.IncreaseTreasuresCollectedCounter = function() | StatisticsTracker.prototype.IncreaseTreasuresCollectedCounter = function() | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 106 Lines • ▼ Show 20 Lines | StatisticsTracker.prototype.OnTerritoriesChanged = function(msg) | ||||
if (newPercent > this.peakPercentMapControlled) | if (newPercent > this.peakPercentMapControlled) | ||||
this.peakPercentMapControlled = newPercent; | this.peakPercentMapControlled = newPercent; | ||||
newPercent = this.GetTeamPercentMapControlled(); | newPercent = this.GetTeamPercentMapControlled(); | ||||
if (newPercent > this.teamPeakPercentMapControlled) | if (newPercent > this.teamPeakPercentMapControlled) | ||||
this.teamPeakPercentMapControlled = newPercent; | this.teamPeakPercentMapControlled = newPercent; | ||||
}; | }; | ||||
/** | |||||
* 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); | |||||
// Don't do this on Init, because GetStatistics doesn't work in this state of the game | |||||
// This is probably, because the simulation hasn't totally started/initialized and we query some simulation values | |||||
if (!this.sequences) | |||||
{ | |||||
this.sequences = clone(this.GetStatistics()); | |||||
this.sequences.time = []; | |||||
} | |||||
this.sequences.time.push(cmpTimer.GetTime() / 1000); | |||||
this.PushValue(this.GetStatistics(), this.sequences); | |||||
} | |||||
Engine.RegisterComponentType(IID_StatisticsTracker, "StatisticsTracker", StatisticsTracker); | Engine.RegisterComponentType(IID_StatisticsTracker, "StatisticsTracker", StatisticsTracker); |
Wildfire Games · Phabricator