BlueGit

timfa questward

Files

questward / shared / courier / giftLetters.js

giftLetters.js

giftLetters.js 1.6 KiB Download Raw
1import { LETTER_MAX_BODY_CHARS, itemDefinition, itemDisplayName } from "../definitions/items.js";
2
3export const QUESTWARD_GIFT_AUTHOR = "John Questward";
4export const QUESTWARD_GIFT_ORG = "CEO of Questward";
5
6/**
7 * @param {{ displayName: string, quantity: number }} item
8 */
9export 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 * @param {{
19 * characterDisplayName: string,
20 * message?: string,
21 * items?: Array<{ displayName?: string, itemId?: number, quantity?: number }>
22 * }} opts
23 */
24export function formatGiftLetterBody ({ characterDisplayName, message = "", items = [] })
25{
26 const name = String(characterDisplayName ?? "").trim() || "Adventurer";
27 const custom = String(message ?? "").replace(/\r\n/g, "\n").trim();
28 const lines = (items ?? []).map((row) =>
29 {
30 const def = row.itemId != null ? itemDefinition(row.itemId) : null;
31 const displayName = row.displayName
32 ?? (def ? itemDisplayName({ itemId: row.itemId, quantity: row.quantity }, def) : null)
33 ?? def?.displayName
34 ?? `Item ${row.itemId ?? "?"}`;
35 return formatGiftItemBullet({ displayName, quantity: row.quantity });
36 });
37 const itemBlock = lines.length
38 ? lines.join("\n")
39 : "• (no items selected)";
40 const parts = [
41 `Dear ${name}`,
42 "",
43 custom || "(Your message here.)",
44 "",
45 "The following items were added to your bank:",
46 itemBlock,
47 "",
48 "Thank you for playing Questward."
49 ];
50 return parts.join("\n").slice(0, LETTER_MAX_BODY_CHARS);
51}