Index: binaries/data/mods/public/simulation/components/tests/test_UpgradeModification.js =================================================================== --- /dev/null +++ binaries/data/mods/public/simulation/components/tests/test_UpgradeModification.js @@ -0,0 +1,169 @@ +Engine.LoadHelperScript("Player.js"); +Engine.LoadHelperScript("ValueModification.js"); +Resources = { + "GetCodes": () => ["food", "metal", "stone", "wood"], + "GetResource": () => ({}), + "BuildSchema": (type) => { + let schema = ""; + for (let res of Resources.GetCodes()) + schema += + "" + + "" + + "" + + "" + + ""; + return "" + schema + ""; + } +}; + +Engine.LoadComponentScript("interfaces/AuraManager.js"); +Engine.LoadComponentScript("interfaces/TechnologyManager.js"); +Engine.LoadComponentScript("interfaces/Timer.js"); +Engine.LoadComponentScript("interfaces/Upgrade.js"); + +Engine.LoadComponentScript("Upgrade.js"); + +// Input (bare minimum needed for tests) +let techs = { + "alter_tower_upgrade_cost": { + "modifications": [ + { "value": "Upgrade/Cost/stone", "add": 60.0 }, + { "value": "Upgrade/Cost/wood", "multiply": 0.5 }, + { "value": "Upgrade/Time", "replace": 90 } + ], + "affects": ["Tower"] + } +}; +let template = { + "Identity": { + "Classes": { '@datatype': "tokens", "_string": "Tower" }, + "VisibleClasses": { '@datatype': "tokens", "_string": "" } + }, + "Upgrade": { + "Tower": { + "Cost": { "stone": "100", "wood": "50" }, + "Entity": "structures/{civ}_defense_tower", + "RequiredTechnology": "phase_town", + "Time": "100", + "Tooltip": "Reinforce with stone and upgrade to a defense tower." + } + } +}; +let civCode = "pony"; + +// Usually, the tech modifications would be worked out by the TechnologyManager +// with assistance from globalscripts. This test is not about testing the +// TechnologyManager, so the modifications (both with and without the technology +// researched) are worked out before hand and placed here. +let isResearched = false; +let templateTechModifications = { + 'Upgrade/Cost/stone': [{ "affects": [["Tower"]], "add": 60 }], + 'Upgrade/Cost/wood': [{ "affects": [["Tower"]], "multiply": 0.5 }], + 'Upgrade/Time': [{ "affects": [["Tower"]], "replace": 90 }] +}; +let entityTechModifications = { + "without": { + 'Upgrade/Cost/stone': { 20: { "origValue": 100, "newValue": 100 } }, + 'Upgrade/Cost/wood': { 20: { "origValue": 50, "newValue": 50 } }, + 'Upgrade/Time': { 20: { "origValue": 100, "newValue": 100 } } + }, + "with": { + 'Upgrade/Cost/stone': { 20: { "origValue": 100, "newValue": 160 } }, + 'Upgrade/Cost/wood': { 20: { "origValue": 50, "newValue": 25 } }, + 'Upgrade/Time': { 20: { "origValue": 100, "newValue": 90 } } + } +}; + +/** + * Initialise various bits + */ +// System Entities +AddMock(SYSTEM_ENTITY, IID_AuraManager, null); +AddMock(SYSTEM_ENTITY, IID_DataTemplateManager, { + "GetAllTechs": () => techs, + "GetTechnologyTemplate": (template) => techs[template] || {} +}); +AddMock(SYSTEM_ENTITY, IID_PlayerManager, { + "GetPlayerByID": (pID) => 10 +}); +AddMock(SYSTEM_ENTITY, IID_RangeManager, { + "GetEntitiesByPlayer": (pID) => [] +}); +AddMock(SYSTEM_ENTITY, IID_TemplateManager, { + "GetCurrentTemplateName": (ent) => "{civ}_sentry_tower", + "GetTemplate": () => template +}); +AddMock(SYSTEM_ENTITY, IID_Timer, { + "SetInterval": () => 1, + "CancelTimer": () => {} +}); + +// Init Player +AddMock(10, IID_Player, { + "AddResources": () => {}, + "GetPlayerID": () => 1, + "GetCheatTimeMultiplier": () => 1.0, + "TrySubtractResources": () => true +}); +AddMock(10, IID_TechnologyManager, { + "ResearchTechnology": (tech) => { isResearched = true; }, + "ApplyModificationsTemplate": (valueName, curValue, template) => { + let mods = isResearched ? templateTechModifications : {}; + return GetTechModifiedProperty(mods, GetIdentityClasses(template.Identity), valueName, curValue); + }, + "ApplyModifications": (valueName, curValue, ent) => { + let mods = isResearched ? entityTechModifications.with : entityTechModifications.without; + return mods[valueName][ent].newValue; + } +}); +let cmpPlayer = Engine.QueryInterface(10, IID_Player); +let cmpTechnologyManager = Engine.QueryInterface(10, IID_TechnologyManager); + +// Create an entity with an Upgrade Component +AddMock(20, IID_Ownership, { + "GetOwner": () => cmpPlayer.GetPlayerID() +}); +AddMock(20, IID_Identity, { + "GetClassesList": () => GetIdentityClasses(template.Identity), + "GetCiv": () => civCode +}); +let cmpUpgrade = ConstructComponent(20, "Upgrade", template.Upgrade); +cmpUpgrade.owner = cmpPlayer.GetPlayerID(); + + +/** + * Now to start the test proper + * To start with, no techs are researched... + */ +// T1: Check the cost of the upgrade without a player value being passed (as it would be in the structree) +let parsed_template = GetTemplateDataHelper(template, null, {}, Resources); +TS_ASSERT_UNEVAL_EQUALS(parsed_template.upgrades[0].cost, {"stone": 100, "wood": 50, "time": 100}); + +// T2: Check the value, with a player ID (as it would be in-session) +parsed_template = GetTemplateDataHelper(template, cmpPlayer.GetPlayerID(), {}, Resources); +TS_ASSERT_UNEVAL_EQUALS(parsed_template.upgrades[0].cost, {"stone": 100, "wood": 50, "time": 100}); + +// T3: Check that the value is correct within the Update Component +TS_ASSERT_UNEVAL_EQUALS(cmpUpgrade.GetUpgrades()[0].cost, {"stone": 100, "wood": 50, "time": 100}); + +/** + * Tell the Upgrade component to start the Upgrade, + * then research the technology that will alter the value of the upgrade cost. + */ +cmpUpgrade.Upgrade("structures/"+civCode+"_defense_tower"); +cmpTechnologyManager.ResearchTechnology("alter_tower_upgrade_cost"); + +// T4: Check that the player-less value hasn't increased... +parsed_template = GetTemplateDataHelper(template, null, {}, Resources); +TS_ASSERT_UNEVAL_EQUALS(parsed_template.upgrades[0].cost, {"stone": 100, "wood": 50, "time": 100}); + +// T5: ...but the player-backed value has. +parsed_template = GetTemplateDataHelper(template, cmpPlayer.GetPlayerID(), {}, Resources); +TS_ASSERT_UNEVAL_EQUALS(parsed_template.upgrades[0].cost, {"stone": 160, "wood": 25, "time": 90}); + +// T6: The upgrade component should still be using the old resource cost (but new time cost) for the upgrade in progress... +TS_ASSERT_UNEVAL_EQUALS(cmpUpgrade.GetUpgrades()[0].cost, {"stone": 100, "wood": 50, "time": 90}); + +// T7: ...but with the upgrade cancelled, it now uses the modified value. +cmpUpgrade.CancelUpgrade(cmpPlayer.GetPlayerID()); +TS_ASSERT_UNEVAL_EQUALS(cmpUpgrade.GetUpgrades()[0].cost, {"stone": 160, "wood": 25, "time": 90});