Index: binaries/data/mods/public/simulation/components/Barter.js =================================================================== --- binaries/data/mods/public/simulation/components/Barter.js +++ binaries/data/mods/public/simulation/components/Barter.js @@ -47,20 +47,6 @@ 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,14 +68,15 @@ return; } - // This can occur when the player issues the order just before the market is destroyed or captured - if (!this.PlayerHasMarket(playerID)) + if (amount != 100 && amount != 500) return; - if (amount != 100 && amount != 500) + let cmpPlayer = QueryPlayerIDInterface(playerID); + + // This can occur when the player issues the order just before the market is destroyed or captured + if (!cmpPlayer.HasMarket()) return; - var cmpPlayer = QueryPlayerIDInterface(playerID); var prices = this.GetPrices(playerID); var amountsToSubtract = {}; amountsToSubtract[resourceToSell] = amount; Index: binaries/data/mods/public/simulation/components/GuiInterface.js =================================================================== --- binaries/data/mods/public/simulation/components/GuiInterface.js +++ binaries/data/mods/public/simulation/components/GuiInterface.js @@ -124,7 +124,7 @@ "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.HasMarket(), "barterPrices": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).GetPrices(i) }); } Index: binaries/data/mods/public/simulation/components/Player.js =================================================================== --- binaries/data/mods/public/simulation/components/Player.js +++ binaries/data/mods/public/simulation/components/Player.js @@ -71,6 +71,7 @@ this.disabledTechnologies = {}; this.startingTechnologies = []; this.spyCostMultiplier = +this.template.SpyCostMultiplier; + this.barterEntities = []; // keep track of markets for cmpPlayer.HasMarket() this.barterMultiplier = { "buy": clone(this.template.BarterMultiplier.Buy), "sell": clone(this.template.BarterMultiplier.Sell) @@ -217,6 +218,11 @@ return Math.round(ApplyValueModificationsToEntity("Player/MaxPopulation", this.maxPop, this.entity)); }; +Player.prototype.HasMarket = function() +{ + return this.barterEntities.length > 0; +}; + Player.prototype.GetBarterMultiplier = function() { return this.barterMultiplier; @@ -748,8 +754,8 @@ if (msg.from != this.playerID && msg.to != this.playerID) return; - var cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity); - var cmpCost = Engine.QueryInterface(msg.entity, IID_Cost); + let cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity); + let cmpCost = Engine.QueryInterface(msg.entity, IID_Cost); if (msg.from == this.playerID) { @@ -762,6 +768,9 @@ if (index >= 0) this.panelEntities.splice(index, 1); } + + this.barterEntities = this.barterEntities.filter( + ent => ent != msg.entity) } if (msg.to == this.playerID) { @@ -770,6 +779,13 @@ if (cmpIdentity && MatchesClassList(cmpIdentity.GetClassesList(), panelEntityClasses)) this.panelEntities.push(msg.entity); + + if (cmpIdentity && cmpIdentity.HasClass("Barter")) + { + let cmpFoundation = Engine.QueryInterface(msg.entity, IID_Foundation); + if (!cmpFoundation) + this.barterEntities.push(msg.entity); + } } }; Index: binaries/data/mods/public/simulation/components/tests/test_Barter.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_Barter.js +++ binaries/data/mods/public/simulation/components/tests/test_Barter.js @@ -61,19 +61,10 @@ bought = amount; return true; }, + "HasMarket": () => 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(playerID).buy, { @@ -100,15 +91,6 @@ }); 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 @@ AddMock(playerEnt, IID_Player, { "TrySubtractResources": () => false, "AddResource": () => {}, + "HasMarket": () => true, "GetBarterMultiplier": () => multiplier }); cmpBarter.ExchangeResources(playerID, "wood", "stone", 100); Index: binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js +++ binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js @@ -63,8 +63,7 @@ "buy": { "food": 150 }, "sell": { "food": 25 } }; - }, - "PlayerHasMarket": function() { return false; } + } }); AddMock(SYSTEM_ENTITY, IID_EndGameManager, { @@ -115,6 +114,7 @@ "IsEnemy": function() { return true; }, "GetDisabledTemplates": function() { return {}; }, "GetDisabledTechnologies": function() { return {}; }, + "HasMarket": function() { return false; }, "GetSpyCostMultiplier": function() { return 1; }, "HasSharedDropsites": function() { return false; }, "HasSharedLos": function() { return false; } @@ -200,6 +200,7 @@ "IsEnemy": function() { return false; }, "GetDisabledTemplates": function() { return {}; }, "GetDisabledTechnologies": function() { return {}; }, + "HasMarket": function() { return false; }, "GetSpyCostMultiplier": function() { return 1; }, "HasSharedDropsites": function() { return false; }, "HasSharedLos": function() { return false; } Index: binaries/data/mods/public/simulation/components/tests/test_Player.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_Player.js +++ binaries/data/mods/public/simulation/components/tests/test_Player.js @@ -18,6 +18,8 @@ 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,8 @@ }, }); +cmpPlayer.SetPlayerID(1); + TS_ASSERT_EQUALS(cmpPlayer.GetPopulationCount(), 0); TS_ASSERT_EQUALS(cmpPlayer.GetPopulationLimit(), 0); @@ -66,3 +70,26 @@ "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": 1}); +TS_ASSERT(!cmpPlayer.HasMarket()); + +AddMock(61, IID_Identity, { + "GetClassesList": () => {}, + "HasClass": (cl) => false +}); +cmpPlayer.OnGlobalOwnershipChanged({ "entity": 61, "from": INVALID_PLAYER, "to": 1}); +TS_ASSERT(!cmpPlayer.HasMarket()); + +AddMock(62, IID_Identity, { + "GetClassesList": () => {}, + "HasClass": (cl) => true +}); +cmpPlayer.OnGlobalOwnershipChanged({ "entity": 62, "from": INVALID_PLAYER, "to": 1}); +TS_ASSERT(cmpPlayer.HasMarket());