Ticket #4149: capturabletest.2.diff

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

thx elexis and sanderd17 for remarks

  • 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");  
    66 * sent from Capturable component.
    77 */
    88Engine.RegisterMessageType("CapturePointsChanged");
    99
    1010/**
    11  * Message in the form of { "regenerating": boolean, "rate": number, "territoryDecay": number }
    12  * or in the form { "ticking": boolean, "rate": number, "territoryDecay": number }
     11 * Message in the form of { "regenerating": boolean, "regenRate": 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 testData = {
     14    "structure": 20,
     15    "playerID": 1,
     16    "regenRate": 2,
     17    "garrisonedEntities": [30,31,32,33],
     18    "garrisonRegenRate": 5,
     19    "decay": false,
     20    "decayRate": 30,
     21    "maxCp": 3000,
     22    "neighbours": [20, 0, 20, 10]
     23};
     24
     25function testCapturable(testData, test_function)
     26{
     27    ResetState();
     28
     29    AddMock(SYSTEM_ENTITY, IID_Timer, {
     30        "SetInterval": (ent, iid, funcname, time, repeattime, data) => {},
     31        "CancelTimer": timer => {}
     32    });
     33
     34    AddMock(testData.structure, IID_Ownership, {
     35        "GetOwner": () => testData.playerID,
     36        "SetOwner": id => {}
     37    });
     38
     39    AddMock(testData.structure, IID_GarrisonHolder, {
     40        "GetEntities": () => testData.garrisonedEntities
     41    });
     42
     43    AddMock(testData.structure, IID_Fogging, {
     44        "Activate": () => {}
     45    });
     46
     47    AddMock(10, IID_Player, {
     48        "IsEnemy": id => id != 0
     49    });
     50
     51    AddMock(11, IID_Player, {
     52        "IsEnemy": id => id != 1 && id != 2
     53    });
     54
     55    AddMock(12, IID_Player, {
     56        "IsEnemy": id => id != 1 && id != 2
     57    });
     58
     59    AddMock(13, IID_Player, {
     60        "IsEnemy": id => id != 3
     61    });
     62
     63    AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
     64        "GetNumPlayers": () => 4,
     65        "GetPlayerByID": id => 10 + id
     66    });
     67
     68    AddMock(testData.structure, IID_StatisticsTracker, {
     69        "LostEntity" : () => {},
     70        "CapturedBuilding": () => {}
     71    });
     72
     73    let cmpCapturable = ConstructComponent(testData.structure, "Capturable", {
     74        "CapturePoints" : testData.maxCp,
     75        "RegenRate" : testData.regenRate,
     76        "GarrisonRegenRate" : testData.garrisonRegenRate
     77    });
     78
     79    AddMock(testData.structure, IID_TerritoryDecay, {
     80        "IsDecaying": () => testData.decay,
     81        "GetDecayRate": () => testData.decayRate,
     82        "GetConnectedNeighbours": () => testData.neighbours
     83    });
     84
     85    TS_ASSERT_EQUALS(cmpCapturable.GetRegenRate(), testData.regenRate + testData.garrisonRegenRate * testData.garrisonedEntities.length);
     86    test_function(cmpCapturable);
     87    Engine.PostMessage = (ent, iid, message) => {};
     88}
     89
     90// Tests initialisation of the capture points when the entity is created
     91testCapturable(testData, cmpCapturable => {
     92    Engine.PostMessage = function(ent, iid, message) {
     93        TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": cmpCapturable.GetRegenRate() , "territoryDecay": 0 });
     94    };
     95    cmpCapturable.OnOwnershipChanged({ "from": -1, "to": testData.playerID });
     96    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 3000, 0, 0])
     97});
     98
     99// Tests if the message is sent when capture points change
     100testCapturable(testData, cmpCapturable => {
     101    cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
     102    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2000, 0 , 1000]);
     103    Engine.PostMessage = function(ent, iid, message)
     104    {
     105        TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2000, 0 , 1000] });
     106    };
     107    cmpCapturable.RegisterCapturePointsChanged();
     108});
     109
     110// Tests reducing capture points (after a capture attack or a decay)
     111testCapturable(testData, cmpCapturable => {
     112    cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
     113    cmpCapturable.CheckTimer();
     114    Engine.PostMessage = function(ent, iid, message) {
     115        if (iid == MT_CapturePointsChanged)
     116            TS_ASSERT_UNEVAL_EQUALS(message, { "capturePoints": [0, 2000 - 100, 0, 1000 + 100] });
     117        if (iid == MT_CaptureRegenStateChanged)
     118            TS_ASSERT_UNEVAL_EQUALS(message, { "regenerating": true, "regenRate": cmpCapturable.GetRegenRate(), "territoryDecay": 0 });
     119    };
     120    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.Reduce(100, 3), 100);
     121    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 2000 - 100, 0, 1000 + 100]);
     122});
     123
     124// Tests reducing capture points (after a capture attack or a decay)
     125testCapturable(testData, cmpCapturable => {
     126    cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
     127    cmpCapturable.CheckTimer();
     128    TS_ASSERT_EQUALS(cmpCapturable.Reduce(2500, 3),2000);
     129    TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), [0, 0, 0, 3000]);
     130});
     131
     132function testRegen(testData, cpIn, cpOut, regenerating)
     133{
     134    testCapturable(testData, cmpCapturable => {
     135        cmpCapturable.SetCapturePoints(cpIn);
     136        cmpCapturable.CheckTimer();
     137        Engine.PostMessage = function(ent, iid, message) {
     138            if (iid == MT_CaptureRegenStateChanged)
     139                TS_ASSERT_UNEVAL_EQUALS(message.regenerating, regenerating);
     140        };
     141        cmpCapturable.TimerTick(cpIn);
     142        TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), cpOut);
     143    });
     144}
     145
     146// With our testData, the total regen rate is 22. That should be taken from the ennemies
     147testRegen(testData, [12, 2950, 2 , 36], [1, 2972, 2, 25], true);
     148testRegen(testData, [0, 2994, 2, 4], [0, 2998, 2, 0], true);
     149testRegen(testData, [0, 2998, 2, 0], [0, 2998, 2, 0], false);
     150
     151// If the regeneration rate becomes negative, capture points are given in favour of gaia
     152testData.regenRate = -32;
     153// With our testData, the total regen rate is -12. That should be taken from all players to gaia
     154testRegen(testData, [100, 2800, 50, 50], [112, 2796, 46, 46], true);
     155testData.regenRate = 2;
     156
     157function testDecay(testData, cpIn, cpOut)
     158{
     159    testCapturable(testData, cmpCapturable => {
     160        cmpCapturable.SetCapturePoints(cpIn);
     161        cmpCapturable.CheckTimer();
     162        Engine.PostMessage = function(ent, iid, message) {
     163            if (iid == MT_CaptureRegenStateChanged)
     164                TS_ASSERT_UNEVAL_EQUALS(message.territoryDecay, testData.decayRate);
     165        };
     166        cmpCapturable.TimerTick();
     167        TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.GetCapturePoints(), cpOut);
     168    });
     169}
     170testData.decay = true;
     171// With our testData, the decay rate is 30, that should be given to all neighbours with weights [20/50, 0, 20/50, 10/50], then it regens.
     172testDecay(testData, [2900, 35, 10, 55], [2901, 27, 22, 50]);
     173testData.decay = false;
     174
     175// Tests Reduce
     176function testReduce(testData, amount, player, taken)
     177{
     178    testCapturable(testData, cmpCapturable => {
     179        cmpCapturable.SetCapturePoints([0, 2000, 0 , 1000]);
     180        cmpCapturable.CheckTimer();
     181        TS_ASSERT_UNEVAL_EQUALS(cmpCapturable.Reduce(amount, player), taken);
     182    });
     183}
     184
     185testReduce(testData, 50, 3, 50);
     186testReduce(testData, 50, 2, 50);
     187testReduce(testData, 50, 1, 50);
     188testReduce(testData, -50, 3, 0);
     189testReduce(testData, 50, 0, 50);
     190testReduce(testData, 0, 3, 0);
     191testReduce(testData, 1500, 3, 1500);
     192testReduce(testData, 2000, 3, 2000);
     193testReduce(testData, 3000, 3, 2000);