Withdraw as much as inventory can hold when bank space is short.
Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -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 @@ import23
stackQuantity,23
stackQuantity,24
consumeFillCharge,24
consumeFillCharge,25
moveInventorySlot,25
moveInventorySlot,26
stacksMatch26
stacksMatch,27
fitStackQuantity27
} from "../../../shared/items/inventory.js";28
} from "../../../shared/items/inventory.js";28
import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";29
import { tileKey, tileToChunk, tileToLocal } from "../world/coordinates.js";29
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";30
import { 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
else852
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 == null859
? available860
: 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 ?? null865
}, 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
862
function getBlankCheques (world, sessionId, command)888
function getBlankCheques (world, sessionId, command)@@ -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 @@1
import test from "node:test";1
import test from "node:test";2
import assert from "node:assert/strict";2
import assert from "node:assert/strict";3
import { BlockType } from "../../shared/definitions/tiles.js";3
import { BlockType } from "../../shared/definitions/tiles.js";4
import { ItemId } from "../../shared/definitions/items.js";4
import { ItemId, itemDefinition } from "../../shared/definitions/items.js";5
import { emptyEquipment } from "../../shared/items/inventory.js";5
import { RangedItemId } from "../../shared/definitions/rangedItems.js";6
import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";6
import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";7
import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";7
import {8
import {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
97
test("bank withdraw transfers what fits and still reports not enough space", () =>98
{99
const harness = makeSimulation({ workerCount: 1 });100
try101
{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: 120125
});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
finally134
{ harness.close(); }135
});136
96
test("write cheque reserves vault goods and redeem returns them", () =>137
test("write cheque reserves vault goods and redeem returns them", () =>97
{138
{98
const harness = makeSimulation({ workerCount: 1 });139
const harness = makeSimulation({ workerCount: 1 });