| 1 | import { LETTER_MAX_BODY_CHARS, itemDefinition, itemDisplayName } from "../definitions/items.js"; |
| 2 | |
| 3 | export const QUESTWARD_GIFT_AUTHOR = "John Questward"; |
| 4 | export const QUESTWARD_GIFT_ORG = "CEO of Questward"; |
| 5 | |
| 6 | /** |
| 7 | * @param {{ displayName: string, quantity: number }} item |
| 8 | */ |
| 9 | export function formatGiftItemBullet (item) |
| 10 | { |
| 11 | const qty = Math.max(1, item.quantity | 0); |
| 12 | const name = String(item.displayName ?? "Item").trim() || "Item"; |
| 13 | return `• ${qty}× ${name}`; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Build the gift pigeon letter body (shared by server send + editor live preview). |
| 18 | * Empty custom message omits that paragraph entirely. |
| 19 | * @param {{ |
| 20 | * characterDisplayName: string, |
| 21 | * message?: string, |
| 22 | * items?: Array<{ displayName?: string, itemId?: number, quantity?: number }> |
| 23 | * }} opts |
| 24 | */ |
| 25 | export function formatGiftLetterBody ({ characterDisplayName, message = "", items = [] }) |
| 26 | { |
| 27 | const name = String(characterDisplayName ?? "").trim() || "Adventurer"; |
| 28 | const custom = String(message ?? "").replace(/\r\n/g, "\n").trim(); |
| 29 | const lines = (items ?? []).map((row) => |
| 30 | { |
| 31 | const def = row.itemId != null ? itemDefinition(row.itemId) : null; |
| 32 | const displayName = row.displayName |
| 33 | ?? (def ? itemDisplayName({ itemId: row.itemId, quantity: row.quantity }, def) : null) |
| 34 | ?? def?.displayName |
| 35 | ?? `Item ${row.itemId ?? "?"}`; |
| 36 | return formatGiftItemBullet({ displayName, quantity: row.quantity }); |
| 37 | }); |
| 38 | const itemBlock = lines.length |
| 39 | ? lines.join("\n") |
| 40 | : "• (no items selected)"; |
| 41 | const parts = [`Dear ${name}`, ""]; |
| 42 | if (custom) |
| 43 | { |
| 44 | parts.push(custom, ""); |
| 45 | } |
| 46 | parts.push( |
| 47 | "The following items were added to your bank:", |
| 48 | itemBlock, |
| 49 | "", |
| 50 | "Thank you for playing Questward." |
| 51 | ); |
| 52 | return parts.join("\n").slice(0, LETTER_MAX_BODY_CHARS); |
| 53 | } |