| 1 | import { |
| 2 | itemDefinition, |
| 3 | itemDescription, |
| 4 | itemAssetPathForStack, |
| 5 | ItemId, |
| 6 | EQUIPMENT_SLOT, |
| 7 | LETTER_MAX_BODY_CHARS |
| 8 | } from "/shared/definitions/items.js"; |
| 9 | |
| 10 | const EQUIP_LABELS = { |
| 11 | [EQUIPMENT_SLOT.head]: "Head", |
| 12 | [EQUIPMENT_SLOT.chest]: "Chest", |
| 13 | [EQUIPMENT_SLOT.legs]: "Legs", |
| 14 | [EQUIPMENT_SLOT.boots]: "Boots", |
| 15 | [EQUIPMENT_SLOT.weapon]: "Weapon", |
| 16 | [EQUIPMENT_SLOT.offhand]: "Off-hand" |
| 17 | }; |
| 18 | |
| 19 | export function initInventory ({ |
| 20 | socket, |
| 21 | MessageType, |
| 22 | contextMenu, |
| 23 | resolveSpriteUrl, |
| 24 | getFriends, |
| 25 | getLocalName |
| 26 | }) |
| 27 | { |
| 28 | const bagPanel = document.getElementById("bag-panel"); |
| 29 | const bagGrid = document.getElementById("bag-grid"); |
| 30 | const equipGrid = document.getElementById("equip-grid"); |
| 31 | const coins = document.getElementById("bag-coins"); |
| 32 | const hint = document.getElementById("bag-hint"); |
| 33 | const bankPanel = document.getElementById("bank-panel"); |
| 34 | const vaultGrid = document.getElementById("vault-grid"); |
| 35 | const vaultCoins = document.getElementById("vault-coins"); |
| 36 | const inheritGrid = document.getElementById("inherit-grid"); |
| 37 | const inheritCoins = document.getElementById("inherit-coins"); |
| 38 | |
| 39 | const sendPanel = document.getElementById("send-panel"); |
| 40 | const sendBackdrop = document.getElementById("send-backdrop"); |
| 41 | const sendTargetPanel = document.getElementById("send-target-panel"); |
| 42 | const sendTargetList = document.getElementById("send-target-list"); |
| 43 | const sendSearchResult = document.getElementById("send-search-result"); |
| 44 | const letterPanel = document.getElementById("letter-panel"); |
| 45 | const letterBackdrop = document.getElementById("letter-backdrop"); |
| 46 | const letterBody = document.getElementById("letter-body"); |
| 47 | const letterSignature = document.getElementById("letter-signature"); |
| 48 | const letterSave = document.getElementById("letter-save"); |
| 49 | const letterTitle = document.getElementById("letter-title"); |
| 50 | const letterStatus = document.getElementById("letter-status"); |
| 51 | |
| 52 | let bag = { inventory: [], equipment: { slots: {} }, currency: 0, vault: [], vaultCurrency: 0 }; |
| 53 | let selected = null; |
| 54 | let bankOpen = false; |
| 55 | let sendState = null; |
| 56 | let letterState = null; |
| 57 | let pendingSearch = false; |
| 58 | let localCharacterId = null; |
| 59 | |
| 60 | document.getElementById("bag-toggle").addEventListener("click", () => |
| 61 | { |
| 62 | bagPanel.hidden = !bagPanel.hidden; |
| 63 | }); |
| 64 | document.getElementById("bag-drop").addEventListener("click", () => |
| 65 | { |
| 66 | if (selected == null) |
| 67 | { return; } |
| 68 | socket.send({ type: MessageType.dropItem, slotIndex: selected }); |
| 69 | }); |
| 70 | document.getElementById("bag-equip").addEventListener("click", () => |
| 71 | { |
| 72 | if (selected == null) |
| 73 | { return; } |
| 74 | socket.send({ type: MessageType.equipItem, slotIndex: selected }); |
| 75 | }); |
| 76 | document.getElementById("bank-close").addEventListener("click", () => |
| 77 | { |
| 78 | bankOpen = false; |
| 79 | bankPanel.hidden = true; |
| 80 | }); |
| 81 | document.getElementById("bank-claim").addEventListener("click", () => |
| 82 | { |
| 83 | socket.send({ type: MessageType.claimInheritance }); |
| 84 | }); |
| 85 | |
| 86 | document.getElementById("send-close").addEventListener("click", () => closeSend()); |
| 87 | sendBackdrop.addEventListener("click", () => closeSend()); |
| 88 | document.getElementById("send-pick-target").addEventListener("click", () => openTargetPicker()); |
| 89 | document.getElementById("send-target-close").addEventListener("click", () => |
| 90 | { |
| 91 | sendTargetPanel.hidden = true; |
| 92 | }); |
| 93 | document.getElementById("send-confirm").addEventListener("click", () => confirmSend()); |
| 94 | document.getElementById("send-search").addEventListener("click", () => |
| 95 | { |
| 96 | const firstName = document.getElementById("send-search-first").value.trim(); |
| 97 | const lastName = document.getElementById("send-search-last").value.trim(); |
| 98 | if (!firstName || !lastName) |
| 99 | { |
| 100 | sendSearchResult.textContent = "Enter a first and last name."; |
| 101 | return; |
| 102 | } |
| 103 | pendingSearch = true; |
| 104 | sendSearchResult.textContent = "Searching…"; |
| 105 | socket.send({ type: MessageType.searchCharacter, firstName, lastName }); |
| 106 | }); |
| 107 | |
| 108 | document.getElementById("letter-close").addEventListener("click", () => closeLetter()); |
| 109 | letterBackdrop.addEventListener("click", () => closeLetter()); |
| 110 | letterSave.addEventListener("click", () => |
| 111 | { |
| 112 | if (!letterState || letterState.readOnly) |
| 113 | { return; } |
| 114 | const body = letterBody.value; |
| 115 | socket.send({ |
| 116 | type: letterState.mode === "edit" ? MessageType.editLetter : MessageType.writeLetter, |
| 117 | slotIndex: letterState.slotIndex, |
| 118 | body |
| 119 | }); |
| 120 | letterStatus.textContent = "Saving…"; |
| 121 | }); |
| 122 | letterBody.addEventListener("input", () => |
| 123 | { |
| 124 | if (letterState?.readOnly) |
| 125 | { return; } |
| 126 | fitLetterBody(); |
| 127 | refreshLetterSignature(); |
| 128 | }); |
| 129 | |
| 130 | function render () |
| 131 | { |
| 132 | coins.textContent = `${bag.currency ?? 0} coins`; |
| 133 | bagGrid.innerHTML = ""; |
| 134 | (bag.inventory ?? []).forEach((slot, index) => |
| 135 | { |
| 136 | const button = document.createElement("button"); |
| 137 | button.className = `item-slot${selected === index ? " selected" : ""}`; |
| 138 | button.title = tooltip(slot); |
| 139 | if (slot) |
| 140 | { |
| 141 | const def = itemDefinition(slot.itemId); |
| 142 | const img = document.createElement("img"); |
| 143 | img.src = itemAssetPathForStack(slot); |
| 144 | img.alt = def?.displayName ?? ""; |
| 145 | button.append(img); |
| 146 | if (slot.quantity > 1) |
| 147 | { |
| 148 | const count = document.createElement("span"); |
| 149 | count.className = "qty"; |
| 150 | count.textContent = String(slot.quantity); |
| 151 | button.append(count); |
| 152 | } |
| 153 | } |
| 154 | button.addEventListener("click", () => |
| 155 | { |
| 156 | selected = index; |
| 157 | hint.textContent = slot |
| 158 | ? `${itemDefinition(slot.itemId)?.displayName ?? "Item"}: ${itemDescription(slot, { localCharacterId })}` |
| 159 | : "Empty slot."; |
| 160 | render(); |
| 161 | }); |
| 162 | button.addEventListener("contextmenu", (event) => |
| 163 | { |
| 164 | event.preventDefault(); |
| 165 | event.stopPropagation(); |
| 166 | if (!slot) |
| 167 | { return; } |
| 168 | selected = index; |
| 169 | render(); |
| 170 | showItemMenu(event.clientX, event.clientY, index, slot); |
| 171 | }); |
| 172 | bagGrid.append(button); |
| 173 | }); |
| 174 | equipGrid.innerHTML = ""; |
| 175 | for (const [slot, label] of Object.entries(EQUIP_LABELS)) |
| 176 | { |
| 177 | const worn = bag.equipment?.slots?.[slot]; |
| 178 | const button = document.createElement("button"); |
| 179 | button.className = "item-slot"; |
| 180 | button.title = worn ? tooltip(worn) : label; |
| 181 | if (worn) |
| 182 | { |
| 183 | const def = itemDefinition(worn.itemId); |
| 184 | const img = document.createElement("img"); |
| 185 | img.src = itemAssetPathForStack(worn); |
| 186 | img.alt = def?.displayName ?? label; |
| 187 | button.append(img); |
| 188 | } |
| 189 | button.addEventListener("click", () => |
| 190 | { |
| 191 | if (worn) |
| 192 | { socket.send({ type: MessageType.unequipItem, equipSlot: slot }); } |
| 193 | }); |
| 194 | equipGrid.append(button); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | function showItemMenu (x, y, slotIndex, slot) |
| 199 | { |
| 200 | const def = itemDefinition(slot.itemId); |
| 201 | const items = []; |
| 202 | const isOwnLetter = slot.itemId === ItemId.letter |
| 203 | && slot.instance?.kind === "letter" |
| 204 | && Number(slot.instance.authorCharacterId) === Number(localCharacterId); |
| 205 | const canSend = slot.itemId !== ItemId.letter || isOwnLetter; |
| 206 | |
| 207 | if (slot.itemId === ItemId.paper) |
| 208 | { |
| 209 | items.push({ |
| 210 | label: "Write letter", |
| 211 | onSelect: () => openLetter({ mode: "write", slotIndex }) |
| 212 | }); |
| 213 | } |
| 214 | if (slot.itemId === ItemId.letter && slot.instance?.kind === "letter") |
| 215 | { |
| 216 | items.push({ |
| 217 | label: "Read", |
| 218 | onSelect: () => |
| 219 | { |
| 220 | if (slot.instance?.opened === false) |
| 221 | { socket.send({ type: MessageType.openLetter, slotIndex }); } |
| 222 | openLetter({ |
| 223 | mode: "read", |
| 224 | slotIndex, |
| 225 | instance: { ...slot.instance, opened: true } |
| 226 | }); |
| 227 | } |
| 228 | }); |
| 229 | if (isOwnLetter) |
| 230 | { |
| 231 | items.push({ |
| 232 | label: "Edit letter", |
| 233 | onSelect: () => openLetter({ |
| 234 | mode: "edit", |
| 235 | slotIndex, |
| 236 | instance: slot.instance |
| 237 | }) |
| 238 | }); |
| 239 | } |
| 240 | } |
| 241 | if (def?.equipment?.slot) |
| 242 | { |
| 243 | items.push({ |
| 244 | label: "Equip", |
| 245 | onSelect: () => socket.send({ type: MessageType.equipItem, slotIndex }) |
| 246 | }); |
| 247 | } |
| 248 | if (canSend) |
| 249 | { |
| 250 | items.push({ |
| 251 | label: "Send", |
| 252 | onSelect: () => openSend(slotIndex, slot) |
| 253 | }); |
| 254 | } |
| 255 | items.push({ |
| 256 | label: "Drop", |
| 257 | onSelect: () => socket.send({ type: MessageType.dropItem, slotIndex }) |
| 258 | }); |
| 259 | contextMenu?.show(x, y, items); |
| 260 | } |
| 261 | |
| 262 | function openSend (slotIndex, slot) |
| 263 | { |
| 264 | const def = itemDefinition(slot.itemId); |
| 265 | sendState = { |
| 266 | slotIndex, |
| 267 | itemId: slot.itemId, |
| 268 | maxQty: slot.quantity | 0, |
| 269 | destination: null |
| 270 | }; |
| 271 | document.getElementById("send-item-img").src = itemAssetPathForStack(slot); |
| 272 | document.getElementById("send-item-name").textContent = def?.displayName ?? "Item"; |
| 273 | const qtyInput = document.getElementById("send-qty"); |
| 274 | qtyInput.value = "1"; |
| 275 | qtyInput.max = String(sendState.maxQty); |
| 276 | qtyInput.disabled = sendState.maxQty <= 1; |
| 277 | document.getElementById("send-target-label").textContent = "No target selected."; |
| 278 | document.getElementById("send-confirm").disabled = true; |
| 279 | document.getElementById("send-status").textContent = ""; |
| 280 | sendPanel.hidden = false; |
| 281 | sendBackdrop.hidden = false; |
| 282 | sendTargetPanel.hidden = true; |
| 283 | } |
| 284 | |
| 285 | function closeSend () |
| 286 | { |
| 287 | sendState = null; |
| 288 | sendPanel.hidden = true; |
| 289 | sendBackdrop.hidden = true; |
| 290 | sendTargetPanel.hidden = true; |
| 291 | } |
| 292 | |
| 293 | function openTargetPicker () |
| 294 | { |
| 295 | if (!sendState) |
| 296 | { return; } |
| 297 | sendTargetPanel.hidden = false; |
| 298 | sendSearchResult.replaceChildren(); |
| 299 | document.getElementById("send-search-first").value = ""; |
| 300 | document.getElementById("send-search-last").value = ""; |
| 301 | renderTargetList(); |
| 302 | } |
| 303 | |
| 304 | function renderTargetList () |
| 305 | { |
| 306 | sendTargetList.replaceChildren(); |
| 307 | sendTargetList.append(targetRow({ |
| 308 | kind: "bank", |
| 309 | label: "Bank", |
| 310 | hint: "Nearest bank → your vault" |
| 311 | })); |
| 312 | const friends = getFriends?.() ?? []; |
| 313 | if (!friends.length) |
| 314 | { |
| 315 | const empty = document.createElement("p"); |
| 316 | empty.className = "hint"; |
| 317 | empty.textContent = "No friends yet."; |
| 318 | sendTargetList.append(empty); |
| 319 | return; |
| 320 | } |
| 321 | for (const friend of friends) |
| 322 | { sendTargetList.append(characterTargetRow(friend)); } |
| 323 | } |
| 324 | |
| 325 | function targetRow ({ kind, label, hint, onPick }) |
| 326 | { |
| 327 | const row = document.createElement("button"); |
| 328 | row.type = "button"; |
| 329 | row.className = "send-target-row"; |
| 330 | row.innerHTML = `<strong>${label}</strong>${hint ? `<span class="hint">${hint}</span>` : ""}`; |
| 331 | row.addEventListener("click", () => |
| 332 | { |
| 333 | if (onPick) |
| 334 | { onPick(); } |
| 335 | else |
| 336 | { pickTarget({ kind: "bank" }, "Bank"); } |
| 337 | }); |
| 338 | return row; |
| 339 | } |
| 340 | |
| 341 | function characterTargetRow (entry) |
| 342 | { |
| 343 | const row = document.createElement("button"); |
| 344 | row.type = "button"; |
| 345 | row.className = "send-target-row"; |
| 346 | const preview = characterPreview(entry.layers); |
| 347 | const text = document.createElement("div"); |
| 348 | text.className = "send-target-text"; |
| 349 | text.innerHTML = `<strong>${entry.firstName} ${entry.lastName}</strong>`; |
| 350 | row.append(preview, text); |
| 351 | row.addEventListener("click", () => |
| 352 | { |
| 353 | pickTarget({ |
| 354 | kind: "player", |
| 355 | firstName: entry.firstName, |
| 356 | lastName: entry.lastName, |
| 357 | characterId: entry.characterId |
| 358 | }, `${entry.firstName} ${entry.lastName}`); |
| 359 | }); |
| 360 | return row; |
| 361 | } |
| 362 | |
| 363 | function characterPreview (layers) |
| 364 | { |
| 365 | const wrap = document.createElement("div"); |
| 366 | wrap.className = "char-preview"; |
| 367 | for (const layer of layers ?? []) |
| 368 | { |
| 369 | const url = resolveSpriteUrl?.(layer.sprite); |
| 370 | if (!url) |
| 371 | { continue; } |
| 372 | const img = document.createElement("img"); |
| 373 | img.src = url; |
| 374 | img.alt = ""; |
| 375 | wrap.append(img); |
| 376 | } |
| 377 | return wrap; |
| 378 | } |
| 379 | |
| 380 | function pickTarget (destination, label) |
| 381 | { |
| 382 | if (!sendState) |
| 383 | { return; } |
| 384 | sendState.destination = destination; |
| 385 | document.getElementById("send-target-label").textContent = `Target: ${label}`; |
| 386 | document.getElementById("send-confirm").disabled = false; |
| 387 | sendTargetPanel.hidden = true; |
| 388 | } |
| 389 | |
| 390 | function confirmSend () |
| 391 | { |
| 392 | if (!sendState?.destination) |
| 393 | { return; } |
| 394 | const quantity = Math.max(1, Math.min(sendState.maxQty, Number(document.getElementById("send-qty").value) | 0)); |
| 395 | socket.send({ |
| 396 | type: MessageType.sendPigeon, |
| 397 | destination: sendState.destination, |
| 398 | cargo: { |
| 399 | kind: "item", |
| 400 | slotIndex: sendState.slotIndex, |
| 401 | quantity |
| 402 | } |
| 403 | }); |
| 404 | document.getElementById("send-status").textContent = "Pigeon dispatched…"; |
| 405 | } |
| 406 | |
| 407 | function openLetter ({ mode, slotIndex, instance }) |
| 408 | { |
| 409 | letterState = { mode, slotIndex, readOnly: mode === "read", instance: instance ?? null }; |
| 410 | letterTitle.textContent = mode === "write" ? "Write letter" : mode === "edit" ? "Edit letter" : "Letter"; |
| 411 | letterBody.value = instance?.body ?? ""; |
| 412 | letterBody.readOnly = letterState.readOnly; |
| 413 | letterBody.maxLength = LETTER_MAX_BODY_CHARS; |
| 414 | letterSave.hidden = letterState.readOnly; |
| 415 | letterStatus.textContent = ""; |
| 416 | refreshLetterSignature(); |
| 417 | letterPanel.hidden = false; |
| 418 | letterBackdrop.hidden = false; |
| 419 | fitLetterBody(); |
| 420 | } |
| 421 | |
| 422 | function fitLetterBody () |
| 423 | { |
| 424 | letterBody.style.height = "auto"; |
| 425 | letterBody.style.height = `${Math.max(letterBody.scrollHeight, 128)}px`; |
| 426 | } |
| 427 | |
| 428 | function refreshLetterSignature () |
| 429 | { |
| 430 | const name = letterState?.instance?.authorDisplayName |
| 431 | || getLocalName?.() |
| 432 | || "Unknown"; |
| 433 | letterSignature.textContent = `Signed:\n${name}`; |
| 434 | } |
| 435 | |
| 436 | function closeLetter () |
| 437 | { |
| 438 | letterState = null; |
| 439 | letterPanel.hidden = true; |
| 440 | letterBackdrop.hidden = true; |
| 441 | } |
| 442 | |
| 443 | function renderVault (grid, slots) |
| 444 | { |
| 445 | grid.innerHTML = ""; |
| 446 | (slots ?? []).forEach((slot, index) => |
| 447 | { |
| 448 | const button = document.createElement("button"); |
| 449 | button.className = "item-slot"; |
| 450 | button.title = tooltip(slot); |
| 451 | button.textContent = slot |
| 452 | ? `${itemDefinition(slot.itemId)?.displayName ?? "Item"}${slot.quantity > 1 ? ` ×${slot.quantity}` : ""}` |
| 453 | : ""; |
| 454 | button.addEventListener("click", () => |
| 455 | { |
| 456 | if (!slot) |
| 457 | { return; } |
| 458 | socket.send({ type: MessageType.bankWithdraw, slotIndex: index }); |
| 459 | }); |
| 460 | grid.append(button); |
| 461 | }); |
| 462 | } |
| 463 | |
| 464 | function tooltip (slot) |
| 465 | { |
| 466 | if (!slot) |
| 467 | { return ""; } |
| 468 | const def = itemDefinition(slot.itemId); |
| 469 | return `${def?.displayName ?? "Item"}${slot.quantity > 1 ? ` ×${slot.quantity}` : ""}`; |
| 470 | } |
| 471 | |
| 472 | return { |
| 473 | isOpen () |
| 474 | { |
| 475 | return !bagPanel.hidden || bankOpen || !sendPanel.hidden || !letterPanel.hidden || !sendTargetPanel.hidden; |
| 476 | }, |
| 477 | isBlocking () |
| 478 | { |
| 479 | // Send/letter dialogs cover the view — keep world clicks blocked. |
| 480 | if (!sendPanel.hidden || !letterPanel.hidden || !sendTargetPanel.hidden) |
| 481 | { return true; } |
| 482 | if (sendBackdrop && !sendBackdrop.hidden) |
| 483 | { return true; } |
| 484 | if (letterBackdrop && !letterBackdrop.hidden) |
| 485 | { return true; } |
| 486 | const x = GameInput.mousePosition.x; |
| 487 | const y = GameInput.mousePosition.y; |
| 488 | for (const element of [bagPanel, bankPanel]) |
| 489 | { |
| 490 | if (!element || element.hidden) |
| 491 | { continue; } |
| 492 | const box = element.getBoundingClientRect(); |
| 493 | if (x >= box.left && x < box.right && y >= box.top && y < box.bottom) |
| 494 | { return true; } |
| 495 | } |
| 496 | return false; |
| 497 | }, |
| 498 | setLocalCharacterId (characterId) |
| 499 | { |
| 500 | localCharacterId = characterId; |
| 501 | }, |
| 502 | handleInventory (message) |
| 503 | { |
| 504 | bag = { ...bag, ...message }; |
| 505 | render(); |
| 506 | if (letterState && letterStatus.textContent === "Saving…") |
| 507 | { |
| 508 | letterStatus.textContent = "Saved."; |
| 509 | if (letterState.mode === "write") |
| 510 | { closeLetter(); } |
| 511 | else if (letterState.mode === "edit") |
| 512 | { |
| 513 | const slot = bag.inventory?.[letterState.slotIndex]; |
| 514 | if (slot?.instance) |
| 515 | { |
| 516 | letterState.instance = slot.instance; |
| 517 | letterBody.value = slot.instance.body ?? ""; |
| 518 | refreshLetterSignature(); |
| 519 | fitLetterBody(); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | }, |
| 524 | handleBank (message) |
| 525 | { |
| 526 | bankOpen = true; |
| 527 | bankPanel.hidden = false; |
| 528 | bag.vault = message.vault; |
| 529 | bag.vaultCurrency = message.vaultCurrency; |
| 530 | vaultCoins.textContent = `${message.vaultCurrency ?? 0} coins in vault · you carry ${message.currency ?? 0}`; |
| 531 | inheritCoins.textContent = `${message.inheritance?.currency ?? 0} coins waiting`; |
| 532 | renderVault(vaultGrid, message.vault); |
| 533 | renderVault(inheritGrid, message.inheritance?.slots ?? []); |
| 534 | inheritGrid.querySelectorAll("button").forEach((button) => |
| 535 | { |
| 536 | button.onclick = null; |
| 537 | }); |
| 538 | render(); |
| 539 | }, |
| 540 | depositSelected () |
| 541 | { |
| 542 | if (selected == null) |
| 543 | { return; } |
| 544 | socket.send({ type: MessageType.bankDeposit, slotIndex: selected }); |
| 545 | }, |
| 546 | handlePigeonDispatched () |
| 547 | { |
| 548 | document.getElementById("send-status").textContent = "Pigeon is in flight."; |
| 549 | closeSend(); |
| 550 | }, |
| 551 | handlePigeonDelivered (message) |
| 552 | { |
| 553 | const kind = message.destinationKind === "bank" ? "bank vault" : "recipient"; |
| 554 | hint.textContent = `Pigeon delivered to ${kind}.`; |
| 555 | }, |
| 556 | handleCharacterSearch (message) |
| 557 | { |
| 558 | if (!pendingSearch) |
| 559 | { return; } |
| 560 | pendingSearch = false; |
| 561 | sendSearchResult.replaceChildren(); |
| 562 | if (!message.character) |
| 563 | { |
| 564 | sendSearchResult.textContent = "No character with that name."; |
| 565 | return; |
| 566 | } |
| 567 | sendSearchResult.append(characterTargetRow(message.character)); |
| 568 | }, |
| 569 | reset () |
| 570 | { |
| 571 | bagPanel.hidden = true; |
| 572 | bankPanel.hidden = true; |
| 573 | bankOpen = false; |
| 574 | selected = null; |
| 575 | closeSend(); |
| 576 | closeLetter(); |
| 577 | } |
| 578 | }; |
| 579 | } |