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