Ticket #4149: capturabletest.diff

File capturabletest.diff, 11.2 KB (added by fatherbushido, 8 years ago)

thx elexis and bb for style comments

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

    Capturable.prototype.Reduce = function(a  
    9696            {
    9797                removedAmount += this.cp[i];
    9898                this.cp[i] = 0;
    9999            }
    100100        }
    101         distributedAmount = numberOfEnemies ? (amount - removedAmount) / numberOfEnemies : 0;       
     101        distributedAmount = numberOfEnemies ? (amount - removedAmount) / numberOfEnemies : 0;
    102102    }
    103    
     103
    104104    // give all cp taken to the player
    105105    var takenCp = this.maxCp - this.cp.reduce((a, b) => a + b);
    106106    this.cp[playerID] += takenCp;
    107107
    108108    this.CheckTimer();
    Capturable.prototype.TimerTick = functio  
    216216
    217217    // nothing changed, stop the timer
    218218    var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    219219    cmpTimer.CancelTimer(this.timer);
    220220    this.timer = 0;
    221     Engine.PostMessage(this.entity, MT_CaptureRegenStateChanged, {"regenerating": false, "regenRate": 0, "territoryDecay": 0});
     221    Engine.PostMessage(this.entity, MT_CaptureRegenStateChanged, { "regenerating": false, "regenRate": 0, "territoryDecay": 0 });
    222222};
    223223
    224224/**
    225225 * Start the regeneration timer when no timer exists.
    226226 * When nothing can be modified (f.e. because it is fully regenerated), the
    Capturable.prototype.CheckTimer = functi  
    237237    if (regenRate == 0 && decay == 0)
    238238        return;
    239239
    240240    var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    241241    this.timer = cmpTimer.SetInterval(this.entity, IID_Capturable, "TimerTick", 1000, 1000, null);
    242     Engine.PostMessage(this.entity, MT_CaptureRegenStateChanged, {"ticking": true, "regenRate": regenRate, "territoryDecay": decay});
     242    Engine.PostMessage(this.entity, MT_CaptureRegenStateChanged, { "regenerating": true, "regenRate": regenRate, "territoryDecay": decay });
    243243};
    244244
    245245//// Message Listeners ////
    246246
    247247Capturable.prototype.OnValueModification = function(msg)
  • binaries/data/mods/public/simulation/components/interfaces/Capturable.js

    Engine.RegisterInterface("Capturable");  
    77 */
    88Engine.RegisterMessageType("CapturePointsChanged");
    99
    1010/**
    1111 * Message in the form of { "regenerating": boolean, "rate": number, "territoryDecay": number }
    12  * or in the form { "ticking": boolean, "rate": number, "territoryDecay": number }
    1312 * where "rate" value is always zero when not decaying,
    1413 * sent from Capturable component.
    1514 */
    1615Engine.RegisterMessageType("CaptureRegenStateChanged");
    17 
  • binaries/data/mods/public/simulation/components/tests/test_Capturable.js

     
     1Engine.LoadHelperScript("Player.js");
     2Engine.LoadHelperScript("ValueModification.js");
     3Engine.LoadComponentScript("interfaces/AuraManager.js");
     4Engine.LoadComponentScript("interfaces/Auras.js");
     5Engine.LoadComponentScript("interfaces/Capturable.js");
     6Engine.LoadComponentScript("interfaces/GarrisonHolder.js");
     7Engine.LoadComponentScript("interfaces/StatisticsTracker.js");
     8Engine.LoadComponentScript("interfaces/TechnologyManager.js");
     9Engine.LoadComponentScript("interfaces/TerritoryDecay.js");
     10Engine.LoadComponentScript("interfaces/Timer.js");
     11Engine.LoadComponentScript("Capturable.js");
     12
     13let structure = 20;
     14let playerID = 1;
     15let regenRate = 2;
     16let garrisonedEntities = [30,31,32,33];
     17let decay = false;
     18let decayRate = 30;
     19let maxCp = 3000;
     20
     21function testCapturable(test_function)
     22{
     23    ResetState();
     24
     25    AddMock(SYSTEM_ENTITY, IID_Timer, {
     26        "SetInterval": (ent, iid, funcname, time, repeattime, data) => {},
     27        "CancelTimer": timer => {}
     28    });
     29
     30    AddMock(structure, IID_Ownership, {
     31        "GetOwner": () => playerID,
     32        "SetOwner": id => {}
     33    });
     34
     35    AddMock(structure, IID_GarrisonHolder, {
     36        "GetEntities": () => garrisonedEntities
     37    });
     38
     39    AddMock(structure, IID_Fogging, {
     40        "Activate": () => {}
     41    });
     42
     43    AddMock(10, IID_Player, {
     44        "IsEnemy": id => id != 0
     45    });
     46
     47    AddMock(11, IID_Player, {
     48        "IsEnemy": id => id != 1 && id != 2
     49    });
     50
     51    AddMock(12, IID_Player, {
     52        "IsEnemy": id => id != 1 && id != 2
     53    });
     54
     55    AddMock(13, IID_Player, {
     56        "IsEnemy": id => id != 3
     57    });
     58
     59    AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
     60        "GetNumPlayers": () => 4,
     61        "GetPlayerByID": id => 10 + id
     62    });
     63
     64    AddMock(structure, IID_StatisticsTracker, {
     65        "LostEntity" : () => {},
     66        "CapturedBuilding": () => {}
     67    });
     68
     69    let cmpCapturable = ConstructComponent(structure, "Capturable", {
     70        "CapturePoints" : maxCp,
     71        "RegenRate" : regenRate,
     72        "GarrisonRegenRate" : 5
     73    });
     74
     75    AddMock(structure, IID_TerritoryDecay, {
     76        "IsDecaying": () => decay,
     77        "GetDecayRate": () => decayRate,
     78        "GetConnectedNeighbours": () => [20, 0, 20, 10]
     79    });
     80
     81    test_function(cmpCapturable);
     82    Engine.PostMessage = (ent, iid, message) => {};
     83}
     84
     85// Tests initialisation of the capture points when the entity is created
     86testCapturable( cmpCapturable => {
     87    Engine.PostMessage = function(ent, iid, message)
     88    {
     89        TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22 , "territoryDecay": 0 });
     90    };
     91    cmpCapturable.OnOwnershipChanged({ "from": -1, "to": playerID });
     92    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, maxCp, 0, 0])
     93});
     94
     95// Tests if the message is sent when capture points change
     96testCapturable( cmpCapturable => {
     97    cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
     98    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2000, 0 , 1000]);
     99    Engine.PostMessage = function(ent, iid, message)
     100    {
     101        TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2000, 0 , 1000] });
     102    };
     103    cmpCapturable.RegisterCapturePointsChanged();
     104});
     105
     106// Tests reducing capture points (after a capture attack or a decay)
     107testCapturable( cmpCapturable => {
     108    cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
     109    cmpCapturable.CheckTimer();
     110    Engine.PostMessage = function(ent, iid, message)
     111    {
     112        if (iid == MT_CapturePointsChanged)
     113            TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2000 - 100, 0, 1000 + 100] });
     114        if (iid == MT_CaptureRegenStateChanged)
     115            TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22, "territoryDecay": 0 });
     116    };
     117    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.Reduce(100, 3), 100);
     118    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2000 - 100, 0, 1000 + 100]);
     119});
     120
     121// Tests reducing capture points (after a capture attack or a decay)
     122testCapturable( cmpCapturable => {
     123    cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
     124    cmpCapturable.CheckTimer();
     125    Engine.PostMessage = function(ent, iid, message)
     126    {
     127        if (iid == MT_CapturePointsChanged)
     128            TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 0, 0, 3000] });
     129        if (iid == MT_CaptureRegenStateChanged)
     130            TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22, "territoryDecay": 0 });
     131    };
     132    cmpCapturable.Reduce(2500, 3);
     133    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 0, 0, 3000]);
     134});
     135
     136// Tests regeneration
     137testCapturable( cmpCapturable => {
     138    cmpCapturable.SetCapturePoints([12, 2950, 2 , 36]);
     139    cmpCapturable.CheckTimer();
     140
     141    Engine.PostMessage = function(ent, iid, message)
     142    {
     143        if (iid == MT_CapturePointsChanged)
     144            TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [1, 2972, 2, 25] });
     145        if (iid == MT_CaptureRegenStateChanged)
     146            TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22, "territoryDecay": 0 });
     147    };
     148    cmpCapturable.TimerTick();
     149    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [1, 2972, 2, 25]);
     150
     151    Engine.PostMessage = function(ent, iid, message)
     152    {
     153        if (iid == MT_CapturePointsChanged)
     154            TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2994, 2, 4] });
     155        if (iid == MT_CaptureRegenStateChanged)
     156            TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22, "territoryDecay": 0 });
     157    };
     158    cmpCapturable.TimerTick();
     159    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2994, 2, 4 ]);
     160
     161    Engine.PostMessage = function(ent, iid, message)
     162    {
     163        if (iid == MT_CapturePointsChanged)
     164            TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2998, 2, 0] });
     165        if (iid == MT_CaptureRegenStateChanged)
     166            TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22, "territoryDecay": 0 });
     167    };
     168    cmpCapturable.TimerTick();
     169    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2998, 2, 0]);
     170
     171    Engine.PostMessage = function(ent, iid, message)
     172    {
     173        if (iid == MT_CapturePointsChanged)
     174            TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2998, 2, 0] });
     175        if (iid == MT_CaptureRegenStateChanged)
     176            TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": false, "regenRate": 0, "territoryDecay": 0 });
     177    };
     178    cmpCapturable.TimerTick();
     179    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2998, 2, 0]);
     180});
     181
     182// If the regeneration rate becomes negative, capture points are given in favour of gaia.
     183regenRate = -32;
     184testCapturable( cmpCapturable => {
     185    cmpCapturable.SetCapturePoints([100, 2800, 50, 50]);
     186    cmpCapturable.CheckTimer();
     187    TS_ASSERT_EQUALS(cmpCapturable.GetRegenRate(), -12);
     188
     189    Engine.PostMessage = function(ent, iid, message)
     190    {
     191        if (iid == MT_CapturePointsChanged)
     192            TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [112, 2796, 46, 46] });
     193        if (iid == MT_CaptureRegenStateChanged)
     194            TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": -12, "territoryDecay": 0 });
     195    };
     196    cmpCapturable.TimerTick();
     197    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [112, 2796, 46, 46]);
     198});
     199regenRate=2;
     200
     201// Tests decay
     202testCapturable( cmpCapturable => {
     203    decay = true;
     204    cmpCapturable.SetCapturePoints([2900, 35, 10, 55]);
     205    cmpCapturable.CheckTimer();
     206
     207    let call = 1;
     208    Engine.PostMessage = function(ent, iid, message)
     209    {
     210        if (iid == MT_CapturePointsChanged && call == 1)
     211        {
     212            TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [2912, 5, 22, 61] });
     213            ++call;
     214        }
     215        else if (iid == MT_CapturePointsChanged && call == 2)
     216                TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [2901, 27, 22, 50] });
     217        else if (iid == MT_CaptureRegenStateChanged)
     218            TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": 22 , "territoryDecay": 30 });
     219    };
     220    cmpCapturable.TimerTick();
     221    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [2901, 27, 22, 50]);
     222    decay = false;
     223});
     224
     225// Tests Reduce
     226function testReduce(amount, player, taken)
     227{
     228    testCapturable(cmpCapturable => {
     229        cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
     230        cmpCapturable.CheckTimer();
     231        TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.Reduce(amount, player), taken);
     232    });
     233}
     234
     235testReduce(50, 3, 50);
     236testReduce(50, 2, 50);
     237testReduce(50, 1, 50);
     238testReduce(-50, 3, 0);
     239testReduce(50, 0, 50);
     240testReduce(0, 3, 0);
     241testReduce(1500, 3, 1500);
     242testReduce(2000, 3, 2000);
     243testReduce(3000, 3, 2000);