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