import { ItemId, itemDefinition, itemDisplayName } from "../definitions/items.js"; import { listingKeyFromStack } from "./listingKey.js"; import { cloneStack, stackQuantity, takeFromSlot } from "../items/inventory.js"; import { isPartiallyConsumed } from "../items/charges.js"; import { chequeIsExpired } from "../items/cheque.js"; /** Filled cheque that reserves coins (usable as market payment, whole only). */ export function isCoinChequeStack (stack) { if ((stack?.itemId | 0) !== ItemId.filledCheque) { return false; } if (chequeIsExpired(stack)) { return false; } const instance = stack.instance; return instance?.kind === "filled-cheque" && (instance.itemId | 0) === ItemId.coins; } export function coinChequeAmount (stack) { if (!isCoinChequeStack(stack)) { return 0; } return Math.max(0, stack.instance.quantity | 0); } /** * Pay `amount` using coin stacks (partial OK) then whole coin-cheques only. * Cheques are never partially spent: if a cheque exceeds the remaining debt, * the surplus is returned as `change` (caller adds coins back). * Taken cheque stacks are listed in `redeemedCheques` so the server can release * vault reservations. */ export function takeMarketFunds (slots, amount) { let remaining = stackQuantity(amount); const redeemedCheques = []; if (!remaining) { return { ok: true, slots: (slots ?? []).map(cloneStack), change: 0, redeemedCheques }; } let next = (slots ?? []).map(cloneStack); let change = 0; for (let i = 0; i < next.length && remaining > 0; i++) { if (!next[i] || next[i].itemId !== ItemId.coins) { continue; } const taken = takeFromSlot(next, i, remaining); if (!taken.ok) { continue; } next = taken.slots; remaining -= stackQuantity(taken.taken?.quantity); } while (remaining > 0) { let fitIndex = -1; let fitAmount = 0; let coverIndex = -1; let coverAmount = 0; for (let i = 0; i < next.length; i++) { const face = coinChequeAmount(next[i]); if (face <= 0) { continue; } if (face <= remaining && face > fitAmount) { fitIndex = i; fitAmount = face; } if (face >= remaining && (coverIndex < 0 || face < coverAmount)) { coverIndex = i; coverAmount = face; } } const useIndex = fitIndex >= 0 ? fitIndex : coverIndex; const useAmount = fitIndex >= 0 ? fitAmount : coverAmount; if (useIndex < 0) { return { ok: false, slots: (slots ?? []).map(cloneStack), change: 0, redeemedCheques: [] }; } const taken = takeFromSlot(next, useIndex, 1); if (!taken.ok || !taken.taken) { return { ok: false, slots: (slots ?? []).map(cloneStack), change: 0, redeemedCheques: [] }; } next = taken.slots; redeemedCheques.push(cloneStack(taken.taken)); if (useAmount >= remaining) { change += useAmount - remaining; remaining = 0; } else { remaining -= useAmount; } } return { ok: true, slots: next, change, redeemedCheques }; } export function countMarketFunds (slots) { let total = 0; for (const slot of slots ?? []) { if (!slot) { continue; } if (slot.itemId === ItemId.coins) { total += stackQuantity(slot.quantity); } else { total += coinChequeAmount(slot); } } return total; } /** Resolve what a market sell order lists from a bag slot. */ export function resolveMarketSellListing (stack) { if (!stack?.itemId) { return null; } if ((stack.itemId | 0) === ItemId.coins) { return null; } if ((stack.itemId | 0) === ItemId.filledCheque) { const instance = stack.instance; if (instance?.kind !== "filled-cheque") { return null; } const goodsItemId = instance.itemId | 0; const goodsQuantity = Math.max(1, instance.quantity | 0); if (!goodsItemId || !itemDefinition(goodsItemId) || goodsItemId === ItemId.coins) { return null; } return { isCheque: true, itemId: goodsItemId, quantity: goodsQuantity, quantityLocked: true, displayName: itemDefinition(goodsItemId)?.displayName ?? "item", escrowStack: cloneStack(stack) }; } const def = itemDefinition(stack.itemId); if (!def || def.marketplaceSellable === false) { return null; } if (isPartiallyConsumed(stack)) { return null; } return { isCheque: false, itemId: stack.itemId | 0, listingKey: listingKeyFromStack(stack), quantity: Math.max(1, stack.quantity | 0), quantityLocked: (stack.itemId | 0) === ItemId.recipePaper, displayName: itemDisplayName(stack, def), escrowStack: null }; } export function isFilledChequeEscrow (stack) { return (stack?.itemId | 0) === ItemId.filledCheque && stack?.instance?.kind === "filled-cheque" && !chequeIsExpired(stack); }