BlueGit

timfa questward

Files

questward / server / tests / central-exchange.test.js

central-exchange.test.js

central-exchange.test.js 13.1 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 NeighborBit,
5 computeNeighborConnectMask,
6 neighborConnectStateName,
7 parseNeighborConnectMask,
8 composeNeighborConnectState,
9 neighborConnectBlocksMatch,
10 parseMaterialNeighborConnectState,
11 worldTileToChunkLocal,
12 neighborConnectRefreshTiles,
13 cardinalConnectionSuffix,
14 composeNamedNeighborConnectState,
15 parseNamedNeighborConnectMask
16} from "../../shared/world/neighborConnect.js";
17import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js";
18import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js";
19import { BlockType, BLOCK_DEFINITIONS } from "../../shared/definitions/tiles.js";
20import { ItemId } from "../../shared/definitions/items.js";
21import { emptyEquipment, emptySlots, addCoinsToSlots } from "../../shared/items/inventory.js";
22import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
23import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
24import { CHUNK_SIZE } from "../../shared/protocol/constants.js";
25import { Component } from "../src/ecs/components.js";
26
27test("neighbor connect masks cover all NESW combinations", () =>
28{
29 assert.equal(neighborConnectStateName(0), "0");
30 assert.equal(neighborConnectStateName(NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w), "15");
31 assert.equal(parseNeighborConnectMask("6"), NeighborBit.e | NeighborBit.s);
32 const mask = computeNeighborConnectMask((dx, dy) => dx === 1 || dy === -1);
33 assert.equal(mask, NeighborBit.n | NeighborBit.e);
34});
35
36test("named wall connection suffixes follow nesw / we / esw / wne", () =>
37{
38 assert.equal(cardinalConnectionSuffix(0), "");
39 assert.equal(cardinalConnectionSuffix(NeighborBit.e | NeighborBit.w), "we");
40 assert.equal(cardinalConnectionSuffix(NeighborBit.n | NeighborBit.s), "ns");
41 assert.equal(
42 cardinalConnectionSuffix(NeighborBit.e | NeighborBit.s | NeighborBit.w),
43 "esw"
44 );
45 assert.equal(
46 cardinalConnectionSuffix(NeighborBit.w | NeighborBit.n | NeighborBit.e),
47 "wne"
48 );
49 assert.equal(
50 cardinalConnectionSuffix(NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w),
51 "nesw"
52 );
53 const def = { key: "thick-stone-wall", neighborConnect: true, neighborConnectNamed: true };
54 assert.equal(composeNamedNeighborConnectState(def, 0), "thick-stone-wall");
55 assert.equal(
56 composeNamedNeighborConnectState(def, NeighborBit.e | NeighborBit.s | NeighborBit.w),
57 "thick-stone-wall-esw"
58 );
59 assert.equal(parseNamedNeighborConnectMask("thick-stone-wall-wne", def), NeighborBit.w | NeighborBit.n | NeighborBit.e);
60 assert.equal(parseNamedNeighborConnectMask("14", def), 14);
61 assert.deepEqual(neighborConnectRefreshTiles(15, 8).length, 5);
62});
63
64test("material neighbor-connect states keep wood id and only match same material", () =>
65{
66 const def = BLOCK_DEFINITIONS[BlockType.table];
67 assert.equal(composeNeighborConnectState(def, "oak", 3), "oak-3");
68 assert.deepEqual(parseMaterialNeighborConnectState("birch-10", def), { material: "birch", mask: 10 });
69 const pine = { typeId: BlockType.table, state: "pine-0" };
70 const pineNeighbor = { typeId: BlockType.table, state: "pine-2" };
71 const oakNeighbor = { typeId: BlockType.table, state: "oak-2" };
72 assert.equal(neighborConnectBlocksMatch(pine, pineNeighbor, def), true);
73 assert.equal(neighborConnectBlocksMatch(pine, oakNeighbor, def), false);
74});
75
76test("wooden fences use the same material neighbor-connect as tables", () =>
77{
78 const def = BLOCK_DEFINITIONS[BlockType.fence];
79 assert.equal(def.neighborConnect, true);
80 assert.equal(def.neighborConnectByMaterial, true);
81 assert.equal(def.passable, false);
82 assert.equal(def.blocksLight, false);
83 assert.equal(composeNeighborConnectState(def, "heartwood", 12), "heartwood-12");
84 const pine = { typeId: BlockType.fence, state: "pine-0" };
85 const pineNeighbor = { typeId: BlockType.fence, state: "pine-4" };
86 const oakNeighbor = { typeId: BlockType.fence, state: "oak-4" };
87 assert.equal(neighborConnectBlocksMatch(pine, pineNeighbor, def), true);
88 assert.equal(neighborConnectBlocksMatch(pine, oakNeighbor, def), false);
89});
90
91test("world tiles map to chunk locals across boundaries", () =>
92{
93 assert.deepEqual(worldTileToChunkLocal(15, 8, 16), {
94 chunkX: 0, chunkY: 0, localX: 15, localY: 8
95 });
96 assert.deepEqual(worldTileToChunkLocal(16, 8, 16), {
97 chunkX: 1, chunkY: 0, localX: 0, localY: 8
98 });
99 assert.deepEqual(worldTileToChunkLocal(-1, 0, 16), {
100 chunkX: -1, chunkY: 0, localX: 15, localY: 0
101 });
102 const tiles = neighborConnectRefreshTiles(15, 8);
103 assert.deepEqual(tiles[2], [16, 8]);
104});
105
106test("CE staff entries include sex labels and pickExchangeClerk returns a name", () =>
107{
108 assert.ok(CENTRAL_EXCHANGE_STAFF.length >= 2);
109 for (const staff of CENTRAL_EXCHANGE_STAFF)
110 {
111 assert.ok(staff.name);
112 assert.ok(staff.sex === "male" || staff.sex === "female");
113 }
114 const name = pickExchangeClerk(() => 0);
115 assert.equal(name, CENTRAL_EXCHANGE_STAFF[0].name);
116});
117
118test("central exchange spawner config locks staff count and strips shop options", () =>
119{
120 const config = normalizeSpawnerConfig({
121 mobTypeId: MobId.centralExchange,
122 maxAlive: 2,
123 sells: [{ itemId: ItemId.apple, quantity: 1 }],
124 buysItems: true,
125 displayName: "ignored"
126 }, "ce:0");
127 assert.equal(config.centralExchange, true);
128 assert.equal(config.maxAlive, CENTRAL_EXCHANGE_STAFF.length);
129 assert.equal(config.sells.length, 0);
130 assert.equal(config.buysItems, false);
131 assert.equal(isCentralExchangeSpawner(config), true);
132});
133
134test("central exchange spawner places every clerk with matching names", () =>
135{
136 const harness = makeSimulation({ workerCount: 1 });
137 try
138 {
139 const data = createEmptyChunkData();
140 data.open = true;
141 data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" };
142 data.blocks[8 * CHUNK_SIZE + 10] = { typeId: BlockType.bank, state: null };
143 data.spawners = [{
144 localX: 8,
145 localY: 8,
146 config: normalizeSpawnerConfig({
147 mobTypeId: MobId.centralExchange,
148 spawnRangeTiles: 6,
149 maxWanderRangeTiles: 6,
150 equipment: { chest: ItemId.clothShirt }
151 }, "ce-test")
152 }];
153 harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data));
154 harness.simulation.reloadWorld();
155 harness.advance(50);
156 for (let i = 0; i < 30; i++)
157 { harness.simulation.step(); }
158
159 const record = harness.simulation.entityRecord(
160 harness.simulation.spawnCharacter(1, makeCharacter(harness.stateDb, { tileX: 7, tileY: 7 }))
161 );
162 const worker = harness.simulation.workers[record.workerId];
163 for (let i = 0; i < 40; i++)
164 { harness.simulation.step(); }
165
166 const clerks = [];
167 for (const entity of worker.world.ecs.query(Component.spawnOrigin, Component.appearance))
168 {
169 if (!entity.spawnOrigin?.centralExchange)
170 { continue; }
171 clerks.push(entity.appearance.firstName);
172 }
173 assert.equal(clerks.length, CENTRAL_EXCHANGE_STAFF.length);
174 for (const staff of CENTRAL_EXCHANGE_STAFF)
175 { assert.ok(clerks.includes(staff.name), staff.name); }
176 }
177 finally
178 { harness.close(); }
179});
180
181test("opening the market stations a CE clerk on the opposite side of the counter", () =>
182{
183 const harness = makeSimulation({ workerCount: 1 });
184 try
185 {
186 const data = createEmptyChunkData();
187 data.open = true;
188 // Counter at (9,8); player will stand at (8,8); staff station = (10,8).
189 data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" };
190 data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null };
191 data.spawners = [{
192 localX: 12,
193 localY: 8,
194 config: normalizeSpawnerConfig({
195 mobTypeId: MobId.centralExchange,
196 spawnRangeTiles: 8,
197 maxWanderRangeTiles: 8
198 }, "ce-station")
199 }];
200 harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data));
201 harness.simulation.reloadWorld();
202
203 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
204 const funded = addCoinsToSlots(emptySlots(), 10);
205 harness.stateDb.saveBag(character.character_id, {
206 inventory: funded.slots,
207 equipment: emptyEquipment(),
208 vault: [],
209 vaultReserved: []
210 });
211 const playerId = harness.simulation.spawnCharacter(1, character);
212 for (let i = 0; i < 40; i++)
213 { harness.simulation.step(); }
214
215 harness.simulation.enqueue(1, {
216 type: "open-market",
217 characterId: character.character_id
218 });
219 harness.simulation.step();
220
221 const record = harness.simulation.entityRecord(playerId);
222 const worker = harness.simulation.workers[record.workerId];
223 let stationed = false;
224 for (const entity of worker.world.ecs.query(Component.spawnOrigin))
225 {
226 if (!entity.spawnOrigin?.centralExchange)
227 { continue; }
228 if (entity.spawnOrigin.stationTileX === 10 && entity.spawnOrigin.stationTileY === 8)
229 { stationed = true; }
230 }
231 assert.equal(stationed, true);
232 }
233 finally
234 { harness.close(); }
235});
236
237test("closing the market releases the CE clerk so wander can resume", () =>
238{
239 const harness = makeSimulation({ workerCount: 1 });
240 try
241 {
242 const data = createEmptyChunkData();
243 data.open = true;
244 data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" };
245 data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null };
246 data.spawners = [{
247 localX: 12,
248 localY: 8,
249 config: normalizeSpawnerConfig({
250 mobTypeId: MobId.centralExchange,
251 spawnRangeTiles: 8,
252 maxWanderRangeTiles: 8
253 }, "ce-release-close")
254 }];
255 harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data));
256 harness.simulation.reloadWorld();
257
258 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
259 const playerId = harness.simulation.spawnCharacter(1, character);
260 for (let i = 0; i < 40; i++)
261 { harness.simulation.step(); }
262
263 harness.simulation.enqueue(1, {
264 type: "open-market",
265 characterId: character.character_id
266 });
267 harness.simulation.step();
268
269 const record = harness.simulation.entityRecord(playerId);
270 const worker = harness.simulation.workers[record.workerId];
271 assert.equal(stationedClerkCount(worker, 10, 8), 1);
272
273 harness.simulation.enqueue(1, {
274 type: "close-market",
275 characterId: character.character_id
276 });
277 harness.simulation.step();
278 assert.equal(stationedClerkCount(worker, 10, 8), 0);
279 }
280 finally
281 { harness.close(); }
282});
283
284test("walking away from the counter releases the clerk; a second player keeps them stationed", () =>
285{
286 const harness = makeSimulation({ workerCount: 1 });
287 try
288 {
289 const data = createEmptyChunkData();
290 data.open = true;
291 data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" };
292 data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null };
293 data.spawners = [{
294 localX: 12,
295 localY: 8,
296 config: normalizeSpawnerConfig({
297 mobTypeId: MobId.centralExchange,
298 spawnRangeTiles: 8,
299 maxWanderRangeTiles: 8
300 }, "ce-release-prune")
301 }];
302 harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data));
303 harness.simulation.reloadWorld();
304
305 const first = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
306 const second = makeCharacter(harness.stateDb, { tileX: 7, tileY: 8 });
307 const firstId = harness.simulation.spawnCharacter(1, first);
308 const secondId = harness.simulation.spawnCharacter(2, second);
309 for (let i = 0; i < 40; i++)
310 { harness.simulation.step(); }
311
312 harness.simulation.enqueue(1, { type: "open-market", characterId: first.character_id });
313 harness.simulation.step();
314
315 const record = harness.simulation.entityRecord(firstId);
316 const worker = harness.simulation.workers[record.workerId];
317 assert.equal(stationedClerkCount(worker, 10, 8), 1);
318
319 // Share the same approach tile / staff station as the first player.
320 const secondHandle = worker.world.ecs.handleOf(secondId);
321 const secondPos = worker.world.ecs.get(secondHandle, Component.position);
322 secondPos.tileX = 8;
323 secondPos.tileY = 8;
324 const secondDir = harness.simulation.directory.get(secondId);
325 secondDir.tileX = 8;
326 secondDir.tileY = 8;
327 harness.simulation.enqueue(2, { type: "open-market", characterId: second.character_id });
328 harness.simulation.step();
329 assert.equal(stationedClerkCount(worker, 10, 8), 1);
330
331 const firstHandle = worker.world.ecs.handleOf(firstId);
332 const firstPos = worker.world.ecs.get(firstHandle, Component.position);
333 firstPos.tileX = 2;
334 firstPos.tileY = 2;
335 const firstDir = harness.simulation.directory.get(firstId);
336 firstDir.tileX = 2;
337 firstDir.tileY = 2;
338 const prune = harness.simulation.market.step();
339 assert.ok(prune.some((event) => event.type === "market-closed" && event.characterId === first.character_id));
340 assert.equal(stationedClerkCount(worker, 10, 8), 1, "second player still keeps the clerk");
341
342 secondPos.tileX = 2;
343 secondPos.tileY = 3;
344 secondDir.tileX = 2;
345 secondDir.tileY = 3;
346 harness.simulation.market.step();
347 assert.equal(stationedClerkCount(worker, 10, 8), 0);
348 }
349 finally
350 { harness.close(); }
351});
352
353function stationedClerkCount (worker, stationTileX, stationTileY)
354{
355 let count = 0;
356 for (const entity of worker.world.ecs.query(Component.spawnOrigin))
357 {
358 if (!entity.spawnOrigin?.centralExchange)
359 { continue; }
360 if (
361 entity.spawnOrigin.stationTileX === stationTileX
362 && entity.spawnOrigin.stationTileY === stationTileY
363 )
364 { count += 1; }
365 }
366 return count;
367}