| 1 | import { randomUUID } from "node:crypto"; |
| 2 | import { ItemId, itemDefinition, LETTER_MAX_BODY_CHARS } from "../../../shared/definitions/items.js"; |
| 3 | import { insertStackGrowable, normalizeVault } from "../../../shared/items/inventory.js"; |
| 4 | import { attachOrigins, godsOrigin, writtenOrigin } from "../../../shared/items/origin.js"; |
| 5 | import { |
| 6 | formatGiftLetterBody, |
| 7 | QUESTWARD_GIFT_AUTHOR, |
| 8 | QUESTWARD_GIFT_ORG |
| 9 | } from "../../../shared/courier/giftLetters.js"; |
| 10 | |
| 11 | function userError (message) |
| 12 | { |
| 13 | const error = new Error(message); |
| 14 | error.userFacing = true; |
| 15 | return error; |
| 16 | } |
| 17 | |
| 18 | export class GiftService |
| 19 | { |
| 20 | constructor (stateDb, simulation) |
| 21 | { |
| 22 | this.stateDb = stateDb; |
| 23 | this.simulation = simulation; |
| 24 | } |
| 25 | |
| 26 | search (query) |
| 27 | { |
| 28 | const rows = this.stateDb.searchCharactersForGift(query); |
| 29 | return { |
| 30 | characters: rows.map((row) => ({ |
| 31 | characterId: row.character_id, |
| 32 | firstName: row.first_name, |
| 33 | lastName: row.last_name, |
| 34 | accountId: row.account_id, |
| 35 | accountUsername: row.account_username ?? null |
| 36 | })) |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Credit gift items to the character bank and queue a signed Questward letter. |
| 42 | * @param {{ characterId: number, message?: string, items?: Array<{ itemId: number, quantity: number }> }} payload |
| 43 | */ |
| 44 | sendGift (payload) |
| 45 | { |
| 46 | const characterId = payload?.characterId | 0; |
| 47 | const character = this.stateDb.getCharacter(characterId); |
| 48 | if (!character) |
| 49 | { throw userError("Character not found."); } |
| 50 | |
| 51 | const items = normalizeGiftItems(payload?.items); |
| 52 | if (!items.length) |
| 53 | { throw userError("Pick at least one item to gift."); } |
| 54 | |
| 55 | const message = String(payload?.message ?? "").replace(/\r\n/g, "\n").trim(); |
| 56 | if (message.length > LETTER_MAX_BODY_CHARS) |
| 57 | { throw userError("Gift message is too long."); } |
| 58 | |
| 59 | const displayName = `${character.first_name} ${character.last_name}`.trim(); |
| 60 | const credited = []; |
| 61 | for (const entry of items) |
| 62 | { |
| 63 | const def = itemDefinition(entry.itemId); |
| 64 | if (!def) |
| 65 | { throw userError(`Unknown item ${entry.itemId}.`); } |
| 66 | const stack = attachOrigins({ |
| 67 | itemId: entry.itemId, |
| 68 | quantity: entry.quantity, |
| 69 | origins: [{ origin: godsOrigin(), quantity: entry.quantity }] |
| 70 | }); |
| 71 | this.#creditBank(characterId, stack, def); |
| 72 | credited.push({ |
| 73 | itemId: entry.itemId, |
| 74 | quantity: entry.quantity, |
| 75 | displayName: def.displayName ?? `Item ${entry.itemId}` |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | const body = formatGiftLetterBody({ |
| 80 | characterDisplayName: displayName, |
| 81 | message, |
| 82 | items: credited |
| 83 | }); |
| 84 | this.#dispatchGiftLetter(character, body); |
| 85 | |
| 86 | return { |
| 87 | ok: true, |
| 88 | characterId, |
| 89 | firstName: character.first_name, |
| 90 | lastName: character.last_name, |
| 91 | items: credited |
| 92 | }; |
| 93 | } |
| 94 | |
| 95 | #creditBank (characterId, stack, def) |
| 96 | { |
| 97 | const bag = this.#loadBag(characterId); |
| 98 | if (!bag) |
| 99 | { throw userError("Character bag not found."); } |
| 100 | const vault = normalizeVault( |
| 101 | bag.vaultReserved != null |
| 102 | ? { slots: bag.vault, reserved: bag.vaultReserved } |
| 103 | : bag.vault |
| 104 | ); |
| 105 | const result = insertStackGrowable(vault.slots, stack, def); |
| 106 | if ((result.remainder | 0) > 0) |
| 107 | { throw userError("That character's bank is full."); } |
| 108 | bag.vault = result.slots; |
| 109 | bag.vaultReserved = vault.reserved; |
| 110 | this.#persistBag(characterId, bag); |
| 111 | } |
| 112 | |
| 113 | #dispatchGiftLetter (character, body) |
| 114 | { |
| 115 | const trimmed = String(body ?? "").replace(/\r\n/g, "\n").trimEnd().slice(0, LETTER_MAX_BODY_CHARS); |
| 116 | const stack = attachOrigins({ |
| 117 | itemId: ItemId.letter, |
| 118 | quantity: 1, |
| 119 | origins: [{ origin: writtenOrigin(QUESTWARD_GIFT_AUTHOR), quantity: 1 }], |
| 120 | instance: { |
| 121 | kind: "letter", |
| 122 | letterId: randomUUID(), |
| 123 | authorCharacterId: 0, |
| 124 | authorDisplayName: QUESTWARD_GIFT_AUTHOR, |
| 125 | authorOrganization: QUESTWARD_GIFT_ORG, |
| 126 | body: trimmed, |
| 127 | opened: false, |
| 128 | writtenAt: Date.now() |
| 129 | } |
| 130 | }); |
| 131 | const originTileX = character.tile_x | 0; |
| 132 | const originTileY = character.tile_y | 0; |
| 133 | const bank = this.simulation.courier?.nearestBank?.(originTileX, originTileY); |
| 134 | this.simulation.courier.dispatchSystemLetter({ |
| 135 | characterId: character.character_id, |
| 136 | originTileX: bank?.tileX ?? originTileX, |
| 137 | originTileY: bank?.tileY ?? originTileY, |
| 138 | stack |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | #loadBag (characterId) |
| 143 | { |
| 144 | const onlineId = this.simulation.characterEntities.get(characterId | 0); |
| 145 | return onlineId |
| 146 | ? this.simulation.bagOf(onlineId) |
| 147 | : this.stateDb.getBag(characterId); |
| 148 | } |
| 149 | |
| 150 | #persistBag (characterId, bag) |
| 151 | { |
| 152 | this.stateDb.saveBag(characterId, bag); |
| 153 | const onlineId = this.simulation.characterEntities.get(characterId | 0); |
| 154 | if (onlineId) |
| 155 | { |
| 156 | this.simulation.applyBag?.(onlineId, bag); |
| 157 | this.simulation.pushInventory?.(characterId, bag); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | function normalizeGiftItems (raw) |
| 163 | { |
| 164 | if (!Array.isArray(raw)) |
| 165 | { return []; } |
| 166 | /** @type {Map<number, number>} */ |
| 167 | const merged = new Map(); |
| 168 | for (const row of raw) |
| 169 | { |
| 170 | const itemId = row?.itemId | 0; |
| 171 | const quantity = Math.max(1, Math.min(100_000, row?.quantity | 0 || 1)); |
| 172 | if (!itemId || !itemDefinition(itemId)) |
| 173 | { continue; } |
| 174 | merged.set(itemId, (merged.get(itemId) ?? 0) + quantity); |
| 175 | } |
| 176 | return [...merged.entries()].map(([itemId, quantity]) => ({ itemId, quantity })); |
| 177 | } |