| 1 | import { |
| 2 | itemDefinition, |
| 3 | itemDisplayName, |
| 4 | itemAssetPathForStack, |
| 5 | formatQuantity |
| 6 | } from "/shared/definitions/items.js"; |
| 7 | import { marketOrderListingFee } from "/shared/market/exchange.js"; |
| 8 | import { countMarketFunds, resolveMarketSellListing } from "/shared/market/funds.js"; |
| 9 | import { applyItemIcon } from "../render/potionIcon.js"; |
| 10 | |
| 11 | export function initMarket ({ |
| 12 | socket, |
| 13 | MessageType, |
| 14 | getInventorySlots, |
| 15 | getCoinCount, |
| 16 | onSuppressWorldClick |
| 17 | }) |
| 18 | { |
| 19 | const backdrop = document.getElementById("market-backdrop"); |
| 20 | const panel = document.getElementById("market-panel"); |
| 21 | const title = document.getElementById("market-title"); |
| 22 | const ordersEl = document.getElementById("market-orders"); |
| 23 | const catalogEl = document.getElementById("market-catalog"); |
| 24 | const sellList = document.getElementById("market-sell-list"); |
| 25 | const sellSelected = document.getElementById("market-sell-selected"); |
| 26 | const buyItem = document.getElementById("market-buy-item"); |
| 27 | const buySearch = document.getElementById("market-buy-search"); |
| 28 | const buyQty = document.getElementById("market-buy-qty"); |
| 29 | const buyMax = document.getElementById("market-buy-max"); |
| 30 | const buyFee = document.getElementById("market-buy-fee"); |
| 31 | const sellQty = document.getElementById("market-sell-qty"); |
| 32 | const sellMin = document.getElementById("market-sell-min"); |
| 33 | const sellFee = document.getElementById("market-sell-fee"); |
| 34 | const coinsEl = document.getElementById("market-coins"); |
| 35 | const hintEl = document.getElementById("market-hint"); |
| 36 | const tabs = [...document.querySelectorAll(".market-tab")]; |
| 37 | const panes = [...document.querySelectorAll(".market-pane")]; |
| 38 | |
| 39 | let open = false; |
| 40 | let catalog = []; |
| 41 | let orders = []; |
| 42 | let searchHits = []; |
| 43 | let selectedSellSlot = null; |
| 44 | let selectedBuyItemId = null; |
| 45 | let activeTab = "orders"; |
| 46 | let sellQtyLocked = false; |
| 47 | |
| 48 | function suppressWorldClick () |
| 49 | { |
| 50 | onSuppressWorldClick?.(); |
| 51 | } |
| 52 | |
| 53 | function setTab (tab) |
| 54 | { |
| 55 | activeTab = tab; |
| 56 | for (const button of tabs) |
| 57 | { button.classList.toggle("active", button.dataset.tab === tab); } |
| 58 | for (const pane of panes) |
| 59 | { pane.hidden = pane.dataset.pane !== tab; } |
| 60 | if (tab === "sell") |
| 61 | { renderSellables(); } |
| 62 | if (tab === "buy") |
| 63 | { updateBuyFee(); } |
| 64 | if (tab === "orders") |
| 65 | { renderOrders(); } |
| 66 | } |
| 67 | |
| 68 | for (const button of tabs) |
| 69 | { |
| 70 | button.addEventListener("click", () => setTab(button.dataset.tab)); |
| 71 | } |
| 72 | |
| 73 | document.getElementById("market-close")?.addEventListener("click", () => close()); |
| 74 | backdrop?.addEventListener("mousedown", (event) => |
| 75 | { |
| 76 | event.preventDefault(); |
| 77 | suppressWorldClick(); |
| 78 | }); |
| 79 | backdrop?.addEventListener("click", () => close()); |
| 80 | panel?.addEventListener("mousedown", (event) => |
| 81 | { |
| 82 | event.stopPropagation(); |
| 83 | suppressWorldClick(); |
| 84 | }); |
| 85 | |
| 86 | buySearch?.addEventListener("keydown", (event) => |
| 87 | { |
| 88 | if (event.key !== "Enter") |
| 89 | { return; } |
| 90 | event.preventDefault(); |
| 91 | runBuySearch(buySearch.value ?? ""); |
| 92 | }); |
| 93 | |
| 94 | buyQty?.addEventListener("input", () => updateBuyFee()); |
| 95 | buyMax?.addEventListener("input", () => updateBuyFee()); |
| 96 | sellQty?.addEventListener("input", () => updateSellFee()); |
| 97 | sellMin?.addEventListener("input", () => updateSellFee()); |
| 98 | |
| 99 | document.getElementById("market-buy-confirm")?.addEventListener("click", () => |
| 100 | { |
| 101 | if (selectedBuyItemId == null) |
| 102 | { return; } |
| 103 | socket.send({ |
| 104 | type: MessageType.placeMarketBuy, |
| 105 | itemId: selectedBuyItemId, |
| 106 | quantity: Math.max(1, buyQty?.valueAsNumber || 1), |
| 107 | maxUnitPrice: Math.max(1, buyMax?.valueAsNumber || 1) |
| 108 | }); |
| 109 | }); |
| 110 | |
| 111 | document.getElementById("market-sell-confirm")?.addEventListener("click", () => |
| 112 | { |
| 113 | if (selectedSellSlot == null) |
| 114 | { return; } |
| 115 | socket.send({ |
| 116 | type: MessageType.placeMarketSell, |
| 117 | slotIndex: selectedSellSlot, |
| 118 | quantity: Math.max(1, sellQty?.valueAsNumber || 1), |
| 119 | minUnitPrice: Math.max(1, sellMin?.valueAsNumber || 1) |
| 120 | }); |
| 121 | }); |
| 122 | |
| 123 | function close () |
| 124 | { |
| 125 | if (!open) |
| 126 | { return; } |
| 127 | open = false; |
| 128 | if (backdrop) |
| 129 | { backdrop.hidden = true; } |
| 130 | if (panel) |
| 131 | { panel.hidden = true; } |
| 132 | socket.send({ type: MessageType.closeMarket }); |
| 133 | } |
| 134 | |
| 135 | function runBuySearch (query) |
| 136 | { |
| 137 | const needle = String(query ?? "").trim().toLowerCase(); |
| 138 | selectedBuyItemId = null; |
| 139 | if (buyItem) |
| 140 | { buyItem.textContent = needle ? "Select an item" : "Search and select an item"; } |
| 141 | if (!needle) |
| 142 | { |
| 143 | searchHits = []; |
| 144 | renderCatalog(); |
| 145 | updateBuyFee(); |
| 146 | return; |
| 147 | } |
| 148 | searchHits = catalog |
| 149 | .filter((entry) => entry.displayName.toLowerCase().includes(needle)) |
| 150 | .slice(0, 10); |
| 151 | renderCatalog(); |
| 152 | updateBuyFee(); |
| 153 | } |
| 154 | |
| 155 | function updateBuyFee () |
| 156 | { |
| 157 | const qty = Math.max(1, buyQty?.valueAsNumber || 1); |
| 158 | const max = Math.max(1, buyMax?.valueAsNumber || 1); |
| 159 | const fee = selectedBuyItemId == null ? 0 : marketOrderListingFee(max, qty); |
| 160 | const escrow = selectedBuyItemId == null ? 0 : max * qty; |
| 161 | if (buyFee) |
| 162 | { |
| 163 | buyFee.textContent = selectedBuyItemId == null |
| 164 | ? "" |
| 165 | : `Escrow ${formatQuantity(escrow)} + listing fee ${formatQuantity(fee)} (3%, non-refundable)`; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | function updateSellFee () |
| 170 | { |
| 171 | const qty = Math.max(1, sellQty?.valueAsNumber || 1); |
| 172 | const min = Math.max(1, sellMin?.valueAsNumber || 1); |
| 173 | const fee = selectedSellSlot == null ? 0 : marketOrderListingFee(min, qty); |
| 174 | if (sellFee) |
| 175 | { |
| 176 | sellFee.textContent = selectedSellSlot == null |
| 177 | ? "" |
| 178 | : `Listing fee ${formatQuantity(fee)} (3% of min value, non-refundable)`; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | function renderOrders () |
| 183 | { |
| 184 | ordersEl.replaceChildren(); |
| 185 | if (!orders.length) |
| 186 | { |
| 187 | const empty = document.createElement("p"); |
| 188 | empty.className = "hint"; |
| 189 | empty.textContent = "No open orders."; |
| 190 | ordersEl.append(empty); |
| 191 | return; |
| 192 | } |
| 193 | for (const order of orders) |
| 194 | { |
| 195 | const row = document.createElement("div"); |
| 196 | row.className = "market-order-row"; |
| 197 | const label = document.createElement("span"); |
| 198 | const priceLabel = order.side === "buy" |
| 199 | ? `max ${order.limitUnitPrice}` |
| 200 | : `min ${order.limitUnitPrice}`; |
| 201 | label.textContent = `${order.side === "buy" ? "Buy" : "Sell"} ${order.quantity}× ${order.displayName} @ ${priceLabel}`; |
| 202 | const cancel = document.createElement("button"); |
| 203 | cancel.type = "button"; |
| 204 | cancel.textContent = "Cancel"; |
| 205 | cancel.addEventListener("click", () => |
| 206 | { |
| 207 | socket.send({ |
| 208 | type: MessageType.cancelMarketOrder, |
| 209 | orderId: order.orderId |
| 210 | }); |
| 211 | }); |
| 212 | row.append(label, cancel); |
| 213 | ordersEl.append(row); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | function renderCatalog () |
| 218 | { |
| 219 | catalogEl.replaceChildren(); |
| 220 | if (!searchHits.length) |
| 221 | { |
| 222 | const empty = document.createElement("p"); |
| 223 | empty.className = "hint"; |
| 224 | empty.textContent = (buySearch?.value ?? "").trim() |
| 225 | ? "No matching items." |
| 226 | : "Press Enter to search."; |
| 227 | catalogEl.append(empty); |
| 228 | return; |
| 229 | } |
| 230 | for (const entry of searchHits) |
| 231 | { |
| 232 | const row = document.createElement("button"); |
| 233 | row.type = "button"; |
| 234 | row.className = "market-catalog-row"; |
| 235 | if (selectedBuyItemId === entry.itemId) |
| 236 | { row.classList.add("selected"); } |
| 237 | row.textContent = `${entry.displayName} (~${entry.hintPrice})`; |
| 238 | row.addEventListener("click", () => |
| 239 | { |
| 240 | selectedBuyItemId = entry.itemId; |
| 241 | if (buyItem) |
| 242 | { buyItem.textContent = entry.displayName; } |
| 243 | if (buyMax && !(buyMax.valueAsNumber > 0)) |
| 244 | { buyMax.value = String(entry.hintPrice); } |
| 245 | renderCatalog(); |
| 246 | updateBuyFee(); |
| 247 | }); |
| 248 | catalogEl.append(row); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | function renderSellables () |
| 253 | { |
| 254 | sellList.replaceChildren(); |
| 255 | const slots = getInventorySlots?.() ?? []; |
| 256 | slots.forEach((slot, slotIndex) => |
| 257 | { |
| 258 | if (!slot || slot.itemId == null) |
| 259 | { return; } |
| 260 | const listing = resolveMarketSellListing(slot); |
| 261 | if (!listing) |
| 262 | { return; } |
| 263 | const def = itemDefinition(slot.itemId); |
| 264 | if (!def || def.marketplaceSellable === false) |
| 265 | { return; } |
| 266 | const row = document.createElement("button"); |
| 267 | row.type = "button"; |
| 268 | row.className = "market-sell-row"; |
| 269 | if (selectedSellSlot === slotIndex) |
| 270 | { row.classList.add("selected"); } |
| 271 | const icon = document.createElement("img"); |
| 272 | icon.alt = ""; |
| 273 | applyItemIcon(icon, itemAssetPathForStack(slot)); |
| 274 | const label = document.createElement("span"); |
| 275 | if (listing.isCheque) |
| 276 | { |
| 277 | label.textContent = `Cheque: ${listing.displayName} ×${formatQuantity(listing.quantity)}`; |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | label.textContent = `${itemDisplayName(slot)} ×${formatQuantity(slot.quantity)}`; |
| 282 | } |
| 283 | row.append(icon, label); |
| 284 | row.addEventListener("click", () => |
| 285 | { |
| 286 | selectedSellSlot = slotIndex; |
| 287 | sellQtyLocked = !!listing.quantityLocked; |
| 288 | if (sellQty) |
| 289 | { |
| 290 | sellQty.value = String(listing.quantity); |
| 291 | sellQty.readOnly = sellQtyLocked; |
| 292 | sellQty.disabled = sellQtyLocked; |
| 293 | } |
| 294 | if (sellSelected) |
| 295 | { |
| 296 | sellSelected.textContent = listing.isCheque |
| 297 | ? `Selling cheque for ${listing.quantity}× ${listing.displayName} (quantity locked)` |
| 298 | : `Selling ${itemDisplayName(slot)}`; |
| 299 | } |
| 300 | renderSellables(); |
| 301 | updateSellFee(); |
| 302 | }); |
| 303 | sellList.append(row); |
| 304 | }); |
| 305 | } |
| 306 | |
| 307 | function refreshChrome () |
| 308 | { |
| 309 | const slots = getInventorySlots?.() ?? []; |
| 310 | const funds = countMarketFunds(slots); |
| 311 | const loose = getCoinCount?.() ?? 0; |
| 312 | if (coinsEl) |
| 313 | { |
| 314 | coinsEl.textContent = funds === loose |
| 315 | ? `Funds: ${formatQuantity(funds)}` |
| 316 | : `Funds: ${formatQuantity(funds)} (coins + coin cheques)`; |
| 317 | } |
| 318 | if (activeTab === "sell") |
| 319 | { renderSellables(); } |
| 320 | updateBuyFee(); |
| 321 | updateSellFee(); |
| 322 | } |
| 323 | |
| 324 | return { |
| 325 | handleOpen (payload) |
| 326 | { |
| 327 | open = true; |
| 328 | orders = payload.orders ?? []; |
| 329 | catalog = payload.catalog ?? []; |
| 330 | searchHits = []; |
| 331 | selectedSellSlot = null; |
| 332 | selectedBuyItemId = null; |
| 333 | sellQtyLocked = false; |
| 334 | if (sellQty) |
| 335 | { |
| 336 | sellQty.readOnly = false; |
| 337 | sellQty.disabled = false; |
| 338 | sellQty.value = "1"; |
| 339 | } |
| 340 | if (buySearch) |
| 341 | { buySearch.value = ""; } |
| 342 | if (title) |
| 343 | { title.textContent = payload.displayName || "Central Exchange"; } |
| 344 | if (hintEl) |
| 345 | { |
| 346 | hintEl.textContent = "Placing an order costs a 3% listing fee (non-refundable). Matches are private; unfilled orders expire after one month."; |
| 347 | } |
| 348 | if (backdrop) |
| 349 | { backdrop.hidden = false; } |
| 350 | if (panel) |
| 351 | { panel.hidden = false; } |
| 352 | setTab("orders"); |
| 353 | renderOrders(); |
| 354 | renderCatalog(); |
| 355 | refreshChrome(); |
| 356 | }, |
| 357 | handleClosed () |
| 358 | { |
| 359 | open = false; |
| 360 | if (backdrop) |
| 361 | { backdrop.hidden = true; } |
| 362 | if (panel) |
| 363 | { panel.hidden = true; } |
| 364 | }, |
| 365 | handleTrade () |
| 366 | { |
| 367 | if (open) |
| 368 | { refreshChrome(); } |
| 369 | }, |
| 370 | refresh () |
| 371 | { |
| 372 | if (open) |
| 373 | { refreshChrome(); } |
| 374 | }, |
| 375 | isOpen () |
| 376 | { |
| 377 | return open; |
| 378 | }, |
| 379 | close |
| 380 | }; |
| 381 | } |