| 1 | import { stackDisplayLabel } from "../definitions/items.js"; |
| 2 | import { originLabel } from "../items/origin.js"; |
| 3 | import { BANK_LETTER_AUTHOR, isBankRestNoticeLetter, isCentralExchangeLetter } from "./systemLetters.js"; |
| 4 | |
| 5 | export { BANK_LETTER_AUTHOR, isBankRestNoticeLetter, isCentralExchangeLetter }; |
| 6 | |
| 7 | /** @param {{ quantity: number, itemName: string, sourceName: string }[]} lines */ |
| 8 | export function bankRestNoticeLetterBody ({ playerName, lines }) |
| 9 | { |
| 10 | const name = String(playerName ?? "Adventurer").trim() || "Adventurer"; |
| 11 | const bodyLines = lines.map((line) => |
| 12 | `- ${line.quantity} ${line.itemName}, received from ${line.sourceName}` |
| 13 | ).join("\n"); |
| 14 | return [ |
| 15 | `Dear ${name},`, |
| 16 | "While you have been resting, the following items were deposited into your bank account:", |
| 17 | "", |
| 18 | bodyLines |
| 19 | ].join("\n"); |
| 20 | } |
| 21 | |
| 22 | export function itemNameForNotice (stack) |
| 23 | { |
| 24 | return stackDisplayLabel(stack); |
| 25 | } |
| 26 | |
| 27 | /** Prefer pigeon sender name; otherwise origin label (`Mined by Ada`, …). */ |
| 28 | export function sourceNameForVaultDeposit (stack, senderCharacterId, senderDisplayName) |
| 29 | { |
| 30 | if (senderDisplayName) |
| 31 | { return senderDisplayName; } |
| 32 | if (senderCharacterId) |
| 33 | { return `character #${senderCharacterId}`; } |
| 34 | const origins = stack?.origins ?? (stack?.origin ? [{ origin: stack.origin, quantity: stack.quantity ?? 1 }] : []); |
| 35 | if (origins.length === 1 && origins[0]?.origin) |
| 36 | { return originLabel(origins[0].origin); } |
| 37 | return "unknown source"; |
| 38 | } |
| 39 | |
| 40 | /** Merge notice rows that share item name and source. */ |
| 41 | export function mergeBankRestNoticeLines (entries) |
| 42 | { |
| 43 | const merged = new Map(); |
| 44 | for (const entry of entries ?? []) |
| 45 | { |
| 46 | const key = `${entry.itemName}|${entry.sourceName}`; |
| 47 | const prev = merged.get(key); |
| 48 | if (prev) |
| 49 | { prev.quantity += entry.quantity | 0; } |
| 50 | else |
| 51 | { merged.set(key, { ...entry, quantity: entry.quantity | 0 }); } |
| 52 | } |
| 53 | return [...merged.values()]; |
| 54 | } |