Index: ps/trunk/binaries/data/mods/public/gui/session/trade/BarterButton.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/session/trade/BarterButton.js (revision 25926) +++ ps/trunk/binaries/data/mods/public/gui/session/trade/BarterButton.js (revision 25927) @@ -1,119 +1,126 @@ /** * This class manages a buy and sell button for one resource. * The sell button selects the resource to be sold. * The buy button selects the resource to be bought and performs the sale. */ class BarterButton { constructor(barterButtonManager, resourceCode, i, panel) { this.barterButtonManager = barterButtonManager; this.resourceCode = resourceCode; this.amountToSell = 0; this.sellButton = panel.children[i].children[0].children[0]; this.sellIcon = this.sellButton.children[0]; this.sellAmount = this.sellButton.children[1]; this.sellSelection = this.sellButton.children[2]; this.buyButton = panel.children[i].children[0].children[1]; this.buyIcon= this.buyButton.children[0]; this.buyAmount = this.buyButton.children[1]; let resourceName = { "resource": resourceNameWithinSentence(resourceCode) }; this.sellButton.tooltip = sprintf(translate(this.SellTooltip), resourceName); this.sellButton.onPress = () => { barterButtonManager.setSelectedResource(this.resourceCode); }; this.sellButton.hidden = false; this.buyButton.tooltip = sprintf(translate(this.BuyTooltip), resourceName); this.buyButton.onPress = () => { this.buy(); }; this.iconPath = this.ResourceIconPath + resourceCode + ".png"; setPanelObjectPosition(panel.children[i], i, Infinity); } buy() { Engine.PostNetworkCommand({ "type": "barter", "sell": this.barterButtonManager.selectedResource, "buy": this.resourceCode, "amount": this.amountToSell }); } /** * The viewed player might be the owner of the selected market. */ update(viewedPlayer) { + if (viewedPlayer < 1) + { + this.buyButton.hidden = true; + this.sellButton.hidden = true; + return; + } + this.amountToSell = this.BarterResourceSellQuantity; if (Engine.HotkeyIsPressed("session.massbarter")) this.amountToSell *= this.Multiplier; let neededResourcesSell = Engine.GuiInterfaceCall("GetNeededResources", { "cost": { [this.resourceCode]: this.amountToSell }, "player": viewedPlayer }); let neededResourcesBuy = Engine.GuiInterfaceCall("GetNeededResources", { "cost": { [this.barterButtonManager.selectedResource]: this.amountToSell }, "player": viewedPlayer }); let isSelected = this.resourceCode == this.barterButtonManager.selectedResource; let icon = "stretched:" + (isSelected ? this.SelectedModifier : "") + this.iconPath; this.sellIcon.sprite = (neededResourcesSell ? this.DisabledModifier : "") + icon; this.buyIcon.sprite = (neededResourcesBuy ? this.DisabledModifier : "") + icon; this.sellAmount.caption = sprintf(translateWithContext("sell action", this.SellCaption), { "amount": this.amountToSell }); let prices = GetSimState().players[viewedPlayer].barterPrices; this.buyAmount.caption = sprintf(translateWithContext("buy action", this.BuyCaption), { "amount": Math.round( prices.sell[this.barterButtonManager.selectedResource] / prices.buy[this.resourceCode] * this.amountToSell) }); this.buyButton.hidden = isSelected; this.buyButton.enabled = controlsPlayer(viewedPlayer); this.sellSelection.hidden = !isSelected; } } BarterButton.getWidth = function(panel) { let size = panel.children[0].size; return size.right - size.left; }; BarterButton.prototype.BuyTooltip = markForTranslation("Buy %(resource)s"); BarterButton.prototype.SellTooltip = markForTranslation("Sell %(resource)s"); BarterButton.prototype.BuyCaption = markForTranslationWithContext("buy action", "+%(amount)s"); BarterButton.prototype.SellCaption = markForTranslationWithContext("sell action", "-%(amount)s"); /** * The barter constants should match with the simulation Quantity of goods to sell per click. */ BarterButton.prototype.BarterResourceSellQuantity = 100; /** * Multiplier to be applied when holding the massbarter hotkey. */ BarterButton.prototype.Multiplier = 5; BarterButton.prototype.SelectedModifier = "color:0 0 0 100:grayscale:"; BarterButton.prototype.DisabledModifier = "color:255 0 0 80:"; BarterButton.prototype.ResourceIconPath = "session/icons/resources/"; Index: ps/trunk/binaries/data/mods/public/gui/session/trade/BarterButtonManager.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/session/trade/BarterButtonManager.js (revision 25926) +++ ps/trunk/binaries/data/mods/public/gui/session/trade/BarterButtonManager.js (revision 25927) @@ -1,54 +1,53 @@ /** * This class provides barter buttons. * This is instantiated once for the selection panels and once for the trade dialog. */ class BarterButtonManager { constructor(panel) { if (!BarterButtonManager.IsAvailable(panel)) throw "BarterButtonManager instantiated with no barterable resources or too few buttons!"; // The player may be the owner of the selected market this.viewedPlayer = -1; let resourceCodes = g_ResourceData.GetBarterableCodes(); this.selectedResource = resourceCodes[0]; this.buttons = resourceCodes.map((resourceCode, i) => new BarterButton(this, resourceCode, i, panel)); panel.onPress = this.update.bind(this); panel.onRelease = this.update.bind(this); } setViewedPlayer(viewedPlayer) { this.viewedPlayer = viewedPlayer; } setSelectedResource(resourceCode) { this.selectedResource = resourceCode; this.update(); } getSelectedButton() { - for (let button of this.buttons) + for (const button of this.buttons) if (!this.selectedResource || this.selectedResource == button.resourceCode) return button; } update() { - if (this.viewedPlayer >= 1) - for (let button of this.buttons) - button.update(this.viewedPlayer); + for (const button of this.buttons) + button.update(this.viewedPlayer); } } BarterButtonManager.IsAvailable = function(panel) { let resourceCount = g_ResourceData.GetBarterableCodes().length; return resourceCount && resourceCount <= panel.children.length; };