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