diff --git a/binaries/data/mods/public/simulation/components/Barter.js b/binaries/data/mods/public/simulation/components/Barter.js index 6408f3dccc..9efec7b6fa 100644 --- a/binaries/data/mods/public/simulation/components/Barter.js +++ b/binaries/data/mods/public/simulation/components/Barter.js @@ -34,10 +34,10 @@ Barter.prototype.Init = function() this.restoreTimer = undefined; }; -Barter.prototype.GetPrices = function(playerID) +Barter.prototype.GetPrices = function(cmpPlayer) { let prices = { "buy": {}, "sell": {} }; - let multiplier = QueryPlayerIDInterface(playerID).GetBarterMultiplier(); + let multiplier = cmpPlayer.GetBarterMultiplier(); for (let resource of Resources.GetBarterableCodes()) { let truePrice = Resources.GetResource(resource).truePrice; @@ -89,17 +89,20 @@ Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resource if (amount != 100 && amount != 500) return; - var cmpPlayer = QueryPlayerIDInterface(playerID); - var prices = this.GetPrices(playerID); - var amountsToSubtract = {}; + let cmpPlayer = QueryPlayerIDInterface(playerID); + if (!cmpPlayer) + return; + + let prices = this.GetPrices(cmpPlayer); + let amountsToSubtract = {}; amountsToSubtract[resourceToSell] = amount; if (cmpPlayer.TrySubtractResources(amountsToSubtract)) { - var amountToAdd = Math.round(prices["sell"][resourceToSell] / prices["buy"][resourceToBuy] * amount); + let amountToAdd = Math.round(prices.sell[resourceToSell] / prices.buy[resourceToBuy] * amount); cmpPlayer.AddResource(resourceToBuy, amountToAdd); // Display chat message to observers. - var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); + let cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); if (cmpGUIInterface) cmpGUIInterface.PushNotification({ "type": "barter", @@ -110,7 +113,7 @@ Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resource "resourceGained": resourceToBuy }); - var cmpStatisticsTracker = QueryPlayerIDInterface(playerID, IID_StatisticsTracker); + let cmpStatisticsTracker = QueryPlayerIDInterface(playerID, IID_StatisticsTracker); if (cmpStatisticsTracker) { cmpStatisticsTracker.IncreaseResourcesSoldCounter(resourceToSell, amount); @@ -127,10 +130,7 @@ Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resource } if (this.restoreTimer === undefined) - { - var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); - this.restoreTimer = cmpTimer.SetInterval(this.entity, IID_Barter, "ProgressTimeout", this.RESTORE_TIMER_INTERVAL, this.RESTORE_TIMER_INTERVAL, {}); - } + this.restoreTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer).SetInterval(this.entity, IID_Barter, "ProgressTimeout", this.RESTORE_TIMER_INTERVAL, this.RESTORE_TIMER_INTERVAL, {}); }; Barter.prototype.ProgressTimeout = function(data) diff --git a/binaries/data/mods/public/simulation/components/GuiInterface.js b/binaries/data/mods/public/simulation/components/GuiInterface.js index 5efd7e2995..febda4ef06 100644 --- a/binaries/data/mods/public/simulation/components/GuiInterface.js +++ b/binaries/data/mods/public/simulation/components/GuiInterface.js @@ -125,7 +125,7 @@ GuiInterface.prototype.GetSimulationState = function() "classCounts": cmpTechnologyManager ? cmpTechnologyManager.GetClassCounts() : null, "typeCountsByClass": cmpTechnologyManager ? cmpTechnologyManager.GetTypeCountsByClass() : null, "canBarter": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).PlayerHasMarket(i), - "barterPrices": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).GetPrices(i) + "barterPrices": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).GetPrices(cmpPlayer) }); } diff --git a/binaries/data/mods/public/simulation/components/ProductionQueue.js b/binaries/data/mods/public/simulation/components/ProductionQueue.js index 872e8ab2b7..8661d52ff4 100644 --- a/binaries/data/mods/public/simulation/components/ProductionQueue.js +++ b/binaries/data/mods/public/simulation/components/ProductionQueue.js @@ -514,13 +514,11 @@ ProductionQueue.prototype.RemoveBatch = function(id) for (let i = 0; i < this.queue.length; ++i) { + // Find the item to remove. let item = this.queue[i]; if (item.id != id) continue; - // Now we've found the item to remove. - let cmpPlayer = QueryPlayerIDInterface(item.player); - // Update entity count in the EntityLimits component. if (item.unitTemplate) { @@ -529,7 +527,8 @@ ProductionQueue.prototype.RemoveBatch = function(id) if (template.TrainingRestrictions) { let cmpPlayerEntityLimits = QueryPlayerIDInterface(item.player, IID_EntityLimits); - cmpPlayerEntityLimits.ChangeCount(template.TrainingRestrictions.Category, -item.count); + if (cmpPlayerEntityLimits) + cmpPlayerEntityLimits.ChangeCount(template.TrainingRestrictions.Category, -item.count); } } @@ -543,18 +542,23 @@ ProductionQueue.prototype.RemoveBatch = function(id) cmpStatisticsTracker.IncreaseResourceUsedCounter(r, -totalCosts[r]); } - cmpPlayer.AddResources(totalCosts); + let cmpPlayer = QueryPlayerIDInterface(item.player); + if (cmpPlayer) + { + cmpPlayer.AddResources(totalCosts); - // Remove reserved population slots if necessary. - if (item.productionStarted && item.unitTemplate) - cmpPlayer.UnReservePopulationSlots(item.population * item.count); + // Remove reserved population slots if necessary. + if (item.productionStarted && item.unitTemplate) + cmpPlayer.UnReservePopulationSlots(item.population * item.count); + } // Mark the research as stopped if we cancel it. if (item.technologyTemplate) { // item.player is used as this.entity's owner may be invalid (deletion, etc.) let cmpTechnologyManager = QueryPlayerIDInterface(item.player, IID_TechnologyManager); - cmpTechnologyManager.StoppedResearch(item.technologyTemplate, true); + if (cmpTechnologyManager) + cmpTechnologyManager.StoppedResearch(item.technologyTemplate, true); this.SetAnimation("idle"); } diff --git a/binaries/data/mods/public/simulation/components/Upgrade.js b/binaries/data/mods/public/simulation/components/Upgrade.js index 6b722dbd8e..cdfbef595e 100644 --- a/binaries/data/mods/public/simulation/components/Upgrade.js +++ b/binaries/data/mods/public/simulation/components/Upgrade.js @@ -115,7 +115,8 @@ Upgrade.prototype.ChangeUpgradedEntityCount = function(amount) return; let cmpEntityLimits = QueryPlayerIDInterface(this.owner, IID_EntityLimits); - cmpEntityLimits.ChangeCount(categoryTo, amount); + if (cmpEntityLimits) + cmpEntityLimits.ChangeCount(categoryTo, amount); }; Upgrade.prototype.CanUpgradeTo = function(template) 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 f5db819754..49fe6bb368 100644 --- a/binaries/data/mods/public/simulation/components/tests/test_Barter.js +++ b/binaries/data/mods/public/simulation/components/tests/test_Barter.js @@ -52,7 +52,7 @@ AddMock(SYSTEM_ENTITY, IID_Timer, { TS_ASSERT_EQUALS(cmpBarter.restoreTimer, undefined); TS_ASSERT_UNEVAL_EQUALS(cmpBarter.priceDifferences, { "wood": 0, "stone": 0, "metal": 0 }); -AddMock(playerEnt, IID_Player, { +let cmpPlayer = AddMock(playerEnt, IID_Player, { "TrySubtractResources": amounts => { sold = amounts[Object.keys(amounts)[0]]; return true; @@ -76,24 +76,24 @@ AddMock(60, IID_Foundation, {}); // GetPrices cmpBarter.priceDifferences = { "wood": 8, "stone": 0, "metal": 0 }; -TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(playerID).buy, { +TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).buy, { "wood": truePrice * (100 + 8 + cmpBarter.CONSTANT_DIFFERENCE) / 100, "stone": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) / 100, "metal": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) / 100 }); -TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(playerID).sell, { +TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).sell, { "wood": truePrice * (100 + 8 - cmpBarter.CONSTANT_DIFFERENCE) / 100, "stone": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100, "metal": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100 }); multiplier.buy.stone = 2.0; -TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(playerID).buy, { +TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).buy, { "wood": truePrice * (100 + 8 + cmpBarter.CONSTANT_DIFFERENCE) / 100, "stone": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) * 2.0 / 100, "metal": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) / 100 }); -TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(playerID).sell, { +TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).sell, { "wood": truePrice * (100 + 8 - cmpBarter.CONSTANT_DIFFERENCE) / 100, "stone": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100, "metal": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100