Ticket #3403: 3403_statistic_tracker_proposal_v2.patch

File 3403_statistic_tracker_proposal_v2.patch, 2.4 KB (added by Imarok, 7 years ago)

Remove unneeded declaration of sequences

  • binaries/data/mods/public/simulation/components/StatisticsTracker.js

     
    11function StatisticsTracker() {}
    22
     3const UPDATE_SEQUENCE_INTERVAL = 30000; // 30 seconds
     4
    35StatisticsTracker.prototype.Schema =
    46    "<a:component type='system'/><empty/>";
    57
     
    142144    this.lootCollected = 0;
    143145    this.peakPercentMapControlled = 0;
    144146    this.teamPeakPercentMapControlled = 0;
     147
     148    let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     149    if (!cmpTimer)
     150        return;
     151    this.updateTimer = cmpTimer.SetInterval(
     152        this.entity, IID_StatisticsTracker, "updateSequences", 0, UPDATE_SEQUENCE_INTERVAL, {
     153            "startTime": cmpTimer.GetTime()
     154        }
     155    );
    145156};
    146157
    147158/**
     
    160171    };
    161172};
    162173
    163 StatisticsTracker.prototype.GetStatistics = function()
     174StatisticsTracker.prototype.GetStatisticsWithoutSequences = function()
    164175{
    165176    return {
    166177        "unitsTrained": this.unitsTrained,
     
    195206    };
    196207};
    197208
     209StatisticsTracker.prototype.GetStatistics = function()
     210{
     211    let data = this.GetStatisticsWithoutSequences();
     212    data.sequences = this.sequences;
     213    return data;
     214}
     215
    198216/**
    199217 * Increments counter associated with certain entity/counter and type of given entity.
    200218 * @param cmpIdentity The entity identity component
     
    492510        this.teamPeakPercentMapControlled = newPercent;
    493511};
    494512
     513StatisticsTracker.prototype.PushValue = function(fromData, toData)
     514{
     515    if (typeof fromData == "object")
     516        for (let prop in fromData)
     517        {
     518            if (typeof toData[prop] != "object")
     519            {
     520                toData[prop] = [];
     521                toData[prop].push(fromData[prop]);
     522            }
     523            else
     524                this.PushValue(fromData[prop], toData[prop]);
     525        }
     526    else
     527        toData.push(fromData);
     528};
     529
     530StatisticsTracker.prototype.updateSequences = function(data)
     531{
     532    let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     533    if (!cmpTimer)
     534        return;
     535    let deltaTime = (cmpTimer.GetTime() - data.startTime) / 1000;
     536
     537    if (!this.sequences)
     538    {
     539        deltaTime = 0;
     540        this.sequences = clone(this.GetStatisticsWithoutSequences());
     541        this.sequences.time = [];
     542    }
     543
     544    this.sequences.time.push(deltaTime);
     545    this.PushValue(this.GetStatisticsWithoutSequences(), this.sequences);
     546}
     547
    495548Engine.RegisterComponentType(IID_StatisticsTracker, "StatisticsTracker", StatisticsTracker);