BlueGit

timfa questward

Files

questward / server / tests / mining.test.js

mining.test.js

mining.test.js 13.1 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 MaterialTier,
5 GatherMaterialId,
6 tierMinSkillLevel,
7 tierMineSkillLevel,
8 tierSmeltSkillLevel,
9 tierRank,
10 miningDurationMs,
11 oreExhaustDurationMs,
12 oreBoulderState,
13 materialFromOreBoulderState,
14 EXHAUSTED_ORE_BOULDER_STATE
15} from "../../shared/definitions/materialTiers.js";
16import {
17 canMineMaterial,
18 oreItemIdForMaterial
19} from "../../shared/definitions/gathering.js";
20import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
21import { SkillId } from "../../shared/definitions/skills.js";
22import { canEquipItem } from "../../shared/equipment/stats.js";
23import { normalizePlayerSkills } from "../../shared/skills/state.js";
24import { totalXpForLevel } from "../../shared/skills/progression.js";
25import { BlockType, blockDefinition } from "../../shared/definitions/tiles.js";
26import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
27import { Component } from "../src/ecs/components.js";
28
29function countItem (slots, itemId)
30{
31 return (slots ?? []).reduce((sum, slot) =>
32 {
33 if (slot?.itemId === itemId)
34 { return sum + (slot.quantity | 0); }
35 return sum;
36 }, 0);
37}
38
39function placeOre (worker, tileX, tileY, state)
40{
41 const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16));
42 const localX = tileX % 16;
43 const localY = tileY % 16;
44 const index = localY * 16 + localX;
45 data.blocks[index] = { typeId: BlockType.boulder, state, config: null };
46 return { data, index };
47}
48
49function bagPickaxe (worker, playerId, itemId)
50{
51 const handle = worker.world.ecs.handleOf(playerId);
52 const inventory = worker.world.ecs.get(handle, Component.inventory);
53 inventory.slots[0] = { itemId, quantity: 1 };
54}
55
56function setSkillLevel (worker, playerId, skillId, level)
57{
58 const handle = worker.world.ecs.handleOf(playerId);
59 const skills = worker.world.ecs.get(handle, Component.skills);
60 skills.xp[skillId] = totalXpForLevel(level);
61}
62
63function advanceMs (harness, ms)
64{
65 const ticks = Math.ceil(ms / 50) + 3;
66 for (let i = 0; i < ticks; i++)
67 {
68 harness.advance(50);
69 harness.simulation.step();
70 }
71}
72
73test("tier skill gates: equip / mine / smelt", () =>
74{
75 assert.equal(tierMinSkillLevel(MaterialTier.bronze), 1);
76 assert.equal(tierMinSkillLevel(MaterialTier.mithril), 35);
77 assert.equal(tierMinSkillLevel(MaterialTier.dragon), 90);
78 assert.equal(tierMineSkillLevel(MaterialTier.iron), 5);
79 assert.equal(tierMineSkillLevel(MaterialTier.mithril), 18);
80 assert.equal(tierSmeltSkillLevel(MaterialTier.iron), 8);
81 assert.equal(tierSmeltSkillLevel(MaterialTier.steel), 15);
82 assert.equal(tierMineSkillLevel(GatherMaterialId.silver), 5);
83 assert.equal(tierSmeltSkillLevel(GatherMaterialId.silver), 8);
84 assert.equal(tierRank(GatherMaterialId.silver), 2);
85 assert.equal(tierMineSkillLevel(GatherMaterialId.gold), 10);
86 assert.equal(tierSmeltSkillLevel(GatherMaterialId.gold), 15);
87 assert.equal(tierRank(GatherMaterialId.gold), 3);
88 assert.equal(tierRank(MaterialTier.dragon), 7);
89});
90
91test("mining duration uses resource and tool ranks", () =>
92{
93 assert.equal(miningDurationMs(MaterialTier.bronze, MaterialTier.bronze), 5000);
94 assert.equal(miningDurationMs(MaterialTier.iron, MaterialTier.bronze), 13000);
95 assert.ok(miningDurationMs(MaterialTier.bronze, MaterialTier.dragon) >= 500);
96});
97
98test("ore exhaust duration is 10s times associated bar rank", () =>
99{
100 assert.equal(oreExhaustDurationMs(MaterialTier.bronze), 10_000);
101 assert.equal(oreExhaustDurationMs(MaterialTier.iron), 20_000);
102 assert.equal(oreExhaustDurationMs(GatherMaterialId.tin), 10_000);
103 assert.equal(oreExhaustDurationMs(GatherMaterialId.copper), 10_000);
104 assert.equal(oreExhaustDurationMs(GatherMaterialId.coal), 30_000);
105 assert.equal(oreExhaustDurationMs(GatherMaterialId.gold), 30_000);
106 assert.equal(oreExhaustDurationMs(GatherMaterialId.silver), 20_000);
107 assert.equal(oreExhaustDurationMs(MaterialTier.dragon), 70_000);
108});
109
110test("ore boulder states map to materials", () =>
111{
112 assert.equal(materialFromOreBoulderState("tin-ore"), GatherMaterialId.tin);
113 assert.equal(materialFromOreBoulderState("copper-ore"), GatherMaterialId.copper);
114 assert.equal(materialFromOreBoulderState("coal"), GatherMaterialId.coal);
115 assert.equal(materialFromOreBoulderState("silver-ore"), GatherMaterialId.silver);
116 assert.equal(materialFromOreBoulderState("gold-ore"), GatherMaterialId.gold);
117 assert.equal(materialFromOreBoulderState("boulder"), null);
118 assert.equal(materialFromOreBoulderState("bronze-ore"), null);
119 assert.equal(oreBoulderState(MaterialTier.mithril), "mithril-ore");
120 assert.equal(oreBoulderState(GatherMaterialId.silver), "silver-ore");
121 assert.equal(oreBoulderState(GatherMaterialId.gold), "gold-ore");
122});
123
124test("boulder block exists", () =>
125{
126 assert.equal(blockDefinition(BlockType.boulder)?.key, "boulder");
127});
128
129test("iron pickaxe requires Mining 10", () =>
130{
131 const skills = normalizePlayerSkills(null);
132 assert.equal(canEquipItem(skills, itemDefinition(ItemId.ironPickaxe)).ok, false);
133 skills.xp[SkillId.mining] = totalXpForLevel(10);
134 assert.equal(canEquipItem(skills, itemDefinition(ItemId.ironPickaxe)).ok, true);
135 assert.equal(canEquipItem(skills, itemDefinition(ItemId.bronzePickaxe)).ok, true);
136});
137
138test("canMineMaterial enforces skill and tool tier", () =>
139{
140 assert.equal(canMineMaterial(1, MaterialTier.bronze, MaterialTier.iron).ok, false);
141 // One tier below is allowed once the Mining skill gate is met.
142 assert.equal(canMineMaterial(5, MaterialTier.bronze, MaterialTier.iron).ok, true);
143 assert.equal(canMineMaterial(5, MaterialTier.iron, MaterialTier.iron).ok, true);
144 assert.equal(canMineMaterial(10, MaterialTier.bronze, MaterialTier.steel).ok, false);
145});
146
147test("mining without a pickaxe is rejected", () =>
148{
149 const harness = makeSimulation({ workerCount: 1 });
150 try
151 {
152 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
153 const playerId = harness.simulation.spawnCharacter(1, character);
154 const record = harness.simulation.entityRecord(playerId);
155 const worker = harness.simulation.workers[record.workerId];
156 placeOre(worker, 9, 8, "copper-ore");
157
158 harness.simulation.enqueue(1, {
159 type: "mine",
160 characterId: character.character_id,
161 tileX: 9,
162 tileY: 8
163 });
164 const result = harness.simulation.step();
165 assert.ok(result.events.some((event) =>
166 event.type === "command-error"
167 && event.message === "You need a pickaxe to mine this!"));
168 }
169 finally
170 { harness.close(); }
171});
172
173test("plain boulder cannot be mined", () =>
174{
175 const harness = makeSimulation({ workerCount: 1 });
176 try
177 {
178 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
179 const playerId = harness.simulation.spawnCharacter(1, character);
180 const record = harness.simulation.entityRecord(playerId);
181 const worker = harness.simulation.workers[record.workerId];
182 bagPickaxe(worker, playerId, ItemId.bronzePickaxe);
183 placeOre(worker, 9, 8, EXHAUSTED_ORE_BOULDER_STATE);
184
185 harness.simulation.enqueue(1, {
186 type: "mine",
187 characterId: character.character_id,
188 tileX: 9,
189 tileY: 8
190 });
191 const result = harness.simulation.step();
192 assert.ok(result.events.some((event) =>
193 event.type === "command-error"
194 && (/no ore|exhausted/i.test(event.message ?? ""))));
195 }
196 finally
197 { harness.close(); }
198});
199
200test("pickaxe two tiers below cannot mine the ore", () =>
201{
202 const harness = makeSimulation({ workerCount: 1 });
203 try
204 {
205 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
206 const playerId = harness.simulation.spawnCharacter(1, character);
207 const record = harness.simulation.entityRecord(playerId);
208 const worker = harness.simulation.workers[record.workerId];
209 bagPickaxe(worker, playerId, ItemId.bronzePickaxe);
210 setSkillLevel(worker, playerId, SkillId.mining, 20);
211 placeOre(worker, 9, 8, "coal");
212
213 harness.simulation.enqueue(1, {
214 type: "mine",
215 characterId: character.character_id,
216 tileX: 9,
217 tileY: 8
218 });
219 const result = harness.simulation.step();
220 assert.ok(result.events.some((event) =>
221 event.type === "command-error"
222 && event.message === "You need a pickaxe to mine this!"));
223 }
224 finally
225 { harness.close(); }
226});
227
228test("bronze pickaxe can mine iron ore (one tier below)", () =>
229{
230 const harness = makeSimulation({ workerCount: 1 });
231 try
232 {
233 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
234 const playerId = harness.simulation.spawnCharacter(1, character);
235 const record = harness.simulation.entityRecord(playerId);
236 const worker = harness.simulation.workers[record.workerId];
237 bagPickaxe(worker, playerId, ItemId.bronzePickaxe);
238 setSkillLevel(worker, playerId, SkillId.mining, 10);
239 placeOre(worker, 9, 8, "iron-ore");
240
241 harness.simulation.enqueue(1, {
242 type: "mine",
243 characterId: character.character_id,
244 tileX: 9,
245 tileY: 8
246 });
247 const start = harness.simulation.step();
248 assert.ok(start.events.some((event) =>
249 event.type === "gather-swing"
250 && event.toolItemId === ItemId.bronzePickaxe), JSON.stringify(start.events));
251 }
252 finally
253 { harness.close(); }
254});
255
256test("mining uses the best pickaxe in inventory without equipping", () =>
257{
258 const harness = makeSimulation({ workerCount: 1 });
259 try
260 {
261 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
262 const playerId = harness.simulation.spawnCharacter(1, character);
263 const record = harness.simulation.entityRecord(playerId);
264 const worker = harness.simulation.workers[record.workerId];
265 setSkillLevel(worker, playerId, SkillId.mining, 10);
266 const handle = worker.world.ecs.handleOf(playerId);
267 const inventory = worker.world.ecs.get(handle, Component.inventory);
268 inventory.slots[0] = { itemId: ItemId.bronzePickaxe, quantity: 1 };
269 inventory.slots[1] = { itemId: ItemId.ironPickaxe, quantity: 1 };
270 placeOre(worker, 9, 8, "iron-ore");
271
272 harness.simulation.enqueue(1, {
273 type: "mine",
274 characterId: character.character_id,
275 tileX: 9,
276 tileY: 8
277 });
278 harness.simulation.step();
279 advanceMs(harness, miningDurationMs(MaterialTier.iron, MaterialTier.iron));
280
281 const bag = harness.simulation.bagOf(playerId);
282 assert.ok(countItem(bag.inventory, ItemId.ironOre) >= 1);
283 const oreStack = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.ironOre);
284 assert.ok(oreStack?.origins?.some((entry) =>
285 entry.origin?.kind === "gathered"
286 && entry.origin?.verb === "Mined"
287 && typeof entry.origin?.displayName === "string"
288 && entry.origin.displayName.length > 0));
289 }
290 finally
291 { harness.close(); }
292});
293
294test("mining grants ore and Mining XP then exhausts and respawns the node", () =>
295{
296 const harness = makeSimulation({ workerCount: 1 });
297 try
298 {
299 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
300 const playerId = harness.simulation.spawnCharacter(1, character);
301 const record = harness.simulation.entityRecord(playerId);
302 const worker = harness.simulation.workers[record.workerId];
303 bagPickaxe(worker, playerId, ItemId.bronzePickaxe);
304 const { data, index } = placeOre(worker, 9, 8, "copper-ore");
305
306 harness.simulation.enqueue(1, {
307 type: "mine",
308 characterId: character.character_id,
309 tileX: 9,
310 tileY: 8
311 });
312 const start = harness.simulation.step();
313 assert.ok(start.events.some((event) => event.type === "gather-swing" || event.type === "tile-shove"));
314 const swing = start.events.find((event) => event.type === "gather-swing");
315 assert.equal(swing?.toolItemId, ItemId.bronzePickaxe);
316
317 advanceMs(harness, miningDurationMs(GatherMaterialId.copper, MaterialTier.bronze));
318
319 const bag = harness.simulation.bagOf(playerId);
320 assert.ok(countItem(bag.inventory, ItemId.copperOre) >= 1);
321 const skills = harness.stateDb.getSkills(character.character_id);
322 assert.ok(skills.xp.mining > 0);
323 assert.equal(oreItemIdForMaterial(GatherMaterialId.copper), ItemId.copperOre);
324 assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE);
325
326 harness.simulation.enqueue(1, {
327 type: "mine",
328 characterId: character.character_id,
329 tileX: 9,
330 tileY: 8
331 });
332 const exhaustedResult = harness.simulation.step();
333 assert.ok(exhaustedResult.events.some((event) =>
334 event.type === "command-error" && /exhausted/i.test(event.message ?? "")));
335
336 advanceMs(harness, oreExhaustDurationMs(GatherMaterialId.copper));
337 assert.equal(data.blocks[index].state, "copper-ore");
338 }
339 finally
340 { harness.close(); }
341});
342
343test("coal mining yields coal and exhausts the node", () =>
344{
345 const harness = makeSimulation({ workerCount: 1 });
346 try
347 {
348 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
349 const playerId = harness.simulation.spawnCharacter(1, character);
350 const record = harness.simulation.entityRecord(playerId);
351 const worker = harness.simulation.workers[record.workerId];
352 setSkillLevel(worker, playerId, SkillId.mining, 20);
353 bagPickaxe(worker, playerId, ItemId.steelPickaxe);
354 const { data, index } = placeOre(worker, 9, 8, "coal");
355
356 harness.simulation.enqueue(1, {
357 type: "mine",
358 characterId: character.character_id,
359 tileX: 9,
360 tileY: 8
361 });
362 harness.simulation.step();
363 advanceMs(harness, miningDurationMs(GatherMaterialId.coal, MaterialTier.steel));
364
365 const bag = harness.simulation.bagOf(playerId);
366 assert.ok(countItem(bag.inventory, ItemId.coal) >= 1);
367 assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE);
368 }
369 finally
370 { harness.close(); }
371});