Changeset View
Standalone View
binaries/data/mods/public/simulation/components/Player.js
Show First 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | Player.prototype.Init = function() | ||||
this.disabledTechnologies = {}; | this.disabledTechnologies = {}; | ||||
this.startingTechnologies = []; | this.startingTechnologies = []; | ||||
this.spyCostMultiplier = +this.template.SpyCostMultiplier; | this.spyCostMultiplier = +this.template.SpyCostMultiplier; | ||||
this.barterMultiplier = { | this.barterMultiplier = { | ||||
"buy": clone(this.template.BarterMultiplier.Buy), | "buy": clone(this.template.BarterMultiplier.Buy), | ||||
"sell": clone(this.template.BarterMultiplier.Sell) | "sell": clone(this.template.BarterMultiplier.Sell) | ||||
}; | }; | ||||
// Initial resources and trading goods probability in steps of 5 | // Initial resources | ||||
let resCodes = Resources.GetCodes(); | let resCodes = Resources.GetCodes(); | ||||
let quotient = Math.floor(20 / resCodes.length); | for (let res of resCodes) | ||||
Stan: Here it could be:
```lang=js
for (let res of resCodes)
{
this.resourceCount[res] = 300… | |||||
let remainder = 20 % resCodes.length; | |||||
for (let i in resCodes) | |||||
{ | { | ||||
let res = resCodes[i]; | |||||
this.resourceCount[res] = 300; | this.resourceCount[res] = 300; | ||||
this.resourceNames[res] = Resources.GetResource(res).name; | this.resourceNames[res] = Resources.GetResource(res).name; | ||||
} | |||||
// Trading goods probability in steps of 5 | |||||
let resTradeCodes = Resources.GetCodes("tradable"); | |||||
let quotient = Math.floor(20 / resTradeCodes.length); | |||||
let remainder = 20 % resTradeCodes.length; | |||||
for (let i in resTradeCodes) | |||||
Done Inline Actionsfor (int i = 0; i < resTradeCodes.length; ++i) maybe Stan: for (int i = 0; i < resTradeCodes.length; ++i) maybe | |||||
Done Inline ActionsWhich is better because...? Freagarach: Which is better because...? | |||||
Done Inline ActionsCause you don't need the cast ("+i"). Other that no reason just mentionning One could also write (that's slower according to benchmarks) resTradeCodes.forEach((resource, i) => { this.tradingGoods.push({ "goods": resource, "proba": 5 * (quotient + (i < remainder ? 1 : 0)) }); }); Stan: Cause you don't need the cast ("+i"). Other that no reason just mentionning
One could also… | |||||
Done Inline ActionsOkay, thanks for the information :D Freagarach: Okay, thanks for the information :D | |||||
{ | |||||
let res = resTradeCodes[i]; | |||||
Done Inline Actionsinline this, you do not use it later, or try to use directly for (let res of ..) as loop above Silier: inline this, you do not use it later, or try to use directly for (let res of ..) as loop above | |||||
this.tradingGoods.push({ | this.tradingGoods.push({ | ||||
"goods": res, | "goods": res, | ||||
"proba": 5 * (quotient + (+i < remainder ? 1 : 0)) | "proba": 5 * (quotient + (+i < remainder ? 1 : 0)) | ||||
Done Inline ActionsIs i a string ? Stan: Is i a string ? | |||||
Done Inline ActionsNay. You mean that the "+" can be removed? Freagarach: Nay. You mean that the "+" can be removed? | |||||
Done Inline ActionsYup. Stan: Yup. | |||||
Done Inline ActionsI just doublechecked, but i *is* a string. Freagarach: I just doublechecked, but `i` *is* a string. | |||||
Done Inline ActionsIndeed, which is a weirdness of Javascript. You need a C-style loop for i to be an integer. wraitii: Indeed, which is a weirdness of Javascript. You need a C-style loop for i to be an integer. | |||||
}); | }); | ||||
} | } | ||||
}; | }; | ||||
Player.prototype.SetPlayerID = function(id) | Player.prototype.SetPlayerID = function(id) | ||||
{ | { | ||||
this.playerID = id; | this.playerID = id; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 306 Lines • ▼ Show 20 Lines | Player.prototype.GetNextTradingGoods = function() | ||||
return this.tradingGoods[last].goods; | return this.tradingGoods[last].goods; | ||||
}; | }; | ||||
Player.prototype.GetTradingGoods = function() | Player.prototype.GetTradingGoods = function() | ||||
{ | { | ||||
var tradingGoods = {}; | var tradingGoods = {}; | ||||
for (let resource of this.tradingGoods) | for (let resource of this.tradingGoods) | ||||
tradingGoods[resource.goods] = resource.proba; | tradingGoods[resource.goods] = resource.proba; | ||||
Done Inline Actions!x elexis: !x | |||||
return tradingGoods; | return tradingGoods; | ||||
}; | }; | ||||
Done Inline ActionsMost of the errors don't uneval any arguments and just print the identifier (rP14417 and rP18964 introduced the one below). Actually it sounds wrong to throw an error when processing invalid player commands, since the error would occur in the GUI of one client, not in the simulation of every client processing that command. But it should be consistent, and we have many places checking for illegal commands (most notably Commands.js). Perhaps it can be demoted to a warn() at least (the error should happen in the GUI if it's possible to trigger it, or better making it impossible to trigger it.) Why not just loop over GetTradable(s)? elexis: Most of the errors don't uneval any arguments and **just print the identifier** (rP14417 and… | |||||
Done Inline Actions
Well, in a way it loops over GetTradables, since this.tradingGoods is defined using GetTradables, so I guess this check is unnecessary then. Freagarach: > Why not just loop over GetTradable(s)?
Well, in a way it loops over GetTradables, since this. | |||||
Player.prototype.SetTradingGoods = function(tradingGoods) | Player.prototype.SetTradingGoods = function(tradingGoods) | ||||
{ | { | ||||
let resCodes = Resources.GetCodes(); | let resCodes = Resources.GetCodes("tradable"); | ||||
let sumProba = 0; | let sumProba = 0; | ||||
for (let resource in tradingGoods) | for (let resource in tradingGoods) | ||||
{ | { | ||||
if (resCodes.indexOf(resource) == -1 || tradingGoods[resource] < 0) | if (resCodes.indexOf(resource) == -1 || tradingGoods[resource] < 0) | ||||
{ | { | ||||
error("Invalid trading goods: " + uneval(tradingGoods)); | error("Invalid trading goods: " + uneval(tradingGoods)); | ||||
return; | return; | ||||
} | } | ||||
sumProba += tradingGoods[resource]; | sumProba += tradingGoods[resource]; | ||||
} | } | ||||
if (sumProba != 100) | if (sumProba != 100) | ||||
{ | { | ||||
error("Invalid trading goods: " + uneval(tradingGoods)); | error("Invalid trading goods probability: " + uneval(sumProba)); | ||||
return; | return; | ||||
} | } | ||||
this.tradingGoods = []; | this.tradingGoods = []; | ||||
for (let resource in tradingGoods) | for (let resource in tradingGoods) | ||||
this.tradingGoods.push({ | this.tradingGoods.push({ | ||||
"goods": resource, | "goods": resource, | ||||
"proba": tradingGoods[resource] | "proba": tradingGoods[resource] | ||||
▲ Show 20 Lines • Show All 415 Lines • ▼ Show 20 Lines | Player.prototype.TributeResource = function(player, amounts) | ||||
var cmpPlayer = QueryPlayerIDInterface(player); | var cmpPlayer = QueryPlayerIDInterface(player); | ||||
if (!cmpPlayer) | if (!cmpPlayer) | ||||
return; | return; | ||||
if (this.state != "active" || cmpPlayer.state != "active") | if (this.state != "active" || cmpPlayer.state != "active") | ||||
return; | return; | ||||
for (let resCode in amounts) | for (let resCode in amounts) | ||||
if (Resources.GetCodes().indexOf(resCode) == -1 || | if (Resources.GetCodes("tributable").indexOf(resCode) == -1 || | ||||
!Number.isInteger(amounts[resCode]) || | !Number.isInteger(amounts[resCode]) || | ||||
amounts[resCode] < 0) | amounts[resCode] < 0) | ||||
{ | { | ||||
warn("Invalid tribute amounts: " + uneval(amounts)); | warn("Invalid tribute amounts: " + uneval(amounts)); | ||||
return; | return; | ||||
} | } | ||||
if (!this.SubtractResourcesOrNotify(amounts)) | if (!this.SubtractResourcesOrNotify(amounts)) | ||||
▲ Show 20 Lines • Show All 106 Lines • Show Last 20 Lines |
Here it could be: