| 1 | import { itemDefinition, itemAssetPath, EQUIPMENT_SLOT } from "/shared/definitions/items.js"; |
| 2 | |
| 3 | const EQUIP_LABELS = { |
| 4 | [EQUIPMENT_SLOT.head]: "Head", |
| 5 | [EQUIPMENT_SLOT.chest]: "Chest", |
| 6 | [EQUIPMENT_SLOT.legs]: "Legs", |
| 7 | [EQUIPMENT_SLOT.boots]: "Boots", |
| 8 | [EQUIPMENT_SLOT.weapon]: "Weapon", |
| 9 | [EQUIPMENT_SLOT.offhand]: "Off-hand" |
| 10 | }; |
| 11 | |
| 12 | export function initInventory ({ socket, MessageType, game }) |
| 13 | { |
| 14 | const bagPanel = document.getElementById("bag-panel"); |
| 15 | const bagGrid = document.getElementById("bag-grid"); |
| 16 | const equipGrid = document.getElementById("equip-grid"); |
| 17 | const coins = document.getElementById("bag-coins"); |
| 18 | const hint = document.getElementById("bag-hint"); |
| 19 | const bankPanel = document.getElementById("bank-panel"); |
| 20 | const vaultGrid = document.getElementById("vault-grid"); |
| 21 | const vaultCoins = document.getElementById("vault-coins"); |
| 22 | const inheritGrid = document.getElementById("inherit-grid"); |
| 23 | const inheritCoins = document.getElementById("inherit-coins"); |
| 24 | let bag = { inventory: [], equipment: { slots: {} }, currency: 0, vault: [], vaultCurrency: 0 }; |
| 25 | let selected = null; |
| 26 | let bankOpen = false; |
| 27 | |
| 28 | document.getElementById("bag-toggle").addEventListener("click", () => |
| 29 | { |
| 30 | bagPanel.hidden = !bagPanel.hidden; |
| 31 | }); |
| 32 | document.getElementById("bag-drop").addEventListener("click", () => |
| 33 | { |
| 34 | if (selected == null) |
| 35 | { return; } |
| 36 | socket.send({ type: MessageType.dropItem, slotIndex: selected }); |
| 37 | }); |
| 38 | document.getElementById("bag-equip").addEventListener("click", () => |
| 39 | { |
| 40 | if (selected == null) |
| 41 | { return; } |
| 42 | socket.send({ type: MessageType.equipItem, slotIndex: selected }); |
| 43 | }); |
| 44 | document.getElementById("bank-close").addEventListener("click", () => |
| 45 | { |
| 46 | bankOpen = false; |
| 47 | bankPanel.hidden = true; |
| 48 | }); |
| 49 | document.getElementById("bank-claim").addEventListener("click", () => |
| 50 | { |
| 51 | socket.send({ type: MessageType.claimInheritance }); |
| 52 | }); |
| 53 | |
| 54 | function render () |
| 55 | { |
| 56 | coins.textContent = `${bag.currency ?? 0} coins`; |
| 57 | bagGrid.innerHTML = ""; |
| 58 | (bag.inventory ?? []).forEach((slot, index) => |
| 59 | { |
| 60 | const button = document.createElement("button"); |
| 61 | button.className = `item-slot${selected === index ? " selected" : ""}`; |
| 62 | button.title = tooltip(slot); |
| 63 | if (slot) |
| 64 | { |
| 65 | const def = itemDefinition(slot.itemId); |
| 66 | const img = document.createElement("img"); |
| 67 | img.src = itemAssetPath(slot.itemId); |
| 68 | img.alt = def?.displayName ?? ""; |
| 69 | button.append(img); |
| 70 | if (slot.quantity > 1) |
| 71 | { |
| 72 | const count = document.createElement("span"); |
| 73 | count.className = "qty"; |
| 74 | count.textContent = String(slot.quantity); |
| 75 | button.append(count); |
| 76 | } |
| 77 | } |
| 78 | button.addEventListener("click", () => |
| 79 | { |
| 80 | selected = index; |
| 81 | const def = slot ? itemDefinition(slot.itemId) : null; |
| 82 | hint.textContent = def ? `${def.displayName}: ${def.description}` : "Empty slot."; |
| 83 | render(); |
| 84 | }); |
| 85 | bagGrid.append(button); |
| 86 | }); |
| 87 | equipGrid.innerHTML = ""; |
| 88 | for (const [slot, label] of Object.entries(EQUIP_LABELS)) |
| 89 | { |
| 90 | const worn = bag.equipment?.slots?.[slot]; |
| 91 | const button = document.createElement("button"); |
| 92 | button.className = "item-slot"; |
| 93 | button.title = worn ? tooltip(worn) : label; |
| 94 | button.textContent = worn ? (itemDefinition(worn.itemId)?.displayName ?? label) : label; |
| 95 | button.addEventListener("click", () => |
| 96 | { |
| 97 | if (worn) |
| 98 | { socket.send({ type: MessageType.unequipItem, equipSlot: slot }); } |
| 99 | }); |
| 100 | equipGrid.append(button); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | function renderVault (grid, slots) |
| 105 | { |
| 106 | grid.innerHTML = ""; |
| 107 | (slots ?? []).forEach((slot, index) => |
| 108 | { |
| 109 | const button = document.createElement("button"); |
| 110 | button.className = "item-slot"; |
| 111 | button.title = tooltip(slot); |
| 112 | button.textContent = slot |
| 113 | ? `${itemDefinition(slot.itemId)?.displayName ?? "Item"}${slot.quantity > 1 ? ` ×${slot.quantity}` : ""}` |
| 114 | : ""; |
| 115 | button.addEventListener("click", () => |
| 116 | { |
| 117 | if (!slot) |
| 118 | { return; } |
| 119 | socket.send({ type: MessageType.bankWithdraw, slotIndex: index }); |
| 120 | }); |
| 121 | grid.append(button); |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | function tooltip (slot) |
| 126 | { |
| 127 | if (!slot) |
| 128 | { return ""; } |
| 129 | const def = itemDefinition(slot.itemId); |
| 130 | return `${def?.displayName ?? "Item"}${slot.quantity > 1 ? ` ×${slot.quantity}` : ""}`; |
| 131 | } |
| 132 | |
| 133 | return { |
| 134 | isOpen () |
| 135 | { |
| 136 | return !bagPanel.hidden || bankOpen; |
| 137 | }, |
| 138 | handleInventory (message) |
| 139 | { |
| 140 | bag = { ...bag, ...message }; |
| 141 | render(); |
| 142 | }, |
| 143 | handleBank (message) |
| 144 | { |
| 145 | bankOpen = true; |
| 146 | bankPanel.hidden = false; |
| 147 | bag.vault = message.vault; |
| 148 | bag.vaultCurrency = message.vaultCurrency; |
| 149 | vaultCoins.textContent = `${message.vaultCurrency ?? 0} coins in vault · you carry ${message.currency ?? 0}`; |
| 150 | inheritCoins.textContent = `${message.inheritance?.currency ?? 0} coins waiting`; |
| 151 | renderVault(vaultGrid, message.vault); |
| 152 | renderVault(inheritGrid, message.inheritance?.slots ?? []); |
| 153 | inheritGrid.querySelectorAll("button").forEach((button) => |
| 154 | { |
| 155 | button.onclick = null; |
| 156 | }); |
| 157 | render(); |
| 158 | }, |
| 159 | depositSelected () |
| 160 | { |
| 161 | if (selected == null) |
| 162 | { return; } |
| 163 | socket.send({ type: MessageType.bankDeposit, slotIndex: selected }); |
| 164 | }, |
| 165 | reset () |
| 166 | { |
| 167 | bagPanel.hidden = true; |
| 168 | bankPanel.hidden = true; |
| 169 | bankOpen = false; |
| 170 | selected = null; |
| 171 | } |
| 172 | }; |
| 173 | } |