BlueGit

timfa questward

Files

questward / shared / market / funds.js

funds.js

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