Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Barter.js
Show All 23 Lines | |||||
/** | /** | ||||
* Interval of timer which slowly restore prices after deals | * Interval of timer which slowly restore prices after deals | ||||
*/ | */ | ||||
Barter.prototype.RESTORE_TIMER_INTERVAL = 5000; | Barter.prototype.RESTORE_TIMER_INTERVAL = 5000; | ||||
Barter.prototype.Init = function() | Barter.prototype.Init = function() | ||||
{ | { | ||||
this.priceDifferences = {}; | this.priceDifferences = {}; | ||||
for (let resource of Resources.GetCodes()) | for (let resource of Resources.GetCodes("barterable")) | ||||
this.priceDifferences[resource] = 0; | this.priceDifferences[resource] = 0; | ||||
this.restoreTimer = undefined; | this.restoreTimer = undefined; | ||||
}; | }; | ||||
Barter.prototype.GetPrices = function(playerID) | Barter.prototype.GetPrices = function(playerID) | ||||
{ | { | ||||
var prices = { "buy": {}, "sell": {} }; | var prices = { "buy": {}, "sell": {} }; | ||||
Silier: could you var -> let ? | |||||
let multiplier = QueryPlayerIDInterface(playerID).GetBarterMultiplier(); | let multiplier = QueryPlayerIDInterface(playerID).GetBarterMultiplier(); | ||||
for (let resource of Resources.GetCodes()) | for (let resource of Resources.GetCodes("barterable")) | ||||
{ | { | ||||
let truePrice = Resources.GetResource(resource).truePrice; | let truePrice = Resources.GetResource(resource).truePrice; | ||||
prices.buy[resource] = truePrice * (100 + this.CONSTANT_DIFFERENCE + this.priceDifferences[resource]) * multiplier.buy[resource] / 100; | prices.buy[resource] = truePrice * (100 + this.CONSTANT_DIFFERENCE + this.priceDifferences[resource]) * multiplier.buy[resource] / 100; | ||||
prices.sell[resource] = truePrice * (100 - this.CONSTANT_DIFFERENCE + this.priceDifferences[resource]) * multiplier.sell[resource] / 100; | prices.sell[resource] = truePrice * (100 - this.CONSTANT_DIFFERENCE + this.priceDifferences[resource]) * multiplier.sell[resource] / 100; | ||||
} | } | ||||
return prices; | return prices; | ||||
}; | }; | ||||
Show All 14 Lines | |||||
Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resourceToBuy, amount) | Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resourceToBuy, amount) | ||||
{ | { | ||||
if (amount <= 0) | if (amount <= 0) | ||||
{ | { | ||||
warn("ExchangeResources: incorrect amount: " + uneval(amount)); | warn("ExchangeResources: incorrect amount: " + uneval(amount)); | ||||
return; | return; | ||||
} | } | ||||
let availResources = Resources.GetCodes(); | let availResources = Resources.GetCodes("barterable"); | ||||
if (availResources.indexOf(resourceToSell) == -1) | if (availResources.indexOf(resourceToSell) == -1) | ||||
{ | { | ||||
warn("ExchangeResources: incorrect resource to sell: " + uneval(resourceToSell)); | warn("ExchangeResources: incorrect resource to sell: " + uneval(resourceToSell)); | ||||
return; | return; | ||||
} | } | ||||
if (availResources.indexOf(resourceToBuy) == -1) | if (availResources.indexOf(resourceToBuy) == -1) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | if (this.restoreTimer === undefined) | ||||
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | 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 = cmpTimer.SetInterval(this.entity, IID_Barter, "ProgressTimeout", this.RESTORE_TIMER_INTERVAL, this.RESTORE_TIMER_INTERVAL, {}); | ||||
} | } | ||||
}; | }; | ||||
Barter.prototype.ProgressTimeout = function(data) | Barter.prototype.ProgressTimeout = function(data) | ||||
{ | { | ||||
var needRestore = false; | var needRestore = false; | ||||
for (let resource of Resources.GetCodes()) | for (let resource of Resources.GetCodes("barterable")) | ||||
{ | { | ||||
// Calculate value to restore, it should be limited to [-DIFFERENCE_RESTORE; DIFFERENCE_RESTORE] interval | // Calculate value to restore, it should be limited to [-DIFFERENCE_RESTORE; DIFFERENCE_RESTORE] interval | ||||
var differenceRestore = Math.min(this.DIFFERENCE_RESTORE, Math.max(-this.DIFFERENCE_RESTORE, this.priceDifferences[resource])); | var differenceRestore = Math.min(this.DIFFERENCE_RESTORE, Math.max(-this.DIFFERENCE_RESTORE, this.priceDifferences[resource])); | ||||
differenceRestore = -differenceRestore; | differenceRestore = -differenceRestore; | ||||
this.priceDifferences[resource] += differenceRestore; | this.priceDifferences[resource] += differenceRestore; | ||||
// If price difference still exists then set flag to run timer again | // If price difference still exists then set flag to run timer again | ||||
if (this.priceDifferences[resource] != 0) | if (this.priceDifferences[resource] != 0) | ||||
needRestore = true; | needRestore = true; | ||||
Show All 11 Lines |
Wildfire Games · Phabricator
could you var -> let ?