Index: StatisticsTracker.js
===================================================================
--- StatisticsTracker.js	(révision 11921)
+++ StatisticsTracker.js	(copie de travail)
@@ -1,127 +1,288 @@
-function StatisticsTracker() {}
-
-StatisticsTracker.prototype.Schema =
-	"<a:component type='system'/><empty/>";
-	
-StatisticsTracker.prototype.Init = function()
-{
-	// units
-	this.unitsTrained = 0;
-	this.unitsLost = 0;
-	this.enemyUnitsKilled = 0;
-	//buildings
-	this.buildingsConstructed = 0;
-	this.buildingsLost = 0;
-	this.enemyBuildingsDestroyed = 0;
-	// civ centres
-	this.civCentresBuilt = 0;
-	this.enemyCivCentresDestroyed = 0;
-	// resources
-	this.resourcesGathered = {
-			"food": 0,	
-			"wood": 0,	
-			"metal": 0,	
-			"stone": 0,
-			"vegetarianFood": 0
-	}
-	this.treasuresCollected = 0;
-};
-
-StatisticsTracker.prototype.GetStatistics = function()
-{
-	return {
- 		"unitsTrained": this.unitsTrained,
- 		"unitsLost": this.unitsLost,
- 		"enemyUnitsKilled": this.enemyUnitsKilled,
- 		"buildingsConstructed": this.buildingsConstructed,
- 		"buildingsLost": this.buildingsLost,
- 		"enemyBuildingsDestroyed": this.enemyBuildingsDestroyed,
- 		"civCentresBuilt": this.civCentresBuilt,
- 		"enemyCivCentresDestroyed": this.enemyCivCentresDestroyed,
- 		"resourcesGathered": this.resourcesGathered,
-		"treasuresCollected": this.treasuresCollected,
-		"percentMapExplored": this.GetPercentMapExplored()
- 	};
-};
-
-StatisticsTracker.prototype.IncreaseTrainedUnitsCounter = function()
-{
-	return this.unitsTrained++;
-};
-
-StatisticsTracker.prototype.IncreaseConstructedBuildingsCounter = function()
-{
-	return this.buildingsConstructed++;
-};
-
-StatisticsTracker.prototype.IncreaseBuiltCivCentresCounter = function()
-{
-	return this.civCentresBuilt++;
-};
-
-StatisticsTracker.prototype.KilledEntity = function(targetEntity)
-{
-	var cmpTargetEntityIdentity = Engine.QueryInterface(targetEntity, IID_Identity);
-	if (cmpTargetEntityIdentity)
-	{
-		var classes = cmpTargetEntityIdentity.GetClassesList();
-		// we want to deal only with real structures, not foundations
-		var cmpFoundation = Engine.QueryInterface(targetEntity, IID_Foundation);
-		var targetIsStructure = classes.indexOf("Structure") != -1 && cmpFoundation == null;
-		var targetIsUnit = classes.indexOf("Unit") != -1;
-		var targetIsCivCentre = classes.indexOf("CivCentre") != -1;
-				
-		var cmpTargetOwnership = Engine.QueryInterface(targetEntity, IID_Ownership);
-		
-		// don't increase counters if target player is gaia (player 0)
-		if (cmpTargetOwnership.GetOwner() != 0)
-		{
-			if (targetIsUnit) this.enemyUnitsKilled++;
-			if (targetIsStructure) this.enemyBuildingsDestroyed++;
-			if (targetIsCivCentre) this.enemyCivCentresDestroyed++;
-		}
-	}
-};
-
-StatisticsTracker.prototype.LostEntity = function(lostEntity)
-{
-	var cmpLostEntityIdentity = Engine.QueryInterface(lostEntity, IID_Identity);
-	if (cmpLostEntityIdentity)
-	{
-		var classes = cmpLostEntityIdentity.GetClassesList();
-		// we want to deal only with real structures, not foundations
-		var cmpFoundation = Engine.QueryInterface(lostEntity, IID_Foundation);
-		var lostEntityIsStructure = classes.indexOf("Structure") != -1 && cmpFoundation == null;
-		var lostEntityIsUnit = classes.indexOf("Unit") != -1;
-
-		if (lostEntityIsUnit) this.unitsLost++;
-		if (lostEntityIsStructure) this.buildingsLost++;
-	}
-};
-
-/**
- * @param type Generic type of resource (string)
- * @param amount Amount of resource, whick should be added (integer)
- * @param specificType Specific type of resource (string, optional)
- */
-StatisticsTracker.prototype.IncreaseResourceGatheredCounter = function(type, amount, specificType)
-{
-	this.resourcesGathered[type] += amount;
-	
-	if (type == "food" && (specificType == "fruit" || specificType == "grain"))
-		this.resourcesGathered["vegetarianFood"] += amount;
-};
-
-StatisticsTracker.prototype.IncreaseTreasuresCollectedCounter = function()
-{
-	return this.treasuresCollected++;
-};
-
-StatisticsTracker.prototype.GetPercentMapExplored = function()
-{
-	var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
-	var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
-	return cmpRangeManager.GetPercentMapExplored(cmpPlayer.GetPlayerID());
-};
-
-Engine.RegisterComponentType(IID_StatisticsTracker, "StatisticsTracker", StatisticsTracker);
+function StatisticsTracker() {}
+
+
+/** used for time-relative stats
+ * This structure is stored in a array and count during 30 seconds
+ * After this time, a new structure is stored in the next index of the array
+ *
+ * TODO: Removing the old structure when the binding with the GUI will be made
+ * TODO: detecting types of units, building, usage of resources
+ *	this tree show what's possible but as the counters doesn't handle all the
+ *	classes but only 'Unit', no difference is made between 'Calvalry',
+ *	'Infantry' and others
+ */
+StatisticsTracker.prototype.InitTracker = function()
+{
+    return Object.freeze(
+    {
+	Units: Object.freeze(
+	{
+	    Trained: {
+		Civilian: 0,	    // Not implemented yet
+		Basic: 0,
+		Champions: 0,	    // Not implemented yet
+		Heroes: 0,	    // Not implemented yet
+		Ships: 0	    // Not implemented yet
+	    },
+	    Lost: {
+		Civilian: 0,	    // Not implemented yet
+		Basic: 0,
+		Advanced : 0,
+		Elite : 0,
+		Champions: 0,	    // Not implemented yet
+		Heroes: 0,	    // Not implemented yet
+		Ships: 0	    // Not implemented yet
+	    },
+	    EnemyKilled: {
+		Civilian: 0,	    // Not implemented yet
+		Basic: 0,
+		Advanced : 0,
+		Elite : 0,
+		Champions: 0,	    // Not implemented yet
+		Heroes: 0,	    // Not implemented yet
+		Ships: 0	    // Not implemented yet
+	    }
+	}),
+	Buildings:
+	{
+	    Built: 0,
+	    Lost: 0,
+	    EnemyDestroyed: 0,
+	    CivCentreBuilt: 0,
+	    CivCentreDestroyed: 0
+	},
+	Resources: Object.freeze(
+	{
+	    Gathered: {
+		Food: 0,
+		VegetarianFood: 0,
+		Wood: 0,
+		Stone: 0,
+		Metal: 0
+	    },
+	    Spend: {
+		Food: 0,	    // Not implemented yet
+		VegetarianFood: 0, // Not implemented yet
+		Wood: 0,	    // Not implemented yet
+		Stone: 0,	    // Not implemented yet
+		Metal: 0	    // Not implemented yet
+	    }
+	}),
+	TimeToReach:
+	{
+	    Town: 0,	    // Not implemented yet
+	    City: 0	    // Not implemented yet
+	},
+	Other:
+	{
+	    TreasuresCollected: 0,
+	    AnimalsHunted: 0	    // Not implemented yet
+	}
+    });
+};
+
+StatisticsTracker.prototype.Schema =
+	"<a:component type='system'/><empty/>";
+
+StatisticsTracker.prototype.Init = function()
+{
+	// units
+	this.unitsTrained = 0;
+	this.unitsLost = 0;
+	this.enemyUnitsKilled = 0;
+	//buildings
+	this.buildingsConstructed = 0;
+	this.buildingsLost = 0;
+	this.enemyBuildingsDestroyed = 0;
+	// civ centres
+	this.civCentresBuilt = 0;
+	this.enemyCivCentresDestroyed = 0;
+	// resources
+	this.resourcesGathered = {
+			'food': 0,
+			'wood': 0,
+			'metal': 0,
+			'stone': 0,
+			'vegetarianFood': 0
+	};
+	this.treasuresCollected = 0;
+
+	// time relative stats
+	this.currTime = 0;
+	this.timeStats = [];
+	this.timeStats.push(StatisticsTracker.prototype.InitTracker());
+	var timer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
+	timer.SetInterval(SYSTEM_ENTITY, IID_StatisticsTracker, 'RotateStats', 30000, 30000, {});
+};
+
+StatisticsTracker.prototype.RotateStats = function()
+{
+	++this.currTime;
+	this.timeStats[this.currTime] = StatisticsTracker.prototype.initTracker();
+};
+
+StatisticsTracker.prototype.GetStatistics = function()
+{
+	return {
+ 		'unitsTrained': this.unitsTrained,
+ 		'unitsLost': this.unitsLost,
+ 		'enemyUnitsKilled': this.enemyUnitsKilled,
+ 		'buildingsConstructed': this.buildingsConstructed,
+ 		'buildingsLost': this.buildingsLost,
+ 		'enemyBuildingsDestroyed': this.enemyBuildingsDestroyed,
+ 		'civCentresBuilt': this.civCentresBuilt,
+ 		'enemyCivCentresDestroyed': this.enemyCivCentresDestroyed,
+ 		'resourcesGathered': this.resourcesGathered,
+		'treasuresCollected': this.treasuresCollected,
+		'percentMapExplored': this.GetPercentMapExplored()
+ 	};
+};
+
+StatisticsTracker.prototype.IncreaseTrainedUnitsCounter = function()
+{
+	++this.timeStats[this.currTime].Units.Trained.Basic;
+	++this.unitsTrained;
+};
+
+StatisticsTracker.prototype.IncreaseConstructedBuildingsCounter = function()
+{
+	++this.timeStats[this.currTime].Buildings.Constructed;
+	++this.buildingsConstructed;
+};
+
+StatisticsTracker.prototype.IncreaseBuiltCivCentresCounter = function()
+{
+	++this.timeStats[this.currTime].CivCenters.Built;
+	++this.civCentresBuilt;
+};
+
+StatisticsTracker.prototype.KilledEntity = function(targetEntity)
+{
+	var cmpTargetEntityIdentity = Engine.QueryInterface(targetEntity, IID_Identity);
+	if (!cmpTargetEntityIdentity)
+	    return;
+
+	// don't increase counters if target player is gaia (player 0)
+	var cmpTargetOwnership = Engine.QueryInterface(targetEntity, IID_Ownership);
+	if (cmpTargetOwnership.GetOwner() == 0)
+	    return;
+
+	// we want to deal only with real structures, not foundations
+	if (Engine.QueryInterface(targetEntity, IID_Foundation) != null)
+	    return;
+
+	cmpTargetEntityIdentity.GetClassesList().forEach(function(elt, index, array) {
+	    switch (elt) {
+		case 'Structure' :
+		    ++this.timeStats[this.currTime].Buildings.EnemyDestroyed.Basic;
+		    ++this.enemyBuildingsDestroyed;
+		    return;
+		case 'Unit' :
+		    ++this.enemyUnitsKilled;
+
+		    switch (cmpTargetEntityIdentity.GetRank()) {
+			case 'Basic' :
+			    ++this.timeStats[this.currTime].Units.EnemyKilled.Basic;
+			    return;
+			case 'Advanced' :
+			    ++this.timeStats[this.currTime].Units.EnemyKilled.Advanced;
+			    return;
+			case 'Elite' :
+			    ++this.timeStats[this.currTime].Units.EnemyKilled.Elite;
+			    return;
+			default :
+			    return;
+		    }
+		case 'CivCentre' :
+		    ++this.timeStats[this.currTime].CivCenters.Destroyed;
+		    ++this.enemyCivCentresDestroyed;
+		    return;
+		default:
+		    return;
+	}});
+};
+
+StatisticsTracker.prototype.LostEntity = function(lostEntity)
+{
+    var cmpLostEntityIdentity = Engine.QueryInterface(lostEntity, IID_Identity);
+    if (!cmpLostEntityIdentity)
+	return;
+    // we want to deal only with real structures, not foundations
+    if (Engine.QueryInterface(lostEntity, IID_Foundation) == null)
+	return;
+
+    cmpLostEntityIdentity.GetClassesList().forEach(function(elt, index, array) {
+	switch (elt) {
+	    case 'Structure' :
+		++this.timeStats[this.currTime].Buildings.Lost;
+		++this.buildingLost;
+		return;
+	    case 'Unit' :
+		++this.unitsLost;
+
+		switch (cmpLostEntityIdentity.GetRank()) {
+		    case 'Basic' :
+			++this.timeStats[this.currTime].Units.Lost.Basic;
+			return;
+		    case 'Advanced' :
+			++this.timeStats[this.currTime].Units.Lost.Advanced;
+			return;
+		    case 'Elite' :
+			++this.timeStats[this.currTime].Units.Lost.Elite;
+			return;
+		    default :
+			return;
+		}
+		return;
+	    default:
+		return;
+	}});
+};
+
+/**
+ * @param type Generic type of resource (string).
+ * @param amount Amount of resource, whick should be added (integer).
+ * @param specificType Specific type of resource (string, optional).
+ */
+StatisticsTracker.prototype.IncreaseResourceGatheredCounter = function(type, amount, specificType)
+{
+	this.resourcesGathered[type] += amount;
+
+	switch (type) {
+	case 'food' :
+	    if (specificType == 'fruit' || specificType == 'grain')
+	    {
+		this.resourcesGathered['vegetarianFood'] += amount;
+		this.timeStats[this.currTime].Resources.Gathered.VegetarianFood += amount;
+	    }
+
+	    this.timeStats[this.currTime].Resources.Gathered.Food += amount;
+	    return;
+	case 'wood' :
+	    this.timeStats[this.currTime].Resources.Gathered.Wood += amount;
+	    return;
+	case 'stone' :
+	    this.timeStats[this.currTime].Resources.Gathered.Stone += amount;
+	    return;
+	case 'metal' :
+	    this.timeStats[this.currTime].Resources.Gathered.Metal += amount;
+	    return;
+	default :
+	    return;
+	}
+};
+
+StatisticsTracker.prototype.IncreaseTreasuresCollectedCounter = function()
+{
+	++this.timeStats[this.currTime].Other.TreasuresCollected;
+	++this.treasuresCollected;
+};
+
+StatisticsTracker.prototype.GetPercentMapExplored = function()
+{
+	var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
+	var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
+	return cmpRangeManager.GetPercentMapExplored(cmpPlayer.GetPlayerID());
+};
+
+Engine.RegisterComponentType(IID_StatisticsTracker, 'StatisticsTracker', StatisticsTracker);
