BlueGit

timfa questward

Files

questward / server / tests / mining.test.js

mining.test.js

mining.test.js 12.0 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 assert.equal(canMineMaterial(5, MaterialTier.bronze, MaterialTier.iron).ok, false);
142 assert.equal(canMineMaterial(5, MaterialTier.iron, MaterialTier.iron).ok, true);
143});
144
145test("mining without a pickaxe is rejected", () =>
146{
147 const harness = makeSimulation({ workerCount: 1 });
148 try
149 {
150 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
151 const playerId = harness.simulation.spawnCharacter(1, character);
152 const record = harness.simulation.entityRecord(playerId);
153 const worker = harness.simulation.workers[record.workerId];
154 placeOre(worker, 9, 8, "copper-ore");
155
156 harness.simulation.enqueue(1, {
157 type: "mine",
158 characterId: character.character_id,
159 tileX: 9,
160 tileY: 8
161 });
162 const result = harness.simulation.step();
163 assert.ok(result.events.some((event) =>
164 event.type === "command-error"
165 && event.message === "You need a pickaxe to mine this!"));
166 }
167 finally
168 { harness.close(); }
169});
170
171test("plain boulder cannot be mined", () =>
172{
173 const harness = makeSimulation({ workerCount: 1 });
174 try
175 {
176 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
177 const playerId = harness.simulation.spawnCharacter(1, character);
178 const record = harness.simulation.entityRecord(playerId);
179 const worker = harness.simulation.workers[record.workerId];
180 bagPickaxe(worker, playerId, ItemId.bronzePickaxe);
181 placeOre(worker, 9, 8, EXHAUSTED_ORE_BOULDER_STATE);
182
183 harness.simulation.enqueue(1, {
184 type: "mine",
185 characterId: character.character_id,
186 tileX: 9,
187 tileY: 8
188 });
189 const result = harness.simulation.step();
190 assert.ok(result.events.some((event) =>
191 event.type === "command-error"
192 && (/no ore|exhausted/i.test(event.message ?? ""))));
193 }
194 finally
195 { harness.close(); }
196});
197
198test("weak pickaxe cannot mine higher-tier ore", () =>
199{
200 const harness = makeSimulation({ workerCount: 1 });
201 try
202 {
203 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
204 const playerId = harness.simulation.spawnCharacter(1, character);
205 const record = harness.simulation.entityRecord(playerId);
206 const worker = harness.simulation.workers[record.workerId];
207 bagPickaxe(worker, playerId, ItemId.bronzePickaxe);
208 setSkillLevel(worker, playerId, SkillId.mining, 10);
209 placeOre(worker, 9, 8, "iron-ore");
210
211 harness.simulation.enqueue(1, {
212 type: "mine",
213 characterId: character.character_id,
214 tileX: 9,
215 tileY: 8
216 });
217 const result = harness.simulation.step();
218 assert.ok(result.events.some((event) =>
219 event.type === "command-error"
220 && event.message === "You need a pickaxe to mine this!"));
221 }
222 finally
223 { harness.close(); }
224});
225
226test("mining uses the best pickaxe in inventory without equipping", () =>
227{
228 const harness = makeSimulation({ workerCount: 1 });
229 try
230 {
231 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
232 const playerId = harness.simulation.spawnCharacter(1, character);
233 const record = harness.simulation.entityRecord(playerId);
234 const worker = harness.simulation.workers[record.workerId];
235 setSkillLevel(worker, playerId, SkillId.mining, 10);
236 const handle = worker.world.ecs.handleOf(playerId);
237 const inventory = worker.world.ecs.get(handle, Component.inventory);
238 inventory.slots[0] = { itemId: ItemId.bronzePickaxe, quantity: 1 };
239 inventory.slots[1] = { itemId: ItemId.ironPickaxe, quantity: 1 };
240 placeOre(worker, 9, 8, "iron-ore");
241
242 harness.simulation.enqueue(1, {
243 type: "mine",
244 characterId: character.character_id,
245 tileX: 9,
246 tileY: 8
247 });
248 harness.simulation.step();
249 advanceMs(harness, miningDurationMs(MaterialTier.iron, MaterialTier.iron));
250
251 const bag = harness.simulation.bagOf(playerId);
252 assert.ok(countItem(bag.inventory, ItemId.ironOre) >= 1);
253 const oreStack = (bag.inventory ?? []).find((slot) => slot?.itemId === ItemId.ironOre);
254 assert.ok(oreStack?.origins?.some((entry) =>
255 entry.origin?.kind === "gathered"
256 && entry.origin?.verb === "Mined"
257 && typeof entry.origin?.displayName === "string"
258 && entry.origin.displayName.length > 0));
259 }
260 finally
261 { harness.close(); }
262});
263
264test("mining grants ore and Mining XP then exhausts and respawns the node", () =>
265{
266 const harness = makeSimulation({ workerCount: 1 });
267 try
268 {
269 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
270 const playerId = harness.simulation.spawnCharacter(1, character);
271 const record = harness.simulation.entityRecord(playerId);
272 const worker = harness.simulation.workers[record.workerId];
273 bagPickaxe(worker, playerId, ItemId.bronzePickaxe);
274 const { data, index } = placeOre(worker, 9, 8, "copper-ore");
275
276 harness.simulation.enqueue(1, {
277 type: "mine",
278 characterId: character.character_id,
279 tileX: 9,
280 tileY: 8
281 });
282 const start = harness.simulation.step();
283 assert.ok(start.events.some((event) => event.type === "gather-swing" || event.type === "tile-shove"));
284 const swing = start.events.find((event) => event.type === "gather-swing");
285 assert.equal(swing?.toolItemId, ItemId.bronzePickaxe);
286
287 advanceMs(harness, miningDurationMs(GatherMaterialId.copper, MaterialTier.bronze));
288
289 const bag = harness.simulation.bagOf(playerId);
290 assert.ok(countItem(bag.inventory, ItemId.copperOre) >= 1);
291 const skills = harness.stateDb.getSkills(character.character_id);
292 assert.ok(skills.xp.mining > 0);
293 assert.equal(oreItemIdForMaterial(GatherMaterialId.copper), ItemId.copperOre);
294 assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE);
295
296 harness.simulation.enqueue(1, {
297 type: "mine",
298 characterId: character.character_id,
299 tileX: 9,
300 tileY: 8
301 });
302 const exhaustedResult = harness.simulation.step();
303 assert.ok(exhaustedResult.events.some((event) =>
304 event.type === "command-error" && /exhausted/i.test(event.message ?? "")));
305
306 advanceMs(harness, oreExhaustDurationMs(GatherMaterialId.copper));
307 assert.equal(data.blocks[index].state, "copper-ore");
308 }
309 finally
310 { harness.close(); }
311});
312
313test("coal mining yields coal and exhausts the node", () =>
314{
315 const harness = makeSimulation({ workerCount: 1 });
316 try
317 {
318 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
319 const playerId = harness.simulation.spawnCharacter(1, character);
320 const record = harness.simulation.entityRecord(playerId);
321 const worker = harness.simulation.workers[record.workerId];
322 setSkillLevel(worker, playerId, SkillId.mining, 20);
323 bagPickaxe(worker, playerId, ItemId.steelPickaxe);
324 const { data, index } = placeOre(worker, 9, 8, "coal");
325
326 harness.simulation.enqueue(1, {
327 type: "mine",
328 characterId: character.character_id,
329 tileX: 9,
330 tileY: 8
331 });
332 harness.simulation.step();
333 advanceMs(harness, miningDurationMs(GatherMaterialId.coal, MaterialTier.steel));
334
335 const bag = harness.simulation.bagOf(playerId);
336 assert.ok(countItem(bag.inventory, ItemId.coal) >= 1);
337 assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE);
338 }
339 finally
340 { harness.close(); }
341});