Index: ps/trunk/binaries/data/mods/public/gui/session/menu.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/session/menu.js +++ ps/trunk/binaries/data/mods/public/gui/session/menu.js @@ -29,6 +29,7 @@ const g_IdleTraderTextColor = "orange"; /** + * The barter constants should match with the simulation * Quantity of goods to sell per click. */ const g_BarterResourceSellQuantity = 100; Index: ps/trunk/binaries/data/mods/public/simulation/components/Barter.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/Barter.js +++ ps/trunk/binaries/data/mods/public/simulation/components/Barter.js @@ -87,6 +87,8 @@ warn("ExchangeResources: player has no markets"); return; } + if (amount != 100 && amount != 500) + return; var cmpPlayer = Engine.QueryInterface(playerEntity, IID_Player); var prices = this.GetPrices(playerEntity); @@ -96,7 +98,6 @@ { var amountToAdd = Math.round(prices["sell"][resourceToSell] / prices["buy"][resourceToBuy] * amount); cmpPlayer.AddResource(resourceToBuy, amountToAdd); - var numberOfDeals = Math.round(amount / 100); // Display chat message to observers var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); @@ -117,11 +118,12 @@ cmpStatisticsTracker.IncreaseResourcesBoughtCounter(resourceToBuy, amountToAdd); } + let difference = this.DIFFERENCE_PER_DEAL * amount / 100; // Increase price difference for both exchange resources. // Overall price difference (dynamic +/- constant) can't exceed +-99%. - this.priceDifferences[resourceToSell] -= this.DIFFERENCE_PER_DEAL * numberOfDeals; + this.priceDifferences[resourceToSell] -= difference; this.priceDifferences[resourceToSell] = Math.min(99 - this.CONSTANT_DIFFERENCE, Math.max(this.CONSTANT_DIFFERENCE - 99, this.priceDifferences[resourceToSell])); - this.priceDifferences[resourceToBuy] += this.DIFFERENCE_PER_DEAL * numberOfDeals; + this.priceDifferences[resourceToBuy] += difference; this.priceDifferences[resourceToBuy] = Math.min(99 - this.CONSTANT_DIFFERENCE, Math.max(this.CONSTANT_DIFFERENCE - 99, this.priceDifferences[resourceToBuy])); } Index: ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Barter.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Barter.js +++ ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Barter.js @@ -120,14 +120,24 @@ TS_ASSERT_EQUALS(sold, 100); TS_ASSERT_EQUALS(bought, Math.round(100 * (100 - cmpBarter.CONSTANT_DIFFERENCE + 0) / (100 + cmpBarter.CONSTANT_DIFFERENCE + 0))); -// With an amount which is not a multiple of 100, price differences are not updated. That sounds wrong. +// Amount which is not 100 or 500 is invalid. cmpBarter.priceDifferences = { "wood": 0, "stone": 0, "metal": 0 }; cmpBarter.ExchangeResources(11, "wood", "stone", 40); -TS_ASSERT_EQUALS(cmpBarter.restoreTimer, 7); -TS_ASSERT(timerActivated); +bought = 0; +sold = 0; +timerActivated = false; +TS_ASSERT(!timerActivated); +TS_ASSERT_UNEVAL_EQUALS(cmpBarter.priceDifferences, { "wood": 0, "stone": 0, "metal": 0 }); +TS_ASSERT_EQUALS(sold, 0); +TS_ASSERT_EQUALS(bought, 0); + +// Amount which is NaN is invalid. +cmpBarter.priceDifferences = { "wood": 0, "stone": 0, "metal": 0 }; +cmpBarter.ExchangeResources(11, "wood", "stone", NaN); +TS_ASSERT(!timerActivated); TS_ASSERT_UNEVAL_EQUALS(cmpBarter.priceDifferences, { "wood": 0, "stone": 0, "metal": 0 }); -TS_ASSERT_EQUALS(sold, 40); -TS_ASSERT_EQUALS(bought, Math.round(40 * (100 - cmpBarter.CONSTANT_DIFFERENCE + 0) / (100 + cmpBarter.CONSTANT_DIFFERENCE + 0))); +TS_ASSERT_EQUALS(sold, 0); +TS_ASSERT_EQUALS(bought, 0); cmpBarter.priceDifferences = { "wood": 0, "stone": 99 - cmpBarter.CONSTANT_DIFFERENCE, "metal": 0 }; cmpBarter.ExchangeResources(11, "wood", "stone", 100);