9 | | var playerEnt1 = 5; |
10 | | var auraEnt = 20; |
11 | | var targetEnt = 30; |
12 | | |
13 | | AddMock(SYSTEM_ENTITY, IID_PlayerManager, { |
14 | | GetPlayerByID: function() { return playerEnt1; }, |
15 | | GetNumPlayers: function() { return 2; }, |
| 9 | let playerID = 1; |
| 10 | let playerEnt = 10; |
| 11 | let auraEnt = 20; |
| 12 | let targetEnt = 30; |
| 13 | let template = { "Identity" : { "Classes" : { "_string" : "CorrectClass OtherClass" } } }; |
| 14 | |
| 15 | function testAuras(name, test_function) |
| 16 | { |
| 17 | ResetState(); |
| 18 | |
| 19 | AddMock(SYSTEM_ENTITY, IID_PlayerManager, { |
| 20 | GetPlayerByID: () => playerEnt, |
| 21 | GetNumPlayers: () => 2, |
| 22 | }); |
| 23 | |
| 24 | AddMock(SYSTEM_ENTITY, IID_RangeManager, { |
| 25 | CreateActiveQuery: (ent, minRange, maxRange, players, iid, flags) => 1, |
| 26 | EnableActiveQuery: id => {}, |
| 27 | ResetActiveQuery: id => {}, |
| 28 | DisableActiveQuery: id => {}, |
| 29 | DestroyActiveQuery: id => {}, |
| 30 | GetEntityFlagMask: identifier => {}, |
| 31 | }); |
| 32 | |
| 33 | AddMock(SYSTEM_ENTITY, IID_DataTemplateManager, { |
| 34 | GetAuraTemplate: name => { |
| 35 | switch(name) |
| 36 | { |
| 37 | case "test_global": |
| 38 | return { |
| 39 | "type": "global", |
| 40 | "affects": ["CorrectClass"], |
| 41 | "modifications": [{ "value": "Component/Value", "add": 10 }], |
| 42 | "auraName": "name", |
| 43 | "auraDescription": "description" |
| 44 | }; |
| 45 | case "test_range": |
| 46 | return { |
| 47 | "type": "range", |
| 48 | "radius": 40, |
| 49 | "affects": ["CorrectClass"], |
| 50 | "modifications": [{ "value": "Component/Value", "add": 10 }], |
| 51 | "auraName": "name", |
| 52 | "auraDescription": "description" |
| 53 | }; |
| 54 | case "test_garrisonedUnits": |
| 55 | return { |
| 56 | "type": "garrisonedUnits", |
| 57 | "affects": ["CorrectClass"], |
| 58 | "modifications": [{ "value": "Component/Value", "add": 10 }], |
| 59 | "auraName": "name", |
| 60 | "auraDescription": "description" |
| 61 | }; |
| 62 | case "test_garrison": |
| 63 | return { |
| 64 | "type": "garrison", |
| 65 | "affects": ["CorrectClass"], |
| 66 | "modifications": [{ "value": "Component/Value", "add": 10 }], |
| 67 | "auraName": "name", |
| 68 | "auraDescription": "description" |
| 69 | }; |
| 70 | case "test_formation": |
| 71 | return { |
| 72 | "type": "formation", |
| 73 | "affects": ["CorrectClass"], |
| 74 | "modifications": [{ "value": "Component/Value", "add": 10 }], |
| 75 | "auraName": "name", |
| 76 | "auraDescription": "description" |
| 77 | }; |
| 78 | default: |
| 79 | return {}; |
| 80 | } |
| 81 | } |
| 82 | }); |
| 83 | |
| 84 | AddMock(playerEnt, IID_Player, { |
| 85 | IsAlly: id => id == 1, |
| 86 | IsEnemy: id => id != 1, |
| 87 | GetPlayerID: () => 1, |
| 88 | }); |
| 89 | |
| 90 | AddMock(targetEnt, IID_Identity, { |
| 91 | GetClassesList: () => ["CorrectClass", "OtherClass"], |
| 92 | }); |
| 93 | |
| 94 | AddMock(auraEnt, IID_Position, { |
| 95 | GetPosition2D: () => new Vector2D(), |
| 96 | }); |
| 97 | |
| 98 | AddMock(targetEnt, IID_Position, { |
| 99 | GetPosition2D: () => new Vector2D(), |
| 100 | }); |
| 101 | |
| 102 | AddMock(auraEnt, IID_Ownership, { |
| 103 | GetOwner: () => 1, |
| 104 | }); |
| 105 | |
| 106 | ConstructComponent(SYSTEM_ENTITY, "AuraManager", {}); |
| 107 | let cmpAuras = ConstructComponent(auraEnt, "Auras", { _string: name }); |
| 108 | |
| 109 | test_function(name, cmpAuras); |
| 110 | } |
| 111 | |
| 112 | testAuras("test_global", (name, cmpAuras) => { cmpAuras.OnRangeUpdate({ "tag": 1, "added": [targetEnt], "removed": [] }); |
| 113 | TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Component/Value", 1, targetEnt), 11); |
| 114 | TS_ASSERT_EQUALS(ApplyValueModificationsToTemplate("Component/Value", 1, playerID, template), 11); |
| 115 | }); |
| 116 | |
| 117 | testAuras("test_range", (name, cmpAuras) => { cmpAuras.OnRangeUpdate({ "tag": 1, "added": [targetEnt], "removed": [] }); |
| 118 | TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Component/Value", 1, targetEnt), 11); |
| 119 | TS_ASSERT_EQUALS(ApplyValueModificationsToTemplate("Component/Value", 1, playerID, template), 1); |
| 120 | cmpAuras.OnRangeUpdate({ "tag": 1, "added": [], "removed": [targetEnt] }); |
| 121 | TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Component/Value", 1, targetEnt), 1); |
| 122 | }); |
| 123 | |
| 124 | testAuras("test_garrisonedUnits", (name, cmpAuras) => { cmpAuras.OnGarrisonedUnitsChanged({ "added" : [targetEnt], "removed": [] }); |
| 125 | TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Component/Value", 1, targetEnt), 11); |
| 126 | cmpAuras.OnGarrisonedUnitsChanged({ "added" : [], "removed": [targetEnt] }); |
| 127 | TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Component/Value", 1, targetEnt), 1); |
| 128 | }); |
| 129 | |
| 130 | testAuras("test_garrison", (name, cmpAuras) => { |
| 131 | TS_ASSERT_EQUALS(cmpAuras.HasGarrisonAura(), true); |
| 132 | cmpAuras.ApplyGarrisonBonus(targetEnt); |
| 133 | TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Component/Value", 1, targetEnt), 11); |
| 134 | cmpAuras.RemoveGarrisonBonus(targetEnt); |
| 135 | TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Component/Value", 1, targetEnt), 1); |
| 136 | }); |
| 137 | |
| 138 | testAuras("test_formation", (name, cmpAuras) => { |
| 139 | TS_ASSERT_EQUALS(cmpAuras.HasFormationAura(), true); |
| 140 | cmpAuras.ApplyFormationBonus([targetEnt]); |
| 141 | TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Component/Value", 1, targetEnt), 11); |
| 142 | cmpAuras.RemoveFormationBonus([targetEnt]); |
| 143 | TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Component/Value", 1, targetEnt), 1); |
17 | | |
18 | | AddMock(SYSTEM_ENTITY, IID_RangeManager, { |
19 | | CreateActiveQuery: function(ent, minRange, maxRange, players, iid, flags) { |
20 | | return 1; |
21 | | }, |
22 | | EnableActiveQuery: function(id) { }, |
23 | | ResetActiveQuery: function(id) { if (mode == 0) return []; else return [enemy]; }, |
24 | | DisableActiveQuery: function(id) { }, |
25 | | GetEntityFlagMask: function(identifier) { }, |
26 | | }); |
27 | | |
28 | | AddMock(SYSTEM_ENTITY, IID_DataTemplateManager, { |
29 | | GetAuraTemplate: function(name) { |
30 | | if (name == "test1") |
31 | | return { |
32 | | "type": "global", |
33 | | "affects": ["CorrectClass"], |
34 | | "modifications": [ { "value": "Component/Value", "add": 1 } ], |
35 | | "auraName": "name", |
36 | | "auraDescription": "description" |
37 | | }; |
38 | | return {}; |
39 | | }, |
40 | | }); |
41 | | |
42 | | AddMock(playerEnt1, IID_Player, { |
43 | | IsAlly: function(id) { return id == 1; }, |
44 | | IsEnemy: function(id) { return id != 1; }, |
45 | | GetPlayerID: function() { return 1; }, |
46 | | }); |
47 | | |
48 | | AddMock(targetEnt, IID_Identity, { |
49 | | GetClassesList: function() { return ["CorrectClass"]; }, |
50 | | }); |
51 | | |
52 | | AddMock(auraEnt, IID_Position, { |
53 | | GetPosition2D: function() { return new Vector2D(); }, |
54 | | }); |
55 | | |
56 | | AddMock(targetEnt, IID_Position, { |
57 | | // target ent at 20m distance from aura ent |
58 | | GetPosition2D: function() { return new Vector2D(); }, |
59 | | }); |
60 | | |
61 | | AddMock(targetEnt, IID_Ownership, { |
62 | | GetOwner: function() { return 1; }, |
63 | | }); |
64 | | |
65 | | AddMock(auraEnt, IID_Ownership, { |
66 | | GetOwner: function() { return 1; }, |
67 | | }); |
68 | | |
69 | | ConstructComponent(SYSTEM_ENTITY, "AuraManager", {}); |
70 | | var auras = ConstructComponent(auraEnt, "Auras", {_string: "test1"}); |
71 | | |
72 | | // send the rangeManager message |
73 | | auras.OnRangeUpdate({"tag": 1, "added": [30], "removed": []}); |
74 | | |
75 | | TS_ASSERT_EQUALS(ApplyValueModificationsToEntity("Component/Value", 1, targetEnt), 2); |