BlueGit

timfa questward

Files

questward / shared / courier / bankNotice.js

bankNotice.js

tweaks to sprites, shading
timfa Tim Falken · pushed by timfa · 2 months ago
48b50124 History
bankNotice.js 2.0 KiB Download Raw
1import { stackDisplayLabel } from "../definitions/items.js";
2import { originLabel } from "../items/origin.js";
3
4export const BANK_LETTER_AUTHOR = "The Bank";
5
6export function isCentralExchangeLetter (instance)
7{
8 return instance?.kind === "letter" && /Central Exchange/.test(String(instance.body ?? ""));
9}
10
11export function isBankRestNoticeLetter (instance)
12{
13 return instance?.kind === "letter" && instance.bankRestNotice === true;
14}
15
16/** @param {{ quantity: number, itemName: string, sourceName: string }[]} lines */
17export function bankRestNoticeLetterBody ({ playerName, lines })
18{
19 const name = String(playerName ?? "Adventurer").trim() || "Adventurer";
20 const bodyLines = lines.map((line) =>
21 `- ${line.quantity} ${line.itemName}, received from ${line.sourceName}`
22 ).join("\n");
23 return [
24 `Dear ${name},`,
25 "While you have been resting, the following items were deposited into your bank account:",
26 "",
27 bodyLines,
28 "",
29 "Signed,",
30 BANK_LETTER_AUTHOR
31 ].join("\n");
32}
33
34export function itemNameForNotice (stack)
35{
36 return stackDisplayLabel(stack);
37}
38
39/** Prefer pigeon sender name; otherwise origin label (`Mined by Ada`, …). */
40export function sourceNameForVaultDeposit (stack, senderCharacterId, senderDisplayName)
41{
42 if (senderDisplayName)
43 { return senderDisplayName; }
44 if (senderCharacterId)
45 { return `character #${senderCharacterId}`; }
46 const origins = stack?.origins ?? (stack?.origin ? [{ origin: stack.origin, quantity: stack.quantity ?? 1 }] : []);
47 if (origins.length === 1 && origins[0]?.origin)
48 { return originLabel(origins[0].origin); }
49 return "unknown source";
50}
51
52/** Merge notice rows that share item name and source. */
53export function mergeBankRestNoticeLines (entries)
54{
55 const merged = new Map();
56 for (const entry of entries ?? [])
57 {
58 const key = `${entry.itemName}|${entry.sourceName}`;
59 const prev = merged.get(key);
60 if (prev)
61 { prev.quantity += entry.quantity | 0; }
62 else
63 { merged.set(key, { ...entry, quantity: entry.quantity | 0 }); }
64 }
65 return [...merged.values()];
66}