BlueGit

timfa questward

Files

questward / shared / quests / dialogueRewards.js

dialogueRewards.js

dialogueRewards.js 1.6 KiB Download Raw
1/**
2 * Format dialogue step item-reward notices (bag vs bank).
3 * @param {{ displayName: string, quantity: number }[]} items
4 * @param {"bag" | "bank"} destination
5 */
6export function formatDialogueRewardMessage (items, destination)
7{
8 const list = (Array.isArray(items) ? items : [])
9 .filter((entry) => entry && entry.displayName);
10 if (!list.length)
11 { return null; }
12 const labels = list.map((entry) =>
13 {
14 const qty = Math.max(1, Number(entry.quantity) || 1);
15 const name = String(entry.displayName);
16 return qty > 1 ? `${qty} ${name}` : name;
17 });
18 const subject = labels.length === 1
19 ? labels[0]
20 : labels.length === 2
21 ? `${labels[0]} and ${labels[1]}`
22 : `${labels.slice(0, -1).join(", ")} and ${labels[labels.length - 1]}`;
23 const plural = labels.length > 1 || (list[0].quantity | 0) > 1;
24 const verb = plural ? "have" : "has";
25 const where = destination === "bank"
26 ? "sent to your bank"
27 : "added to your bag";
28 return `${subject} ${verb} been ${where}.`;
29}
30
31/**
32 * Format dialogue step quest-reward notices (player-facing).
33 * @param {string[]} questTitles
34 */
35export function formatDialogueQuestRewardMessage (questTitles)
36{
37 const list = (Array.isArray(questTitles) ? questTitles : [])
38 .map((title) => String(title ?? "").trim())
39 .filter(Boolean);
40 if (!list.length)
41 { return null; }
42 const subject = list.length === 1
43 ? `The ${list[0]} quest`
44 : list.length === 2
45 ? `The ${list[0]} and ${list[1]} quests`
46 : `The ${list.slice(0, -1).join(", ")}, and ${list[list.length - 1]} quests`;
47 const verb = list.length > 1 ? "have" : "has";
48 return `${subject} ${verb} been added to your journal.`;
49}