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