| 1 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 2 | import { BlockType } from "../../../shared/definitions/tiles.js"; |
| 3 | import { MobId, mobDefinition, mobSpriteName } from "../../../shared/definitions/mobs.js"; |
| 4 | import { ItemId, itemDefinition } from "../../../shared/definitions/items.js"; |
| 5 | import { insertStack, insertStackGrowable, takeFromSlot, emptySlots, normalizeVault } from "../../../shared/items/inventory.js"; |
| 6 | import { advanceCourierFlight } from "../../../shared/courier/flight.js"; |
| 7 | import { GAIT } from "../../../shared/protocol/constants.js"; |
| 8 | import { decodeChunkBlob } from "../world/chunkData.js"; |
| 9 | import { findReservedChequeIndex } from "../items/bags.js"; |
| 10 | import { randomUUID } from "node:crypto"; |
| 11 | import { |
| 12 | BANK_LETTER_AUTHOR, |
| 13 | bankRestNoticeLetterBody, |
| 14 | isBankRestNoticeLetter, |
| 15 | isCentralExchangeLetter, |
| 16 | itemNameForNotice, |
| 17 | mergeBankRestNoticeLines, |
| 18 | sourceNameForVaultDeposit |
| 19 | } from "../../../shared/courier/bankNotice.js"; |
| 20 | |
| 21 | export class CourierService |
| 22 | { |
| 23 | constructor ({ stateDb, worldDb, simulation }) |
| 24 | { |
| 25 | this.stateDb = stateDb; |
| 26 | this.worldDb = worldDb; |
| 27 | this.simulation = simulation; |
| 28 | this.bankTiles = []; |
| 29 | this.bountyOfficeTiles = []; |
| 30 | this.refreshBankIndex(); |
| 31 | this.refreshBountyOfficeIndex(); |
| 32 | } |
| 33 | |
| 34 | refreshBankIndex () |
| 35 | { |
| 36 | this.bankTiles = this.#listBlockTiles(BlockType.bank); |
| 37 | } |
| 38 | |
| 39 | refreshBountyOfficeIndex () |
| 40 | { |
| 41 | this.bountyOfficeTiles = this.#listBlockTiles(BlockType.bountyOffice); |
| 42 | } |
| 43 | |
| 44 | #listBlockTiles (typeId) |
| 45 | { |
| 46 | const revisionId = this.worldDb.getActiveRevisionId(); |
| 47 | const tiles = []; |
| 48 | for (const coord of this.worldDb.listChunkCoords(revisionId)) |
| 49 | { |
| 50 | const row = this.worldDb.getChunkVersion(coord.chunk_x, coord.chunk_y, revisionId); |
| 51 | if (!row) |
| 52 | { continue; } |
| 53 | const data = decodeChunkBlob(row.data_blob, this.simulation.settings.defaultGroundTypeId, coord.chunk_x, coord.chunk_y); |
| 54 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 55 | { |
| 56 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 57 | { |
| 58 | const block = data.blocks[localY * CHUNK_SIZE + localX]; |
| 59 | if (block?.typeId !== typeId) |
| 60 | { continue; } |
| 61 | tiles.push({ |
| 62 | tileX: coord.chunk_x * CHUNK_SIZE + localX, |
| 63 | tileY: coord.chunk_y * CHUNK_SIZE + localY |
| 64 | }); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | return tiles; |
| 69 | } |
| 70 | |
| 71 | nearestBank (tileX, tileY) |
| 72 | { |
| 73 | return this.#nearestTile(this.bankTiles, tileX, tileY); |
| 74 | } |
| 75 | |
| 76 | nearestBountyOffice (tileX, tileY) |
| 77 | { |
| 78 | return this.#nearestTile(this.bountyOfficeTiles, tileX, tileY); |
| 79 | } |
| 80 | |
| 81 | isAdjacentToBountyOffice (tileX, tileY) |
| 82 | { |
| 83 | return this.bountyOfficeTiles.some((office) => |
| 84 | Math.max(Math.abs(office.tileX - tileX), Math.abs(office.tileY - tileY)) <= 1); |
| 85 | } |
| 86 | |
| 87 | #nearestTile (tiles, tileX, tileY) |
| 88 | { |
| 89 | if (!tiles.length) |
| 90 | { return null; } |
| 91 | let best = null; |
| 92 | let bestDist = Infinity; |
| 93 | for (const tile of tiles) |
| 94 | { |
| 95 | const dist = Math.hypot(tile.tileX - tileX, tile.tileY - tileY); |
| 96 | if (dist < bestDist) |
| 97 | { |
| 98 | bestDist = dist; |
| 99 | best = tile; |
| 100 | } |
| 101 | } |
| 102 | return best; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Player dispatch: cargo is money or one inventory stack. |
| 107 | * destination: { kind: "bank" } | { kind: "player", firstName, lastName } |
| 108 | */ |
| 109 | dispatchFromPlayer ({ characterId, originTileX, originTileY, destination, cargo }) |
| 110 | { |
| 111 | const onlineId = this.simulation.characterEntities.get(characterId); |
| 112 | const bag = onlineId |
| 113 | ? this.simulation.bagOf(onlineId) |
| 114 | : this.stateDb.getBag(characterId); |
| 115 | if (!bag) |
| 116 | { |
| 117 | const error = new Error("No bag."); |
| 118 | error.userFacing = true; |
| 119 | throw error; |
| 120 | } |
| 121 | |
| 122 | let takenCargo; |
| 123 | if (cargo?.kind === "item") |
| 124 | { |
| 125 | const slot = bag.inventory?.[cargo.slotIndex | 0]; |
| 126 | if (slot?.itemId === ItemId.blankCheque) |
| 127 | { |
| 128 | const error = new Error("Blank cheques cannot be sent by pigeon."); |
| 129 | error.userFacing = true; |
| 130 | throw error; |
| 131 | } |
| 132 | if (slot?.itemId === ItemId.letter) |
| 133 | { |
| 134 | if (Number(slot.instance?.authorCharacterId) !== Number(characterId)) |
| 135 | { |
| 136 | const error = new Error("You can only send letters you wrote."); |
| 137 | error.userFacing = true; |
| 138 | throw error; |
| 139 | } |
| 140 | } |
| 141 | const taken = takeFromSlot(bag.inventory, cargo.slotIndex | 0, cargo.quantity ?? null); |
| 142 | if (!taken.ok || !taken.taken) |
| 143 | { |
| 144 | const error = new Error("That item is not available."); |
| 145 | error.userFacing = true; |
| 146 | throw error; |
| 147 | } |
| 148 | bag.inventory = taken.slots; |
| 149 | const stack = taken.taken; |
| 150 | if (stack.itemId === ItemId.letter && stack.instance) |
| 151 | { stack.instance = { ...stack.instance, opened: false }; } |
| 152 | takenCargo = { kind: "item", stack }; |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | const error = new Error("Pigeon cargo must be money or one item stack."); |
| 157 | error.userFacing = true; |
| 158 | throw error; |
| 159 | } |
| 160 | |
| 161 | const dest = this.#resolveDestination(destination, originTileX, originTileY); |
| 162 | this.#persistBag(characterId, bag); |
| 163 | const transit = this.stateDb.insertCourierTransit({ |
| 164 | senderCharacterId: characterId, |
| 165 | destinationKind: dest.kind, |
| 166 | destinationCharacterId: dest.characterId ?? null, |
| 167 | vaultCharacterId: dest.vaultCharacterId ?? null, |
| 168 | cargo: takenCargo, |
| 169 | originX: originTileX + 0.5, |
| 170 | originY: originTileY + 0.5, |
| 171 | destX: dest.tileX + 0.5, |
| 172 | destY: dest.tileY + 0.5, |
| 173 | traveled: 0, |
| 174 | status: "in_transit", |
| 175 | entityId: null |
| 176 | }); |
| 177 | return { transit, bag }; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Market / system dispatch already holding cargo (no bag take). |
| 182 | * When the recipient is offline, holds delivery until login (CE letters, etc.). |
| 183 | */ |
| 184 | dispatchHeldCargo ({ senderCharacterId, originTileX, originTileY, destination, cargo, vaultCharacterId = null }) |
| 185 | { |
| 186 | const recipientId = vaultCharacterId ?? senderCharacterId; |
| 187 | if (!this.simulation.isCharacterOnline(recipientId)) |
| 188 | { |
| 189 | const dest = this.#resolveDestination(destination, originTileX, originTileY); |
| 190 | this.stateDb.insertHeldCourierDelivery({ |
| 191 | characterId: recipientId, |
| 192 | senderCharacterId, |
| 193 | originTileX, |
| 194 | originTileY, |
| 195 | destinationKind: dest.kind, |
| 196 | destinationCharacterId: dest.characterId ?? recipientId, |
| 197 | vaultCharacterId: vaultCharacterId ?? dest.vaultCharacterId ?? recipientId, |
| 198 | cargo |
| 199 | }); |
| 200 | return null; |
| 201 | } |
| 202 | const dest = this.#resolveDestination(destination, originTileX, originTileY); |
| 203 | return this.stateDb.insertCourierTransit({ |
| 204 | senderCharacterId, |
| 205 | destinationKind: dest.kind, |
| 206 | destinationCharacterId: dest.characterId ?? null, |
| 207 | vaultCharacterId: vaultCharacterId ?? dest.vaultCharacterId ?? null, |
| 208 | cargo, |
| 209 | originX: originTileX + 0.5, |
| 210 | originY: originTileY + 0.5, |
| 211 | destX: dest.tileX + 0.5, |
| 212 | destY: dest.tileY + 0.5, |
| 213 | traveled: 0, |
| 214 | status: "in_transit", |
| 215 | entityId: null |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | /** Flush offline-held deliveries and send a bank resting notice when the player enters the world. */ |
| 220 | onCharacterEnteredWorld (characterId, tileX, tileY) |
| 221 | { |
| 222 | for (const held of this.stateDb.listHeldCourierDeliveries(characterId)) |
| 223 | { |
| 224 | this.stateDb.deleteHeldCourierDelivery(held.holdId); |
| 225 | const row = this.stateDb.getCharacter(held.destinationCharacterId ?? characterId); |
| 226 | this.dispatchHeldCargo({ |
| 227 | senderCharacterId: held.senderCharacterId, |
| 228 | originTileX: held.originTileX, |
| 229 | originTileY: held.originTileY, |
| 230 | destination: held.destinationKind === "bank" |
| 231 | ? { kind: "bank" } |
| 232 | : { |
| 233 | kind: "player", |
| 234 | firstName: row?.first_name ?? "Unknown", |
| 235 | lastName: row?.last_name ?? "" |
| 236 | }, |
| 237 | cargo: held.cargo, |
| 238 | vaultCharacterId: held.vaultCharacterId ?? characterId |
| 239 | }); |
| 240 | } |
| 241 | const noticeTransit = this.#dispatchBankRestNotice(characterId, tileX, tileY); |
| 242 | void noticeTransit; |
| 243 | } |
| 244 | |
| 245 | step (now, dt) |
| 246 | { |
| 247 | const events = []; |
| 248 | const settings = this.simulation.settings; |
| 249 | for (const transit of this.stateDb.listActiveCourierTransits()) |
| 250 | { |
| 251 | this.#retargetPlayerDest(transit); |
| 252 | const advanced = advanceCourierFlight({ |
| 253 | originX: transit.originX, |
| 254 | originY: transit.originY, |
| 255 | destX: transit.destX, |
| 256 | destY: transit.destY, |
| 257 | traveled: transit.traveled, |
| 258 | dt, |
| 259 | settings |
| 260 | }); |
| 261 | transit.traveled = advanced.traveled; |
| 262 | transit.updatedAt = now; |
| 263 | |
| 264 | if (advanced.arrived) |
| 265 | { |
| 266 | events.push(...this.#deliver(transit, now)); |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | events.push(...this.#syncVisiblePigeon(transit, advanced, now)); |
| 271 | this.stateDb.updateCourierTransit(transit); |
| 272 | } |
| 273 | return events; |
| 274 | } |
| 275 | |
| 276 | #retargetPlayerDest (transit) |
| 277 | { |
| 278 | if (transit.destinationKind !== "player" || !transit.destinationCharacterId) |
| 279 | { return; } |
| 280 | const onlineId = this.simulation.characterEntities.get(transit.destinationCharacterId); |
| 281 | if (onlineId) |
| 282 | { |
| 283 | const pos = this.simulation.positionOf(onlineId); |
| 284 | if (pos) |
| 285 | { |
| 286 | transit.destX = pos.tileX + 0.5; |
| 287 | transit.destY = pos.tileY + 0.5; |
| 288 | } |
| 289 | return; |
| 290 | } |
| 291 | this.#routeTransitToBank(transit, transit.destinationCharacterId); |
| 292 | } |
| 293 | |
| 294 | #routeTransitToBank (transit, vaultCharacterId) |
| 295 | { |
| 296 | const bank = this.nearestBank(Math.floor(transit.destX), Math.floor(transit.destY)) |
| 297 | ?? this.nearestBank(Math.floor(transit.originX), Math.floor(transit.originY)); |
| 298 | if (!bank) |
| 299 | { return; } |
| 300 | transit.destinationKind = "bank"; |
| 301 | transit.vaultCharacterId = vaultCharacterId ?? transit.vaultCharacterId ?? transit.destinationCharacterId ?? transit.senderCharacterId; |
| 302 | transit.destX = bank.tileX + 0.5; |
| 303 | transit.destY = bank.tileY + 0.5; |
| 304 | } |
| 305 | |
| 306 | #resolveDestination (destination, originTileX, originTileY) |
| 307 | { |
| 308 | if (destination?.kind === "player") |
| 309 | { |
| 310 | const character = this.stateDb.getCharacterByName(destination.firstName, destination.lastName); |
| 311 | if (!character) |
| 312 | { |
| 313 | const error = new Error("No character with that name."); |
| 314 | error.userFacing = true; |
| 315 | throw error; |
| 316 | } |
| 317 | const onlineId = this.simulation.characterEntities.get(character.character_id); |
| 318 | if (!onlineId) |
| 319 | { |
| 320 | const bank = this.nearestBank(originTileX, originTileY) |
| 321 | ?? this.nearestBank(character.tile_x, character.tile_y); |
| 322 | if (!bank) |
| 323 | { |
| 324 | const error = new Error("No bank exists in the world yet."); |
| 325 | error.userFacing = true; |
| 326 | throw error; |
| 327 | } |
| 328 | return { |
| 329 | kind: "bank", |
| 330 | characterId: character.character_id, |
| 331 | vaultCharacterId: character.character_id, |
| 332 | tileX: bank.tileX, |
| 333 | tileY: bank.tileY |
| 334 | }; |
| 335 | } |
| 336 | const pos = this.simulation.positionOf(onlineId); |
| 337 | return { |
| 338 | kind: "player", |
| 339 | characterId: character.character_id, |
| 340 | tileX: pos.tileX, |
| 341 | tileY: pos.tileY |
| 342 | }; |
| 343 | } |
| 344 | if (destination?.kind === "bank") |
| 345 | { |
| 346 | const bank = this.nearestBank(originTileX, originTileY); |
| 347 | if (!bank) |
| 348 | { |
| 349 | const error = new Error("No bank exists in the world yet."); |
| 350 | error.userFacing = true; |
| 351 | throw error; |
| 352 | } |
| 353 | return { kind: "bank", tileX: bank.tileX, tileY: bank.tileY }; |
| 354 | } |
| 355 | const error = new Error("Pigeon destination must be bank or player."); |
| 356 | error.userFacing = true; |
| 357 | throw error; |
| 358 | } |
| 359 | |
| 360 | #syncVisiblePigeon (transit, advanced, now) |
| 361 | { |
| 362 | const events = []; |
| 363 | const tileX = Math.floor(advanced.x); |
| 364 | const tileY = Math.floor(advanced.y); |
| 365 | if (!this.simulation.chunkIsSimulating(tileX, tileY)) |
| 366 | { |
| 367 | if (transit.entityId) |
| 368 | { |
| 369 | this.simulation.despawnEntity(transit.entityId); |
| 370 | events.push({ type: "entity-despawn", entityId: transit.entityId, tileX, tileY }); |
| 371 | transit.entityId = null; |
| 372 | } |
| 373 | return events; |
| 374 | } |
| 375 | |
| 376 | if (!transit.entityId || !this.simulation.entityRecord(transit.entityId)) |
| 377 | { |
| 378 | transit.entityId = null; |
| 379 | const def = mobDefinition(MobId.pigeon); |
| 380 | const entityId = this.simulation.spawnEntity({ |
| 381 | position: { tileX, tileY }, |
| 382 | movement: { gait: GAIT.walk, walkCarry: 0 }, |
| 383 | path: { tiles: [] }, |
| 384 | flight: { |
| 385 | x: advanced.x, |
| 386 | y: advanced.y, |
| 387 | targetX: transit.destX, |
| 388 | targetY: transit.destY, |
| 389 | speed: advanced.speed, |
| 390 | nextPickAt: now + 60_000, |
| 391 | lastAt: now, |
| 392 | courierId: transit.courierId |
| 393 | }, |
| 394 | appearance: { |
| 395 | sex: def.sex, |
| 396 | firstName: def.displayName, |
| 397 | lastName: "", |
| 398 | sprite: mobSpriteName(MobId.pigeon) |
| 399 | }, |
| 400 | creature: { mobTypeId: MobId.pigeon }, |
| 401 | health: { current: 1, max: 1 } |
| 402 | }, { requireResident: true }); |
| 403 | if (entityId != null) |
| 404 | { transit.entityId = entityId; } |
| 405 | return events; |
| 406 | } |
| 407 | |
| 408 | const moved = this.simulation.updateCourierFlight(transit.entityId, { |
| 409 | x: advanced.x, |
| 410 | y: advanced.y, |
| 411 | targetX: transit.destX, |
| 412 | targetY: transit.destY, |
| 413 | speed: advanced.speed, |
| 414 | lastAt: now, |
| 415 | courierId: transit.courierId |
| 416 | }, now); |
| 417 | if (moved?.despawned) |
| 418 | { |
| 419 | transit.entityId = null; |
| 420 | events.push({ type: "entity-despawn", entityId: moved.entityId, tileX, tileY }); |
| 421 | } |
| 422 | else if (moved?.event) |
| 423 | { events.push(moved.event); } |
| 424 | return events; |
| 425 | } |
| 426 | |
| 427 | #deliver (transit, now) |
| 428 | { |
| 429 | const events = []; |
| 430 | if (transit.entityId) |
| 431 | { |
| 432 | this.simulation.despawnEntity(transit.entityId); |
| 433 | events.push({ |
| 434 | type: "entity-despawn", |
| 435 | entityId: transit.entityId, |
| 436 | tileX: Math.floor(transit.destX), |
| 437 | tileY: Math.floor(transit.destY) |
| 438 | }); |
| 439 | transit.entityId = null; |
| 440 | } |
| 441 | |
| 442 | if (transit.destinationKind === "bank") |
| 443 | { |
| 444 | const vaultCharacterId = transit.vaultCharacterId |
| 445 | ?? transit.destinationCharacterId |
| 446 | ?? transit.senderCharacterId; |
| 447 | const toast = this.#creditVault(vaultCharacterId, transit.cargo, { |
| 448 | senderCharacterId: transit.senderCharacterId |
| 449 | }); |
| 450 | if (toast) |
| 451 | { events.push(toast); } |
| 452 | } |
| 453 | else if (transit.destinationKind === "player") |
| 454 | { |
| 455 | const delivered = this.#tryDeliverToPlayer(transit.destinationCharacterId, transit.cargo); |
| 456 | if (!delivered) |
| 457 | { |
| 458 | const bank = this.nearestBank(Math.floor(transit.destX), Math.floor(transit.destY)) |
| 459 | ?? this.nearestBank(Math.floor(transit.originX), Math.floor(transit.originY)); |
| 460 | if (bank) |
| 461 | { |
| 462 | transit.originX = transit.destX; |
| 463 | transit.originY = transit.destY; |
| 464 | transit.destX = bank.tileX + 0.5; |
| 465 | transit.destY = bank.tileY + 0.5; |
| 466 | transit.traveled = 0; |
| 467 | transit.destinationKind = "bank"; |
| 468 | transit.vaultCharacterId = transit.destinationCharacterId; |
| 469 | transit.updatedAt = now; |
| 470 | this.stateDb.updateCourierTransit(transit); |
| 471 | events.push({ |
| 472 | type: "pigeon-reroute-bank", |
| 473 | courierId: transit.courierId, |
| 474 | characterId: transit.destinationCharacterId |
| 475 | }); |
| 476 | return events; |
| 477 | } |
| 478 | const toast = this.#creditVault(transit.destinationCharacterId, transit.cargo, { |
| 479 | senderCharacterId: transit.senderCharacterId |
| 480 | }); |
| 481 | if (toast) |
| 482 | { events.push(toast); } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | transit.status = "delivered"; |
| 487 | transit.traveled = Math.hypot(transit.destX - transit.originX, transit.destY - transit.originY); |
| 488 | transit.updatedAt = now; |
| 489 | this.stateDb.updateCourierTransit(transit); |
| 490 | events.push({ |
| 491 | type: "pigeon-delivered", |
| 492 | courierId: transit.courierId, |
| 493 | senderCharacterId: transit.senderCharacterId, |
| 494 | destinationKind: transit.destinationKind, |
| 495 | destinationCharacterId: transit.destinationCharacterId, |
| 496 | cargo: transit.cargo |
| 497 | }); |
| 498 | return events; |
| 499 | } |
| 500 | |
| 501 | #normalizeCargo (cargo) |
| 502 | { |
| 503 | if (cargo?.kind === "money") |
| 504 | { |
| 505 | return { |
| 506 | kind: "item", |
| 507 | stack: { itemId: ItemId.coins, quantity: Math.max(0, cargo.amount | 0) } |
| 508 | }; |
| 509 | } |
| 510 | return cargo; |
| 511 | } |
| 512 | |
| 513 | #tryDeliverToPlayer (characterId, cargo) |
| 514 | { |
| 515 | cargo = this.#normalizeCargo(cargo); |
| 516 | const onlineId = this.simulation.characterEntities.get(characterId); |
| 517 | if (!onlineId) |
| 518 | { return false; } |
| 519 | if (cargo.kind === "item") |
| 520 | { |
| 521 | const bag = this.simulation.bagOf(onlineId); |
| 522 | if (!bag) |
| 523 | { return false; } |
| 524 | const def = itemDefinition(cargo.stack.itemId); |
| 525 | const result = insertStack(bag.inventory, cargo.stack, def); |
| 526 | if (!result.ok || result.remainder) |
| 527 | { return false; } |
| 528 | bag.inventory = result.slots; |
| 529 | this.#persistBag(characterId, bag); |
| 530 | return true; |
| 531 | } |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | #creditVault (characterId, cargo, { senderCharacterId = null, recordRestNotice = true } = {}) |
| 536 | { |
| 537 | cargo = this.#normalizeCargo(cargo); |
| 538 | const onlineId = this.simulation.characterEntities.get(characterId); |
| 539 | const bag = onlineId |
| 540 | ? this.simulation.bagOf(onlineId) |
| 541 | : this.stateDb.getBag(characterId); |
| 542 | if (!bag) |
| 543 | { return null; } |
| 544 | if (cargo.kind !== "item") |
| 545 | { return null; } |
| 546 | |
| 547 | const stack = cargo.stack; |
| 548 | const isCeLetter = stack?.itemId === ItemId.letter && isCentralExchangeLetter(stack.instance); |
| 549 | const isRestNoticeLetter = stack?.itemId === ItemId.letter && isBankRestNoticeLetter(stack.instance); |
| 550 | |
| 551 | if (stack?.itemId === ItemId.filledCheque && stack.instance?.kind === "filled-cheque") |
| 552 | { |
| 553 | this.#redeemChequeIntoVault(characterId, bag, stack); |
| 554 | this.#maybeRecordRestNotice({ |
| 555 | characterId, |
| 556 | stack, |
| 557 | senderCharacterId, |
| 558 | recordRestNotice: recordRestNotice && !onlineId && !isCeLetter && !isRestNoticeLetter |
| 559 | }); |
| 560 | return null; |
| 561 | } |
| 562 | |
| 563 | const def = itemDefinition(stack.itemId); |
| 564 | const vault = normalizeVault( |
| 565 | bag.vaultReserved != null |
| 566 | ? { slots: bag.vault, reserved: bag.vaultReserved } |
| 567 | : bag.vault |
| 568 | ); |
| 569 | const result = insertStackGrowable(vault.slots, stack, def); |
| 570 | bag.vault = result.slots; |
| 571 | bag.vaultReserved = vault.reserved; |
| 572 | this.#persistBag(characterId, bag); |
| 573 | |
| 574 | this.#maybeRecordRestNotice({ |
| 575 | characterId, |
| 576 | stack, |
| 577 | senderCharacterId, |
| 578 | recordRestNotice: recordRestNotice && !onlineId && !isCeLetter && !isRestNoticeLetter |
| 579 | }); |
| 580 | |
| 581 | if (onlineId && isRestNoticeLetter) |
| 582 | { |
| 583 | return { |
| 584 | type: "toast", |
| 585 | characterId, |
| 586 | message: "A letter arrived for you at the bank." |
| 587 | }; |
| 588 | } |
| 589 | return null; |
| 590 | } |
| 591 | |
| 592 | #maybeRecordRestNotice ({ characterId, stack, senderCharacterId, recordRestNotice }) |
| 593 | { |
| 594 | if (!recordRestNotice || !stack) |
| 595 | { return; } |
| 596 | const sender = senderCharacterId ? this.stateDb.getCharacter(senderCharacterId) : null; |
| 597 | const senderDisplayName = sender |
| 598 | ? `${sender.first_name} ${sender.last_name}`.trim() |
| 599 | : null; |
| 600 | this.stateDb.appendBankRestNotice(characterId, { |
| 601 | itemId: stack.itemId, |
| 602 | itemName: itemNameForNotice(stack), |
| 603 | quantity: stack.quantity | 0 || 1, |
| 604 | sourceName: sourceNameForVaultDeposit(stack, senderCharacterId, senderDisplayName) |
| 605 | }); |
| 606 | } |
| 607 | |
| 608 | /** Pigeon/bank delivery of a filled cheque deposits the reserved goods, not the paper. */ |
| 609 | #redeemChequeIntoVault (characterId, bag, chequeStack) |
| 610 | { |
| 611 | const instance = chequeStack.instance; |
| 612 | const issuerId = instance.issuerCharacterId; |
| 613 | const goodsDef = itemDefinition(instance.itemId); |
| 614 | if (!goodsDef) |
| 615 | { return; } |
| 616 | |
| 617 | let reservedStack = null; |
| 618 | if (Number(issuerId) === Number(characterId)) |
| 619 | { |
| 620 | const reserved = [...(bag.vaultReserved ?? [])]; |
| 621 | const index = findReservedChequeIndex(reserved, instance); |
| 622 | if (index < 0) |
| 623 | { |
| 624 | // Paper arrived but reservation is gone — keep the cheque so it isn't silently lost. |
| 625 | const def = itemDefinition(ItemId.filledCheque); |
| 626 | const vault = normalizeVault({ slots: bag.vault, reserved: bag.vaultReserved }); |
| 627 | const result = insertStackGrowable(vault.slots, chequeStack, def); |
| 628 | bag.vault = result.slots; |
| 629 | bag.vaultReserved = vault.reserved; |
| 630 | this.#persistBag(characterId, bag); |
| 631 | return; |
| 632 | } |
| 633 | reservedStack = reserved[index]; |
| 634 | reserved.splice(index, 1); |
| 635 | bag.vaultReserved = reserved; |
| 636 | } |
| 637 | else |
| 638 | { |
| 639 | const release = this.simulation.releaseChequeReservation(issuerId, instance.chequeId, instance); |
| 640 | if (!release?.ok) |
| 641 | { |
| 642 | const def = itemDefinition(ItemId.filledCheque); |
| 643 | const vault = normalizeVault({ slots: bag.vault, reserved: bag.vaultReserved }); |
| 644 | const result = insertStackGrowable(vault.slots, chequeStack, def); |
| 645 | bag.vault = result.slots; |
| 646 | bag.vaultReserved = vault.reserved; |
| 647 | this.#persistBag(characterId, bag); |
| 648 | return; |
| 649 | } |
| 650 | reservedStack = release.stack; |
| 651 | } |
| 652 | |
| 653 | const vault = normalizeVault({ slots: bag.vault, reserved: bag.vaultReserved }); |
| 654 | const inserted = insertStackGrowable(vault.slots, { |
| 655 | itemId: reservedStack.itemId, |
| 656 | quantity: reservedStack.quantity, |
| 657 | origin: reservedStack.origin ?? null, |
| 658 | origins: reservedStack.origins ?? null, |
| 659 | instance: reservedStack.instance ?? null |
| 660 | }, goodsDef); |
| 661 | bag.vault = inserted.slots; |
| 662 | bag.vaultReserved = vault.reserved; |
| 663 | this.#persistBag(characterId, bag); |
| 664 | this.#maybeRecordRestNotice({ |
| 665 | characterId, |
| 666 | stack: { |
| 667 | itemId: reservedStack.itemId, |
| 668 | quantity: reservedStack.quantity, |
| 669 | origin: reservedStack.origin ?? null, |
| 670 | origins: reservedStack.origins ?? null, |
| 671 | instance: reservedStack.instance ?? null |
| 672 | }, |
| 673 | senderCharacterId: null, |
| 674 | recordRestNotice: !this.simulation.characterEntities.get(characterId) |
| 675 | }); |
| 676 | } |
| 677 | |
| 678 | #persistBag (characterId, bag) |
| 679 | { |
| 680 | this.stateDb.saveBag(characterId, bag); |
| 681 | const onlineId = this.simulation.characterEntities.get(characterId); |
| 682 | if (onlineId) |
| 683 | { |
| 684 | this.simulation.applyBag(onlineId, bag); |
| 685 | this.simulation.pushInventory(characterId, bag); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | #characterDisplayName (characterId) |
| 690 | { |
| 691 | const row = this.stateDb.getCharacter(characterId); |
| 692 | return { |
| 693 | firstName: row?.first_name ?? "Unknown", |
| 694 | lastName: row?.last_name ?? "", |
| 695 | full: row ? `${row.first_name} ${row.last_name}`.trim() : "Adventurer" |
| 696 | }; |
| 697 | } |
| 698 | |
| 699 | #dispatchBankRestNotice (characterId, tileX, tileY) |
| 700 | { |
| 701 | const entries = this.stateDb.listBankRestNotices(characterId); |
| 702 | if (!entries.length) |
| 703 | { return null; } |
| 704 | const bank = this.nearestBank(tileX, tileY); |
| 705 | if (!bank) |
| 706 | { return null; } |
| 707 | |
| 708 | const lines = mergeBankRestNoticeLines(entries.map((entry) => ({ |
| 709 | quantity: entry.quantity, |
| 710 | itemName: entry.itemName, |
| 711 | sourceName: entry.sourceName |
| 712 | }))); |
| 713 | const { full: playerName } = this.#characterDisplayName(characterId); |
| 714 | const body = bankRestNoticeLetterBody({ playerName, lines }); |
| 715 | const stack = { |
| 716 | itemId: ItemId.letter, |
| 717 | quantity: 1, |
| 718 | instance: { |
| 719 | kind: "letter", |
| 720 | letterId: randomUUID(), |
| 721 | authorCharacterId: 0, |
| 722 | authorDisplayName: BANK_LETTER_AUTHOR, |
| 723 | body, |
| 724 | opened: false, |
| 725 | writtenAt: Date.now(), |
| 726 | bankRestNotice: true |
| 727 | } |
| 728 | }; |
| 729 | this.stateDb.clearBankRestNotices(characterId); |
| 730 | return this.stateDb.insertCourierTransit({ |
| 731 | senderCharacterId: characterId, |
| 732 | destinationKind: "player", |
| 733 | destinationCharacterId: characterId, |
| 734 | vaultCharacterId: characterId, |
| 735 | cargo: { kind: "item", stack }, |
| 736 | originX: bank.tileX + 0.5, |
| 737 | originY: bank.tileY + 0.5, |
| 738 | destX: tileX + 0.5, |
| 739 | destY: tileY + 0.5, |
| 740 | traveled: 0, |
| 741 | status: "in_transit", |
| 742 | entityId: null |
| 743 | }); |
| 744 | } |
| 745 | } |