| 1 | import { |
| 2 | itemDefinition, |
| 3 | itemDisplayName, |
| 4 | itemAssetPathForStack, |
| 5 | formatQuantity |
| 6 | } from "/shared/definitions/items.js"; |
| 7 | import { insertStack, stackQuantity } from "/shared/items/inventory.js"; |
| 8 | import { |
| 9 | canPlayerSellToNpc, |
| 10 | resolveShopSellQuote |
| 11 | } from "/shared/shop/pricing.js"; |
| 12 | import { subjectPronoun } from "/shared/speech/pronouns.js"; |
| 13 | import { applyItemIcon } from "../render/potionIcon.js"; |
| 14 | |
| 15 | export function initShop ({ |
| 16 | socket, |
| 17 | MessageType, |
| 18 | getCoinCount, |
| 19 | getInventorySlots, |
| 20 | onSuppressWorldClick |
| 21 | }) |
| 22 | { |
| 23 | const backdrop = document.getElementById("shop-backdrop"); |
| 24 | const panel = document.getElementById("shop-panel"); |
| 25 | const title = document.getElementById("shop-title"); |
| 26 | const tabBuy = document.getElementById("shop-tab-buy"); |
| 27 | const tabSell = document.getElementById("shop-tab-sell"); |
| 28 | const paneBuy = document.getElementById("shop-pane-buy"); |
| 29 | const paneSell = document.getElementById("shop-pane-sell"); |
| 30 | const grid = document.getElementById("shop-grid"); |
| 31 | const detail = document.getElementById("shop-detail"); |
| 32 | const detailIcon = document.getElementById("shop-detail-icon"); |
| 33 | const detailName = document.getElementById("shop-detail-name"); |
| 34 | const detailPrice = document.getElementById("shop-detail-price"); |
| 35 | const detailStock = document.getElementById("shop-detail-stock"); |
| 36 | const bagWarn = document.getElementById("shop-bag-warn"); |
| 37 | const coinsEl = document.getElementById("shop-coins"); |
| 38 | const sellGrid = document.getElementById("shop-sell-grid"); |
| 39 | const sellDetail = document.getElementById("shop-sell-detail"); |
| 40 | const sellIcon = document.getElementById("shop-sell-icon"); |
| 41 | const sellName = document.getElementById("shop-sell-name"); |
| 42 | const sellPrice = document.getElementById("shop-sell-price"); |
| 43 | const sellFee = document.getElementById("shop-sell-fee"); |
| 44 | const sellActions = document.getElementById("shop-sell-actions"); |
| 45 | const sellEmpty = document.getElementById("shop-sell-empty"); |
| 46 | const qtyBackdrop = document.getElementById("shop-qty-backdrop"); |
| 47 | const qtyPanel = document.getElementById("shop-qty-panel"); |
| 48 | const qtyTitle = document.getElementById("shop-qty-title"); |
| 49 | const qtyConfirm = document.getElementById("shop-qty-confirm"); |
| 50 | const qtyInput = document.getElementById("shop-qty-input"); |
| 51 | const qtyHint = document.getElementById("shop-qty-hint"); |
| 52 | |
| 53 | let npcEntityId = null; |
| 54 | let stock = []; |
| 55 | let selectedListingId = null; |
| 56 | let selectedSellSlotIndex = null; |
| 57 | let buysItems = false; |
| 58 | let displayName = "Shop"; |
| 59 | let sex = "male"; |
| 60 | /** @type {"buy"|"sell"} */ |
| 61 | let activeTab = "buy"; |
| 62 | /** @type {"buy"|"sell"|null} */ |
| 63 | let qtyMode = null; |
| 64 | |
| 65 | function suppressWorldClick () |
| 66 | { |
| 67 | onSuppressWorldClick?.(); |
| 68 | } |
| 69 | |
| 70 | document.getElementById("shop-close")?.addEventListener("click", () => close()); |
| 71 | backdrop?.addEventListener("mousedown", (event) => |
| 72 | { |
| 73 | event.preventDefault(); |
| 74 | suppressWorldClick(); |
| 75 | }); |
| 76 | backdrop?.addEventListener("click", () => close()); |
| 77 | panel?.addEventListener("mousedown", (event) => |
| 78 | { |
| 79 | event.stopPropagation(); |
| 80 | suppressWorldClick(); |
| 81 | }); |
| 82 | qtyBackdrop?.addEventListener("mousedown", (event) => |
| 83 | { |
| 84 | event.preventDefault(); |
| 85 | suppressWorldClick(); |
| 86 | }); |
| 87 | qtyPanel?.addEventListener("mousedown", (event) => |
| 88 | { |
| 89 | event.stopPropagation(); |
| 90 | suppressWorldClick(); |
| 91 | }); |
| 92 | tabBuy?.addEventListener("click", () => setTab("buy")); |
| 93 | tabSell?.addEventListener("click", () => setTab("sell")); |
| 94 | document.getElementById("shop-buy-1")?.addEventListener("click", () => buy(1)); |
| 95 | document.getElementById("shop-buy-5")?.addEventListener("click", () => buy(5)); |
| 96 | document.getElementById("shop-buy-10")?.addEventListener("click", () => buy(10)); |
| 97 | document.getElementById("shop-buy-x")?.addEventListener("click", () => openQtyModal("buy")); |
| 98 | document.getElementById("shop-qty-close")?.addEventListener("click", () => closeQtyModal()); |
| 99 | qtyConfirm?.addEventListener("click", () => confirmQtyModal()); |
| 100 | qtyBackdrop?.addEventListener("click", () => closeQtyModal()); |
| 101 | qtyInput?.addEventListener("keydown", (event) => |
| 102 | { |
| 103 | if (event.key === "Enter") |
| 104 | { confirmQtyModal(); } |
| 105 | }); |
| 106 | |
| 107 | function setTab (tab) |
| 108 | { |
| 109 | if (tab === "sell" && !buysItems) |
| 110 | { tab = "buy"; } |
| 111 | activeTab = tab; |
| 112 | const buyActive = tab === "buy"; |
| 113 | if (tabBuy) |
| 114 | { |
| 115 | tabBuy.classList.toggle("active", buyActive); |
| 116 | tabBuy.setAttribute("aria-selected", buyActive ? "true" : "false"); |
| 117 | } |
| 118 | if (tabSell) |
| 119 | { |
| 120 | tabSell.classList.toggle("active", !buyActive); |
| 121 | tabSell.setAttribute("aria-selected", buyActive ? "false" : "true"); |
| 122 | } |
| 123 | if (paneBuy) |
| 124 | { paneBuy.hidden = !buyActive; } |
| 125 | if (paneSell) |
| 126 | { paneSell.hidden = buyActive; } |
| 127 | render(); |
| 128 | } |
| 129 | |
| 130 | function open (payload) |
| 131 | { |
| 132 | npcEntityId = payload.npcEntityId | 0; |
| 133 | displayName = payload.displayName || "Shop"; |
| 134 | sex = payload.sex === "female" ? "female" : "male"; |
| 135 | title.textContent = displayName; |
| 136 | buysItems = !!payload.buysItems; |
| 137 | stock = Array.isArray(payload.stock) ? payload.stock : []; |
| 138 | if (selectedListingId == null || !stock.some((row) => row.listingId === selectedListingId)) |
| 139 | { selectedListingId = stock[0]?.listingId ?? null; } |
| 140 | if (tabSell) |
| 141 | { tabSell.hidden = !buysItems; } |
| 142 | closeQtyModal(); |
| 143 | setTab(buysItems && activeTab === "sell" ? "sell" : "buy"); |
| 144 | backdrop.hidden = false; |
| 145 | panel.hidden = false; |
| 146 | suppressWorldClick(); |
| 147 | } |
| 148 | |
| 149 | function close ({ notifyServer = true } = {}) |
| 150 | { |
| 151 | const closingNpcId = npcEntityId; |
| 152 | closeQtyModal(); |
| 153 | backdrop.hidden = true; |
| 154 | panel.hidden = true; |
| 155 | npcEntityId = null; |
| 156 | stock = []; |
| 157 | selectedListingId = null; |
| 158 | selectedSellSlotIndex = null; |
| 159 | buysItems = false; |
| 160 | sex = "male"; |
| 161 | activeTab = "buy"; |
| 162 | suppressWorldClick(); |
| 163 | if (notifyServer && closingNpcId) |
| 164 | { |
| 165 | socket.send({ |
| 166 | type: MessageType.closeShop, |
| 167 | npcEntityId: closingNpcId |
| 168 | }); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | function selectedEntry () |
| 173 | { |
| 174 | return stock.find((row) => row.listingId === selectedListingId) ?? null; |
| 175 | } |
| 176 | |
| 177 | function maxBuyable (entry) |
| 178 | { |
| 179 | const price = Math.max(1, entry?.unitPrice | 0 || 1); |
| 180 | const coins = getCoinCount?.() ?? 0; |
| 181 | const affordable = Math.max(0, Math.floor(coins / price)); |
| 182 | const available = entry?.available == null ? affordable : Math.max(0, entry.available | 0); |
| 183 | return Math.min(affordable, available); |
| 184 | } |
| 185 | |
| 186 | function inventoryFits (itemId, quantity, instance = null) |
| 187 | { |
| 188 | const def = itemDefinition(itemId); |
| 189 | if (!def) |
| 190 | { return false; } |
| 191 | const slots = getInventorySlots?.() ?? []; |
| 192 | const added = insertStack(slots.map((slot) => (slot ? structuredClone(slot) : null)), { |
| 193 | itemId, |
| 194 | quantity: Math.max(1, quantity | 0 || 1), |
| 195 | instance: instance ? structuredClone(instance) : null |
| 196 | }, def); |
| 197 | return added.ok && added.remainder === 0; |
| 198 | } |
| 199 | |
| 200 | function buy (quantity) |
| 201 | { |
| 202 | const entry = selectedEntry(); |
| 203 | if (!npcEntityId || !entry) |
| 204 | { return; } |
| 205 | const qty = Math.max(1, quantity | 0 || 1); |
| 206 | const max = maxBuyable(entry); |
| 207 | if (qty > max) |
| 208 | { return; } |
| 209 | socket.send({ |
| 210 | type: MessageType.buyFromShop, |
| 211 | npcEntityId, |
| 212 | itemId: entry.itemId, |
| 213 | quantity: qty, |
| 214 | listingId: entry.listingId, |
| 215 | source: entry.source |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | function openQtyModal (mode) |
| 220 | { |
| 221 | qtyMode = mode; |
| 222 | if (mode === "buy") |
| 223 | { |
| 224 | const entry = selectedEntry(); |
| 225 | if (!entry) |
| 226 | { return; } |
| 227 | const max = maxBuyable(entry); |
| 228 | if (max < 1) |
| 229 | { return; } |
| 230 | if (qtyTitle) |
| 231 | { qtyTitle.textContent = "Buy quantity"; } |
| 232 | if (qtyConfirm) |
| 233 | { qtyConfirm.textContent = "Buy"; } |
| 234 | qtyInput.min = "1"; |
| 235 | qtyInput.max = String(max); |
| 236 | qtyInput.value = String(Math.min(max, 1)); |
| 237 | qtyHint.textContent = `Max ${max}`; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | const slot = selectedSellSlot(); |
| 242 | if (!slot) |
| 243 | { return; } |
| 244 | const max = Math.max(1, stackQuantity(slot.quantity)); |
| 245 | if (max < 4) |
| 246 | { return; } |
| 247 | if (qtyTitle) |
| 248 | { qtyTitle.textContent = "Sell quantity"; } |
| 249 | if (qtyConfirm) |
| 250 | { qtyConfirm.textContent = "Sell"; } |
| 251 | qtyInput.min = "1"; |
| 252 | qtyInput.max = String(max); |
| 253 | qtyInput.value = "1"; |
| 254 | qtyHint.textContent = `Max ${max}`; |
| 255 | } |
| 256 | qtyBackdrop.hidden = false; |
| 257 | qtyPanel.hidden = false; |
| 258 | suppressWorldClick(); |
| 259 | qtyInput.focus(); |
| 260 | qtyInput.select(); |
| 261 | } |
| 262 | |
| 263 | function closeQtyModal () |
| 264 | { |
| 265 | qtyMode = null; |
| 266 | qtyBackdrop.hidden = true; |
| 267 | qtyPanel.hidden = true; |
| 268 | } |
| 269 | |
| 270 | function confirmQtyModal () |
| 271 | { |
| 272 | if (qtyMode === "sell") |
| 273 | { |
| 274 | const slot = selectedSellSlot(); |
| 275 | if (!slot) |
| 276 | { |
| 277 | closeQtyModal(); |
| 278 | return; |
| 279 | } |
| 280 | const max = Math.max(1, stackQuantity(slot.quantity)); |
| 281 | const qty = Math.min(max, Math.max(1, Number(qtyInput.value) | 0 || 1)); |
| 282 | closeQtyModal(); |
| 283 | sellQuantity(qty); |
| 284 | return; |
| 285 | } |
| 286 | const entry = selectedEntry(); |
| 287 | if (!entry) |
| 288 | { |
| 289 | closeQtyModal(); |
| 290 | return; |
| 291 | } |
| 292 | const max = maxBuyable(entry); |
| 293 | const qty = Math.min(max, Math.max(1, Number(qtyInput.value) | 0 || 1)); |
| 294 | closeQtyModal(); |
| 295 | buy(qty); |
| 296 | } |
| 297 | |
| 298 | function selectedSellSlot () |
| 299 | { |
| 300 | if (selectedSellSlotIndex == null) |
| 301 | { return null; } |
| 302 | const slots = getInventorySlots?.() ?? []; |
| 303 | const slot = slots[selectedSellSlotIndex]; |
| 304 | if (!slot?.itemId || !canPlayerSellToNpc(slot)) |
| 305 | { return null; } |
| 306 | return slot; |
| 307 | } |
| 308 | |
| 309 | function sellQuantity (quantity) |
| 310 | { |
| 311 | if (!npcEntityId || selectedSellSlotIndex == null || !selectedSellSlot()) |
| 312 | { return; } |
| 313 | const slot = selectedSellSlot(); |
| 314 | const max = Math.max(1, stackQuantity(slot.quantity)); |
| 315 | const qty = Math.min(max, Math.max(1, quantity | 0 || 1)); |
| 316 | socket.send({ |
| 317 | type: MessageType.sellToShop, |
| 318 | npcEntityId, |
| 319 | slotIndex: selectedSellSlotIndex, |
| 320 | quantity: qty |
| 321 | }); |
| 322 | } |
| 323 | |
| 324 | function renderSellActions (slot) |
| 325 | { |
| 326 | if (!sellActions) |
| 327 | { return; } |
| 328 | sellActions.replaceChildren(); |
| 329 | const qty = Math.max(1, stackQuantity(slot.quantity)); |
| 330 | const addBtn = (label, onClick) => |
| 331 | { |
| 332 | const btn = document.createElement("button"); |
| 333 | btn.type = "button"; |
| 334 | btn.textContent = label; |
| 335 | btn.addEventListener("click", onClick); |
| 336 | sellActions.append(btn); |
| 337 | }; |
| 338 | if (qty <= 1) |
| 339 | { |
| 340 | addBtn("Sell", () => sellQuantity(1)); |
| 341 | return; |
| 342 | } |
| 343 | addBtn("Sell 1", () => sellQuantity(1)); |
| 344 | if (qty === 2) |
| 345 | { |
| 346 | addBtn("Sell all", () => sellQuantity(qty)); |
| 347 | return; |
| 348 | } |
| 349 | if (qty === 3) |
| 350 | { |
| 351 | addBtn("Sell 2", () => sellQuantity(2)); |
| 352 | addBtn("Sell all", () => sellQuantity(qty)); |
| 353 | return; |
| 354 | } |
| 355 | addBtn("Sell x", () => openQtyModal("sell")); |
| 356 | addBtn("Sell all", () => sellQuantity(qty)); |
| 357 | } |
| 358 | |
| 359 | function renderSellDetail () |
| 360 | { |
| 361 | if (!sellDetail || !sellPrice || !sellFee) |
| 362 | { return; } |
| 363 | const slot = selectedSellSlot(); |
| 364 | if (!slot) |
| 365 | { |
| 366 | sellDetail.hidden = true; |
| 367 | sellFee.hidden = true; |
| 368 | if (sellActions) |
| 369 | { sellActions.replaceChildren(); } |
| 370 | return; |
| 371 | } |
| 372 | const quote = resolveShopSellQuote(slot, { |
| 373 | quantity: Math.max(1, stackQuantity(slot.quantity)) |
| 374 | }); |
| 375 | const unitQuote = resolveShopSellQuote(slot, { quantity: 1 }); |
| 376 | if (!quote || !unitQuote) |
| 377 | { |
| 378 | sellDetail.hidden = true; |
| 379 | sellFee.hidden = true; |
| 380 | return; |
| 381 | } |
| 382 | sellDetail.hidden = false; |
| 383 | if (sellEmpty) |
| 384 | { sellEmpty.hidden = true; } |
| 385 | const name = itemDisplayName(slot); |
| 386 | if (sellName) |
| 387 | { sellName.textContent = name; } |
| 388 | if (sellIcon) |
| 389 | { applyItemIcon(sellIcon, slot, itemAssetPathForStack(slot)); } |
| 390 | sellPrice.textContent = quote.isCheque |
| 391 | ? `You receive ${formatQuantity(quote.payout)} coins` |
| 392 | : `${formatQuantity(unitQuote.unitPrice)} coins each`; |
| 393 | if (quote.isCheque) |
| 394 | { |
| 395 | sellFee.hidden = false; |
| 396 | sellFee.textContent = `${formatQuantity(quote.adminFee)} coins charged as administrative fee`; |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | sellFee.hidden = true; |
| 401 | sellFee.textContent = ""; |
| 402 | } |
| 403 | renderSellActions(slot); |
| 404 | } |
| 405 | |
| 406 | function renderSellPane () |
| 407 | { |
| 408 | if (!sellGrid) |
| 409 | { return; } |
| 410 | sellGrid.replaceChildren(); |
| 411 | if (!buysItems) |
| 412 | { return; } |
| 413 | const slots = getInventorySlots?.() ?? []; |
| 414 | const sellableIndexes = []; |
| 415 | slots.forEach((slot, slotIndex) => |
| 416 | { |
| 417 | if (slot?.itemId && canPlayerSellToNpc(slot)) |
| 418 | { sellableIndexes.push(slotIndex); } |
| 419 | }); |
| 420 | if (selectedSellSlotIndex != null && !sellableIndexes.includes(selectedSellSlotIndex)) |
| 421 | { selectedSellSlotIndex = null; } |
| 422 | if (!sellableIndexes.length) |
| 423 | { |
| 424 | selectedSellSlotIndex = null; |
| 425 | if (sellDetail) |
| 426 | { sellDetail.hidden = true; } |
| 427 | if (sellEmpty) |
| 428 | { sellEmpty.hidden = true; } |
| 429 | const note = document.createElement("p"); |
| 430 | note.className = "hint"; |
| 431 | note.textContent = `Nothing ${subjectPronoun(sex)} will buy in your bag.`; |
| 432 | sellGrid.append(note); |
| 433 | return; |
| 434 | } |
| 435 | if (sellEmpty) |
| 436 | { sellEmpty.hidden = true; } |
| 437 | if (selectedSellSlotIndex == null) |
| 438 | { |
| 439 | if (sellDetail) |
| 440 | { |
| 441 | sellDetail.hidden = false; |
| 442 | if (sellName) |
| 443 | { sellName.textContent = "Select an item"; } |
| 444 | if (sellIcon) |
| 445 | { |
| 446 | sellIcon.removeAttribute("src"); |
| 447 | sellIcon.alt = ""; |
| 448 | } |
| 449 | if (sellPrice) |
| 450 | { sellPrice.textContent = ""; } |
| 451 | if (sellFee) |
| 452 | { sellFee.hidden = true; } |
| 453 | if (sellActions) |
| 454 | { sellActions.replaceChildren(); } |
| 455 | } |
| 456 | } |
| 457 | slots.forEach((slot, slotIndex) => |
| 458 | { |
| 459 | const button = document.createElement("button"); |
| 460 | button.type = "button"; |
| 461 | button.className = "item-slot"; |
| 462 | const sellable = !!slot?.itemId && canPlayerSellToNpc(slot); |
| 463 | if (!slot) |
| 464 | { |
| 465 | button.disabled = true; |
| 466 | sellGrid.append(button); |
| 467 | return; |
| 468 | } |
| 469 | if (!sellable) |
| 470 | { button.classList.add("disabled"); } |
| 471 | if (slotIndex === selectedSellSlotIndex) |
| 472 | { button.classList.add("selected"); } |
| 473 | const img = document.createElement("img"); |
| 474 | img.alt = itemDisplayName(slot); |
| 475 | applyItemIcon(img, slot, itemAssetPathForStack(slot)); |
| 476 | button.append(img); |
| 477 | const qty = stackQuantity(slot.quantity); |
| 478 | if (qty > 1) |
| 479 | { |
| 480 | const count = document.createElement("span"); |
| 481 | count.className = "qty"; |
| 482 | count.textContent = formatQuantity(qty); |
| 483 | button.append(count); |
| 484 | } |
| 485 | button.title = itemDisplayName(slot); |
| 486 | button.addEventListener("click", () => |
| 487 | { |
| 488 | if (!sellable) |
| 489 | { return; } |
| 490 | selectedSellSlotIndex = slotIndex; |
| 491 | render(); |
| 492 | }); |
| 493 | sellGrid.append(button); |
| 494 | }); |
| 495 | if (selectedSellSlotIndex != null) |
| 496 | { renderSellDetail(); } |
| 497 | } |
| 498 | |
| 499 | function renderBuyPane () |
| 500 | { |
| 501 | grid.innerHTML = ""; |
| 502 | if (!stock.length) |
| 503 | { |
| 504 | grid.textContent = buysItems ? "No stock for sale right now." : "Nothing for sale."; |
| 505 | detail.hidden = true; |
| 506 | bagWarn.hidden = true; |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | for (const entry of stock) |
| 511 | { |
| 512 | const stack = { itemId: entry.itemId, instance: entry.instance ?? null }; |
| 513 | const name = entry.displayName ?? itemDisplayName(stack); |
| 514 | const btn = document.createElement("button"); |
| 515 | btn.type = "button"; |
| 516 | btn.className = "shop-cell"; |
| 517 | if (entry.listingId === selectedListingId) |
| 518 | { btn.classList.add("selected"); } |
| 519 | btn.title = name; |
| 520 | const img = document.createElement("img"); |
| 521 | img.alt = name; |
| 522 | applyItemIcon(img, stack, itemAssetPathForStack(stack)); |
| 523 | btn.append(img); |
| 524 | btn.addEventListener("click", () => |
| 525 | { |
| 526 | selectedListingId = entry.listingId; |
| 527 | render(); |
| 528 | }); |
| 529 | grid.append(btn); |
| 530 | } |
| 531 | |
| 532 | const entry = selectedEntry(); |
| 533 | if (!entry) |
| 534 | { |
| 535 | detail.hidden = true; |
| 536 | bagWarn.hidden = true; |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | detail.hidden = false; |
| 541 | const detailStack = { itemId: entry.itemId, instance: entry.instance ?? null }; |
| 542 | const name = entry.displayName ?? itemDisplayName(detailStack); |
| 543 | detailName.textContent = name; |
| 544 | detailPrice.textContent = `${formatQuantity(entry.unitPrice)} coins each`; |
| 545 | detailStock.textContent = entry.available == null |
| 546 | ? "In stock" |
| 547 | : `Stock: ${formatQuantity(entry.available)}`; |
| 548 | applyItemIcon(detailIcon, detailStack, itemAssetPathForStack(detailStack)); |
| 549 | |
| 550 | const max = maxBuyable(entry); |
| 551 | const setBuy = (id, qty) => |
| 552 | { |
| 553 | const el = document.getElementById(id); |
| 554 | if (!el) |
| 555 | { return; } |
| 556 | el.disabled = max < qty; |
| 557 | }; |
| 558 | setBuy("shop-buy-1", 1); |
| 559 | setBuy("shop-buy-5", 5); |
| 560 | setBuy("shop-buy-10", 10); |
| 561 | const buyX = document.getElementById("shop-buy-x"); |
| 562 | if (buyX) |
| 563 | { buyX.disabled = max < 1; } |
| 564 | |
| 565 | bagWarn.hidden = inventoryFits(entry.itemId, 1, entry.instance ?? null); |
| 566 | } |
| 567 | |
| 568 | function render () |
| 569 | { |
| 570 | const coins = getCoinCount?.() ?? 0; |
| 571 | coinsEl.textContent = `Coins: ${formatQuantity(coins)}`; |
| 572 | if (tabSell) |
| 573 | { tabSell.hidden = !buysItems; } |
| 574 | if (activeTab === "sell" && buysItems) |
| 575 | { renderSellPane(); } |
| 576 | else |
| 577 | { renderBuyPane(); } |
| 578 | } |
| 579 | |
| 580 | function handleShopOpen (message) |
| 581 | { |
| 582 | open(message); |
| 583 | } |
| 584 | |
| 585 | function handleShopClosed (message) |
| 586 | { |
| 587 | if ((message?.npcEntityId | 0) === npcEntityId) |
| 588 | { close({ notifyServer: false }); } |
| 589 | } |
| 590 | |
| 591 | function refresh () |
| 592 | { |
| 593 | if (npcEntityId != null && !panel.hidden) |
| 594 | { render(); } |
| 595 | } |
| 596 | |
| 597 | return { |
| 598 | handleShopOpen, |
| 599 | handleShopClosed, |
| 600 | refresh, |
| 601 | close, |
| 602 | isBlocking: () => !panel.hidden || !qtyPanel.hidden, |
| 603 | isOpen: () => !panel.hidden |
| 604 | }; |
| 605 | } |