| 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 | document.getElementById("bag-pigeon-bank").addEventListener("click", () => sendPigeon({ kind: "bank" })); |
| 54 | document.getElementById("bag-pigeon-player").addEventListener("click", () => |
| 55 | { |
| 56 | const firstName = document.getElementById("bag-pigeon-first").value.trim(); |
| 57 | const lastName = document.getElementById("bag-pigeon-last").value.trim(); |
| 58 | if (!firstName || !lastName) |
| 59 | { |
| 60 | setPigeonStatus("Enter a first and last name, or pick a friend."); |
| 61 | return; |
| 62 | } |
| 63 | sendPigeon({ kind: "player", firstName, lastName }); |
| 64 | }); |
| 65 | |
| 66 | function cargoPayload () |
| 67 | { |
| 68 | if (selected != null && bag.inventory?.[selected]) |
| 69 | { |
| 70 | return { kind: "item", slotIndex: selected }; |
| 71 | } |
| 72 | const amount = Math.max(1, Number(document.getElementById("bag-pigeon-coins").value) | 0); |
| 73 | return { kind: "money", amount }; |
| 74 | } |
| 75 | |
| 76 | function sendPigeon (destination) |
| 77 | { |
| 78 | const cargo = cargoPayload(); |
| 79 | if (cargo.kind === "money" && (bag.currency | 0) < cargo.amount) |
| 80 | { |
| 81 | setPigeonStatus("Not enough coins."); |
| 82 | return; |
| 83 | } |
| 84 | if (cargo.kind === "item" && selected == null) |
| 85 | { |
| 86 | setPigeonStatus("Select an item slot, or clear selection to send coins."); |
| 87 | return; |
| 88 | } |
| 89 | socket.send({ type: MessageType.sendPigeon, destination, cargo }); |
| 90 | setPigeonStatus("Pigeon dispatched…"); |
| 91 | } |
| 92 | |
| 93 | function setPigeonStatus (text) |
| 94 | { |
| 95 | document.getElementById("bag-pigeon-status").textContent = text; |
| 96 | } |
| 97 | |
| 98 | function render () |
| 99 | { |
| 100 | coins.textContent = `${bag.currency ?? 0} coins`; |
| 101 | bagGrid.innerHTML = ""; |
| 102 | (bag.inventory ?? []).forEach((slot, index) => |
| 103 | { |
| 104 | const button = document.createElement("button"); |
| 105 | button.className = `item-slot${selected === index ? " selected" : ""}`; |
| 106 | button.title = tooltip(slot); |
| 107 | if (slot) |
| 108 | { |
| 109 | const def = itemDefinition(slot.itemId); |
| 110 | const img = document.createElement("img"); |
| 111 | img.src = itemAssetPath(slot.itemId); |
| 112 | img.alt = def?.displayName ?? ""; |
| 113 | button.append(img); |
| 114 | if (slot.quantity > 1) |
| 115 | { |
| 116 | const count = document.createElement("span"); |
| 117 | count.className = "qty"; |
| 118 | count.textContent = String(slot.quantity); |
| 119 | button.append(count); |
| 120 | } |
| 121 | } |
| 122 | button.addEventListener("click", () => |
| 123 | { |
| 124 | selected = index; |
| 125 | const def = slot ? itemDefinition(slot.itemId) : null; |
| 126 | hint.textContent = def ? `${def.displayName}: ${def.description}` : "Empty slot."; |
| 127 | render(); |
| 128 | }); |
| 129 | bagGrid.append(button); |
| 130 | }); |
| 131 | equipGrid.innerHTML = ""; |
| 132 | for (const [slot, label] of Object.entries(EQUIP_LABELS)) |
| 133 | { |
| 134 | const worn = bag.equipment?.slots?.[slot]; |
| 135 | const button = document.createElement("button"); |
| 136 | button.className = "item-slot"; |
| 137 | button.title = worn ? tooltip(worn) : label; |
| 138 | button.textContent = worn ? (itemDefinition(worn.itemId)?.displayName ?? label) : label; |
| 139 | button.addEventListener("click", () => |
| 140 | { |
| 141 | if (worn) |
| 142 | { socket.send({ type: MessageType.unequipItem, equipSlot: slot }); } |
| 143 | }); |
| 144 | equipGrid.append(button); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | function renderVault (grid, slots) |
| 149 | { |
| 150 | grid.innerHTML = ""; |
| 151 | (slots ?? []).forEach((slot, index) => |
| 152 | { |
| 153 | const button = document.createElement("button"); |
| 154 | button.className = "item-slot"; |
| 155 | button.title = tooltip(slot); |
| 156 | button.textContent = slot |
| 157 | ? `${itemDefinition(slot.itemId)?.displayName ?? "Item"}${slot.quantity > 1 ? ` ×${slot.quantity}` : ""}` |
| 158 | : ""; |
| 159 | button.addEventListener("click", () => |
| 160 | { |
| 161 | if (!slot) |
| 162 | { return; } |
| 163 | socket.send({ type: MessageType.bankWithdraw, slotIndex: index }); |
| 164 | }); |
| 165 | grid.append(button); |
| 166 | }); |
| 167 | } |
| 168 | |
| 169 | function tooltip (slot) |
| 170 | { |
| 171 | if (!slot) |
| 172 | { return ""; } |
| 173 | const def = itemDefinition(slot.itemId); |
| 174 | return `${def?.displayName ?? "Item"}${slot.quantity > 1 ? ` ×${slot.quantity}` : ""}`; |
| 175 | } |
| 176 | |
| 177 | return { |
| 178 | isOpen () |
| 179 | { |
| 180 | return !bagPanel.hidden || bankOpen; |
| 181 | }, |
| 182 | handleInventory (message) |
| 183 | { |
| 184 | bag = { ...bag, ...message }; |
| 185 | render(); |
| 186 | }, |
| 187 | handleBank (message) |
| 188 | { |
| 189 | bankOpen = true; |
| 190 | bankPanel.hidden = false; |
| 191 | bag.vault = message.vault; |
| 192 | bag.vaultCurrency = message.vaultCurrency; |
| 193 | vaultCoins.textContent = `${message.vaultCurrency ?? 0} coins in vault · you carry ${message.currency ?? 0}`; |
| 194 | inheritCoins.textContent = `${message.inheritance?.currency ?? 0} coins waiting`; |
| 195 | renderVault(vaultGrid, message.vault); |
| 196 | renderVault(inheritGrid, message.inheritance?.slots ?? []); |
| 197 | inheritGrid.querySelectorAll("button").forEach((button) => |
| 198 | { |
| 199 | button.onclick = null; |
| 200 | }); |
| 201 | render(); |
| 202 | }, |
| 203 | depositSelected () |
| 204 | { |
| 205 | if (selected == null) |
| 206 | { return; } |
| 207 | socket.send({ type: MessageType.bankDeposit, slotIndex: selected }); |
| 208 | }, |
| 209 | fillPigeonRecipient (firstName, lastName) |
| 210 | { |
| 211 | document.getElementById("bag-pigeon-first").value = firstName ?? ""; |
| 212 | document.getElementById("bag-pigeon-last").value = lastName ?? ""; |
| 213 | bagPanel.hidden = false; |
| 214 | setPigeonStatus(`Recipient set to ${firstName} ${lastName}.`); |
| 215 | }, |
| 216 | handlePigeonDispatched () |
| 217 | { |
| 218 | setPigeonStatus("Pigeon is in flight."); |
| 219 | }, |
| 220 | handlePigeonDelivered (message) |
| 221 | { |
| 222 | const kind = message.destinationKind === "bank" ? "bank vault" : "recipient"; |
| 223 | setPigeonStatus(`Pigeon delivered to ${kind}.`); |
| 224 | }, |
| 225 | reset () |
| 226 | { |
| 227 | bagPanel.hidden = true; |
| 228 | bankPanel.hidden = true; |
| 229 | bankOpen = false; |
| 230 | selected = null; |
| 231 | setPigeonStatus(""); |
| 232 | } |
| 233 | }; |
| 234 | } |