Ticket #3403: 3403_statistic_tracker_proposal.patch

File 3403_statistic_tracker_proposal.patch, 2.5 KB (added by Imarok, 7 years ago)

Here is a proposal on how to save the progression of all summary values. based on patch by vladislavbelov. Probably fixing the tests is needed.

  • 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    this.sequences = 0;
     148
     149    let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     150    if (!cmpTimer)
     151        return;
     152    this.updateTimer = cmpTimer.SetInterval(
     153        this.entity, IID_StatisticsTracker, "updateSequences", 0, UPDATE_SEQUENCE_INTERVAL, {
     154            "startTime": cmpTimer.GetTime()
     155        }
     156    );
    145157};
    146158
    147159/**
     
    160172    };
    161173};
    162174
    163 StatisticsTracker.prototype.GetStatistics = function()
     175StatisticsTracker.prototype.GetStatisticsWithoutSequences = function()
    164176{
    165177    return {
    166178        "unitsTrained": this.unitsTrained,
     
    195207    };
    196208};
    197209
     210StatisticsTracker.prototype.GetStatistics = function()
     211{
     212    let data = this.GetStatisticsWithoutSequences();
     213    data.sequences = this.sequences;
     214    return data;}
     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        this.sequences = clone(this.GetStatisticsWithoutSequences());
     540        this.sequences.time = [];
     541    }
     542
     543    this.sequences.time.push(deltaTime);
     544    this.PushValue(this.GetStatisticsWithoutSequences(), this.sequences);
     545}
     546
    495547Engine.RegisterComponentType(IID_StatisticsTracker, "StatisticsTracker", StatisticsTracker);