| 1 | import { randomUUID } from "node:crypto"; |
| 2 | import { ItemId, LETTER_MAX_BODY_CHARS } from "../../../shared/definitions/items.js"; |
| 3 | import { attachOrigins, writtenOrigin } from "../../../shared/items/origin.js"; |
| 4 | |
| 5 | /** |
| 6 | * Build a system-authored letter stack (authorCharacterId 0). |
| 7 | * Callers should dispatch via CourierService.enqueueSystemLetter / dispatchSystemLetter |
| 8 | * so online players get a pigeon immediately and offline players are held until login. |
| 9 | */ |
| 10 | export function makeSystemLetterStack ({ |
| 11 | authorDisplayName, |
| 12 | authorOrganization = null, |
| 13 | body, |
| 14 | centralExchange = false, |
| 15 | bankRestNotice = false, |
| 16 | letterId = null, |
| 17 | writtenAt = Date.now() |
| 18 | }) |
| 19 | { |
| 20 | const name = String(authorDisplayName ?? "").trim() || "Unknown"; |
| 21 | const trimmed = String(body ?? "").replace(/\r\n/g, "\n").trimEnd().slice(0, LETTER_MAX_BODY_CHARS); |
| 22 | const org = String(authorOrganization ?? "").trim() || null; |
| 23 | const instance = { |
| 24 | kind: "letter", |
| 25 | letterId: letterId ?? randomUUID(), |
| 26 | authorCharacterId: 0, |
| 27 | authorDisplayName: name, |
| 28 | body: trimmed, |
| 29 | opened: false, |
| 30 | writtenAt: writtenAt | 0 || Date.now() |
| 31 | }; |
| 32 | if (org) |
| 33 | { instance.authorOrganization = org; } |
| 34 | if (centralExchange) |
| 35 | { instance.centralExchange = true; } |
| 36 | if (bankRestNotice) |
| 37 | { instance.bankRestNotice = true; } |
| 38 | return attachOrigins({ |
| 39 | itemId: ItemId.letter, |
| 40 | quantity: 1, |
| 41 | origins: [{ origin: writtenOrigin(name), quantity: 1 }], |
| 42 | instance |
| 43 | }); |
| 44 | } |