Ticket #4158: 4158.diff

File 4158.diff, 3.8 KB (added by Stan, 7 years ago)

Replace the array by an object.

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

     
    1919    return this.time;
    2020};
    2121
     22/**
     23 * Returns the duration of the latest turn of the game, in integer milliseconds.
     24 */
    2225Timer.prototype.GetLatestTurnLength = function()
    2326{
    2427    return this.turnLength;
     
    3235 */
    3336Timer.prototype.SetTimeout = function(ent, iid, funcname, time, data)
    3437{
    35     var id = ++this.id;
    36     this.timers.set(id, [ent, iid, funcname, this.time + time, 0, data]);
     38    let id = ++this.id;
     39
     40    this.timers.set(id,
     41    {
     42        "entity": ent,
     43        "iid": iid,
     44        "functionName": funcname,
     45        "time": this.time + time,
     46        "repeatTime": 0,
     47        "data": data
     48    });
     49
    3750    return id;
    3851};
    3952
     
    5063{
    5164    if (typeof repeattime != "number" || !(repeattime > 0))
    5265        error("Invalid repeattime to SetInterval of "+funcname);
    53     var id = ++this.id;
    54     this.timers.set(id, [ent, iid, funcname, this.time + time, repeattime, data]);
     66
     67    let id = ++this.id;
     68
     69    this.timers.set(id,
     70    {
     71        "entity": ent,
     72        "iid": iid,
     73        "functionName": funcname,
     74        "time": this.time + time,
     75        "repeatTime": 0,
     76        "data": data
     77    });
     78
    5579    return id;
    5680};
    5781
     
    6690
    6791Timer.prototype.OnUpdate = function(msg)
    6892{
    69     var dt = Math.round(msg.turnLength * 1000);
    70     this.time += dt;
    71     this.turnLength = dt;
     93    this.turnLength = Math.round(msg.turnLength * 1000);
     94    this.time += this.turnLength;
    7295
    7396    // Collect the timers that need to run
    7497    // (We do this in two stages to avoid deleting from the timer list while
    7598    // we're in the middle of iterating through it)
    76     var run = [];
     99    let run = [];
     100   
    77101    for (let id of this.timers.keys())
    78     {
    79         if (this.timers.get(id)[3] <= this.time)
     102        if (this.timers.get(id).time <= this.time)
    80103            run.push(id);
    81     }
    82     for (var i = 0; i < run.length; ++i)
     104
     105    for (let i = 0; i < run.length; ++i)
    83106    {
    84         var id = run[i];
     107        let id = run[i];
     108        let timer = this.timers.get(id);
    85109
    86         var t = this.timers.get(id);
    87         if (!t)
    88             continue; // an earlier timer might have cancelled this one, so skip it
     110        // An earlier timer might have cancelled this one, so skip it
     111        if (!timer)
     112            continue;
    89113
    90         var cmp = Engine.QueryInterface(t[0], t[1]);
    91         if (!cmp)
     114        // The entity was probably destroyed; clean up the timer
     115        let cmpTimer = Engine.QueryInterface(timer.entity, timer.iid);
     116        if (!cmpTimer)
    92117        {
    93             // The entity was probably destroyed; clean up the timer
    94118            this.timers.delete(id);
    95119            continue;
    96120        }
    97121
    98         try {
    99             var lateness = this.time - t[3];
    100             cmp[t[2]](t[5], lateness);
    101         } catch (e) {
    102             var stack = e.stack.trimRight().replace(/^/mg, '  '); // indent the stack trace
    103             error("Error in timer on entity "+t[0]+", IID "+t[1]+", function "+t[2]+": "+e+"\n"+stack+"\n");
     122        try
     123        {
     124            let lateness = this.time - timer.time;
     125            cmpTimer[timer.functionName](timer.data, lateness);
    104126        }
    105 
    106         // Handle repeating timers
    107         if (t[4])
     127        catch (e)
    108128        {
    109             // Add the repeat time to the execution time
    110             t[3] += t[4];
    111             // Add it to the list to get re-executed if it's soon enough
    112             if (t[3] <= this.time)
    113                 run.push(id);
     129            // Indent the stack trace
     130            let stack = e.stack.trimRight().replace(/^/mg, '  ');
     131            error("Error in timer on entity " + timer.entity + ", IID " + timer.iid + ", function " + timer.functionName + ": " + e + "\n" + stack + "\n");
    114132        }
    115         else
     133
     134        // Non-repeating time - delete it
     135        if (!timer.repeatTime)
    116136        {
    117             // Non-repeating time - delete it
    118137            this.timers.delete(id);
     138            continue;
    119139        }
     140        // Handle repeating timers
     141        // Add the repeat time to the execution time
     142        timer.time += timer.repeatTime;
     143        // Add it to the list to get re-executed if it's soon enough
     144        if (timer.time <= this.time)
     145            run.push(id);
    120146    }
    121147};
    122148