BlueGit

timfa questward

Files

questward / server / tests / mining.test.js

mining.test.js

mining.test.js 9.4 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, EQUIPMENT_SLOT } 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 equipPickaxe (worker, playerId, itemId)
50{
51 const handle = worker.world.ecs.handleOf(playerId);
52 const equipment = worker.world.ecs.get(handle, Component.equipment);
53 equipment.slots[EQUIPMENT_SLOT.weapon] = { 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(tierRank(MaterialTier.dragon), 7);
83});
84
85test("mining duration uses resource and tool ranks", () =>
86{
87 assert.equal(miningDurationMs(MaterialTier.bronze, MaterialTier.bronze), 5000);
88 assert.equal(miningDurationMs(MaterialTier.iron, MaterialTier.bronze), 13000);
89 assert.ok(miningDurationMs(MaterialTier.bronze, MaterialTier.dragon) >= 500);
90});
91
92test("ore exhaust duration is 10s times material rank", () =>
93{
94 assert.equal(oreExhaustDurationMs(MaterialTier.bronze), 10_000);
95 assert.equal(oreExhaustDurationMs(MaterialTier.iron), 20_000);
96 assert.equal(oreExhaustDurationMs(GatherMaterialId.coal), 20_000);
97 assert.equal(oreExhaustDurationMs(MaterialTier.dragon), 70_000);
98});
99
100test("ore boulder states map to materials", () =>
101{
102 assert.equal(materialFromOreBoulderState("bronze-ore"), MaterialTier.bronze);
103 assert.equal(materialFromOreBoulderState("coal"), GatherMaterialId.coal);
104 assert.equal(materialFromOreBoulderState("boulder"), null);
105 assert.equal(oreBoulderState(MaterialTier.mithril), "mithril-ore");
106});
107
108test("boulder block exists", () =>
109{
110 assert.equal(blockDefinition(BlockType.boulder)?.key, "boulder");
111});
112
113test("iron pickaxe requires Mining 10", () =>
114{
115 const skills = normalizePlayerSkills(null);
116 assert.equal(canEquipItem(skills, itemDefinition(ItemId.ironPickaxe)).ok, false);
117 skills.xp[SkillId.mining] = totalXpForLevel(10);
118 assert.equal(canEquipItem(skills, itemDefinition(ItemId.ironPickaxe)).ok, true);
119 assert.equal(canEquipItem(skills, itemDefinition(ItemId.bronzePickaxe)).ok, true);
120});
121
122test("canMineMaterial enforces skill and tool tier", () =>
123{
124 assert.equal(canMineMaterial(1, MaterialTier.bronze, MaterialTier.iron).ok, false);
125 assert.equal(canMineMaterial(5, MaterialTier.bronze, MaterialTier.iron).ok, false);
126 assert.equal(canMineMaterial(5, MaterialTier.iron, MaterialTier.iron).ok, true);
127});
128
129test("mining without a pickaxe is rejected", () =>
130{
131 const harness = makeSimulation({ workerCount: 1 });
132 try
133 {
134 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
135 const playerId = harness.simulation.spawnCharacter(1, character);
136 const record = harness.simulation.entityRecord(playerId);
137 const worker = harness.simulation.workers[record.workerId];
138 placeOre(worker, 9, 8, "bronze-ore");
139
140 harness.simulation.enqueue(1, {
141 type: "mine",
142 characterId: character.character_id,
143 tileX: 9,
144 tileY: 8
145 });
146 const result = harness.simulation.step();
147 assert.ok(result.events.some((event) =>
148 event.type === "command-error" && /pickaxe/i.test(event.message ?? "")));
149 }
150 finally
151 { harness.close(); }
152});
153
154test("plain boulder cannot be mined", () =>
155{
156 const harness = makeSimulation({ workerCount: 1 });
157 try
158 {
159 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
160 const playerId = harness.simulation.spawnCharacter(1, character);
161 const record = harness.simulation.entityRecord(playerId);
162 const worker = harness.simulation.workers[record.workerId];
163 equipPickaxe(worker, playerId, ItemId.bronzePickaxe);
164 placeOre(worker, 9, 8, EXHAUSTED_ORE_BOULDER_STATE);
165
166 harness.simulation.enqueue(1, {
167 type: "mine",
168 characterId: character.character_id,
169 tileX: 9,
170 tileY: 8
171 });
172 const result = harness.simulation.step();
173 assert.ok(result.events.some((event) =>
174 event.type === "command-error"
175 && (/no ore|exhausted/i.test(event.message ?? ""))));
176 }
177 finally
178 { harness.close(); }
179});
180
181test("weak pickaxe cannot mine higher-tier ore", () =>
182{
183 const harness = makeSimulation({ workerCount: 1 });
184 try
185 {
186 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
187 const playerId = harness.simulation.spawnCharacter(1, character);
188 const record = harness.simulation.entityRecord(playerId);
189 const worker = harness.simulation.workers[record.workerId];
190 equipPickaxe(worker, playerId, ItemId.bronzePickaxe);
191 setSkillLevel(worker, playerId, SkillId.mining, 10);
192 placeOre(worker, 9, 8, "iron-ore");
193
194 harness.simulation.enqueue(1, {
195 type: "mine",
196 characterId: character.character_id,
197 tileX: 9,
198 tileY: 8
199 });
200 const result = harness.simulation.step();
201 assert.ok(result.events.some((event) =>
202 event.type === "command-error" && /too weak|pickaxe/i.test(event.message ?? "")));
203 }
204 finally
205 { harness.close(); }
206});
207
208test("mining grants ore and Mining XP then exhausts and respawns the node", () =>
209{
210 const harness = makeSimulation({ workerCount: 1 });
211 try
212 {
213 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
214 const playerId = harness.simulation.spawnCharacter(1, character);
215 const record = harness.simulation.entityRecord(playerId);
216 const worker = harness.simulation.workers[record.workerId];
217 equipPickaxe(worker, playerId, ItemId.bronzePickaxe);
218 const { data, index } = placeOre(worker, 9, 8, "bronze-ore");
219
220 harness.simulation.enqueue(1, {
221 type: "mine",
222 characterId: character.character_id,
223 tileX: 9,
224 tileY: 8
225 });
226 const start = harness.simulation.step();
227 assert.ok(start.events.some((event) => event.type === "gather-swing" || event.type === "tile-shove"));
228
229 advanceMs(harness, miningDurationMs(MaterialTier.bronze, MaterialTier.bronze));
230
231 const bag = harness.simulation.bagOf(playerId);
232 assert.ok(countItem(bag.inventory, ItemId.bronzeOre) >= 1);
233 const skills = harness.stateDb.getSkills(character.character_id);
234 assert.ok(skills.xp.mining > 0);
235 assert.equal(oreItemIdForMaterial(MaterialTier.bronze), ItemId.bronzeOre);
236 assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE);
237
238 harness.simulation.enqueue(1, {
239 type: "mine",
240 characterId: character.character_id,
241 tileX: 9,
242 tileY: 8
243 });
244 const exhaustedResult = harness.simulation.step();
245 assert.ok(exhaustedResult.events.some((event) =>
246 event.type === "command-error" && /exhausted/i.test(event.message ?? "")));
247
248 advanceMs(harness, oreExhaustDurationMs(MaterialTier.bronze));
249 assert.equal(data.blocks[index].state, "bronze-ore");
250 }
251 finally
252 { harness.close(); }
253});
254
255test("coal mining yields coal and exhausts the node", () =>
256{
257 const harness = makeSimulation({ workerCount: 1 });
258 try
259 {
260 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
261 const playerId = harness.simulation.spawnCharacter(1, character);
262 const record = harness.simulation.entityRecord(playerId);
263 const worker = harness.simulation.workers[record.workerId];
264 setSkillLevel(worker, playerId, SkillId.mining, 10);
265 equipPickaxe(worker, playerId, ItemId.ironPickaxe);
266 const { data, index } = placeOre(worker, 9, 8, "coal");
267
268 harness.simulation.enqueue(1, {
269 type: "mine",
270 characterId: character.character_id,
271 tileX: 9,
272 tileY: 8
273 });
274 harness.simulation.step();
275 advanceMs(harness, miningDurationMs(GatherMaterialId.coal, MaterialTier.iron));
276
277 const bag = harness.simulation.bagOf(playerId);
278 assert.ok(countItem(bag.inventory, ItemId.coal) >= 1);
279 assert.equal(data.blocks[index].state, EXHAUSTED_ORE_BOULDER_STATE);
280 }
281 finally
282 { harness.close(); }
283});