1
0
forked from 0ad/0ad

Limit the bartered amount in the simulation. Advices from mimo. Reviewed by elexis.

Refs #4655.
Differential Revision: https://code.wildfiregames.com/D381
This was SVN commit r19831.
This commit is contained in:
fatherbushido 2017-06-26 11:56:31 +00:00
parent 1fe9532680
commit 8ba425d89f
3 changed files with 21 additions and 8 deletions

View File

@ -29,6 +29,7 @@ const STEP = 5;
const g_IdleTraderTextColor = "orange";
/**
* The barter constants should match with the simulation
* Quantity of goods to sell per click.
*/
const g_BarterResourceSellQuantity = 100;

View File

@ -87,6 +87,8 @@ Barter.prototype.ExchangeResources = function(playerEntity, resourceToSell, reso
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 @@ Barter.prototype.ExchangeResources = function(playerEntity, resourceToSell, reso
{
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 @@ Barter.prototype.ExchangeResources = function(playerEntity, resourceToSell, reso
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]));
}

View File

@ -120,14 +120,24 @@ TS_ASSERT_UNEVAL_EQUALS(cmpBarter.priceDifferences, { "wood": -cmpBarter.DIFFERE
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, 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);
// 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, 0);
TS_ASSERT_EQUALS(bought, 0);
cmpBarter.priceDifferences = { "wood": 0, "stone": 99 - cmpBarter.CONSTANT_DIFFERENCE, "metal": 0 };
cmpBarter.ExchangeResources(11, "wood", "stone", 100);