BlueGit

timfa questward

Merge branch 'feature/bank-partial-withdraw'

timfa Tim Falken · pushed by timfa

Authored 2026-09-04 · 2 months ago

Hash
328cdbc8e0cdf222cea39dbdb7fdea2e7048a81c
3 files changed +126 −15
server/src/items/bags.js +39 −13
@@ -23,7 +23,8 @@ import
23 23 stackQuantity,
24 24 consumeFillCharge,
25 25 moveInventorySlot,
26 stacksMatch
26 stacksMatch,
27 fitStackQuantity
27 28 } from "../../../shared/items/inventory.js";
28 29 import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";
29 30 import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
@@ -837,26 +838,51 @@ function bankMove (world, sessionId, command, fromKey, toKey)
837 838 if (slot?.itemId === ItemId.filledCheque)
838 839 { return redeemCheque(world, sessionId, command); }
839 840 }
840 const fromSlots = fromKey === "vault" ? vault.slots : inventory.slots;
841 const taken = takeFromSlot(fromSlots, command.slotIndex, command.quantity ?? null);
842 if (!taken.ok)
843 { throw userError("That slot is empty."); }
844 const def = itemDefinition(taken.taken.itemId);
845 841 if (toKey === "vault")
846 842 {
843 const taken = takeFromSlot(inventory.slots, command.slotIndex, command.quantity ?? null);
844 if (!taken.ok)
845 { throw userError("That slot is empty."); }
846 const def = itemDefinition(taken.taken.itemId);
847 847 const inserted = insertStackGrowable(vault.slots, taken.taken, def);
848 848 inventory.slots = taken.slots;
849 849 vault.slots = inserted.slots;
850 return { events: [], private: bagView(world.ecs, handle), bank: true };
850 851 }
851 else
852
853 const vaultSlot = vault.slots[command.slotIndex | 0];
854 if (!vaultSlot)
855 { throw userError("That slot is empty."); }
856 const def = itemDefinition(vaultSlot.itemId);
857 const available = stackQuantity(vaultSlot.quantity);
858 const want = command.quantity == null
859 ? available
860 : Math.min(available, Math.max(1, stackQuantity(command.quantity)));
861 const fit = fitStackQuantity(inventory.slots, {
862 itemId: vaultSlot.itemId,
863 quantity: want,
864 instance: vaultSlot.instance ?? null
865 }, def);
866 if (fit <= 0)
867 { throw userError("Not enough space."); }
868 const taken = takeFromSlot(vault.slots, command.slotIndex, fit);
869 if (!taken.ok)
870 { throw userError("That slot is empty."); }
871 const inserted = insertStack(inventory.slots, taken.taken, def);
872 if (!inserted.ok)
873 { throw userError("Not enough space."); }
874 vault.slots = taken.slots;
875 inventory.slots = inserted.slots;
876 const events = [];
877 if (fit < want)
852 878 {
853 const inserted = insertStack(inventory.slots, taken.taken, def);
854 if (!inserted.ok)
855 { throw userError("Not enough space."); }
856 vault.slots = taken.slots;
857 inventory.slots = inserted.slots;
879 events.push({
880 type: "command-error",
881 sessionId,
882 message: "Not enough space."
883 });
858 884 }
859 return { events: [], private: bagView(world.ecs, handle), bank: true };
885 return { events, private: bagView(world.ecs, handle), bank: true };
860 886 }
861 887
862 888 function getBlankCheques (world, sessionId, command)
@@ -23,7 +23,8 @@ import
23 stackQuantity,
23 stackQuantity,
24 consumeFillCharge,
24 consumeFillCharge,
25 moveInventorySlot,
25 moveInventorySlot,
26 stacksMatch
26 stacksMatch,
27 fitStackQuantity
27 } from "../../../shared/items/inventory.js";
28 } from "../../../shared/items/inventory.js";
28import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";
29import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";
29import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
30import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
@@ -837,26 +838,51 @@ function bankMove (world, sessionId, command, fromKey, toKey)
837 if (slot?.itemId === ItemId.filledCheque)
838 if (slot?.itemId === ItemId.filledCheque)
838 { return redeemCheque(world, sessionId, command); }
839 { return redeemCheque(world, sessionId, command); }
839 }
840 }
840 const fromSlots = fromKey === "vault" ? vault.slots : inventory.slots;
841 const taken = takeFromSlot(fromSlots, command.slotIndex, command.quantity ?? null);
842 if (!taken.ok)
843 { throw userError("That slot is empty."); }
844 const def = itemDefinition(taken.taken.itemId);
845 if (toKey === "vault")
841 if (toKey === "vault")
846 {
842 {
843 const taken = takeFromSlot(inventory.slots, command.slotIndex, command.quantity ?? null);
844 if (!taken.ok)
845 { throw userError("That slot is empty."); }
846 const def = itemDefinition(taken.taken.itemId);
847 const inserted = insertStackGrowable(vault.slots, taken.taken, def);
847 const inserted = insertStackGrowable(vault.slots, taken.taken, def);
848 inventory.slots = taken.slots;
848 inventory.slots = taken.slots;
849 vault.slots = inserted.slots;
849 vault.slots = inserted.slots;
850 return { events: [], private: bagView(world.ecs, handle), bank: true };
850 }
851 }
851 else
852
853 const vaultSlot = vault.slots[command.slotIndex | 0];
854 if (!vaultSlot)
855 { throw userError("That slot is empty."); }
856 const def = itemDefinition(vaultSlot.itemId);
857 const available = stackQuantity(vaultSlot.quantity);
858 const want = command.quantity == null
859 ? available
860 : Math.min(available, Math.max(1, stackQuantity(command.quantity)));
861 const fit = fitStackQuantity(inventory.slots, {
862 itemId: vaultSlot.itemId,
863 quantity: want,
864 instance: vaultSlot.instance ?? null
865 }, def);
866 if (fit <= 0)
867 { throw userError("Not enough space."); }
868 const taken = takeFromSlot(vault.slots, command.slotIndex, fit);
869 if (!taken.ok)
870 { throw userError("That slot is empty."); }
871 const inserted = insertStack(inventory.slots, taken.taken, def);
872 if (!inserted.ok)
873 { throw userError("Not enough space."); }
874 vault.slots = taken.slots;
875 inventory.slots = inserted.slots;
876 const events = [];
877 if (fit < want)
852 {
878 {
853 const inserted = insertStack(inventory.slots, taken.taken, def);
879 events.push({
854 if (!inserted.ok)
880 type: "command-error",
855 { throw userError("Not enough space."); }
881 sessionId,
856 vault.slots = taken.slots;
882 message: "Not enough space."
857 inventory.slots = inserted.slots;
883 });
858 }
884 }
859 return { events: [], private: bagView(world.ecs, handle), bank: true };
885 return { events, private: bagView(world.ecs, handle), bank: true };
860}
886}
861
887
862function getBlankCheques (world, sessionId, command)
888function getBlankCheques (world, sessionId, command)
server/tests/bank.test.js +43 −2
@@ -1,8 +1,9 @@
1 1 import test from "node:test";
2 2 import assert from "node:assert/strict";
3 3 import { BlockType } from "../../shared/definitions/tiles.js";
4 import { ItemId } from "../../shared/definitions/items.js";
5 import { emptyEquipment } from "../../shared/items/inventory.js";
4 import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
5 import { RangedItemId } from "../../shared/definitions/rangedItems.js";
6 import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
6 7 import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
7 8 import {
8 9 makeCharacter,
@@ -93,6 +94,46 @@ test("bank deposit and withdraw move stacks with quantity", () =>
93 94 { harness.close(); }
94 95 });
95 96
97 test("bank withdraw transfers what fits and still reports not enough space", () =>
98 {
99 const harness = makeSimulation({ workerCount: 1 });
100 try
101 {
102 placeBank(harness.worldDb, 8, 8);
103 harness.simulation.reloadWorld();
104 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
105 const entityId = harness.simulation.spawnCharacter(1, character);
106 const tipId = RangedItemId.bronzeArrowTip;
107 assert.equal(itemDefinition(tipId).maxStackSize, 99);
108
109 const inventory = emptySlots();
110 inventory[0] = { itemId: ItemId.apple, quantity: 1 };
111 for (let i = 2; i < inventory.length; i++)
112 { inventory[i] = { itemId: ItemId.paper, quantity: 1 }; }
113 harness.simulation.applyBag(entityId, {
114 inventory,
115 equipment: emptyEquipment(),
116 vault: [{ itemId: tipId, quantity: 120 }],
117 vaultReserved: []
118 });
119
120 harness.simulation.enqueue(1, {
121 type: "bank-withdraw",
122 characterId: character.character_id,
123 slotIndex: 0,
124 quantity: 120
125 });
126 const events = harness.simulation.step().events;
127 const bag = harness.simulation.bagOf(entityId);
128 assert.equal(countItem(bag.inventory, tipId), 99);
129 assert.equal(countItem(bag.vault, tipId), 21);
130 assert.ok(events.some((event) =>
131 event.type === "command-error" && event.message === "Not enough space."));
132 }
133 finally
134 { harness.close(); }
135 });
136
96 137 test("write cheque reserves vault goods and redeem returns them", () =>
97 138 {
98 139 const harness = makeSimulation({ workerCount: 1 });
@@ -1,8 +1,9 @@
1import test from "node:test";
1import test from "node:test";
2import assert from "node:assert/strict";
2import assert from "node:assert/strict";
3import { BlockType } from "../../shared/definitions/tiles.js";
3import { BlockType } from "../../shared/definitions/tiles.js";
4import { ItemId } from "../../shared/definitions/items.js";
4import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
5import { emptyEquipment } from "../../shared/items/inventory.js";
5import { RangedItemId } from "../../shared/definitions/rangedItems.js";
6import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
6import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
7import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
7import {
8import {
8 makeCharacter,
9 makeCharacter,
@@ -93,6 +94,46 @@ test("bank deposit and withdraw move stacks with quantity", () =>
93 { harness.close(); }
94 { harness.close(); }
94});
95});
95
96
97test("bank withdraw transfers what fits and still reports not enough space", () =>
98{
99 const harness = makeSimulation({ workerCount: 1 });
100 try
101 {
102 placeBank(harness.worldDb, 8, 8);
103 harness.simulation.reloadWorld();
104 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
105 const entityId = harness.simulation.spawnCharacter(1, character);
106 const tipId = RangedItemId.bronzeArrowTip;
107 assert.equal(itemDefinition(tipId).maxStackSize, 99);
108
109 const inventory = emptySlots();
110 inventory[0] = { itemId: ItemId.apple, quantity: 1 };
111 for (let i = 2; i < inventory.length; i++)
112 { inventory[i] = { itemId: ItemId.paper, quantity: 1 }; }
113 harness.simulation.applyBag(entityId, {
114 inventory,
115 equipment: emptyEquipment(),
116 vault: [{ itemId: tipId, quantity: 120 }],
117 vaultReserved: []
118 });
119
120 harness.simulation.enqueue(1, {
121 type: "bank-withdraw",
122 characterId: character.character_id,
123 slotIndex: 0,
124 quantity: 120
125 });
126 const events = harness.simulation.step().events;
127 const bag = harness.simulation.bagOf(entityId);
128 assert.equal(countItem(bag.inventory, tipId), 99);
129 assert.equal(countItem(bag.vault, tipId), 21);
130 assert.ok(events.some((event) =>
131 event.type === "command-error" && event.message === "Not enough space."));
132 }
133 finally
134 { harness.close(); }
135});
136
96test("write cheque reserves vault goods and redeem returns them", () =>
137test("write cheque reserves vault goods and redeem returns them", () =>
97{
138{
98 const harness = makeSimulation({ workerCount: 1 });
139 const harness = makeSimulation({ workerCount: 1 });
shared/items/inventory.js +44 −0
@@ -173,6 +173,50 @@ export function insertStack (slots, stack, definition, options = {})
173 173 return { ok: false, slots, remainder: remaining };
174 174 }
175 175
176 /** How many of `stack` can fit into `slots` under bag/vault stack rules. */
177 export function fitStackQuantity (slots, stack, definition, options = {})
178 {
179 if (!stack)
180 { return 0; }
181 const want = stackQuantity(stack.quantity);
182 if (want <= 0)
183 { return 0; }
184 const rules = stackRulesFor(definition, options);
185 let space = 0;
186
187 if (rules.stackable && !rules.uniqueInstance)
188 {
189 for (const slot of slots ?? [])
190 {
191 if (!slot || !stacksMatch(slot, stack, definition, options))
192 { continue; }
193 space += Math.max(0, rules.maxStackSize - stackQuantity(slot.quantity));
194 if (space >= want)
195 { return want; }
196 }
197 }
198
199 for (const slot of slots ?? [])
200 {
201 if (slot)
202 { continue; }
203 if (rules.stackable && !rules.uniqueInstance)
204 {
205 const cap = rules.maxStackSize === Number.POSITIVE_INFINITY
206 ? want - space
207 : rules.maxStackSize;
208 space += Math.max(0, cap);
209 }
210 else
211 { space += 1; }
212 if (space >= want)
213 { return want; }
214 if (!rules.stackable || rules.uniqueInstance)
215 { break; }
216 }
217 return Math.min(want, space);
218 }
219
176 220 export function takeFromSlot (slots, slotIndex, quantity = null)
177 221 {
178 222 const next = slots.map(cloneStack);
@@ -173,6 +173,50 @@ export function insertStack (slots, stack, definition, options = {})
173 return { ok: false, slots, remainder: remaining };
173 return { ok: false, slots, remainder: remaining };
174}
174}
175
175
176/** How many of `stack` can fit into `slots` under bag/vault stack rules. */
177export function fitStackQuantity (slots, stack, definition, options = {})
178{
179 if (!stack)
180 { return 0; }
181 const want = stackQuantity(stack.quantity);
182 if (want <= 0)
183 { return 0; }
184 const rules = stackRulesFor(definition, options);
185 let space = 0;
186
187 if (rules.stackable && !rules.uniqueInstance)
188 {
189 for (const slot of slots ?? [])
190 {
191 if (!slot || !stacksMatch(slot, stack, definition, options))
192 { continue; }
193 space += Math.max(0, rules.maxStackSize - stackQuantity(slot.quantity));
194 if (space >= want)
195 { return want; }
196 }
197 }
198
199 for (const slot of slots ?? [])
200 {
201 if (slot)
202 { continue; }
203 if (rules.stackable && !rules.uniqueInstance)
204 {
205 const cap = rules.maxStackSize === Number.POSITIVE_INFINITY
206 ? want - space
207 : rules.maxStackSize;
208 space += Math.max(0, cap);
209 }
210 else
211 { space += 1; }
212 if (space >= want)
213 { return want; }
214 if (!rules.stackable || rules.uniqueInstance)
215 { break; }
216 }
217 return Math.min(want, space);
218}
219
176export function takeFromSlot (slots, slotIndex, quantity = null)
220export function takeFromSlot (slots, slotIndex, quantity = null)
177{
221{
178 const next = slots.map(cloneStack);
222 const next = slots.map(cloneStack);