From 726d59a9955079710180fe5240153c34373b53dd Mon Sep 17 00:00:00 2001 From: Itms Date: Thu, 29 Oct 2020 09:22:48 +0000 Subject: [PATCH] Make PlayerHasMarket event-based. This function is currently polling-based, and called by the GUI on each turn, which results in a big performance hit. Use the opportunity to rename the set of functions with more generic names. Reviewed By: Freagarach, wraitii Differential Revision: https://code.wildfiregames.com/D2919 This was SVN commit r24114. --- .../public/simulation/components/Barter.js | 20 +----------- .../simulation/components/GuiInterface.js | 2 +- .../public/simulation/components/Player.js | 29 ++++++++++++----- .../components/tests/test_Barter.js | 21 ++---------- .../components/tests/test_GuiInterface.js | 5 +-- .../components/tests/test_Player.js | 32 +++++++++++++++++++ 6 files changed, 60 insertions(+), 49 deletions(-) diff --git a/binaries/data/mods/public/simulation/components/Barter.js b/binaries/data/mods/public/simulation/components/Barter.js index 9efec7b6fa..3dd48bfb52 100644 --- a/binaries/data/mods/public/simulation/components/Barter.js +++ b/binaries/data/mods/public/simulation/components/Barter.js @@ -47,20 +47,6 @@ Barter.prototype.GetPrices = function(cmpPlayer) return prices; }; -Barter.prototype.PlayerHasMarket = function(playerID) -{ - var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); - var entities = cmpRangeManager.GetEntitiesByPlayer(playerID); - for (var entity of entities) - { - var cmpFoundation = Engine.QueryInterface(entity, IID_Foundation); - var cmpIdentity = Engine.QueryInterface(entity, IID_Identity); - if (!cmpFoundation && cmpIdentity && cmpIdentity.HasClass("Barter")) - return true; - } - return false; -}; - Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resourceToBuy, amount) { if (amount <= 0) @@ -82,15 +68,11 @@ Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resource return; } - // This can occur when the player issues the order just before the market is destroyed or captured - if (!this.PlayerHasMarket(playerID)) - return; - if (amount != 100 && amount != 500) return; let cmpPlayer = QueryPlayerIDInterface(playerID); - if (!cmpPlayer) + if (!cmpPlayer || !cmpPlayer.CanBarter()) return; let prices = this.GetPrices(cmpPlayer); diff --git a/binaries/data/mods/public/simulation/components/GuiInterface.js b/binaries/data/mods/public/simulation/components/GuiInterface.js index 7da06233db..a96e6e6325 100644 --- a/binaries/data/mods/public/simulation/components/GuiInterface.js +++ b/binaries/data/mods/public/simulation/components/GuiInterface.js @@ -124,7 +124,7 @@ GuiInterface.prototype.GetSimulationState = function() "researchedTechs": cmpTechnologyManager ? cmpTechnologyManager.GetResearchedTechs() : null, "classCounts": cmpTechnologyManager ? cmpTechnologyManager.GetClassCounts() : null, "typeCountsByClass": cmpTechnologyManager ? cmpTechnologyManager.GetTypeCountsByClass() : null, - "canBarter": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).PlayerHasMarket(i), + "canBarter": cmpPlayer.CanBarter(), "barterPrices": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).GetPrices(cmpPlayer) }); } diff --git a/binaries/data/mods/public/simulation/components/Player.js b/binaries/data/mods/public/simulation/components/Player.js index 2cda187a9b..3eed4a99ff 100644 --- a/binaries/data/mods/public/simulation/components/Player.js +++ b/binaries/data/mods/public/simulation/components/Player.js @@ -71,6 +71,7 @@ Player.prototype.Init = function() this.disabledTechnologies = {}; this.startingTechnologies = []; this.spyCostMultiplier = +this.template.SpyCostMultiplier; + this.barterEntities = []; this.barterMultiplier = { "buy": clone(this.template.BarterMultiplier.Buy), "sell": clone(this.template.BarterMultiplier.Sell) @@ -215,6 +216,11 @@ Player.prototype.GetMaxPopulation = function() return Math.round(ApplyValueModificationsToEntity("Player/MaxPopulation", this.maxPop, this.entity)); }; +Player.prototype.CanBarter = function() +{ + return this.barterEntities.length > 0; +}; + Player.prototype.GetBarterMultiplier = function() { return this.barterMultiplier; @@ -748,7 +754,6 @@ Player.prototype.OnGlobalOwnershipChanged = function(msg) if (msg.from != this.playerID && msg.to != this.playerID) return; - let cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity); let cmpCost = Engine.QueryInterface(msg.entity, IID_Cost); if (msg.from == this.playerID) @@ -756,20 +761,28 @@ Player.prototype.OnGlobalOwnershipChanged = function(msg) if (cmpCost) this.popUsed -= cmpCost.GetPopCost(); - if (cmpIdentity && MatchesClassList(cmpIdentity.GetClassesList(), panelEntityClasses)) - { - let index = this.panelEntities.indexOf(msg.entity); - if (index >= 0) - this.panelEntities.splice(index, 1); - } + let panelIndex = this.panelEntities.indexOf(msg.entity); + if (panelIndex >= 0) + this.panelEntities.splice(panelIndex, 1); + + let barterIndex = this.barterEntities.indexOf(msg.entity); + if (barterIndex >= 0) + this.barterEntities.splice(barterIndex, 1); } if (msg.to == this.playerID) { if (cmpCost) this.popUsed += cmpCost.GetPopCost(); - if (cmpIdentity && MatchesClassList(cmpIdentity.GetClassesList(), panelEntityClasses)) + let cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity); + if (!cmpIdentity) + return; + + if (MatchesClassList(cmpIdentity.GetClassesList(), panelEntityClasses)) this.panelEntities.push(msg.entity); + + if (cmpIdentity.HasClass("Barter") && !Engine.QueryInterface(msg.entity, IID_Foundation)) + this.barterEntities.push(msg.entity); } }; diff --git a/binaries/data/mods/public/simulation/components/tests/test_Barter.js b/binaries/data/mods/public/simulation/components/tests/test_Barter.js index 49fe6bb368..c896f11f5f 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_Barter.js +++ b/binaries/data/mods/public/simulation/components/tests/test_Barter.js @@ -61,19 +61,10 @@ let cmpPlayer = AddMock(playerEnt, IID_Player, { bought = amount; return true; }, + "CanBarter": () => true, "GetBarterMultiplier": () => multiplier }); -AddMock(SYSTEM_ENTITY, IID_RangeManager, { - "GetEntitiesByPlayer": (id) => id == playerID ? [62, 60, 61, 63] : [] -}); - -AddMock(60, IID_Identity, { - "HasClass": (cl) => true -}); - -AddMock(60, IID_Foundation, {}); - // GetPrices cmpBarter.priceDifferences = { "wood": 8, "stone": 0, "metal": 0 }; TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).buy, { @@ -100,15 +91,6 @@ TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).sell, { }); multiplier.buy.stone = 1.0; -// PlayerHasMarket -TS_ASSERT(!cmpBarter.PlayerHasMarket(playerID)); - -AddMock(61, IID_Identity, { - "HasClass": (cl) => true -}); - -TS_ASSERT(cmpBarter.PlayerHasMarket(playerID)); - // ExchangeResources // Price differences magnitude are caped by 99 - CONSTANT_DIFFERENCE. // If we have a bigger DIFFERENCE_PER_DEAL than 99 - CONSTANT_DIFFERENCE at each deal we reach the cap. @@ -157,6 +139,7 @@ timerActivated = false; AddMock(playerEnt, IID_Player, { "TrySubtractResources": () => false, "AddResource": () => {}, + "CanBarter": () => true, "GetBarterMultiplier": () => multiplier }); cmpBarter.ExchangeResources(playerID, "wood", "stone", 100); diff --git a/binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js b/binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js index a94dab0dbb..5726b7670e 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js +++ b/binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js @@ -62,8 +62,7 @@ AddMock(SYSTEM_ENTITY, IID_Barter, { "buy": { "food": 150 }, "sell": { "food": 25 } }; - }, - "PlayerHasMarket": function() { return false; } + } }); AddMock(SYSTEM_ENTITY, IID_EndGameManager, { @@ -114,6 +113,7 @@ AddMock(100, IID_Player, { "IsEnemy": function() { return true; }, "GetDisabledTemplates": function() { return {}; }, "GetDisabledTechnologies": function() { return {}; }, + "CanBarter": function() { return false; }, "GetSpyCostMultiplier": function() { return 1; }, "HasSharedDropsites": function() { return false; }, "HasSharedLos": function() { return false; } @@ -199,6 +199,7 @@ AddMock(101, IID_Player, { "IsEnemy": function() { return false; }, "GetDisabledTemplates": function() { return {}; }, "GetDisabledTechnologies": function() { return {}; }, + "CanBarter": function() { return false; }, "GetSpyCostMultiplier": function() { return 1; }, "HasSharedDropsites": function() { return false; }, "HasSharedLos": function() { return false; } diff --git a/binaries/data/mods/public/simulation/components/tests/test_Player.js b/binaries/data/mods/public/simulation/components/tests/test_Player.js index dc6d145c31..90090b11d5 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_Player.js +++ b/binaries/data/mods/public/simulation/components/tests/test_Player.js @@ -18,6 +18,8 @@ Resources = { Engine.LoadHelperScript("ValueModification.js"); Engine.LoadComponentScript("interfaces/Player.js"); +Engine.LoadComponentScript("interfaces/Cost.js"); +Engine.LoadComponentScript("interfaces/Foundation.js"); Engine.LoadComponentScript("interfaces/ModifiersManager.js"); Engine.LoadComponentScript("Player.js"); @@ -37,6 +39,10 @@ var cmpPlayer = ConstructComponent(10, "Player", { }, }); +var playerID = 1; +cmpPlayer.SetPlayerID(playerID); +TS_ASSERT_EQUALS(cmpPlayer.GetPlayerID(), playerID); + TS_ASSERT_EQUALS(cmpPlayer.GetPopulationCount(), 0); TS_ASSERT_EQUALS(cmpPlayer.GetPopulationLimit(), 0); @@ -66,3 +72,29 @@ TS_ASSERT_UNEVAL_EQUALS(cmpPlayer.GetBarterMultiplier(), { "metal": 1.0 } }); + +AddMock(60, IID_Identity, { + "GetClassesList": () => {}, + "HasClass": (cl) => true +}); +AddMock(60, IID_Ownership); +AddMock(60, IID_Foundation, {}); +cmpPlayer.OnGlobalOwnershipChanged({ "entity": 60, "from": INVALID_PLAYER, "to": playerID }); +TS_ASSERT(!cmpPlayer.CanBarter()); + +AddMock(61, IID_Identity, { + "GetClassesList": () => {}, + "HasClass": (cl) => false +}); +cmpPlayer.OnGlobalOwnershipChanged({ "entity": 61, "from": INVALID_PLAYER, "to": playerID }); +TS_ASSERT(!cmpPlayer.CanBarter()); + +AddMock(62, IID_Identity, { + "GetClassesList": () => {}, + "HasClass": (cl) => true +}); +cmpPlayer.OnGlobalOwnershipChanged({ "entity": 62, "from": INVALID_PLAYER, "to": playerID }); +TS_ASSERT(cmpPlayer.CanBarter()); + +cmpPlayer.OnGlobalOwnershipChanged({ "entity": 62, "from": playerID, "to": INVALID_PLAYER }); +TS_ASSERT(!cmpPlayer.CanBarter());