Ticket #1459: patch.diff

File patch.diff, 12.4 KB (added by mackwic, 12 years ago)
  • StatisticsTracker.js

     
    1 function StatisticsTracker() {}
    2 
    3 StatisticsTracker.prototype.Schema =
    4     "<a:component type='system'/><empty/>";
    5    
    6 StatisticsTracker.prototype.Init = function()
    7 {
    8     // units
    9     this.unitsTrained = 0;
    10     this.unitsLost = 0;
    11     this.enemyUnitsKilled = 0;
    12     //buildings
    13     this.buildingsConstructed = 0;
    14     this.buildingsLost = 0;
    15     this.enemyBuildingsDestroyed = 0;
    16     // civ centres
    17     this.civCentresBuilt = 0;
    18     this.enemyCivCentresDestroyed = 0;
    19     // resources
    20     this.resourcesGathered = {
    21             "food": 0, 
    22             "wood": 0, 
    23             "metal": 0,
    24             "stone": 0,
    25             "vegetarianFood": 0
    26     }
    27     this.treasuresCollected = 0;
    28 };
    29 
    30 StatisticsTracker.prototype.GetStatistics = function()
    31 {
    32     return {
    33         "unitsTrained": this.unitsTrained,
    34         "unitsLost": this.unitsLost,
    35         "enemyUnitsKilled": this.enemyUnitsKilled,
    36         "buildingsConstructed": this.buildingsConstructed,
    37         "buildingsLost": this.buildingsLost,
    38         "enemyBuildingsDestroyed": this.enemyBuildingsDestroyed,
    39         "civCentresBuilt": this.civCentresBuilt,
    40         "enemyCivCentresDestroyed": this.enemyCivCentresDestroyed,
    41         "resourcesGathered": this.resourcesGathered,
    42         "treasuresCollected": this.treasuresCollected,
    43         "percentMapExplored": this.GetPercentMapExplored()
    44     };
    45 };
    46 
    47 StatisticsTracker.prototype.IncreaseTrainedUnitsCounter = function()
    48 {
    49     return this.unitsTrained++;
    50 };
    51 
    52 StatisticsTracker.prototype.IncreaseConstructedBuildingsCounter = function()
    53 {
    54     return this.buildingsConstructed++;
    55 };
    56 
    57 StatisticsTracker.prototype.IncreaseBuiltCivCentresCounter = function()
    58 {
    59     return this.civCentresBuilt++;
    60 };
    61 
    62 StatisticsTracker.prototype.KilledEntity = function(targetEntity)
    63 {
    64     var cmpTargetEntityIdentity = Engine.QueryInterface(targetEntity, IID_Identity);
    65     if (cmpTargetEntityIdentity)
    66     {
    67         var classes = cmpTargetEntityIdentity.GetClassesList();
    68         // we want to deal only with real structures, not foundations
    69         var cmpFoundation = Engine.QueryInterface(targetEntity, IID_Foundation);
    70         var targetIsStructure = classes.indexOf("Structure") != -1 && cmpFoundation == null;
    71         var targetIsUnit = classes.indexOf("Unit") != -1;
    72         var targetIsCivCentre = classes.indexOf("CivCentre") != -1;
    73                
    74         var cmpTargetOwnership = Engine.QueryInterface(targetEntity, IID_Ownership);
    75        
    76         // don't increase counters if target player is gaia (player 0)
    77         if (cmpTargetOwnership.GetOwner() != 0)
    78         {
    79             if (targetIsUnit) this.enemyUnitsKilled++;
    80             if (targetIsStructure) this.enemyBuildingsDestroyed++;
    81             if (targetIsCivCentre) this.enemyCivCentresDestroyed++;
    82         }
    83     }
    84 };
    85 
    86 StatisticsTracker.prototype.LostEntity = function(lostEntity)
    87 {
    88     var cmpLostEntityIdentity = Engine.QueryInterface(lostEntity, IID_Identity);
    89     if (cmpLostEntityIdentity)
    90     {
    91         var classes = cmpLostEntityIdentity.GetClassesList();
    92         // we want to deal only with real structures, not foundations
    93         var cmpFoundation = Engine.QueryInterface(lostEntity, IID_Foundation);
    94         var lostEntityIsStructure = classes.indexOf("Structure") != -1 && cmpFoundation == null;
    95         var lostEntityIsUnit = classes.indexOf("Unit") != -1;
    96 
    97         if (lostEntityIsUnit) this.unitsLost++;
    98         if (lostEntityIsStructure) this.buildingsLost++;
    99     }
    100 };
    101 
    102 /**
    103  * @param type Generic type of resource (string)
    104  * @param amount Amount of resource, whick should be added (integer)
    105  * @param specificType Specific type of resource (string, optional)
    106  */
    107 StatisticsTracker.prototype.IncreaseResourceGatheredCounter = function(type, amount, specificType)
    108 {
    109     this.resourcesGathered[type] += amount;
    110    
    111     if (type == "food" && (specificType == "fruit" || specificType == "grain"))
    112         this.resourcesGathered["vegetarianFood"] += amount;
    113 };
    114 
    115 StatisticsTracker.prototype.IncreaseTreasuresCollectedCounter = function()
    116 {
    117     return this.treasuresCollected++;
    118 };
    119 
    120 StatisticsTracker.prototype.GetPercentMapExplored = function()
    121 {
    122     var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    123     var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
    124     return cmpRangeManager.GetPercentMapExplored(cmpPlayer.GetPlayerID());
    125 };
    126 
    127 Engine.RegisterComponentType(IID_StatisticsTracker, "StatisticsTracker", StatisticsTracker);
     1function StatisticsTracker() {}
     2
     3
     4/** used for time-relative stats
     5 * This structure is stored in a array and count during 30 seconds
     6 * After this time, a new structure is stored in the next index of the array
     7 *
     8 * TODO: Removing the old structure when the binding with the GUI will be made
     9 * TODO: detecting types of units, building, usage of resources
     10 *  this tree show what's possible but as the counters doesn't handle all the
     11 *  classes but only 'Unit', no difference is made between 'Calvalry',
     12 *  'Infantry' and others
     13 */
     14StatisticsTracker.prototype.InitTracker = function()
     15{
     16    return Object.freeze(
     17    {
     18    Units: Object.freeze(
     19    {
     20        Trained: {
     21        Civilian: 0,        // Not implemented yet
     22        Basic: 0,
     23        Champions: 0,       // Not implemented yet
     24        Heroes: 0,      // Not implemented yet
     25        Ships: 0        // Not implemented yet
     26        },
     27        Lost: {
     28        Civilian: 0,        // Not implemented yet
     29        Basic: 0,
     30        Advanced : 0,
     31        Elite : 0,
     32        Champions: 0,       // Not implemented yet
     33        Heroes: 0,      // Not implemented yet
     34        Ships: 0        // Not implemented yet
     35        },
     36        EnemyKilled: {
     37        Civilian: 0,        // Not implemented yet
     38        Basic: 0,
     39        Advanced : 0,
     40        Elite : 0,
     41        Champions: 0,       // Not implemented yet
     42        Heroes: 0,      // Not implemented yet
     43        Ships: 0        // Not implemented yet
     44        }
     45    }),
     46    Buildings:
     47    {
     48        Built: 0,
     49        Lost: 0,
     50        EnemyDestroyed: 0,
     51        CivCentreBuilt: 0,
     52        CivCentreDestroyed: 0
     53    },
     54    Resources: Object.freeze(
     55    {
     56        Gathered: {
     57        Food: 0,
     58        VegetarianFood: 0,
     59        Wood: 0,
     60        Stone: 0,
     61        Metal: 0
     62        },
     63        Spend: {
     64        Food: 0,        // Not implemented yet
     65        VegetarianFood: 0, // Not implemented yet
     66        Wood: 0,        // Not implemented yet
     67        Stone: 0,       // Not implemented yet
     68        Metal: 0        // Not implemented yet
     69        }
     70    }),
     71    TimeToReach:
     72    {
     73        Town: 0,        // Not implemented yet
     74        City: 0     // Not implemented yet
     75    },
     76    Other:
     77    {
     78        TreasuresCollected: 0,
     79        AnimalsHunted: 0        // Not implemented yet
     80    }
     81    });
     82};
     83
     84StatisticsTracker.prototype.Schema =
     85    "<a:component type='system'/><empty/>";
     86
     87StatisticsTracker.prototype.Init = function()
     88{
     89    // units
     90    this.unitsTrained = 0;
     91    this.unitsLost = 0;
     92    this.enemyUnitsKilled = 0;
     93    //buildings
     94    this.buildingsConstructed = 0;
     95    this.buildingsLost = 0;
     96    this.enemyBuildingsDestroyed = 0;
     97    // civ centres
     98    this.civCentresBuilt = 0;
     99    this.enemyCivCentresDestroyed = 0;
     100    // resources
     101    this.resourcesGathered = {
     102            'food': 0,
     103            'wood': 0,
     104            'metal': 0,
     105            'stone': 0,
     106            'vegetarianFood': 0
     107    };
     108    this.treasuresCollected = 0;
     109
     110    // time relative stats
     111    this.currTime = 0;
     112    this.timeStats = [];
     113    this.timeStats.push(StatisticsTracker.prototype.InitTracker());
     114    var timer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     115    timer.SetInterval(SYSTEM_ENTITY, IID_StatisticsTracker, 'RotateStats', 30000, 30000, {});
     116};
     117
     118StatisticsTracker.prototype.RotateStats = function()
     119{
     120    ++this.currTime;
     121    this.timeStats[this.currTime] = StatisticsTracker.prototype.initTracker();
     122};
     123
     124StatisticsTracker.prototype.GetStatistics = function()
     125{
     126    return {
     127        'unitsTrained': this.unitsTrained,
     128        'unitsLost': this.unitsLost,
     129        'enemyUnitsKilled': this.enemyUnitsKilled,
     130        'buildingsConstructed': this.buildingsConstructed,
     131        'buildingsLost': this.buildingsLost,
     132        'enemyBuildingsDestroyed': this.enemyBuildingsDestroyed,
     133        'civCentresBuilt': this.civCentresBuilt,
     134        'enemyCivCentresDestroyed': this.enemyCivCentresDestroyed,
     135        'resourcesGathered': this.resourcesGathered,
     136        'treasuresCollected': this.treasuresCollected,
     137        'percentMapExplored': this.GetPercentMapExplored()
     138    };
     139};
     140
     141StatisticsTracker.prototype.IncreaseTrainedUnitsCounter = function()
     142{
     143    ++this.timeStats[this.currTime].Units.Trained.Basic;
     144    ++this.unitsTrained;
     145};
     146
     147StatisticsTracker.prototype.IncreaseConstructedBuildingsCounter = function()
     148{
     149    ++this.timeStats[this.currTime].Buildings.Constructed;
     150    ++this.buildingsConstructed;
     151};
     152
     153StatisticsTracker.prototype.IncreaseBuiltCivCentresCounter = function()
     154{
     155    ++this.timeStats[this.currTime].CivCenters.Built;
     156    ++this.civCentresBuilt;
     157};
     158
     159StatisticsTracker.prototype.KilledEntity = function(targetEntity)
     160{
     161    var cmpTargetEntityIdentity = Engine.QueryInterface(targetEntity, IID_Identity);
     162    if (!cmpTargetEntityIdentity)
     163        return;
     164
     165    // don't increase counters if target player is gaia (player 0)
     166    var cmpTargetOwnership = Engine.QueryInterface(targetEntity, IID_Ownership);
     167    if (cmpTargetOwnership.GetOwner() == 0)
     168        return;
     169
     170    // we want to deal only with real structures, not foundations
     171    if (Engine.QueryInterface(targetEntity, IID_Foundation) != null)
     172        return;
     173
     174    cmpTargetEntityIdentity.GetClassesList().forEach(function(elt, index, array) {
     175        switch (elt) {
     176        case 'Structure' :
     177            ++this.timeStats[this.currTime].Buildings.EnemyDestroyed.Basic;
     178            ++this.enemyBuildingsDestroyed;
     179            return;
     180        case 'Unit' :
     181            ++this.enemyUnitsKilled;
     182
     183            switch (cmpTargetEntityIdentity.GetRank()) {
     184            case 'Basic' :
     185                ++this.timeStats[this.currTime].Units.EnemyKilled.Basic;
     186                return;
     187            case 'Advanced' :
     188                ++this.timeStats[this.currTime].Units.EnemyKilled.Advanced;
     189                return;
     190            case 'Elite' :
     191                ++this.timeStats[this.currTime].Units.EnemyKilled.Elite;
     192                return;
     193            default :
     194                return;
     195            }
     196        case 'CivCentre' :
     197            ++this.timeStats[this.currTime].CivCenters.Destroyed;
     198            ++this.enemyCivCentresDestroyed;
     199            return;
     200        default:
     201            return;
     202    }});
     203};
     204
     205StatisticsTracker.prototype.LostEntity = function(lostEntity)
     206{
     207    var cmpLostEntityIdentity = Engine.QueryInterface(lostEntity, IID_Identity);
     208    if (!cmpLostEntityIdentity)
     209    return;
     210    // we want to deal only with real structures, not foundations
     211    if (Engine.QueryInterface(lostEntity, IID_Foundation) == null)
     212    return;
     213
     214    cmpLostEntityIdentity.GetClassesList().forEach(function(elt, index, array) {
     215    switch (elt) {
     216        case 'Structure' :
     217        ++this.timeStats[this.currTime].Buildings.Lost;
     218        ++this.buildingLost;
     219        return;
     220        case 'Unit' :
     221        ++this.unitsLost;
     222
     223        switch (cmpLostEntityIdentity.GetRank()) {
     224            case 'Basic' :
     225            ++this.timeStats[this.currTime].Units.Lost.Basic;
     226            return;
     227            case 'Advanced' :
     228            ++this.timeStats[this.currTime].Units.Lost.Advanced;
     229            return;
     230            case 'Elite' :
     231            ++this.timeStats[this.currTime].Units.Lost.Elite;
     232            return;
     233            default :
     234            return;
     235        }
     236        return;
     237        default:
     238        return;
     239    }});
     240};
     241
     242/**
     243 * @param type Generic type of resource (string).
     244 * @param amount Amount of resource, whick should be added (integer).
     245 * @param specificType Specific type of resource (string, optional).
     246 */
     247StatisticsTracker.prototype.IncreaseResourceGatheredCounter = function(type, amount, specificType)
     248{
     249    this.resourcesGathered[type] += amount;
     250
     251    switch (type) {
     252    case 'food' :
     253        if (specificType == 'fruit' || specificType == 'grain')
     254        {
     255        this.resourcesGathered['vegetarianFood'] += amount;
     256        this.timeStats[this.currTime].Resources.Gathered.VegetarianFood += amount;
     257        }
     258
     259        this.timeStats[this.currTime].Resources.Gathered.Food += amount;
     260        return;
     261    case 'wood' :
     262        this.timeStats[this.currTime].Resources.Gathered.Wood += amount;
     263        return;
     264    case 'stone' :
     265        this.timeStats[this.currTime].Resources.Gathered.Stone += amount;
     266        return;
     267    case 'metal' :
     268        this.timeStats[this.currTime].Resources.Gathered.Metal += amount;
     269        return;
     270    default :
     271        return;
     272    }
     273};
     274
     275StatisticsTracker.prototype.IncreaseTreasuresCollectedCounter = function()
     276{
     277    ++this.timeStats[this.currTime].Other.TreasuresCollected;
     278    ++this.treasuresCollected;
     279};
     280
     281StatisticsTracker.prototype.GetPercentMapExplored = function()
     282{
     283    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     284    var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
     285    return cmpRangeManager.GetPercentMapExplored(cmpPlayer.GetPlayerID());
     286};
     287
     288Engine.RegisterComponentType(IID_StatisticsTracker, 'StatisticsTracker', StatisticsTracker);