| 1 | import { |
| 2 | itemDefinition, |
| 3 | itemDisplayName, |
| 4 | itemAssetPathForStack, |
| 5 | formatQuantity |
| 6 | } from "/shared/definitions/items.js"; |
| 7 | import { applyItemIcon } from "../render/potionIcon.js"; |
| 8 | |
| 9 | export function initMarket ({ |
| 10 | socket, |
| 11 | MessageType, |
| 12 | getInventorySlots, |
| 13 | getCoinCount, |
| 14 | onSuppressWorldClick |
| 15 | }) |
| 16 | { |
| 17 | const backdrop = document.getElementById("market-backdrop"); |
| 18 | const panel = document.getElementById("market-panel"); |
| 19 | const title = document.getElementById("market-title"); |
| 20 | const ordersEl = document.getElementById("market-orders"); |
| 21 | const catalogEl = document.getElementById("market-catalog"); |
| 22 | const sellList = document.getElementById("market-sell-list"); |
| 23 | const buyItem = document.getElementById("market-buy-item"); |
| 24 | const buyQty = document.getElementById("market-buy-qty"); |
| 25 | const buyMax = document.getElementById("market-buy-max"); |
| 26 | const sellQty = document.getElementById("market-sell-qty"); |
| 27 | const sellMin = document.getElementById("market-sell-min"); |
| 28 | const coinsEl = document.getElementById("market-coins"); |
| 29 | const hintEl = document.getElementById("market-hint"); |
| 30 | |
| 31 | let open = false; |
| 32 | let catalog = []; |
| 33 | let orders = []; |
| 34 | let selectedSellSlot = null; |
| 35 | let selectedBuyItemId = null; |
| 36 | |
| 37 | function suppressWorldClick () |
| 38 | { |
| 39 | onSuppressWorldClick?.(); |
| 40 | } |
| 41 | |
| 42 | document.getElementById("market-close")?.addEventListener("click", () => close()); |
| 43 | backdrop?.addEventListener("mousedown", (event) => |
| 44 | { |
| 45 | event.preventDefault(); |
| 46 | suppressWorldClick(); |
| 47 | }); |
| 48 | backdrop?.addEventListener("click", () => close()); |
| 49 | panel?.addEventListener("mousedown", (event) => |
| 50 | { |
| 51 | event.stopPropagation(); |
| 52 | suppressWorldClick(); |
| 53 | }); |
| 54 | |
| 55 | document.getElementById("market-buy-confirm")?.addEventListener("click", () => |
| 56 | { |
| 57 | if (selectedBuyItemId == null) |
| 58 | { return; } |
| 59 | socket.send({ |
| 60 | type: MessageType.placeMarketBuy, |
| 61 | itemId: selectedBuyItemId, |
| 62 | quantity: Math.max(1, buyQty?.valueAsNumber || 1), |
| 63 | maxUnitPrice: Math.max(1, buyMax?.valueAsNumber || 1) |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | document.getElementById("market-sell-confirm")?.addEventListener("click", () => |
| 68 | { |
| 69 | if (selectedSellSlot == null) |
| 70 | { return; } |
| 71 | socket.send({ |
| 72 | type: MessageType.placeMarketSell, |
| 73 | slotIndex: selectedSellSlot, |
| 74 | quantity: Math.max(1, sellQty?.valueAsNumber || 1), |
| 75 | minUnitPrice: Math.max(1, sellMin?.valueAsNumber || 1) |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | function close () |
| 80 | { |
| 81 | if (!open) |
| 82 | { return; } |
| 83 | open = false; |
| 84 | if (backdrop) |
| 85 | { backdrop.hidden = true; } |
| 86 | if (panel) |
| 87 | { panel.hidden = true; } |
| 88 | socket.send({ type: MessageType.closeMarket }); |
| 89 | } |
| 90 | |
| 91 | function renderOrders () |
| 92 | { |
| 93 | ordersEl.replaceChildren(); |
| 94 | if (!orders.length) |
| 95 | { |
| 96 | const empty = document.createElement("p"); |
| 97 | empty.className = "hint"; |
| 98 | empty.textContent = "No open orders."; |
| 99 | ordersEl.append(empty); |
| 100 | return; |
| 101 | } |
| 102 | for (const order of orders) |
| 103 | { |
| 104 | const row = document.createElement("div"); |
| 105 | row.className = "market-order-row"; |
| 106 | const label = document.createElement("span"); |
| 107 | label.textContent = `${order.side === "buy" ? "Buy" : "Sell"} ${order.quantity}× ${order.displayName} @ ${order.limitUnitPrice}`; |
| 108 | const cancel = document.createElement("button"); |
| 109 | cancel.type = "button"; |
| 110 | cancel.textContent = "Cancel"; |
| 111 | cancel.addEventListener("click", () => |
| 112 | { |
| 113 | socket.send({ |
| 114 | type: MessageType.cancelMarketOrder, |
| 115 | orderId: order.orderId |
| 116 | }); |
| 117 | }); |
| 118 | row.append(label, cancel); |
| 119 | ordersEl.append(row); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | function renderCatalog () |
| 124 | { |
| 125 | catalogEl.replaceChildren(); |
| 126 | for (const entry of catalog) |
| 127 | { |
| 128 | const row = document.createElement("button"); |
| 129 | row.type = "button"; |
| 130 | row.className = "market-catalog-row"; |
| 131 | if (selectedBuyItemId === entry.itemId) |
| 132 | { row.classList.add("selected"); } |
| 133 | row.textContent = `${entry.displayName} (~${entry.hintPrice})`; |
| 134 | row.addEventListener("click", () => |
| 135 | { |
| 136 | selectedBuyItemId = entry.itemId; |
| 137 | if (buyItem) |
| 138 | { buyItem.textContent = entry.displayName; } |
| 139 | if (buyMax && !(buyMax.valueAsNumber > 0)) |
| 140 | { buyMax.value = String(entry.hintPrice); } |
| 141 | renderCatalog(); |
| 142 | }); |
| 143 | catalogEl.append(row); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | function renderSellables () |
| 148 | { |
| 149 | sellList.replaceChildren(); |
| 150 | const slots = getInventorySlots?.() ?? []; |
| 151 | slots.forEach((slot, slotIndex) => |
| 152 | { |
| 153 | if (!slot || slot.itemId == null) |
| 154 | { return; } |
| 155 | const def = itemDefinition(slot.itemId); |
| 156 | if (!def || def.marketplaceSellable === false) |
| 157 | { return; } |
| 158 | const row = document.createElement("button"); |
| 159 | row.type = "button"; |
| 160 | row.className = "market-sell-row"; |
| 161 | if (selectedSellSlot === slotIndex) |
| 162 | { row.classList.add("selected"); } |
| 163 | const icon = document.createElement("img"); |
| 164 | icon.alt = ""; |
| 165 | applyItemIcon(icon, itemAssetPathForStack(slot)); |
| 166 | const label = document.createElement("span"); |
| 167 | label.textContent = `${itemDisplayName(slot)} ×${formatQuantity(slot.quantity)}`; |
| 168 | row.append(icon, label); |
| 169 | row.addEventListener("click", () => |
| 170 | { |
| 171 | selectedSellSlot = slotIndex; |
| 172 | if (sellQty) |
| 173 | { sellQty.value = "1"; } |
| 174 | renderSellables(); |
| 175 | }); |
| 176 | sellList.append(row); |
| 177 | }); |
| 178 | } |
| 179 | |
| 180 | function refreshChrome () |
| 181 | { |
| 182 | if (coinsEl) |
| 183 | { coinsEl.textContent = `Coins: ${formatQuantity(getCoinCount?.() ?? 0)}`; } |
| 184 | renderSellables(); |
| 185 | } |
| 186 | |
| 187 | return { |
| 188 | handleOpen (payload) |
| 189 | { |
| 190 | open = true; |
| 191 | orders = payload.orders ?? []; |
| 192 | catalog = payload.catalog ?? []; |
| 193 | if (title) |
| 194 | { title.textContent = payload.displayName || "Central Exchange"; } |
| 195 | if (hintEl) |
| 196 | { |
| 197 | hintEl.textContent = "Orders are matched privately. Live asks are not listed. Unfilled orders expire after one month."; |
| 198 | } |
| 199 | if (backdrop) |
| 200 | { backdrop.hidden = false; } |
| 201 | if (panel) |
| 202 | { panel.hidden = false; } |
| 203 | renderOrders(); |
| 204 | renderCatalog(); |
| 205 | refreshChrome(); |
| 206 | }, |
| 207 | handleClosed () |
| 208 | { |
| 209 | open = false; |
| 210 | if (backdrop) |
| 211 | { backdrop.hidden = true; } |
| 212 | if (panel) |
| 213 | { panel.hidden = true; } |
| 214 | }, |
| 215 | handleTrade () |
| 216 | { |
| 217 | if (open) |
| 218 | { refreshChrome(); } |
| 219 | }, |
| 220 | refresh () |
| 221 | { |
| 222 | if (open) |
| 223 | { refreshChrome(); } |
| 224 | }, |
| 225 | isOpen () |
| 226 | { |
| 227 | return open; |
| 228 | }, |
| 229 | close |
| 230 | }; |
| 231 | } |