| 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 initShop ({ |
| 10 | socket, |
| 11 | MessageType, |
| 12 | getCoinCount, |
| 13 | onError |
| 14 | }) |
| 15 | { |
| 16 | const backdrop = document.getElementById("shop-backdrop"); |
| 17 | const panel = document.getElementById("shop-panel"); |
| 18 | const title = document.getElementById("shop-title"); |
| 19 | const list = document.getElementById("shop-stock"); |
| 20 | const statusEl = document.getElementById("shop-status"); |
| 21 | let npcEntityId = null; |
| 22 | let stock = []; |
| 23 | |
| 24 | document.getElementById("shop-close")?.addEventListener("click", () => close()); |
| 25 | backdrop?.addEventListener("click", () => close()); |
| 26 | |
| 27 | function open (payload) |
| 28 | { |
| 29 | npcEntityId = payload.npcEntityId | 0; |
| 30 | title.textContent = payload.displayName || "Shop"; |
| 31 | stock = Array.isArray(payload.stock) ? payload.stock : []; |
| 32 | statusEl.textContent = ""; |
| 33 | render(); |
| 34 | backdrop.hidden = false; |
| 35 | panel.hidden = false; |
| 36 | } |
| 37 | |
| 38 | function close () |
| 39 | { |
| 40 | backdrop.hidden = true; |
| 41 | panel.hidden = true; |
| 42 | npcEntityId = null; |
| 43 | stock = []; |
| 44 | } |
| 45 | |
| 46 | function render () |
| 47 | { |
| 48 | list.innerHTML = ""; |
| 49 | if (!stock.length) |
| 50 | { |
| 51 | list.textContent = "Nothing for sale."; |
| 52 | return; |
| 53 | } |
| 54 | const coins = getCoinCount?.() ?? 0; |
| 55 | for (const entry of stock) |
| 56 | { |
| 57 | const def = itemDefinition(entry.itemId); |
| 58 | const row = document.createElement("div"); |
| 59 | row.className = "shop-row"; |
| 60 | const img = document.createElement("img"); |
| 61 | img.alt = entry.displayName ?? def?.displayName ?? "item"; |
| 62 | applyItemIcon(img, { itemId: entry.itemId }, itemAssetPathForStack({ itemId: entry.itemId })); |
| 63 | const meta = document.createElement("div"); |
| 64 | meta.className = "shop-row-meta"; |
| 65 | const name = document.createElement("strong"); |
| 66 | name.textContent = entry.displayName ?? itemDisplayName({ itemId: entry.itemId }); |
| 67 | const price = document.createElement("span"); |
| 68 | price.className = "hint"; |
| 69 | price.textContent = `${formatQuantity(entry.unitPrice)} coins`; |
| 70 | meta.append(name, price); |
| 71 | const buy = document.createElement("button"); |
| 72 | buy.type = "button"; |
| 73 | buy.textContent = "Buy"; |
| 74 | buy.disabled = coins < (entry.unitPrice | 0); |
| 75 | buy.addEventListener("click", () => |
| 76 | { |
| 77 | if (!npcEntityId) |
| 78 | { return; } |
| 79 | socket.send({ |
| 80 | type: MessageType.buyFromShop, |
| 81 | npcEntityId, |
| 82 | itemId: entry.itemId, |
| 83 | quantity: 1 |
| 84 | }); |
| 85 | }); |
| 86 | row.append(img, meta, buy); |
| 87 | list.append(row); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | function handleShopOpen (message) |
| 92 | { |
| 93 | open(message); |
| 94 | } |
| 95 | |
| 96 | function refresh () |
| 97 | { |
| 98 | if (npcEntityId != null && !panel.hidden) |
| 99 | { render(); } |
| 100 | } |
| 101 | |
| 102 | return { |
| 103 | handleShopOpen, |
| 104 | refresh, |
| 105 | close, |
| 106 | isOpen: () => !panel.hidden |
| 107 | }; |
| 108 | } |