BlueGit

timfa questward

Free cheque vault reservations when the paper despawns.

timfa Tim Falken · pushed by timfa

Authored 2026-09-04 · 2 months ago

Hash
f8e5e63d7198b093371c1f17e486a48eecbc5977
Parent
84c23329
3 files changed +135 −5
server/src/items/bags.js +3 −0
@@ -195,6 +195,9 @@ export function expireGroundItems (world, now)
195 195 {
196 196 if (item.expiresAt > now)
197 197 { continue; }
198 // Destroyed paper frees reserved vault goods; a live ground cheque keeps them reserved.
199 if (item.stack?.itemId === ItemId.filledCheque && item.stack?.instance?.kind === "filled-cheque")
200 { world.voidFilledCheque?.(item.stack); }
198 201 removeGroundItem(world, item.groundItemId);
199 202 events.push({ type: "ground-item-despawn", ...publicGroundItem(item) });
200 203 }
@@ -195,6 +195,9 @@ export function expireGroundItems (world, now)
195 {
195 {
196 if (item.expiresAt > now)
196 if (item.expiresAt > now)
197 { continue; }
197 { continue; }
198 // Destroyed paper frees reserved vault goods; a live ground cheque keeps them reserved.
199 if (item.stack?.itemId === ItemId.filledCheque && item.stack?.instance?.kind === "filled-cheque")
200 { world.voidFilledCheque?.(item.stack); }
198 removeGroundItem(world, item.groundItemId);
201 removeGroundItem(world, item.groundItemId);
199 events.push({ type: "ground-item-despawn", ...publicGroundItem(item) });
202 events.push({ type: "ground-item-despawn", ...publicGroundItem(item) });
200 }
203 }
server/src/simulation/simulation.js +65 −5
@@ -31,7 +31,7 @@ import { ThreadWorker } from "./threadWorker.js";
31 31 import { WorkerMessage } from "./protocol.js";
32 32 import { replicaNeededForOwnedChunks } from "./workerWorld.js";
33 33 import { replicationAppearance, replicationDespawn, replicationSpawn } from "../ecs/serialize.js";
34 import { emptyEquipment, emptySlots } from "../../../shared/items/inventory.js";
34 import { emptyEquipment, emptySlots, insertStackGrowable } from "../../../shared/items/inventory.js";
35 35 import { attachOrigins, lootOrigin } from "../../../shared/items/origin.js";
36 36 import { skullValue, decayKillCounter, SKULL_KILL_BONUS } from "../../../shared/combat/skull.js";
37 37 import { COMBAT_LOGOUT_MS } from "../../../shared/combat/combatLogout.js";
@@ -49,7 +49,7 @@ import { defaultPlayerSkills, enforceMinimumSkillLevels } from "../../../shared/
49 49 import { defaultPlayerSpells, publicSpellsView } from "../../../shared/spells/state.js";
50 50 import { playerMaxHealth, playerMaxStamina } from "../../../shared/definitions/skills.js";
51 51 import { BlockType } from "../../../shared/definitions/tiles.js";
52 import { ItemId } from "../../../shared/definitions/items.js";
52 import { ItemId, itemDefinition } from "../../../shared/definitions/items.js";
53 53 import { seasonIdAt } from "../../../shared/world/season.js";
54 54 import { CourierService } from "../courier/service.js";
55 55 import { MarketService } from "../market/service.js";
@@ -116,8 +116,10 @@ export class Simulation
116 116 {
117 117 if (!worker.world)
118 118 { continue; }
119 worker.world.releaseChequeReservation = (issuerId, chequeId) => this.releaseChequeReservation(issuerId, chequeId);
119 worker.world.releaseChequeReservation = (issuerId, chequeId, instance) =>
120 this.releaseChequeReservation(issuerId, chequeId, instance);
120 121 worker.world.restoreChequeReservation = (issuerId, stack) => this.restoreChequeReservation(issuerId, stack);
122 worker.world.voidFilledCheque = (stack) => this.voidFilledCheque(stack);
121 123 }
122 124 this.courier = new CourierService({
123 125 stateDb: this.stateDb,
@@ -3009,8 +3011,17 @@ export class Simulation
3009 3011 #restoreGroundItems (regionX, regionY, workerId)
3010 3012 {
3011 3013 const bounds = regionTileBounds(regionX, regionY);
3012 const items = this.stateDb.listGroundItemsInRegion(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY)
3013 .filter((item) => item.expiresAt > this.now);
3014 const items = [];
3015 for (const item of this.stateDb.listGroundItemsInRegion(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY))
3016 {
3017 if (item.expiresAt > this.now)
3018 {
3019 items.push(item);
3020 continue;
3021 }
3022 // Expired while unloaded: destroy and free any cheque reservation.
3023 this.#destroyExpiredGroundItem(item);
3024 }
3014 3025 this.nextGroundItemId = Math.max(this.nextGroundItemId, this.stateDb.nextGroundItemId());
3015 3026 this.workers[workerId].call({
3016 3027 type: WorkerMessage.setGroundItems,
@@ -3020,6 +3031,55 @@ export class Simulation
3020 3031 });
3021 3032 }
3022 3033
3034 /** Cheque paper that no longer exists must free reserved vault goods. */
3035 voidFilledCheque (stack)
3036 {
3037 if ((stack?.itemId | 0) !== ItemId.filledCheque || stack?.instance?.kind !== "filled-cheque")
3038 { return { ok: false }; }
3039 const issuerId = stack.instance.issuerCharacterId | 0;
3040 const chequeId = stack.instance.chequeId;
3041 const release = this.releaseChequeReservation(issuerId, chequeId, stack.instance);
3042 if (!release?.ok || !release.stack)
3043 {
3044 // Already redeemed/voided — treat as success so despawn stays idempotent.
3045 return { ok: true, alreadyReleased: true };
3046 }
3047 const onlineId = this.characterEntities.get(issuerId);
3048 const bag = onlineId ? this.bagOf(onlineId) : this.stateDb.getBag(issuerId);
3049 if (!bag)
3050 { return { ok: false, error: "Issuer vault not found." }; }
3051 const goodsDef = itemDefinition(release.stack.itemId);
3052 if (!goodsDef)
3053 { return { ok: false, error: "Unknown reserved goods." }; }
3054 const goods = attachOrigins({
3055 itemId: release.stack.itemId,
3056 quantity: release.stack.quantity,
3057 instance: release.stack.instance ? structuredClone(release.stack.instance) : null,
3058 origins: release.stack.origins
3059 });
3060 const inserted = insertStackGrowable(bag.vault ?? [], goods, goodsDef);
3061 bag.vault = inserted.slots;
3062 this.stateDb.saveBag(issuerId, bag);
3063 if (onlineId)
3064 {
3065 this.applyBag(onlineId, bag);
3066 this.pushInventory(issuerId, bag);
3067 }
3068 this.pendingPrivate.push({
3069 type: "toast",
3070 characterId: issuerId,
3071 message: "An uncashed cheque expired. Reserved goods returned to your vault."
3072 });
3073 return { ok: true };
3074 }
3075
3076 #destroyExpiredGroundItem (item)
3077 {
3078 if (item?.stack?.itemId === ItemId.filledCheque && item.stack?.instance?.kind === "filled-cheque")
3079 { this.voidFilledCheque(item.stack); }
3080 this.stateDb.deleteGroundItem(item.groundItemId);
3081 }
3082
3023 3083 #persistGroundEvent (event)
3024 3084 {
3025 3085 if (event.type === "ground-item-appear")
@@ -31,7 +31,7 @@ import { ThreadWorker } from "./threadWorker.js";
31import { WorkerMessage } from "./protocol.js";
31import { WorkerMessage } from "./protocol.js";
32import { replicaNeededForOwnedChunks } from "./workerWorld.js";
32import { replicaNeededForOwnedChunks } from "./workerWorld.js";
33import { replicationAppearance, replicationDespawn, replicationSpawn } from "../ecs/serialize.js";
33import { replicationAppearance, replicationDespawn, replicationSpawn } from "../ecs/serialize.js";
34import { emptyEquipment, emptySlots } from "../../../shared/items/inventory.js";
34import { emptyEquipment, emptySlots, insertStackGrowable } from "../../../shared/items/inventory.js";
35import { attachOrigins, lootOrigin } from "../../../shared/items/origin.js";
35import { attachOrigins, lootOrigin } from "../../../shared/items/origin.js";
36import { skullValue, decayKillCounter, SKULL_KILL_BONUS } from "../../../shared/combat/skull.js";
36import { skullValue, decayKillCounter, SKULL_KILL_BONUS } from "../../../shared/combat/skull.js";
37import { COMBAT_LOGOUT_MS } from "../../../shared/combat/combatLogout.js";
37import { COMBAT_LOGOUT_MS } from "../../../shared/combat/combatLogout.js";
@@ -49,7 +49,7 @@ import { defaultPlayerSkills, enforceMinimumSkillLevels } from "../../../shared/
49import { defaultPlayerSpells, publicSpellsView } from "../../../shared/spells/state.js";
49import { defaultPlayerSpells, publicSpellsView } from "../../../shared/spells/state.js";
50import { playerMaxHealth, playerMaxStamina } from "../../../shared/definitions/skills.js";
50import { playerMaxHealth, playerMaxStamina } from "../../../shared/definitions/skills.js";
51import { BlockType } from "../../../shared/definitions/tiles.js";
51import { BlockType } from "../../../shared/definitions/tiles.js";
52import { ItemId } from "../../../shared/definitions/items.js";
52import { ItemId, itemDefinition } from "../../../shared/definitions/items.js";
53import { seasonIdAt } from "../../../shared/world/season.js";
53import { seasonIdAt } from "../../../shared/world/season.js";
54import { CourierService } from "../courier/service.js";
54import { CourierService } from "../courier/service.js";
55import { MarketService } from "../market/service.js";
55import { MarketService } from "../market/service.js";
@@ -116,8 +116,10 @@ export class Simulation
116 {
116 {
117 if (!worker.world)
117 if (!worker.world)
118 { continue; }
118 { continue; }
119 worker.world.releaseChequeReservation = (issuerId, chequeId) => this.releaseChequeReservation(issuerId, chequeId);
119 worker.world.releaseChequeReservation = (issuerId, chequeId, instance) =>
120 this.releaseChequeReservation(issuerId, chequeId, instance);
120 worker.world.restoreChequeReservation = (issuerId, stack) => this.restoreChequeReservation(issuerId, stack);
121 worker.world.restoreChequeReservation = (issuerId, stack) => this.restoreChequeReservation(issuerId, stack);
122 worker.world.voidFilledCheque = (stack) => this.voidFilledCheque(stack);
121 }
123 }
122 this.courier = new CourierService({
124 this.courier = new CourierService({
123 stateDb: this.stateDb,
125 stateDb: this.stateDb,
@@ -3009,8 +3011,17 @@ export class Simulation
3009 #restoreGroundItems (regionX, regionY, workerId)
3011 #restoreGroundItems (regionX, regionY, workerId)
3010 {
3012 {
3011 const bounds = regionTileBounds(regionX, regionY);
3013 const bounds = regionTileBounds(regionX, regionY);
3012 const items = this.stateDb.listGroundItemsInRegion(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY)
3014 const items = [];
3013 .filter((item) => item.expiresAt > this.now);
3015 for (const item of this.stateDb.listGroundItemsInRegion(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY))
3016 {
3017 if (item.expiresAt > this.now)
3018 {
3019 items.push(item);
3020 continue;
3021 }
3022 // Expired while unloaded: destroy and free any cheque reservation.
3023 this.#destroyExpiredGroundItem(item);
3024 }
3014 this.nextGroundItemId = Math.max(this.nextGroundItemId, this.stateDb.nextGroundItemId());
3025 this.nextGroundItemId = Math.max(this.nextGroundItemId, this.stateDb.nextGroundItemId());
3015 this.workers[workerId].call({
3026 this.workers[workerId].call({
3016 type: WorkerMessage.setGroundItems,
3027 type: WorkerMessage.setGroundItems,
@@ -3020,6 +3031,55 @@ export class Simulation
3020 });
3031 });
3021 }
3032 }
3022
3033
3034 /** Cheque paper that no longer exists must free reserved vault goods. */
3035 voidFilledCheque (stack)
3036 {
3037 if ((stack?.itemId | 0) !== ItemId.filledCheque || stack?.instance?.kind !== "filled-cheque")
3038 { return { ok: false }; }
3039 const issuerId = stack.instance.issuerCharacterId | 0;
3040 const chequeId = stack.instance.chequeId;
3041 const release = this.releaseChequeReservation(issuerId, chequeId, stack.instance);
3042 if (!release?.ok || !release.stack)
3043 {
3044 // Already redeemed/voided — treat as success so despawn stays idempotent.
3045 return { ok: true, alreadyReleased: true };
3046 }
3047 const onlineId = this.characterEntities.get(issuerId);
3048 const bag = onlineId ? this.bagOf(onlineId) : this.stateDb.getBag(issuerId);
3049 if (!bag)
3050 { return { ok: false, error: "Issuer vault not found." }; }
3051 const goodsDef = itemDefinition(release.stack.itemId);
3052 if (!goodsDef)
3053 { return { ok: false, error: "Unknown reserved goods." }; }
3054 const goods = attachOrigins({
3055 itemId: release.stack.itemId,
3056 quantity: release.stack.quantity,
3057 instance: release.stack.instance ? structuredClone(release.stack.instance) : null,
3058 origins: release.stack.origins
3059 });
3060 const inserted = insertStackGrowable(bag.vault ?? [], goods, goodsDef);
3061 bag.vault = inserted.slots;
3062 this.stateDb.saveBag(issuerId, bag);
3063 if (onlineId)
3064 {
3065 this.applyBag(onlineId, bag);
3066 this.pushInventory(issuerId, bag);
3067 }
3068 this.pendingPrivate.push({
3069 type: "toast",
3070 characterId: issuerId,
3071 message: "An uncashed cheque expired. Reserved goods returned to your vault."
3072 });
3073 return { ok: true };
3074 }
3075
3076 #destroyExpiredGroundItem (item)
3077 {
3078 if (item?.stack?.itemId === ItemId.filledCheque && item.stack?.instance?.kind === "filled-cheque")
3079 { this.voidFilledCheque(item.stack); }
3080 this.stateDb.deleteGroundItem(item.groundItemId);
3081 }
3082
3023 #persistGroundEvent (event)
3083 #persistGroundEvent (event)
3024 {
3084 {
3025 if (event.type === "ground-item-appear")
3085 if (event.type === "ground-item-appear")
server/tests/bank.test.js +67 −0
@@ -338,3 +338,70 @@ test("blank cheque ground pickup stops at 99 carried", () =>
338 338 finally
339 339 { harness.close(); }
340 340 });
341
342 test("dropping a filled cheque keeps reservation; despawn returns goods to vault", () =>
343 {
344 const harness = makeSimulation({ workerCount: 1, now: 10_000 });
345 try
346 {
347 placeBank(harness.worldDb, 8, 8);
348 harness.simulation.reloadWorld();
349 harness.simulation.applySettings({ groundItemDespawnDurationMs: 1_000 });
350 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
351 const entityId = harness.simulation.spawnCharacter(1, character);
352 harness.simulation.enqueue(1, {
353 type: "get-blank-cheques",
354 characterId: character.character_id
355 });
356 harness.simulation.step();
357 let bag = harness.simulation.bagOf(entityId);
358 const appleQtyBefore = countItem(bag.inventory, ItemId.apple);
359 harness.simulation.enqueue(1, {
360 type: "bank-deposit",
361 characterId: character.character_id,
362 slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple),
363 quantity: null
364 });
365 harness.simulation.step();
366 bag = harness.simulation.bagOf(entityId);
367 harness.simulation.enqueue(1, {
368 type: "write-cheque",
369 characterId: character.character_id,
370 slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque),
371 vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple),
372 quantity: 2
373 });
374 harness.simulation.step();
375 bag = harness.simulation.bagOf(entityId);
376 assert.equal((bag.vaultReserved ?? []).length, 1);
377 assert.equal(bag.vaultReserved[0].quantity, 2);
378 const vaultApplesAfterWrite = countItem(bag.vault, ItemId.apple);
379 const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque);
380 assert.ok(chequeIndex >= 0);
381
382 harness.simulation.enqueue(1, {
383 type: "drop-item",
384 characterId: character.character_id,
385 slotIndex: chequeIndex,
386 quantity: 1
387 });
388 const drop = harness.simulation.step();
389 assert.ok(drop.events.some((event) => event.type === "ground-item-appear"));
390 bag = harness.simulation.bagOf(entityId);
391 assert.equal((bag.vaultReserved ?? []).length, 1, "drop must keep goods reserved");
392 assert.equal(countItem(bag.vault, ItemId.apple), vaultApplesAfterWrite);
393
394 harness.setNow(harness.now() + 5_000);
395 const expired = harness.simulation.step();
396 assert.ok(
397 expired.events.some((event) => event.type === "ground-item-despawn"),
398 JSON.stringify(expired.events.map((event) => event.type))
399 );
400 bag = harness.simulation.bagOf(entityId);
401 assert.equal((bag.vaultReserved ?? []).length, 0);
402 assert.equal(countItem(bag.vault, ItemId.apple), vaultApplesAfterWrite + 2);
403 assert.ok(appleQtyBefore >= 2);
404 }
405 finally
406 { harness.close(); }
407 });
@@ -338,3 +338,70 @@ test("blank cheque ground pickup stops at 99 carried", () =>
338 finally
338 finally
339 { harness.close(); }
339 { harness.close(); }
340});
340});
341
342test("dropping a filled cheque keeps reservation; despawn returns goods to vault", () =>
343{
344 const harness = makeSimulation({ workerCount: 1, now: 10_000 });
345 try
346 {
347 placeBank(harness.worldDb, 8, 8);
348 harness.simulation.reloadWorld();
349 harness.simulation.applySettings({ groundItemDespawnDurationMs: 1_000 });
350 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
351 const entityId = harness.simulation.spawnCharacter(1, character);
352 harness.simulation.enqueue(1, {
353 type: "get-blank-cheques",
354 characterId: character.character_id
355 });
356 harness.simulation.step();
357 let bag = harness.simulation.bagOf(entityId);
358 const appleQtyBefore = countItem(bag.inventory, ItemId.apple);
359 harness.simulation.enqueue(1, {
360 type: "bank-deposit",
361 characterId: character.character_id,
362 slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple),
363 quantity: null
364 });
365 harness.simulation.step();
366 bag = harness.simulation.bagOf(entityId);
367 harness.simulation.enqueue(1, {
368 type: "write-cheque",
369 characterId: character.character_id,
370 slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque),
371 vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple),
372 quantity: 2
373 });
374 harness.simulation.step();
375 bag = harness.simulation.bagOf(entityId);
376 assert.equal((bag.vaultReserved ?? []).length, 1);
377 assert.equal(bag.vaultReserved[0].quantity, 2);
378 const vaultApplesAfterWrite = countItem(bag.vault, ItemId.apple);
379 const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque);
380 assert.ok(chequeIndex >= 0);
381
382 harness.simulation.enqueue(1, {
383 type: "drop-item",
384 characterId: character.character_id,
385 slotIndex: chequeIndex,
386 quantity: 1
387 });
388 const drop = harness.simulation.step();
389 assert.ok(drop.events.some((event) => event.type === "ground-item-appear"));
390 bag = harness.simulation.bagOf(entityId);
391 assert.equal((bag.vaultReserved ?? []).length, 1, "drop must keep goods reserved");
392 assert.equal(countItem(bag.vault, ItemId.apple), vaultApplesAfterWrite);
393
394 harness.setNow(harness.now() + 5_000);
395 const expired = harness.simulation.step();
396 assert.ok(
397 expired.events.some((event) => event.type === "ground-item-despawn"),
398 JSON.stringify(expired.events.map((event) => event.type))
399 );
400 bag = harness.simulation.bagOf(entityId);
401 assert.equal((bag.vaultReserved ?? []).length, 0);
402 assert.equal(countItem(bag.vault, ItemId.apple), vaultApplesAfterWrite + 2);
403 assert.ok(appleQtyBefore >= 2);
404 }
405 finally
406 { harness.close(); }
407});