BlueGit

timfa questward

Files

questward / shared / items / cheque.js

cheque.js

cheque.js 3.1 KiB Download Raw
1/**
2 * Cheque identity, lifetime, and reserved-goods helpers.
3 * Matching is exact on itemId + full instance JSON so future metadata fields
4 * automatically participate in which vault stacks a cheque can reserve.
5 *
6 * Note: filled cheque item id is duplicated here to avoid a circular import with
7 * `definitions/items.js` (`ItemId.filledCheque === 7`).
8 */
9
10/** @see ItemId.filledCheque in definitions/items.js */
11export const FILLED_CHEQUE_ITEM_ID = 7;
12
13/** Written cheques lapse after 30 calendar days from `createdAt`. */
14export const CHEQUE_LIFETIME_MS = 30 * 24 * 60 * 60 * 1000;
15
16/** Stable identity for cheque reservation / vault selection (ignores qty & origins). */
17export function stackIdentityKey (stack)
18{
19 return JSON.stringify({
20 itemId: stack?.itemId ?? null,
21 instance: stack?.instance ?? null
22 });
23}
24
25/** True when two stacks are the same item with identical instance metadata. */
26export function stacksIdentical (left, right)
27{
28 if (!left || !right)
29 { return false; }
30 return stackIdentityKey(left) === stackIdentityKey(right);
31}
32
33export function countIdenticalInSlots (slots, reference)
34{
35 if (!reference?.itemId)
36 { return 0; }
37 let total = 0;
38 for (const slot of slots ?? [])
39 {
40 if (!stacksIdentical(slot, reference))
41 { continue; }
42 total += Math.max(0, Math.floor(Number(slot.quantity) || 0));
43 }
44 return total;
45}
46
47export function isFilledChequeStack (stack)
48{
49 return (stack?.itemId | 0) === FILLED_CHEQUE_ITEM_ID
50 && stack?.instance?.kind === "filled-cheque";
51}
52
53export function chequeCreatedAtMs (stack)
54{
55 if (stack?.instance?.createdAt == null || stack.instance.createdAt === "")
56 { return null; }
57 const created = Number(stack.instance.createdAt);
58 return Number.isFinite(created) ? created : null;
59}
60
61export function chequeExpiresAtMs (stack)
62{
63 const created = chequeCreatedAtMs(stack);
64 return created == null ? 0 : created + CHEQUE_LIFETIME_MS;
65}
66
67export function chequeIsExpired (stack, now = Date.now())
68{
69 if (!isFilledChequeStack(stack))
70 { return false; }
71 const created = chequeCreatedAtMs(stack);
72 if (created == null)
73 { return false; }
74 return now >= created + CHEQUE_LIFETIME_MS;
75}
76
77/** Whole days remaining until expiry (ceil). 0 if already expired; null if unknown. */
78export function chequeDaysRemaining (stack, now = Date.now())
79{
80 if (!isFilledChequeStack(stack))
81 { return null; }
82 const created = chequeCreatedAtMs(stack);
83 if (created == null)
84 { return null; }
85 const ms = created + CHEQUE_LIFETIME_MS - now;
86 if (ms <= 0)
87 { return 0; }
88 return Math.ceil(ms / (24 * 60 * 60 * 1000));
89}
90
91export function chequeExpiryTooltipLine (stack, now = Date.now())
92{
93 const days = chequeDaysRemaining(stack, now);
94 if (days == null)
95 { return ""; }
96 if (days <= 0)
97 { return "Expired."; }
98 if (days === 1)
99 { return "Expires in 1 day."; }
100 return `Expires in ${days} days.`;
101}
102
103/** Stack shape for the goods reserved on a filled cheque (icons / names). */
104export function chequeReservedGoodsStack (chequeStack)
105{
106 const instance = chequeStack?.instance;
107 if (!instance || instance.kind !== "filled-cheque")
108 { return null; }
109 return {
110 itemId: instance.itemId | 0,
111 quantity: Math.max(1, instance.quantity | 0 || 1),
112 instance: instance.reservedInstance ?? null
113 };
114}