BlueGit

timfa questward

Files

questward / shared / market / funds.js

funds.js

rweaks
timfa Tim Falken · pushed by timfa · 2 months ago
2b34e2dd History
funds.js 4.1 KiB Download Raw
1import { ItemId, itemDefinition } from "../definitions/items.js";
2import { cloneStack, stackQuantity, takeFromSlot } from "../items/inventory.js";
3
4/** Filled cheque that reserves coins (usable as market payment, whole only). */
5export function isCoinChequeStack (stack)
6{
7 if ((stack?.itemId | 0) !== ItemId.filledCheque)
8 { return false; }
9 const instance = stack.instance;
10 return instance?.kind === "filled-cheque" && (instance.itemId | 0) === ItemId.coins;
11}
12
13export function coinChequeAmount (stack)
14{
15 if (!isCoinChequeStack(stack))
16 { return 0; }
17 return Math.max(0, stack.instance.quantity | 0);
18}
19
20/**
21 * Pay `amount` using coin stacks (partial OK) then whole coin-cheques only.
22 * Cheques are never partially spent: if a cheque exceeds the remaining debt,
23 * the surplus is returned as `change` (caller adds coins back).
24 * Taken cheque stacks are listed in `redeemedCheques` so the server can release
25 * vault reservations.
26 */
27export function takeMarketFunds (slots, amount)
28{
29 let remaining = stackQuantity(amount);
30 const redeemedCheques = [];
31 if (!remaining)
32 {
33 return {
34 ok: true,
35 slots: (slots ?? []).map(cloneStack),
36 change: 0,
37 redeemedCheques
38 };
39 }
40
41 let next = (slots ?? []).map(cloneStack);
42 let change = 0;
43
44 for (let i = 0; i < next.length && remaining > 0; i++)
45 {
46 if (!next[i] || next[i].itemId !== ItemId.coins)
47 { continue; }
48 const taken = takeFromSlot(next, i, remaining);
49 if (!taken.ok)
50 { continue; }
51 next = taken.slots;
52 remaining -= stackQuantity(taken.taken?.quantity);
53 }
54
55 while (remaining > 0)
56 {
57 let fitIndex = -1;
58 let fitAmount = 0;
59 let coverIndex = -1;
60 let coverAmount = 0;
61 for (let i = 0; i < next.length; i++)
62 {
63 const face = coinChequeAmount(next[i]);
64 if (face <= 0)
65 { continue; }
66 if (face <= remaining && face > fitAmount)
67 {
68 fitIndex = i;
69 fitAmount = face;
70 }
71 if (face >= remaining && (coverIndex < 0 || face < coverAmount))
72 {
73 coverIndex = i;
74 coverAmount = face;
75 }
76 }
77 const useIndex = fitIndex >= 0 ? fitIndex : coverIndex;
78 const useAmount = fitIndex >= 0 ? fitAmount : coverAmount;
79 if (useIndex < 0)
80 {
81 return {
82 ok: false,
83 slots: (slots ?? []).map(cloneStack),
84 change: 0,
85 redeemedCheques: []
86 };
87 }
88 const taken = takeFromSlot(next, useIndex, 1);
89 if (!taken.ok || !taken.taken)
90 {
91 return {
92 ok: false,
93 slots: (slots ?? []).map(cloneStack),
94 change: 0,
95 redeemedCheques: []
96 };
97 }
98 next = taken.slots;
99 redeemedCheques.push(cloneStack(taken.taken));
100 if (useAmount >= remaining)
101 {
102 change += useAmount - remaining;
103 remaining = 0;
104 }
105 else
106 { remaining -= useAmount; }
107 }
108
109 return { ok: true, slots: next, change, redeemedCheques };
110}
111
112export function countMarketFunds (slots)
113{
114 let total = 0;
115 for (const slot of slots ?? [])
116 {
117 if (!slot)
118 { continue; }
119 if (slot.itemId === ItemId.coins)
120 { total += stackQuantity(slot.quantity); }
121 else
122 { total += coinChequeAmount(slot); }
123 }
124 return total;
125}
126
127/** Resolve what a market sell order lists from a bag slot. */
128export function resolveMarketSellListing (stack)
129{
130 if (!stack?.itemId)
131 { return null; }
132 if ((stack.itemId | 0) === ItemId.coins)
133 { return null; }
134 if ((stack.itemId | 0) === ItemId.filledCheque)
135 {
136 const instance = stack.instance;
137 if (instance?.kind !== "filled-cheque")
138 { return null; }
139 const goodsItemId = instance.itemId | 0;
140 const goodsQuantity = Math.max(1, instance.quantity | 0);
141 if (!goodsItemId || !itemDefinition(goodsItemId) || goodsItemId === ItemId.coins)
142 { return null; }
143 return {
144 isCheque: true,
145 itemId: goodsItemId,
146 quantity: goodsQuantity,
147 quantityLocked: true,
148 displayName: itemDefinition(goodsItemId)?.displayName ?? "item",
149 escrowStack: cloneStack(stack)
150 };
151 }
152 const def = itemDefinition(stack.itemId);
153 if (!def || def.marketplaceSellable === false)
154 { return null; }
155 return {
156 isCheque: false,
157 itemId: stack.itemId | 0,
158 quantity: Math.max(1, stack.quantity | 0),
159 quantityLocked: false,
160 displayName: def.displayName,
161 escrowStack: null
162 };
163}
164
165export function isFilledChequeEscrow (stack)
166{
167 return (stack?.itemId | 0) === ItemId.filledCheque
168 && stack?.instance?.kind === "filled-cheque";
169}