1 | /**
|
---|
2 | * This class manages a buy and sell button for one resource.
|
---|
3 | * The sell button selects the resource to be sold.
|
---|
4 | * The buy button selects the resource to be bought and performs the sale.
|
---|
5 | */
|
---|
6 | class BarterButton
|
---|
7 | {
|
---|
8 | constructor(barterButtonManager, resourceCode, i, panel)
|
---|
9 | {
|
---|
10 | this.barterButtonManager = barterButtonManager;
|
---|
11 | this.resourceCode = resourceCode;
|
---|
12 | this.amountToSell = 0;
|
---|
13 |
|
---|
14 | this.sellButton = panel.children[i].children[0].children[0];
|
---|
15 | this.sellIcon = this.sellButton.children[0];
|
---|
16 | this.sellAmount = this.sellButton.children[1];
|
---|
17 | this.sellSelection = this.sellButton.children[2];
|
---|
18 |
|
---|
19 | this.buyButton = panel.children[i].children[0].children[1];
|
---|
20 | this.buyIcon= this.buyButton.children[0];
|
---|
21 | this.buyAmount = this.buyButton.children[1];
|
---|
22 |
|
---|
23 | let resourceName = { "resource": resourceNameWithinSentence(resourceCode) };
|
---|
24 |
|
---|
25 | this.sellButton.tooltip = sprintf(translate(this.SellTooltip), resourceName);
|
---|
26 | this.sellButton.onPress = () => { barterButtonManager.setSelectedResource(this.resourceCode); };
|
---|
27 | this.sellButton.hidden = false;
|
---|
28 |
|
---|
29 | this.buyButton.tooltip = sprintf(translate(this.BuyTooltip), resourceName);
|
---|
30 | this.buyButton.onPress = () => { this.buy(); };
|
---|
31 |
|
---|
32 | this.iconPath = this.ResourceIconPath + resourceCode + ".png";
|
---|
33 |
|
---|
34 | setPanelObjectPosition(panel.children[i], i, Infinity);
|
---|
35 | }
|
---|
36 |
|
---|
37 | buy()
|
---|
38 | {
|
---|
39 | Engine.PostNetworkCommand({
|
---|
40 | "type": "barter",
|
---|
41 | "sell": this.barterButtonManager.selectedResource,
|
---|
42 | "buy": this.resourceCode,
|
---|
43 | "amount": this.amountToSell
|
---|
44 | });
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * The viewed player might be the owner of the selected market.
|
---|
49 | */
|
---|
50 | update(viewedPlayer)
|
---|
51 | {
|
---|
52 | this.amountToSell = this.BarterResourceSellQuantity;
|
---|
53 |
|
---|
54 | if (Engine.HotkeyIsPressed("session.massbarter"))
|
---|
55 | this.amountToSell *= this.Multiplier;
|
---|
56 |
|
---|
57 | let neededResourcesSell = Engine.GuiInterfaceCall("GetNeededResources", {
|
---|
58 | "cost": {
|
---|
59 | [this.resourceCode]: this.amountToSell
|
---|
60 | },
|
---|
61 | "player": viewedPlayer
|
---|
62 | });
|
---|
63 |
|
---|
64 | let neededResourcesBuy = Engine.GuiInterfaceCall("GetNeededResources", {
|
---|
65 | "cost": {
|
---|
66 | [this.barterButtonManager.selectedResource]: this.amountToSell
|
---|
67 | },
|
---|
68 | "player": viewedPlayer
|
---|
69 | });
|
---|
70 |
|
---|
71 | let isSelected = this.resourceCode == this.barterButtonManager.selectedResource;
|
---|
72 | let icon = "stretched:" + (isSelected ? this.SelectedModifier : "") + this.iconPath;
|
---|
73 | this.sellIcon.sprite = (neededResourcesSell ? this.DisabledModifier : "") + icon;
|
---|
74 | this.buyIcon.sprite = (neededResourcesBuy ? this.DisabledModifier : "") + icon;
|
---|
75 |
|
---|
76 | this.sellAmount.caption = sprintf(translateWithContext("sell action", this.SellCaption), {
|
---|
77 | "amount": this.amountToSell
|
---|
78 | });
|
---|
79 |
|
---|
80 | let prices = GetSimState().players[viewedPlayer].barterPrices;
|
---|
81 |
|
---|
82 | this.buyAmount.caption = sprintf(translateWithContext("buy action", this.BuyCaption), {
|
---|
83 | "amount": Math.round(
|
---|
84 | prices.sell[this.barterButtonManager.selectedResource] /
|
---|
85 | prices.buy[this.resourceCode] * this.amountToSell)
|
---|
86 | });
|
---|
87 |
|
---|
88 | this.buyButton.hidden = isSelected;
|
---|
89 | this.buyButton.enabled = controlsPlayer(viewedPlayer);
|
---|
90 | this.sellSelection.hidden = !isSelected;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | BarterButton.getWidth = function(panel)
|
---|
95 | {
|
---|
96 | let size = panel.children[0].size;
|
---|
97 | return size.right - size.left;
|
---|
98 | };
|
---|
99 |
|
---|
100 | BarterButton.prototype.BuyTooltip = markForTranslation("Buy %(resource)s");
|
---|
101 | BarterButton.prototype.SellTooltip = markForTranslation("Sell %(resource)s");
|
---|
102 | BarterButton.prototype.BuyCaption = markForTranslationWithContext("buy action", "+%(amount)s");
|
---|
103 | BarterButton.prototype.SellCaption = markForTranslationWithContext("sell action", "-%(amount)s");
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * The barter constants should match with the simulation Quantity of goods to sell per click.
|
---|
107 | */
|
---|
108 | BarterButton.prototype.BarterResourceSellQuantity = 100;
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Multiplier to be applied when holding the massbarter hotkey.
|
---|
112 | */
|
---|
113 | BarterButton.prototype.Multiplier = 5;
|
---|
114 |
|
---|
115 | BarterButton.prototype.SelectedModifier = "color:0 0 0 100:grayscale:";
|
---|
116 |
|
---|
117 | BarterButton.prototype.DisabledModifier = "color:255 0 0 80:";
|
---|
118 |
|
---|
119 | BarterButton.prototype.ResourceIconPath = "session/icons/resources/";
|
---|