| 1 | import |
| 2 | { |
| 3 | itemDefinition, |
| 4 | itemDescription, |
| 5 | itemOriginSummary, |
| 6 | itemAssetPathForStack, |
| 7 | itemDisplayName, |
| 8 | formatQuantity, |
| 9 | ItemId, |
| 10 | EQUIPMENT_SLOT, |
| 11 | LETTER_MAX_BODY_CHARS |
| 12 | } from "/shared/definitions/items.js"; |
| 13 | import { formatLetterSignature } from "/shared/courier/systemLetters.js"; |
| 14 | import { applyItemIcon } from "../render/potionIcon.js"; |
| 15 | |
| 16 | const EQUIP_LABELS = { |
| 17 | [EQUIPMENT_SLOT.head]: "Head", |
| 18 | [EQUIPMENT_SLOT.chest]: "Chest", |
| 19 | [EQUIPMENT_SLOT.legs]: "Legs", |
| 20 | [EQUIPMENT_SLOT.boots]: "Boots", |
| 21 | [EQUIPMENT_SLOT.weapon]: "Weapon", |
| 22 | [EQUIPMENT_SLOT.offhand]: "Off-hand" |
| 23 | }; |
| 24 | |
| 25 | export function initInventory ({ |
| 26 | socket, |
| 27 | MessageType, |
| 28 | contextMenu, |
| 29 | resolveSpriteUrl, |
| 30 | getFriends, |
| 31 | getLocalName, |
| 32 | tipArrows = null |
| 33 | }) |
| 34 | { |
| 35 | const bagPanel = document.getElementById("bag-panel"); |
| 36 | const bagGrid = document.getElementById("bag-grid"); |
| 37 | const equipGrid = document.getElementById("equip-grid"); |
| 38 | const hint = document.getElementById("bag-hint"); |
| 39 | const bankPanel = document.getElementById("bank-panel"); |
| 40 | const vaultGrid = document.getElementById("vault-grid"); |
| 41 | const vaultReservedGrid = document.getElementById("vault-reserved-grid"); |
| 42 | const inheritGrid = document.getElementById("inherit-grid"); |
| 43 | const bankAvailablePane = document.getElementById("bank-available-pane"); |
| 44 | const bankReservedPane = document.getElementById("bank-reserved-pane"); |
| 45 | const bankInheritPane = document.getElementById("bank-inherit-pane"); |
| 46 | const bankTabAvailable = document.getElementById("bank-tab-available"); |
| 47 | const bankTabReserved = document.getElementById("bank-tab-reserved"); |
| 48 | const bankTabInherit = document.getElementById("bank-tab-inherit"); |
| 49 | const bankTabsRow = document.querySelector("#bank-panel .bank-tabs"); |
| 50 | |
| 51 | const sendPanel = document.getElementById("send-panel"); |
| 52 | const sendBackdrop = document.getElementById("send-backdrop"); |
| 53 | const sendTargetPanel = document.getElementById("send-target-panel"); |
| 54 | const sendTargetList = document.getElementById("send-target-list"); |
| 55 | const sendSearchResult = document.getElementById("send-search-result"); |
| 56 | const letterPanel = document.getElementById("letter-panel"); |
| 57 | const letterBackdrop = document.getElementById("letter-backdrop"); |
| 58 | const letterBody = document.getElementById("letter-body"); |
| 59 | const letterSignature = document.getElementById("letter-signature"); |
| 60 | const letterSave = document.getElementById("letter-save"); |
| 61 | const letterTitle = document.getElementById("letter-title"); |
| 62 | const letterStatus = document.getElementById("letter-status"); |
| 63 | const transferPanel = document.getElementById("transfer-panel"); |
| 64 | const transferBackdrop = document.getElementById("transfer-backdrop"); |
| 65 | const writeChequePanel = document.getElementById("write-cheque-panel"); |
| 66 | const writeChequeBackdrop = document.getElementById("write-cheque-backdrop"); |
| 67 | const writeChequeVault = document.getElementById("write-cheque-vault"); |
| 68 | |
| 69 | let bag = { inventory: [], equipment: { slots: {} }, vault: [], vaultReserved: [] }; |
| 70 | let inheritance = { slots: [], currency: 0 }; |
| 71 | let selected = null; |
| 72 | let bankOpen = false; |
| 73 | let bankTab = "available"; |
| 74 | let sendState = null; |
| 75 | let letterState = null; |
| 76 | let transferState = null; |
| 77 | let writeChequeState = null; |
| 78 | let pendingSearch = false; |
| 79 | let localCharacterId = null; |
| 80 | let dragFromIndex = null; |
| 81 | let suppressSlotClick = false; |
| 82 | |
| 83 | document.getElementById("bag-toggle").addEventListener("click", () => |
| 84 | { |
| 85 | bagPanel.hidden = !bagPanel.hidden; |
| 86 | }); |
| 87 | document.getElementById("bank-close").addEventListener("click", () => closeBank()); |
| 88 | document.getElementById("bank-claim").addEventListener("click", () => |
| 89 | { |
| 90 | socket.send({ type: MessageType.claimInheritance }); |
| 91 | }); |
| 92 | document.getElementById("bank-tab-available").addEventListener("click", () => setBankTab("available")); |
| 93 | document.getElementById("bank-tab-reserved").addEventListener("click", () => setBankTab("reserved")); |
| 94 | document.getElementById("bank-tab-inherit").addEventListener("click", () => setBankTab("inherit")); |
| 95 | document.getElementById("transfer-close").addEventListener("click", () => closeTransfer()); |
| 96 | transferBackdrop.addEventListener("click", () => closeTransfer()); |
| 97 | document.getElementById("transfer-confirm").addEventListener("click", () => confirmTransfer()); |
| 98 | document.getElementById("write-cheque-close").addEventListener("click", () => closeWriteCheque()); |
| 99 | writeChequeBackdrop.addEventListener("click", () => closeWriteCheque()); |
| 100 | document.getElementById("write-cheque-confirm").addEventListener("click", () => confirmWriteCheque()); |
| 101 | |
| 102 | document.getElementById("send-close").addEventListener("click", () => closeSend()); |
| 103 | sendBackdrop.addEventListener("click", () => closeSend()); |
| 104 | document.getElementById("send-pick-target").addEventListener("click", () => openTargetPicker()); |
| 105 | document.getElementById("send-target-close").addEventListener("click", () => |
| 106 | { |
| 107 | sendTargetPanel.hidden = true; |
| 108 | }); |
| 109 | document.getElementById("send-confirm").addEventListener("click", () => confirmSend()); |
| 110 | document.getElementById("send-search").addEventListener("click", () => |
| 111 | { |
| 112 | const firstName = document.getElementById("send-search-first").value.trim(); |
| 113 | const lastName = document.getElementById("send-search-last").value.trim(); |
| 114 | if (!firstName || !lastName) |
| 115 | { |
| 116 | sendSearchResult.textContent = "Enter a first and last name."; |
| 117 | return; |
| 118 | } |
| 119 | pendingSearch = true; |
| 120 | sendSearchResult.textContent = "Searching…"; |
| 121 | socket.send({ type: MessageType.searchCharacter, firstName, lastName }); |
| 122 | }); |
| 123 | |
| 124 | document.getElementById("letter-close").addEventListener("click", () => closeLetter()); |
| 125 | letterBackdrop.addEventListener("click", () => closeLetter()); |
| 126 | letterSave.addEventListener("click", () => |
| 127 | { |
| 128 | if (!letterState || letterState.readOnly) |
| 129 | { return; } |
| 130 | const body = letterBody.value; |
| 131 | socket.send({ |
| 132 | type: letterState.mode === "edit" ? MessageType.editLetter : MessageType.writeLetter, |
| 133 | slotIndex: letterState.slotIndex, |
| 134 | body |
| 135 | }); |
| 136 | letterStatus.textContent = "Saving…"; |
| 137 | }); |
| 138 | letterBody.addEventListener("input", () => |
| 139 | { |
| 140 | if (letterState?.readOnly) |
| 141 | { return; } |
| 142 | fitLetterBody(); |
| 143 | refreshLetterSignature(); |
| 144 | }); |
| 145 | |
| 146 | function render () |
| 147 | { |
| 148 | bagGrid.innerHTML = ""; |
| 149 | (bag.inventory ?? []).forEach((slot, index) => |
| 150 | { |
| 151 | const button = document.createElement("button"); |
| 152 | button.type = "button"; |
| 153 | button.className = `item-slot${selected === index ? " selected" : ""}`; |
| 154 | button.dataset.slotIndex = String(index); |
| 155 | button.title = tooltip(slot); |
| 156 | button.draggable = !!slot; |
| 157 | if (slot) |
| 158 | { |
| 159 | const img = document.createElement("img"); |
| 160 | applyItemIcon(img, slot, itemAssetPathForStack(slot)); |
| 161 | img.alt = itemDisplayName(slot); |
| 162 | img.draggable = false; |
| 163 | button.append(img); |
| 164 | if (slot.quantity > 1) |
| 165 | { |
| 166 | const count = document.createElement("span"); |
| 167 | count.className = "qty"; |
| 168 | count.textContent = formatQuantity(slot.quantity); |
| 169 | button.append(count); |
| 170 | } |
| 171 | } |
| 172 | button.addEventListener("dragstart", (event) => |
| 173 | { |
| 174 | if (!slot) |
| 175 | { |
| 176 | event.preventDefault(); |
| 177 | return; |
| 178 | } |
| 179 | dragFromIndex = index; |
| 180 | suppressSlotClick = false; |
| 181 | event.dataTransfer.effectAllowed = "move"; |
| 182 | event.dataTransfer.setData("text/plain", String(index)); |
| 183 | button.classList.add("dragging"); |
| 184 | }); |
| 185 | button.addEventListener("dragend", () => |
| 186 | { |
| 187 | button.classList.remove("dragging"); |
| 188 | dragFromIndex = null; |
| 189 | for (const el of bagGrid.querySelectorAll(".item-slot.drag-over")) |
| 190 | { el.classList.remove("drag-over"); } |
| 191 | // Click fires after a completed drag in some browsers; ignore it. |
| 192 | suppressSlotClick = true; |
| 193 | queueMicrotask(() => |
| 194 | { suppressSlotClick = false; }); |
| 195 | }); |
| 196 | button.addEventListener("dragover", (event) => |
| 197 | { |
| 198 | if (dragFromIndex == null || dragFromIndex === index) |
| 199 | { return; } |
| 200 | event.preventDefault(); |
| 201 | event.dataTransfer.dropEffect = "move"; |
| 202 | button.classList.add("drag-over"); |
| 203 | }); |
| 204 | button.addEventListener("dragleave", () => |
| 205 | { button.classList.remove("drag-over"); }); |
| 206 | button.addEventListener("drop", (event) => |
| 207 | { |
| 208 | event.preventDefault(); |
| 209 | button.classList.remove("drag-over"); |
| 210 | const raw = event.dataTransfer.getData("text/plain"); |
| 211 | const from = raw === "" ? dragFromIndex : (raw | 0); |
| 212 | if (from == null || from === index) |
| 213 | { return; } |
| 214 | selected = index; |
| 215 | socket.send({ |
| 216 | type: MessageType.moveInventorySlot, |
| 217 | fromSlotIndex: from, |
| 218 | toSlotIndex: index |
| 219 | }); |
| 220 | }); |
| 221 | button.addEventListener("click", () => |
| 222 | { |
| 223 | if (suppressSlotClick) |
| 224 | { |
| 225 | suppressSlotClick = false; |
| 226 | return; |
| 227 | } |
| 228 | if (!slot) |
| 229 | { return; } |
| 230 | if (bankOpen) |
| 231 | { |
| 232 | transferSlot("inventory", index, null); |
| 233 | return; |
| 234 | } |
| 235 | selected = index; |
| 236 | hint.textContent = `${itemDisplayName(slot)}: ${itemDescription(slot, { localCharacterId })}`; |
| 237 | render(); |
| 238 | }); |
| 239 | button.addEventListener("contextmenu", (event) => |
| 240 | { |
| 241 | event.preventDefault(); |
| 242 | event.stopPropagation(); |
| 243 | if (!slot) |
| 244 | { return; } |
| 245 | selected = index; |
| 246 | render(); |
| 247 | if (bankOpen) |
| 248 | { showBankTransferMenu(event.clientX, event.clientY, "inventory", index, slot); } |
| 249 | else |
| 250 | { showItemMenu(event.clientX, event.clientY, index, slot); } |
| 251 | }); |
| 252 | bagGrid.append(button); |
| 253 | }); |
| 254 | equipGrid.innerHTML = ""; |
| 255 | for (const [slot, label] of Object.entries(EQUIP_LABELS)) |
| 256 | { |
| 257 | const worn = bag.equipment?.slots?.[slot]; |
| 258 | const button = document.createElement("button"); |
| 259 | button.type = "button"; |
| 260 | button.className = "item-slot"; |
| 261 | button.title = worn ? tooltip(worn) : label; |
| 262 | if (worn) |
| 263 | { |
| 264 | const img = document.createElement("img"); |
| 265 | applyItemIcon(img, worn, itemAssetPathForStack(worn)); |
| 266 | img.alt = itemDisplayName(worn); |
| 267 | button.append(img); |
| 268 | } |
| 269 | button.addEventListener("click", () => |
| 270 | { |
| 271 | if (worn) |
| 272 | { socket.send({ type: MessageType.unequipItem, equipSlot: slot }); } |
| 273 | }); |
| 274 | equipGrid.append(button); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | function showItemMenu (x, y, slotIndex, slot) |
| 279 | { |
| 280 | const def = itemDefinition(slot.itemId); |
| 281 | const items = []; |
| 282 | const isOwnLetter = slot.itemId === ItemId.letter |
| 283 | && slot.instance?.kind === "letter" |
| 284 | && Number(slot.instance.authorCharacterId) === Number(localCharacterId); |
| 285 | const canSend = slot.itemId !== ItemId.blankCheque |
| 286 | && (slot.itemId !== ItemId.letter || isOwnLetter); |
| 287 | const canEquip = def?.equipment?.slot && slot.itemId !== ItemId.coins; |
| 288 | |
| 289 | if (slot.itemId === ItemId.paper) |
| 290 | { |
| 291 | items.push({ |
| 292 | label: "Write letter", |
| 293 | onSelect: () => openLetter({ mode: "write", slotIndex }) |
| 294 | }); |
| 295 | } |
| 296 | if (slot.itemId === ItemId.letter && slot.instance?.kind === "letter") |
| 297 | { |
| 298 | items.push({ |
| 299 | label: "Read", |
| 300 | onSelect: () => |
| 301 | { |
| 302 | if (slot.instance?.opened === false) |
| 303 | { socket.send({ type: MessageType.openLetter, slotIndex }); } |
| 304 | openLetter({ |
| 305 | mode: "read", |
| 306 | slotIndex, |
| 307 | instance: { ...slot.instance, opened: true } |
| 308 | }); |
| 309 | } |
| 310 | }); |
| 311 | if (isOwnLetter) |
| 312 | { |
| 313 | items.push({ |
| 314 | label: "Edit letter", |
| 315 | onSelect: () => openLetter({ |
| 316 | mode: "edit", |
| 317 | slotIndex, |
| 318 | instance: slot.instance |
| 319 | }) |
| 320 | }); |
| 321 | } |
| 322 | } |
| 323 | if (slot.instance?.kind === "spellbook") |
| 324 | { |
| 325 | items.push({ |
| 326 | label: "Study", |
| 327 | onSelect: () => socket.send({ type: MessageType.readSpellbook, slotIndex }) |
| 328 | }); |
| 329 | } |
| 330 | if (slot.itemId === ItemId.recipePaper && slot.instance?.kind === "recipe-paper") |
| 331 | { |
| 332 | items.push({ |
| 333 | label: "Read", |
| 334 | onSelect: () => openLetter({ |
| 335 | mode: "read", |
| 336 | slotIndex, |
| 337 | instance: { |
| 338 | documentTitle: slot.instance.displayName ?? "Recipe", |
| 339 | body: slot.instance.body ?? "", |
| 340 | authorDisplayName: slot.instance.authorDisplayName ?? "Unknown" |
| 341 | } |
| 342 | }) |
| 343 | }); |
| 344 | } |
| 345 | if (slot.itemId === ItemId.blankCheque) |
| 346 | { |
| 347 | items.push({ |
| 348 | label: "Write Cheque", |
| 349 | onSelect: () => openWriteCheque(slotIndex) |
| 350 | }); |
| 351 | } |
| 352 | if (slot.itemId === ItemId.filledCheque && bankOpen) |
| 353 | { |
| 354 | items.push({ |
| 355 | label: "Redeem Cheque", |
| 356 | onSelect: () => socket.send({ type: MessageType.redeemCheque, slotIndex }) |
| 357 | }); |
| 358 | } |
| 359 | if (canEquip) |
| 360 | { |
| 361 | items.push({ |
| 362 | label: "Equip", |
| 363 | onSelect: () => socket.send({ type: MessageType.equipItem, slotIndex }) |
| 364 | }); |
| 365 | } |
| 366 | if (canSend) |
| 367 | { |
| 368 | items.push({ |
| 369 | label: "Send", |
| 370 | onSelect: () => openSend(slotIndex, slot) |
| 371 | }); |
| 372 | } |
| 373 | const healing = itemDefinition(slot.itemId)?.healing ?? 0; |
| 374 | if (healing > 0) |
| 375 | { |
| 376 | items.push({ |
| 377 | label: "Eat", |
| 378 | onSelect: () => socket.send({ type: MessageType.eatItem, slotIndex }) |
| 379 | }); |
| 380 | } |
| 381 | if (slot.itemId === ItemId.potion && slot.instance?.kind === "potion") |
| 382 | { |
| 383 | if (tipArrows?.isArrowCoatingPotion?.(slot)) |
| 384 | { |
| 385 | items.push({ |
| 386 | label: "Apply to arrows", |
| 387 | onSelect: () => tipArrows.open(slotIndex) |
| 388 | }); |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | items.push({ |
| 393 | label: "Drink", |
| 394 | onSelect: () => socket.send({ type: MessageType.drinkItem, slotIndex }) |
| 395 | }); |
| 396 | } |
| 397 | } |
| 398 | items.push({ |
| 399 | label: "Drop", |
| 400 | onSelect: () => socket.send({ type: MessageType.dropItem, slotIndex }) |
| 401 | }); |
| 402 | contextMenu?.show(x, y, items); |
| 403 | } |
| 404 | |
| 405 | function showBankTransferMenu (x, y, side, slotIndex, slot) |
| 406 | { |
| 407 | if (side === "inventory" && slot.itemId === ItemId.filledCheque) |
| 408 | { |
| 409 | contextMenu?.show(x, y, [{ |
| 410 | label: "Redeem Cheque", |
| 411 | onSelect: () => socket.send({ type: MessageType.redeemCheque, slotIndex }) |
| 412 | }]); |
| 413 | return; |
| 414 | } |
| 415 | if (side === "inventory" && slot.itemId === ItemId.blankCheque) |
| 416 | { |
| 417 | contextMenu?.show(x, y, [ |
| 418 | { |
| 419 | label: `Return ${slot.quantity | 0}`, |
| 420 | onSelect: () => socket.send({ |
| 421 | type: MessageType.returnBlankCheques, |
| 422 | slotIndex, |
| 423 | quantity: slot.quantity | 0 |
| 424 | }) |
| 425 | }, |
| 426 | { |
| 427 | label: "Write Cheque", |
| 428 | onSelect: () => openWriteCheque(slotIndex) |
| 429 | } |
| 430 | ]); |
| 431 | return; |
| 432 | } |
| 433 | const qty = slot.quantity | 0; |
| 434 | const name = itemDisplayName(slot); |
| 435 | if (qty <= 1) |
| 436 | { |
| 437 | contextMenu?.show(x, y, [{ |
| 438 | label: `Transfer ${name}`, |
| 439 | onSelect: () => transferSlot(side, slotIndex, 1) |
| 440 | }]); |
| 441 | return; |
| 442 | } |
| 443 | contextMenu?.show(x, y, [ |
| 444 | { |
| 445 | label: `Transfer all ${name}`, |
| 446 | onSelect: () => transferSlot(side, slotIndex, null) |
| 447 | }, |
| 448 | { |
| 449 | label: `Transfer half ${name}`, |
| 450 | onSelect: () => transferSlot(side, slotIndex, Math.max(1, Math.floor(qty / 2))) |
| 451 | }, |
| 452 | { |
| 453 | label: `Transfer one ${name}`, |
| 454 | onSelect: () => transferSlot(side, slotIndex, 1) |
| 455 | }, |
| 456 | { |
| 457 | label: `Transfer X ${name}`, |
| 458 | onSelect: () => openTransfer(side, slotIndex, slot) |
| 459 | } |
| 460 | ]); |
| 461 | } |
| 462 | |
| 463 | function transferSlot (side, slotIndex, quantity) |
| 464 | { |
| 465 | if (side === "inventory") |
| 466 | { |
| 467 | socket.send({ |
| 468 | type: MessageType.bankDeposit, |
| 469 | slotIndex, |
| 470 | quantity |
| 471 | }); |
| 472 | return; |
| 473 | } |
| 474 | socket.send({ |
| 475 | type: MessageType.bankWithdraw, |
| 476 | slotIndex, |
| 477 | quantity |
| 478 | }); |
| 479 | } |
| 480 | |
| 481 | function openTransfer (side, slotIndex, slot) |
| 482 | { |
| 483 | transferState = { side, slotIndex, maxQty: slot.quantity | 0 }; |
| 484 | document.getElementById("transfer-item-name").textContent = itemDisplayName(slot); |
| 485 | const input = document.getElementById("transfer-qty"); |
| 486 | input.value = "1"; |
| 487 | input.max = String(transferState.maxQty); |
| 488 | transferPanel.hidden = false; |
| 489 | transferBackdrop.hidden = false; |
| 490 | } |
| 491 | |
| 492 | function closeTransfer () |
| 493 | { |
| 494 | transferState = null; |
| 495 | transferPanel.hidden = true; |
| 496 | transferBackdrop.hidden = true; |
| 497 | } |
| 498 | |
| 499 | function confirmTransfer () |
| 500 | { |
| 501 | if (!transferState) |
| 502 | { return; } |
| 503 | const quantity = Math.min( |
| 504 | transferState.maxQty, |
| 505 | Math.max(1, Number(document.getElementById("transfer-qty").value) | 0) |
| 506 | ); |
| 507 | transferSlot(transferState.side, transferState.slotIndex, quantity); |
| 508 | closeTransfer(); |
| 509 | } |
| 510 | |
| 511 | function openWriteCheque (slotIndex) |
| 512 | { |
| 513 | writeChequeState = { slotIndex, vaultSlotIndex: null, maxQty: 1 }; |
| 514 | document.getElementById("write-cheque-item").textContent = "No vault item selected."; |
| 515 | document.getElementById("write-cheque-qty").value = "1"; |
| 516 | document.getElementById("write-cheque-confirm").disabled = true; |
| 517 | document.getElementById("write-cheque-status").textContent = ""; |
| 518 | writeChequePanel.hidden = false; |
| 519 | writeChequeBackdrop.hidden = false; |
| 520 | renderWriteChequeVault(); |
| 521 | } |
| 522 | |
| 523 | function closeWriteCheque () |
| 524 | { |
| 525 | writeChequeState = null; |
| 526 | writeChequePanel.hidden = true; |
| 527 | writeChequeBackdrop.hidden = true; |
| 528 | if (bankOpen) |
| 529 | { renderBank(); } |
| 530 | } |
| 531 | |
| 532 | function renderWriteChequeVault () |
| 533 | { |
| 534 | if (!writeChequeVault) |
| 535 | { return; } |
| 536 | writeChequeVault.innerHTML = ""; |
| 537 | const slots = bag.vault ?? []; |
| 538 | if (!slots.some(Boolean)) |
| 539 | { |
| 540 | const empty = document.createElement("p"); |
| 541 | empty.className = "hint"; |
| 542 | empty.textContent = "Your vault is empty. Deposit goods at a bank first."; |
| 543 | writeChequeVault.append(empty); |
| 544 | return; |
| 545 | } |
| 546 | slots.forEach((slot, index) => |
| 547 | { |
| 548 | if (!slot) |
| 549 | { return; } |
| 550 | const button = document.createElement("button"); |
| 551 | button.className = "item-slot"; |
| 552 | if (writeChequeState?.vaultSlotIndex === index) |
| 553 | { button.classList.add("selected"); } |
| 554 | button.title = tooltip(slot); |
| 555 | const def = itemDefinition(slot.itemId); |
| 556 | const img = document.createElement("img"); |
| 557 | applyItemIcon(img, slot, itemAssetPathForStack(slot)); |
| 558 | img.alt = itemDisplayName(slot); |
| 559 | button.append(img); |
| 560 | if (slot.quantity > 1) |
| 561 | { |
| 562 | const count = document.createElement("span"); |
| 563 | count.className = "qty"; |
| 564 | count.textContent = formatQuantity(slot.quantity); |
| 565 | button.append(count); |
| 566 | } |
| 567 | button.addEventListener("click", () => |
| 568 | { |
| 569 | if (!writeChequeState) |
| 570 | { return; } |
| 571 | writeChequeState.vaultSlotIndex = index; |
| 572 | writeChequeState.maxQty = def?.uniqueInstance ? 1 : availableOf(slot.itemId); |
| 573 | document.getElementById("write-cheque-item").textContent = |
| 574 | `${itemDisplayName(slot)} (up to ${formatQuantity(writeChequeState.maxQty)})`; |
| 575 | const qtyInput = document.getElementById("write-cheque-qty"); |
| 576 | qtyInput.value = "1"; |
| 577 | qtyInput.max = String(writeChequeState.maxQty); |
| 578 | qtyInput.disabled = writeChequeState.maxQty <= 1; |
| 579 | document.getElementById("write-cheque-confirm").disabled = false; |
| 580 | renderWriteChequeVault(); |
| 581 | }); |
| 582 | writeChequeVault.append(button); |
| 583 | }); |
| 584 | } |
| 585 | |
| 586 | function confirmWriteCheque () |
| 587 | { |
| 588 | if (!writeChequeState || writeChequeState.vaultSlotIndex == null) |
| 589 | { return; } |
| 590 | const quantity = Math.max(1, Number(document.getElementById("write-cheque-qty").value) | 0); |
| 591 | socket.send({ |
| 592 | type: MessageType.writeCheque, |
| 593 | slotIndex: writeChequeState.slotIndex, |
| 594 | vaultSlotIndex: writeChequeState.vaultSlotIndex, |
| 595 | quantity |
| 596 | }); |
| 597 | document.getElementById("write-cheque-status").textContent = "Writing…"; |
| 598 | closeWriteCheque(); |
| 599 | } |
| 600 | |
| 601 | function closeBank () |
| 602 | { |
| 603 | bankOpen = false; |
| 604 | bankPanel.hidden = true; |
| 605 | closeWriteCheque(); |
| 606 | closeTransfer(); |
| 607 | } |
| 608 | |
| 609 | function bankHasReserved () |
| 610 | { |
| 611 | return (bag.vaultReserved ?? []).some((slot) => slot); |
| 612 | } |
| 613 | |
| 614 | function bankHasInheritance () |
| 615 | { |
| 616 | const slots = inheritance?.slots ?? []; |
| 617 | const hasSlots = slots.some((slot) => slot); |
| 618 | const hasCoins = (inheritance?.currency | 0) > 0; |
| 619 | return hasSlots || hasCoins; |
| 620 | } |
| 621 | |
| 622 | function syncBankTabs () |
| 623 | { |
| 624 | const showReserved = bankHasReserved(); |
| 625 | const showInherit = bankHasInheritance(); |
| 626 | if (bankTabReserved) |
| 627 | { bankTabReserved.hidden = !showReserved; } |
| 628 | if (bankTabInherit) |
| 629 | { bankTabInherit.hidden = !showInherit; } |
| 630 | if (bankTabsRow) |
| 631 | { bankTabsRow.hidden = !showReserved && !showInherit; } |
| 632 | if ((bankTab === "reserved" && !showReserved) || (bankTab === "inherit" && !showInherit)) |
| 633 | { bankTab = "available"; } |
| 634 | } |
| 635 | |
| 636 | function setBankTab (tab) |
| 637 | { |
| 638 | syncBankTabs(); |
| 639 | if (tab === "reserved" && !bankHasReserved()) |
| 640 | { tab = "available"; } |
| 641 | if (tab === "inherit" && !bankHasInheritance()) |
| 642 | { tab = "available"; } |
| 643 | bankTab = tab; |
| 644 | bankTabAvailable?.classList.toggle("active", tab === "available"); |
| 645 | bankTabReserved?.classList.toggle("active", tab === "reserved"); |
| 646 | bankTabInherit?.classList.toggle("active", tab === "inherit"); |
| 647 | if (bankAvailablePane) |
| 648 | { bankAvailablePane.hidden = tab !== "available"; } |
| 649 | if (bankReservedPane) |
| 650 | { bankReservedPane.hidden = tab !== "reserved"; } |
| 651 | if (bankInheritPane) |
| 652 | { bankInheritPane.hidden = tab !== "inherit"; } |
| 653 | } |
| 654 | |
| 655 | function openSend (slotIndex, slot) |
| 656 | { |
| 657 | const def = itemDefinition(slot.itemId); |
| 658 | sendState = { |
| 659 | slotIndex, |
| 660 | itemId: slot.itemId, |
| 661 | maxQty: slot.quantity | 0, |
| 662 | destination: null |
| 663 | }; |
| 664 | applyItemIcon( |
| 665 | document.getElementById("send-item-img"), |
| 666 | slot, |
| 667 | itemAssetPathForStack(slot) |
| 668 | ); |
| 669 | document.getElementById("send-item-name").textContent = itemDisplayName(slot); |
| 670 | const qtyInput = document.getElementById("send-qty"); |
| 671 | qtyInput.value = String(Math.max(1, sendState.maxQty)); |
| 672 | qtyInput.max = String(sendState.maxQty); |
| 673 | qtyInput.disabled = sendState.maxQty <= 1; |
| 674 | document.getElementById("send-target-label").textContent = "No target selected."; |
| 675 | document.getElementById("send-confirm").disabled = true; |
| 676 | document.getElementById("send-status").textContent = ""; |
| 677 | sendPanel.hidden = false; |
| 678 | sendBackdrop.hidden = false; |
| 679 | sendTargetPanel.hidden = true; |
| 680 | } |
| 681 | |
| 682 | function closeSend () |
| 683 | { |
| 684 | sendState = null; |
| 685 | sendPanel.hidden = true; |
| 686 | sendBackdrop.hidden = true; |
| 687 | sendTargetPanel.hidden = true; |
| 688 | } |
| 689 | |
| 690 | function openTargetPicker () |
| 691 | { |
| 692 | if (!sendState) |
| 693 | { return; } |
| 694 | sendTargetPanel.hidden = false; |
| 695 | sendSearchResult.replaceChildren(); |
| 696 | document.getElementById("send-search-first").value = ""; |
| 697 | document.getElementById("send-search-last").value = ""; |
| 698 | renderTargetList(); |
| 699 | } |
| 700 | |
| 701 | function renderTargetList () |
| 702 | { |
| 703 | sendTargetList.replaceChildren(); |
| 704 | sendTargetList.append(targetRow({ |
| 705 | kind: "bank", |
| 706 | label: "Bank", |
| 707 | hint: "Nearest bank → your vault" |
| 708 | })); |
| 709 | const friends = getFriends?.() ?? []; |
| 710 | if (!friends.length) |
| 711 | { |
| 712 | const empty = document.createElement("p"); |
| 713 | empty.className = "hint"; |
| 714 | empty.textContent = "No friends yet."; |
| 715 | sendTargetList.append(empty); |
| 716 | return; |
| 717 | } |
| 718 | for (const friend of friends) |
| 719 | { sendTargetList.append(characterTargetRow(friend)); } |
| 720 | } |
| 721 | |
| 722 | function targetRow ({ kind, label, hint, onPick }) |
| 723 | { |
| 724 | const row = document.createElement("button"); |
| 725 | row.type = "button"; |
| 726 | row.className = "send-target-row"; |
| 727 | row.innerHTML = `<strong>${label}</strong>${hint ? `<span class="hint">${hint}</span>` : ""}`; |
| 728 | row.addEventListener("click", () => |
| 729 | { |
| 730 | if (onPick) |
| 731 | { onPick(); } |
| 732 | else |
| 733 | { pickTarget({ kind: "bank" }, "Bank"); } |
| 734 | }); |
| 735 | return row; |
| 736 | } |
| 737 | |
| 738 | function characterTargetRow (entry) |
| 739 | { |
| 740 | const row = document.createElement("button"); |
| 741 | row.type = "button"; |
| 742 | row.className = "send-target-row"; |
| 743 | const preview = characterPreview(entry.layers); |
| 744 | const text = document.createElement("div"); |
| 745 | text.className = "send-target-text"; |
| 746 | text.innerHTML = `<strong>${entry.firstName} ${entry.lastName}</strong>`; |
| 747 | row.append(preview, text); |
| 748 | row.addEventListener("click", () => |
| 749 | { |
| 750 | pickTarget({ |
| 751 | kind: "player", |
| 752 | firstName: entry.firstName, |
| 753 | lastName: entry.lastName, |
| 754 | characterId: entry.characterId |
| 755 | }, `${entry.firstName} ${entry.lastName}`); |
| 756 | }); |
| 757 | return row; |
| 758 | } |
| 759 | |
| 760 | function characterPreview (layers) |
| 761 | { |
| 762 | const wrap = document.createElement("div"); |
| 763 | wrap.className = "char-preview"; |
| 764 | for (const layer of layers ?? []) |
| 765 | { |
| 766 | const url = resolveSpriteUrl?.(layer.sprite); |
| 767 | if (!url) |
| 768 | { continue; } |
| 769 | const img = document.createElement("img"); |
| 770 | img.src = url; |
| 771 | img.alt = ""; |
| 772 | wrap.append(img); |
| 773 | } |
| 774 | return wrap; |
| 775 | } |
| 776 | |
| 777 | function pickTarget (destination, label) |
| 778 | { |
| 779 | if (!sendState) |
| 780 | { return; } |
| 781 | sendState.destination = destination; |
| 782 | document.getElementById("send-target-label").textContent = `Target: ${label}`; |
| 783 | document.getElementById("send-confirm").disabled = false; |
| 784 | sendTargetPanel.hidden = true; |
| 785 | } |
| 786 | |
| 787 | function confirmSend () |
| 788 | { |
| 789 | if (!sendState?.destination) |
| 790 | { return; } |
| 791 | const quantity = Math.max(1, Math.min(sendState.maxQty, Number(document.getElementById("send-qty").value) | 0)); |
| 792 | socket.send({ |
| 793 | type: MessageType.sendPigeon, |
| 794 | destination: sendState.destination, |
| 795 | cargo: { |
| 796 | kind: "item", |
| 797 | slotIndex: sendState.slotIndex, |
| 798 | quantity |
| 799 | } |
| 800 | }); |
| 801 | document.getElementById("send-status").textContent = "Pigeon dispatched…"; |
| 802 | } |
| 803 | |
| 804 | function openLetter ({ mode, slotIndex, instance }) |
| 805 | { |
| 806 | letterState = { mode, slotIndex, readOnly: mode === "read", instance: instance ?? null }; |
| 807 | letterTitle.textContent = instance?.documentTitle |
| 808 | ?? (mode === "write" ? "Write letter" : mode === "edit" ? "Edit letter" : "Letter"); |
| 809 | letterBody.value = instance?.body ?? ""; |
| 810 | letterBody.readOnly = letterState.readOnly; |
| 811 | letterBody.maxLength = LETTER_MAX_BODY_CHARS; |
| 812 | letterSave.hidden = letterState.readOnly; |
| 813 | letterStatus.textContent = ""; |
| 814 | refreshLetterSignature(); |
| 815 | letterPanel.hidden = false; |
| 816 | letterBackdrop.hidden = false; |
| 817 | fitLetterBody(); |
| 818 | } |
| 819 | |
| 820 | function fitLetterBody () |
| 821 | { |
| 822 | letterBody.style.height = "auto"; |
| 823 | letterBody.style.height = `${Math.max(letterBody.scrollHeight, 128)}px`; |
| 824 | } |
| 825 | |
| 826 | function refreshLetterSignature () |
| 827 | { |
| 828 | const fallback = getLocalName?.() || "Unknown"; |
| 829 | letterSignature.textContent = formatLetterSignature(letterState?.instance, fallback); |
| 830 | } |
| 831 | |
| 832 | function closeLetter () |
| 833 | { |
| 834 | letterState = null; |
| 835 | letterPanel.hidden = true; |
| 836 | letterBackdrop.hidden = true; |
| 837 | } |
| 838 | |
| 839 | function renderVault (grid, slots, { side = "vault", interactive = true } = {}) |
| 840 | { |
| 841 | grid.innerHTML = ""; |
| 842 | (slots ?? []).forEach((slot, index) => |
| 843 | { |
| 844 | if (!slot) |
| 845 | { return; } |
| 846 | const button = document.createElement("button"); |
| 847 | button.className = "item-slot"; |
| 848 | if (writeChequeState && side === "vault" && writeChequeState.vaultSlotIndex === index) |
| 849 | { button.classList.add("selected"); } |
| 850 | button.title = tooltip(slot, { reserved: side === "reserved" }); |
| 851 | const def = itemDefinition(slot.itemId); |
| 852 | const img = document.createElement("img"); |
| 853 | applyItemIcon(img, slot, itemAssetPathForStack(slot)); |
| 854 | img.alt = itemDisplayName(slot); |
| 855 | button.append(img); |
| 856 | if (slot.quantity > 1) |
| 857 | { |
| 858 | const count = document.createElement("span"); |
| 859 | count.className = "qty"; |
| 860 | count.textContent = formatQuantity(slot.quantity); |
| 861 | button.append(count); |
| 862 | } |
| 863 | if (interactive && side === "vault") |
| 864 | { |
| 865 | button.addEventListener("click", () => |
| 866 | { |
| 867 | if (writeChequeState) |
| 868 | { |
| 869 | writeChequeState.vaultSlotIndex = index; |
| 870 | writeChequeState.maxQty = itemDefinition(slot.itemId)?.uniqueInstance |
| 871 | ? 1 |
| 872 | : availableOf(slot.itemId); |
| 873 | document.getElementById("write-cheque-item").textContent = |
| 874 | `${itemDisplayName(slot)} (up to ${formatQuantity(writeChequeState.maxQty)})`; |
| 875 | const qtyInput = document.getElementById("write-cheque-qty"); |
| 876 | qtyInput.value = "1"; |
| 877 | qtyInput.max = String(writeChequeState.maxQty); |
| 878 | qtyInput.disabled = writeChequeState.maxQty <= 1; |
| 879 | document.getElementById("write-cheque-confirm").disabled = false; |
| 880 | renderBank(); |
| 881 | return; |
| 882 | } |
| 883 | transferSlot("vault", index, null); |
| 884 | }); |
| 885 | button.addEventListener("contextmenu", (event) => |
| 886 | { |
| 887 | event.preventDefault(); |
| 888 | event.stopPropagation(); |
| 889 | if (writeChequeState) |
| 890 | { return; } |
| 891 | showBankTransferMenu(event.clientX, event.clientY, "vault", index, slot); |
| 892 | }); |
| 893 | } |
| 894 | grid.append(button); |
| 895 | }); |
| 896 | } |
| 897 | |
| 898 | function availableOf (itemId) |
| 899 | { |
| 900 | let total = 0; |
| 901 | for (const slot of bag.vault ?? []) |
| 902 | { |
| 903 | if (slot?.itemId === itemId) |
| 904 | { total += slot.quantity | 0; } |
| 905 | } |
| 906 | return total; |
| 907 | } |
| 908 | |
| 909 | function renderBank () |
| 910 | { |
| 911 | renderVault(vaultGrid, bag.vault, { side: "vault", interactive: true }); |
| 912 | renderVault(vaultReservedGrid, bag.vaultReserved, { side: "reserved", interactive: false }); |
| 913 | setBankTab(bankTab); |
| 914 | } |
| 915 | |
| 916 | function tooltip (slot, { reserved = false } = {}) |
| 917 | { |
| 918 | if (!slot) |
| 919 | { return ""; } |
| 920 | const base = `${itemDisplayName(slot)}${slot.quantity > 1 ? ` ×${formatQuantity(slot.quantity)}` : ""}`; |
| 921 | const origin = itemOriginSummary(slot); |
| 922 | const desc = itemDescription(slot, { localCharacterId }); |
| 923 | const lines = [base]; |
| 924 | if (reserved) |
| 925 | { |
| 926 | lines.push(slot.chequeId |
| 927 | ? "Reserved against a written cheque." |
| 928 | : "Reserved in the vault."); |
| 929 | } |
| 930 | if (origin) |
| 931 | { lines.push(origin); } |
| 932 | if (desc) |
| 933 | { lines.push(desc); } |
| 934 | return lines.join("\n"); |
| 935 | } |
| 936 | |
| 937 | return { |
| 938 | isOpen () |
| 939 | { |
| 940 | return !bagPanel.hidden || bankOpen || !sendPanel.hidden || !letterPanel.hidden |
| 941 | || !sendTargetPanel.hidden || !transferPanel.hidden || !writeChequePanel.hidden; |
| 942 | }, |
| 943 | isBankOpen () |
| 944 | { |
| 945 | return bankOpen; |
| 946 | }, |
| 947 | closeBank, |
| 948 | isBlocking () |
| 949 | { |
| 950 | if (!sendPanel.hidden || !letterPanel.hidden || !sendTargetPanel.hidden |
| 951 | || !transferPanel.hidden || !writeChequePanel.hidden) |
| 952 | { return true; } |
| 953 | if (sendBackdrop && !sendBackdrop.hidden) |
| 954 | { return true; } |
| 955 | if (letterBackdrop && !letterBackdrop.hidden) |
| 956 | { return true; } |
| 957 | if (transferBackdrop && !transferBackdrop.hidden) |
| 958 | { return true; } |
| 959 | if (writeChequeBackdrop && !writeChequeBackdrop.hidden) |
| 960 | { return true; } |
| 961 | const x = GameInput.mousePosition.x; |
| 962 | const y = GameInput.mousePosition.y; |
| 963 | for (const element of [bagPanel, bankPanel]) |
| 964 | { |
| 965 | if (!element || element.hidden) |
| 966 | { continue; } |
| 967 | const box = element.getBoundingClientRect(); |
| 968 | if (x >= box.left && x < box.right && y >= box.top && y < box.bottom) |
| 969 | { return true; } |
| 970 | } |
| 971 | return false; |
| 972 | }, |
| 973 | setLocalCharacterId (characterId) |
| 974 | { |
| 975 | localCharacterId = characterId; |
| 976 | }, |
| 977 | getLocalCharacterId () |
| 978 | { |
| 979 | return localCharacterId; |
| 980 | }, |
| 981 | getInventorySlots () |
| 982 | { |
| 983 | return bag.inventory ?? []; |
| 984 | }, |
| 985 | getBag () |
| 986 | { |
| 987 | return { |
| 988 | slots: bag.inventory ?? [], |
| 989 | equipment: bag.equipment ?? null |
| 990 | }; |
| 991 | }, |
| 992 | getEquipment () |
| 993 | { |
| 994 | return bag.equipment?.slots ?? bag.equipment ?? {}; |
| 995 | }, |
| 996 | handleInventory (message) |
| 997 | { |
| 998 | bag = { ...bag, ...message }; |
| 999 | render(); |
| 1000 | if (bankOpen) |
| 1001 | { |
| 1002 | renderBank(); |
| 1003 | syncBankTabs(); |
| 1004 | } |
| 1005 | if (writeChequeState) |
| 1006 | { renderWriteChequeVault(); } |
| 1007 | if (letterState && letterStatus.textContent === "Saving…") |
| 1008 | { |
| 1009 | letterStatus.textContent = "Saved."; |
| 1010 | if (letterState.mode === "write") |
| 1011 | { closeLetter(); } |
| 1012 | else if (letterState.mode === "edit") |
| 1013 | { |
| 1014 | const slot = bag.inventory?.[letterState.slotIndex]; |
| 1015 | if (slot?.instance) |
| 1016 | { |
| 1017 | letterState.instance = slot.instance; |
| 1018 | letterBody.value = slot.instance.body ?? ""; |
| 1019 | refreshLetterSignature(); |
| 1020 | fitLetterBody(); |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | }, |
| 1025 | handleBank (message) |
| 1026 | { |
| 1027 | bankOpen = true; |
| 1028 | bankPanel.hidden = false; |
| 1029 | bagPanel.hidden = false; |
| 1030 | bag.vault = message.vault ?? []; |
| 1031 | bag.vaultReserved = message.vaultReserved ?? []; |
| 1032 | inheritance = { |
| 1033 | slots: message.inheritance?.slots ?? [], |
| 1034 | currency: message.inheritance?.currency | 0 || 0 |
| 1035 | }; |
| 1036 | renderVault(inheritGrid, inheritance.slots, { interactive: false }); |
| 1037 | inheritGrid.querySelectorAll("button").forEach((button) => |
| 1038 | { |
| 1039 | button.onclick = null; |
| 1040 | }); |
| 1041 | const inheritCoins = document.getElementById("inherit-coins"); |
| 1042 | if (inheritCoins) |
| 1043 | { |
| 1044 | const coins = inheritance.currency | 0; |
| 1045 | inheritCoins.hidden = coins <= 0; |
| 1046 | inheritCoins.textContent = coins > 0 ? `${formatQuantity(coins)} coins` : ""; |
| 1047 | } |
| 1048 | setBankTab("available"); |
| 1049 | renderBank(); |
| 1050 | render(); |
| 1051 | }, |
| 1052 | getBlankCheques () |
| 1053 | { |
| 1054 | socket.send({ type: MessageType.getBlankCheques }); |
| 1055 | }, |
| 1056 | handlePigeonDispatched () |
| 1057 | { |
| 1058 | document.getElementById("send-status").textContent = "Pigeon is in flight."; |
| 1059 | closeSend(); |
| 1060 | }, |
| 1061 | handlePigeonDelivered (message) |
| 1062 | { |
| 1063 | const kind = message.destinationKind === "bank" ? "bank vault" : "recipient"; |
| 1064 | hint.textContent = `Pigeon delivered to ${kind}.`; |
| 1065 | }, |
| 1066 | handleCharacterSearch (message) |
| 1067 | { |
| 1068 | if (!pendingSearch) |
| 1069 | { return; } |
| 1070 | pendingSearch = false; |
| 1071 | sendSearchResult.replaceChildren(); |
| 1072 | if (!message.character) |
| 1073 | { |
| 1074 | sendSearchResult.textContent = "No character with that name."; |
| 1075 | return; |
| 1076 | } |
| 1077 | sendSearchResult.append(characterTargetRow(message.character)); |
| 1078 | }, |
| 1079 | reset () |
| 1080 | { |
| 1081 | bagPanel.hidden = true; |
| 1082 | closeBank(); |
| 1083 | selected = null; |
| 1084 | closeSend(); |
| 1085 | closeLetter(); |
| 1086 | } |
| 1087 | }; |
| 1088 | } |