BlueGit

timfa questward

Files

questward / shared / market / funds.js

funds.js

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