BlueGit

timfa questward

Files

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

central-exchange.test.js

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