BlueGit

timfa questward

Files

questward / server / tests / courier.test.js

courier.test.js

courier.test.js 12.2 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType } from "../../shared/definitions/tiles.js";
4import { ItemId } from "../../shared/definitions/items.js";
5import { pigeonSpeedAtProgress, advanceCourierFlight } from "../../shared/courier/flight.js";
6import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
7import {
8 makeCharacter,
9 makeSimulation,
10 stepTimes
11} from "./helpers/simulationHarness.js";
12
13test("pigeon flight uses takeoff, cruise, then landing speeds", () =>
14{
15 const settings = {
16 pigeonTakeoffTilesPerSec: 3,
17 pigeonCruiseTilesPerSec: 8,
18 pigeonLandingTilesPerSec: 3,
19 pigeonPhaseTiles: 5
20 };
21 assert.equal(pigeonSpeedAtProgress(0, 40, settings), 3);
22 assert.equal(pigeonSpeedAtProgress(4.9, 40, settings), 3);
23 assert.equal(pigeonSpeedAtProgress(10, 40, settings), 8);
24 assert.equal(pigeonSpeedAtProgress(36, 40, settings), 3);
25
26 const takeoff = advanceCourierFlight({
27 originX: 0, originY: 0, destX: 40, destY: 0, traveled: 0, dt: 1, settings
28 });
29 assert.equal(takeoff.speed, 3);
30 assert.ok(Math.abs(takeoff.traveled - 3) < 1e-6);
31
32 const cruise = advanceCourierFlight({
33 originX: 0, originY: 0, destX: 40, destY: 0, traveled: 10, dt: 1, settings
34 });
35 assert.equal(cruise.speed, 8);
36 assert.ok(Math.abs(cruise.traveled - 18) < 1e-6);
37});
38
39function placeBank (worldDb, tileX, tileY)
40{
41 const chunkX = Math.floor(tileX / 16);
42 const chunkY = Math.floor(tileY / 16);
43 const data = createEmptyChunkData();
44 data.open = true;
45 const localX = tileX - chunkX * 16;
46 const localY = tileY - chunkY * 16;
47 data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 };
48 worldDb.saveChunkVersion(
49 chunkX,
50 chunkY,
51 worldDb.getActiveRevisionId(),
52 true,
53 encodeChunkBlob(data)
54 );
55}
56
57test("pigeon to bank deposits coin stacks into the sender vault", () =>
58{
59 const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 });
60 try
61 {
62 placeBank(harness.worldDb, 20, 8);
63 harness.simulation.reloadWorld();
64 harness.simulation.applySettings({
65 pigeonTakeoffTilesPerSec: 20,
66 pigeonCruiseTilesPerSec: 20,
67 pigeonLandingTilesPerSec: 20,
68 pigeonPhaseTiles: 5,
69 simulationTickHz: 20
70 });
71 const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 });
72 const entityId = harness.simulation.spawnCharacter(1, character);
73 const before = harness.simulation.bagOf(entityId);
74 const coinSlot = before.inventory.findIndex((slot) => slot?.itemId === ItemId.coins);
75 assert.ok(coinSlot >= 0);
76 assert.ok(before.inventory[coinSlot].quantity >= 5);
77 const beforeCoins = before.inventory[coinSlot].quantity;
78 const result = harness.simulation.dispatchPigeon({
79 characterId: character.character_id,
80 destination: { kind: "bank" },
81 cargo: { kind: "item", slotIndex: coinSlot, quantity: 5 }
82 });
83 assert.equal(result.transit.destinationKind, "bank");
84 assert.equal(harness.simulation.bagOf(entityId).inventory[coinSlot].quantity, beforeCoins - 5);
85
86 stepTimes(harness.simulation, 80);
87 const after = harness.stateDb.getBag(character.character_id);
88 assert.equal(countItem(after.vault, ItemId.coins), 5);
89 assert.equal(harness.stateDb.listActiveCourierTransits().length, 0);
90 }
91 finally
92 { harness.close(); }
93});
94
95function countItem (slots, itemId)
96{
97 return (slots ?? [])
98 .filter((slot) => slot?.itemId === itemId)
99 .reduce((sum, slot) => sum + (slot.quantity | 0), 0);
100}
101
102test("pigeon to offline player flies to the bank and credits the recipient vault", () =>
103{
104 const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 });
105 try
106 {
107 placeBank(harness.worldDb, 20, 8);
108 harness.simulation.reloadWorld();
109 harness.simulation.applySettings({
110 pigeonTakeoffTilesPerSec: 40,
111 pigeonCruiseTilesPerSec: 40,
112 pigeonLandingTilesPerSec: 40,
113 simulationTickHz: 20
114 });
115 const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" });
116 const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" });
117 const senderId = harness.simulation.spawnCharacter(1, sender);
118 harness.simulation.despawnCharacter(receiver.character_id);
119 const bag = harness.simulation.bagOf(senderId);
120 const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
121 assert.ok(appleSlot >= 0);
122
123 const result = harness.simulation.dispatchPigeon({
124 characterId: sender.character_id,
125 destination: { kind: "player", firstName: "Bob", lastName: "Receiver" },
126 cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 }
127 });
128 assert.equal(result.transit.destinationKind, "bank");
129 assert.equal(result.transit.vaultCharacterId, receiver.character_id);
130 assert.ok(Math.hypot(result.transit.destX - 20.5, result.transit.destY - 8.5) < 1);
131
132 stepTimes(harness.simulation, 40);
133 const receiverVault = harness.stateDb.getBag(receiver.character_id);
134 assert.equal(countItem(receiverVault.vault, ItemId.apple), 1);
135 assert.equal(harness.stateDb.listActiveCourierTransits().length, 0);
136 }
137 finally
138 { harness.close(); }
139});
140
141test("logging in after offline bank deposits sends a bank resting notice by pigeon", () =>
142{
143 const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 });
144 try
145 {
146 placeBank(harness.worldDb, 20, 8);
147 harness.simulation.reloadWorld();
148 harness.simulation.applySettings({
149 pigeonTakeoffTilesPerSec: 40,
150 pigeonCruiseTilesPerSec: 40,
151 pigeonLandingTilesPerSec: 40,
152 simulationTickHz: 20
153 });
154 const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" });
155 const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" });
156 const senderId = harness.simulation.spawnCharacter(1, sender);
157 harness.simulation.despawnCharacter(receiver.character_id);
158
159 const bag = harness.simulation.bagOf(senderId);
160 const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
161 harness.simulation.dispatchPigeon({
162 characterId: sender.character_id,
163 destination: { kind: "player", firstName: "Bob", lastName: "Receiver" },
164 cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 }
165 });
166 stepTimes(harness.simulation, 40);
167 assert.equal(harness.stateDb.listBankRestNotices(receiver.character_id).length, 1);
168
169 const receiverId = harness.simulation.spawnCharacter(2, receiver);
170 const active = harness.stateDb.listActiveCourierTransits();
171 assert.equal(active.length, 1);
172 assert.equal(active[0].destinationKind, "player");
173 assert.equal(active[0].cargo.stack.itemId, ItemId.letter);
174 assert.match(active[0].cargo.stack.instance.body, /While you have been resting/);
175 assert.match(active[0].cargo.stack.instance.body, /Ada Sender/);
176
177 stepTimes(harness.simulation, 40);
178 const receiverBag = harness.simulation.bagOf(receiverId);
179 const letterSlot = receiverBag.inventory.find((slot) => slot?.itemId === ItemId.letter);
180 assert.ok(letterSlot);
181 assert.equal(letterSlot.instance.authorDisplayName, "The Bank");
182 assert.equal(letterSlot.origins?.[0]?.origin?.verb, "Written");
183 assert.equal(letterSlot.origins?.[0]?.origin?.displayName, "The Bank");
184 assert.doesNotMatch(letterSlot.instance.body, /Signed,/);
185 }
186 finally
187 { harness.close(); }
188});
189test("pigeon delivers an item to an online player inventory", () =>
190{
191 const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 });
192 try
193 {
194 harness.simulation.applySettings({
195 pigeonTakeoffTilesPerSec: 40,
196 pigeonCruiseTilesPerSec: 40,
197 pigeonLandingTilesPerSec: 40,
198 simulationTickHz: 20
199 });
200 const sender = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8, firstName: "Ada", lastName: "Sender" });
201 const receiver = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Bob", lastName: "Receiver" });
202 const senderId = harness.simulation.spawnCharacter(1, sender);
203 const receiverId = harness.simulation.spawnCharacter(2, receiver);
204 const bag = harness.simulation.bagOf(senderId);
205 const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
206 assert.ok(appleSlot >= 0);
207 const senderQty = bag.inventory[appleSlot].quantity;
208 const receiverBefore = harness.simulation.bagOf(receiverId);
209 const receiverApples = receiverBefore.inventory
210 .filter((slot) => slot?.itemId === ItemId.apple)
211 .reduce((sum, slot) => sum + slot.quantity, 0);
212
213 harness.simulation.dispatchPigeon({
214 characterId: sender.character_id,
215 destination: { kind: "player", firstName: "Bob", lastName: "Receiver" },
216 cargo: { kind: "item", slotIndex: appleSlot, quantity: 1 }
217 });
218 assert.equal(harness.simulation.bagOf(senderId).inventory[appleSlot].quantity, senderQty - 1);
219
220 const events = stepTimes(harness.simulation, 40);
221 const receiverAfter = harness.simulation.bagOf(receiverId);
222 const receiverApplesAfter = receiverAfter.inventory
223 .filter((slot) => slot?.itemId === ItemId.apple)
224 .reduce((sum, slot) => sum + slot.quantity, 0);
225 assert.equal(receiverApplesAfter, receiverApples + 1);
226 assert.equal(harness.stateDb.listActiveCourierTransits().length, 0);
227 assert.ok(events.some((event) =>
228 event.type === "item-bubble"
229 && event.entityId === receiverId
230 && event.itemId === ItemId.apple
231 && event.quantity === 1
232 ), "pigeon delivery should show the craft-style item bubble");
233 }
234 finally
235 { harness.close(); }
236});
237
238test("pigeon rejects blank cheques", () =>
239{
240 const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 });
241 try
242 {
243 placeBank(harness.worldDb, 8, 8);
244 harness.simulation.reloadWorld();
245 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
246 const entityId = harness.simulation.spawnCharacter(1, character);
247 harness.simulation.enqueue(1, {
248 type: "get-blank-cheques",
249 characterId: character.character_id
250 });
251 harness.simulation.step();
252 const bag = harness.simulation.bagOf(entityId);
253 const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque);
254 assert.ok(blankIndex >= 0);
255 assert.throws(
256 () => harness.simulation.dispatchPigeon({
257 characterId: character.character_id,
258 destination: { kind: "bank" },
259 cargo: { kind: "item", slotIndex: blankIndex, quantity: 1 }
260 }),
261 (err) => err.message === "Blank cheques cannot be sent by pigeon."
262 );
263 }
264 finally
265 { harness.close(); }
266});
267
268test("pigeon to bank redeems a filled cheque into reserved goods", () =>
269{
270 const harness = makeSimulation({ workerCount: 1, maxChunkX: 2 });
271 try
272 {
273 placeBank(harness.worldDb, 20, 8);
274 placeBank(harness.worldDb, 8, 8);
275 harness.simulation.reloadWorld();
276 harness.simulation.applySettings({
277 pigeonTakeoffTilesPerSec: 40,
278 pigeonCruiseTilesPerSec: 40,
279 pigeonLandingTilesPerSec: 40,
280 simulationTickHz: 20
281 });
282 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
283 const entityId = harness.simulation.spawnCharacter(1, character);
284 harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id });
285 harness.simulation.step();
286 let bag = harness.simulation.bagOf(entityId);
287 harness.simulation.enqueue(1, {
288 type: "bank-deposit",
289 characterId: character.character_id,
290 slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple),
291 quantity: null
292 });
293 harness.simulation.step();
294 bag = harness.simulation.bagOf(entityId);
295 const applesBefore = countItem(bag.vault, ItemId.apple);
296 harness.simulation.enqueue(1, {
297 type: "write-cheque",
298 characterId: character.character_id,
299 slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque),
300 vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple),
301 quantity: 2
302 });
303 harness.simulation.step();
304 bag = harness.simulation.bagOf(entityId);
305 const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque);
306 assert.ok(chequeIndex >= 0);
307 assert.equal((bag.vaultReserved ?? []).length, 1);
308
309 harness.simulation.dispatchPigeon({
310 characterId: character.character_id,
311 destination: { kind: "bank" },
312 cargo: { kind: "item", slotIndex: chequeIndex, quantity: 1 }
313 });
314 stepTimes(harness.simulation, 80);
315 bag = harness.simulation.bagOf(entityId);
316 assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1);
317 assert.equal(countItem(bag.vault, ItemId.filledCheque), 0);
318 assert.equal((bag.vaultReserved ?? []).length, 0);
319 assert.equal(countItem(bag.vault, ItemId.apple), applesBefore);
320 }
321 finally
322 { harness.close(); }
323});