| 1 | export const INVENTORY_SLOTS = 32; |
| 2 | export const EQUIPMENT_SLOTS = Object.freeze(["head", "chest", "legs", "boots", "weapon", "offhand"]); |
| 3 | |
| 4 | export function emptySlots (count = INVENTORY_SLOTS) |
| 5 | { |
| 6 | return Array.from({ length: count }, () => null); |
| 7 | } |
| 8 | |
| 9 | export function emptyEquipment () |
| 10 | { |
| 11 | const slots = {}; |
| 12 | for (const slot of EQUIPMENT_SLOTS) |
| 13 | { slots[slot] = null; } |
| 14 | return { slots }; |
| 15 | } |
| 16 | |
| 17 | export function cloneStack (stack) |
| 18 | { |
| 19 | return stack ? structuredClone(stack) : null; |
| 20 | } |
| 21 | |
| 22 | export function stacksMatch (left, right) |
| 23 | { |
| 24 | if (!left || !right) |
| 25 | { return false; } |
| 26 | if (left.itemId !== right.itemId) |
| 27 | { return false; } |
| 28 | return JSON.stringify(left.origin ?? null) === JSON.stringify(right.origin ?? null) |
| 29 | && JSON.stringify(left.instance ?? null) === JSON.stringify(right.instance ?? null); |
| 30 | } |
| 31 | |
| 32 | export function insertStack (slots, stack, definition) |
| 33 | { |
| 34 | if (!stack || stack.quantity <= 0) |
| 35 | { return { ok: true, slots: slots.map(cloneStack), remainder: 0 }; } |
| 36 | const next = slots.map(cloneStack); |
| 37 | let remaining = stack.quantity; |
| 38 | if (definition.stackable && !definition.uniqueInstance) |
| 39 | { |
| 40 | for (const slot of next) |
| 41 | { |
| 42 | if (!stacksMatch(slot, stack)) |
| 43 | { continue; } |
| 44 | const space = (definition.maxStackSize ?? 99) - slot.quantity; |
| 45 | if (space <= 0) |
| 46 | { continue; } |
| 47 | const add = Math.min(space, remaining); |
| 48 | slot.quantity += add; |
| 49 | remaining -= add; |
| 50 | if (remaining === 0) |
| 51 | { return { ok: true, slots: next, remainder: 0 }; } |
| 52 | } |
| 53 | } |
| 54 | for (let i = 0; i < next.length; i++) |
| 55 | { |
| 56 | if (next[i]) |
| 57 | { continue; } |
| 58 | const amount = definition.stackable && !definition.uniqueInstance |
| 59 | ? Math.min(remaining, definition.maxStackSize ?? 99) |
| 60 | : 1; |
| 61 | next[i] = { |
| 62 | itemId: stack.itemId, |
| 63 | quantity: amount, |
| 64 | origin: stack.origin ?? null, |
| 65 | instance: stack.instance ?? null |
| 66 | }; |
| 67 | remaining -= amount; |
| 68 | if (remaining === 0) |
| 69 | { return { ok: true, slots: next, remainder: 0 }; } |
| 70 | if (!definition.stackable || definition.uniqueInstance) |
| 71 | { break; } |
| 72 | } |
| 73 | return { ok: false, slots, remainder: remaining }; |
| 74 | } |
| 75 | |
| 76 | export function takeFromSlot (slots, slotIndex, quantity = null) |
| 77 | { |
| 78 | const next = slots.map(cloneStack); |
| 79 | const slot = next[slotIndex]; |
| 80 | if (!slot) |
| 81 | { return { ok: false, slots, taken: null }; } |
| 82 | const amount = quantity == null ? slot.quantity : Math.min(slot.quantity, Math.max(1, quantity | 0)); |
| 83 | if (amount <= 0) |
| 84 | { return { ok: false, slots, taken: null }; } |
| 85 | const taken = { ...cloneStack(slot), quantity: amount }; |
| 86 | if (amount >= slot.quantity) |
| 87 | { next[slotIndex] = null; } |
| 88 | else |
| 89 | { slot.quantity -= amount; } |
| 90 | return { ok: true, slots: next, taken }; |
| 91 | } |
| 92 | |
| 93 | export function publicSlots (slots) |
| 94 | { |
| 95 | return (slots ?? emptySlots()).map((slot) => slot ? { |
| 96 | itemId: slot.itemId, |
| 97 | quantity: slot.quantity, |
| 98 | origin: slot.origin ?? null, |
| 99 | instance: slot.instance ?? null |
| 100 | } : null); |
| 101 | } |
| 102 | |
| 103 | export function publicEquipment (equipment) |
| 104 | { |
| 105 | const slots = {}; |
| 106 | for (const slot of EQUIPMENT_SLOTS) |
| 107 | { slots[slot] = equipment?.slots?.[slot] ? cloneStack(equipment.slots[slot]) : null; } |
| 108 | return { slots }; |
| 109 | } |