From c14a7a0b0bcb00af4368af2c24ece78de213bf13 Mon Sep 17 00:00:00 2001 From: leper Date: Mon, 1 May 2017 00:44:08 +0000 Subject: [PATCH] Builder component tests and some cleanup. Patch by Stan. Reviewed By: leper, bb Differential Revision: https://code.wildfiregames.com/D99 This was SVN commit r19488. --- .../public/simulation/components/Builder.js | 43 ++++++------- .../components/tests/test_Builder.js | 61 +++++++++++++++++++ 2 files changed, 80 insertions(+), 24 deletions(-) create mode 100644 binaries/data/mods/public/simulation/components/tests/test_Builder.js diff --git a/binaries/data/mods/public/simulation/components/Builder.js b/binaries/data/mods/public/simulation/components/Builder.js index 2cecb12aa6..27b9530e06 100644 --- a/binaries/data/mods/public/simulation/components/Builder.js +++ b/binaries/data/mods/public/simulation/components/Builder.js @@ -8,10 +8,10 @@ Builder.prototype.Schema = "\n structures/{civ}_barracks\n structures/{civ}_civil_centre\n structures/pers_apadana\n " + "" + "" + - "" + + "" + "" + "" + - "" + + "" + "" + "tokens" + "" + @@ -26,31 +26,29 @@ Builder.prototype.Serialize = null; // we have no dynamic state to save Builder.prototype.GetEntitiesList = function() { - var entities = []; - var string = this.template.Entities._string; - if (string) - { - // Replace the "{civ}" codes with this entity's civ ID - var cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity); - if (cmpIdentity) - string = string.replace(/\{civ\}/g, cmpIdentity.GetCiv()); - entities = string.split(/\s+/); + let string = this.template.Entities._string; - // Remove disabled entities - var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player); - var disabledEntities = cmpPlayer.GetDisabledTemplates(); + if (!string) + return []; - for (var i = entities.length - 1; i >= 0; --i) - if (disabledEntities[entities[i]]) - entities.splice(i, 1); - } - return entities; + let cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity); + if (cmpIdentity) + string = string.replace(/\{civ\}/g, cmpIdentity.GetCiv()); + + let entities = string.split(/\s+/); + + let cmpPlayer = QueryOwnerInterface(this.entity); + if (!cmpPlayer) + return entities; + + let disabledTemplates = cmpPlayer.GetDisabledTemplates(); + return entities.filter(ent => !disabledTemplates[ent]); }; Builder.prototype.GetRange = function() { - var cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction); - var max = 2; + let max = 2; + let cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction); if (cmpObstruction) max += cmpObstruction.GetUnitRadius(); @@ -60,13 +58,11 @@ Builder.prototype.GetRange = function() /** * Build/repair the target entity. This should only be called after a successful range check. * It should be called at a rate of once per second. - * Returns obj with obj.finished==true if this is a repair and it's fully repaired. */ Builder.prototype.PerformBuilding = function(target) { let rate = ApplyValueModificationsToEntity("Builder/Rate", +this.template.Rate, this.entity); - // If it's a foundation, then build it let cmpFoundation = Engine.QueryInterface(target, IID_Foundation); if (cmpFoundation) { @@ -74,7 +70,6 @@ Builder.prototype.PerformBuilding = function(target) return; } - // Otherwise try to repair it let cmpRepairable = Engine.QueryInterface(target, IID_Repairable); if (cmpRepairable) { diff --git a/binaries/data/mods/public/simulation/components/tests/test_Builder.js b/binaries/data/mods/public/simulation/components/tests/test_Builder.js new file mode 100644 index 0000000000..8e0be67d34 --- /dev/null +++ b/binaries/data/mods/public/simulation/components/tests/test_Builder.js @@ -0,0 +1,61 @@ +Engine.LoadHelperScript("ValueModification.js"); +Engine.LoadHelperScript("Player.js"); +Engine.LoadComponentScript("interfaces/Builder.js"); +Engine.LoadComponentScript("Builder.js"); + +const builderId = 6; +const playerId = 1; + +let cmpBuilder = ConstructComponent(builderId, "Builder", { + "Rate": 1.0, + "Entities": { "_string": "structures/{civ}_barracks structures/{civ}_civil_centre" } +}); + +TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/{civ}_barracks", "structures/{civ}_civil_centre"]); + +AddMock(builderId, IID_Identity, { + "GetCiv": () => "iber" +}); + +AddMock(builderId, IID_Player, { + "GetPlayerID": () => playerId +}); + +TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber_barracks", "structures/iber_civil_centre"]); + +AddMock(SYSTEM_ENTITY, IID_PlayerManager, { + "GetPlayerByID": (id) => playerId +}); + +AddMock(1, IID_Player, { + "GetDisabledTemplates": () => ({}), + "GetPlayerID": () => playerId +}); + +AddMock(builderId, IID_Ownership, { + "GetOwner": () => playerId +}); + +TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber_barracks", "structures/iber_civil_centre"]); + +AddMock(1, IID_Player, { + "GetDisabledTemplates": () => ({ "structures/athen_barracks": true }), + "GetPlayerID": () => playerId +}); + +TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber_barracks", "structures/iber_civil_centre"]); + +AddMock(1, IID_Player, { + "GetDisabledTemplates": () => ({ "structures/iber_barracks": true }), + "GetPlayerID": () => playerId +}); + +TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber_civil_centre"]); + +TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetRange(), { "max": 2, "min": 0 }); + +AddMock(builderId, IID_Obstruction, { + "GetUnitRadius": () => 1.0 +}); + +TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetRange(), { "max": 3, "min": 0 });