BlueGit

timfa questward

Files

questward / server / tests / fishing.test.js

fishing.test.js

fishing.test.js 10.0 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType, GroundType } from "../../shared/definitions/tiles.js";
4import
5 {
6 rawFishItemId,
7 cookedFishItemId,
8 baitItemIdForCloth,
9 FishingToolItemId,
10 fishingDifficulty,
11 fishingExhaustDurationMs,
12 normalizeFishingSpotConfig
13 } from "../../shared/definitions/fishItems.js";
14import { ClothTier } from "../../shared/definitions/materialTiers.js";
15import { yarnItemId } from "../../shared/definitions/clothItems.js";
16import { EquipmentKind } from "../../shared/definitions/equipmentKinds.js";
17import { SkillId } from "../../shared/definitions/skills.js";
18import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
19import { totalXpForLevel } from "../../shared/skills/progression.js";
20import { itemDefinition } from "../../shared/definitions/items.js";
21import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
22import { Component } from "../src/ecs/components.js";
23
24function workerOf (simulation, playerId)
25{
26 const record = simulation.entityRecord(playerId);
27 return simulation.workers[record.workerId];
28}
29
30function setSkillLevel (worker, playerId, skillId, level)
31{
32 const handle = worker.world.ecs.handleOf(playerId);
33 const skills = worker.world.ecs.get(handle, Component.skills);
34 skills.xp[skillId] = totalXpForLevel(level);
35}
36
37function giveInventory (harness, playerId, items)
38{
39 const inventory = emptySlots();
40 items.forEach((stack, index) =>
41 {
42 inventory[index] = { itemId: stack.itemId, quantity: stack.quantity };
43 });
44 harness.simulation.applyBag(playerId, {
45 inventory,
46 equipment: emptyEquipment(),
47 vault: [],
48 vaultReserved: []
49 });
50}
51
52function advanceMs (harness, ms)
53{
54 const ticks = Math.ceil(ms / 50) + 3;
55 for (let i = 0; i < ticks; i++)
56 {
57 harness.advance(50);
58 harness.simulation.step();
59 }
60}
61
62function placeFishingSpot (worker, tileX, tileY, fishes, groundType = GroundType.deepWater)
63{
64 const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16));
65 const localX = ((tileX % 16) + 16) % 16;
66 const localY = ((tileY % 16) + 16) % 16;
67 const index = localY * 16 + localX;
68 data.grounds[index] = { typeId: groundType, state: null };
69 data.blocks[index] = {
70 typeId: BlockType.fishingSpot,
71 state: "fishing-spot",
72 config: normalizeFishingSpotConfig({ fishes })
73 };
74}
75
76test("fishing difficulty and exhaust scale with tier", () =>
77{
78 assert.equal(fishingDifficulty(1), 1);
79 assert.equal(fishingDifficulty(2), 10);
80 assert.equal(fishingExhaustDurationMs(3), 30_000);
81});
82
83test("cooked fish healing is tier times 5", () =>
84{
85 assert.equal(itemDefinition(cookedFishItemId("minnow")).healing, 5);
86 assert.equal(itemDefinition(cookedFishItemId("shark")).healing, 35);
87});
88
89test("net catches tier-1 fish without bait and exhausts spot", () =>
90{
91 const harness = makeSimulation({ workerCount: 1 });
92 try
93 {
94 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
95 const playerId = harness.simulation.spawnCharacter(1, character);
96 const worker = workerOf(harness.simulation, playerId);
97 worker.world.rng = () => 0.999;
98 setSkillLevel(worker, playerId, SkillId.fishing, 1);
99 const minnow = rawFishItemId("minnow");
100 const trout = rawFishItemId("trout");
101 placeFishingSpot(worker, 9, 8, [minnow, trout]);
102 giveInventory(harness, playerId, [
103 { itemId: FishingToolItemId.net, quantity: 1 }
104 ]);
105
106 harness.simulation.enqueue(1, {
107 type: "start-fish",
108 characterId: character.character_id,
109 tileX: 9,
110 tileY: 8
111 });
112 advanceMs(harness, 10_000);
113
114 const bag = harness.simulation.bagOf(playerId);
115 assert.ok(bag.inventory.some((s) => s?.itemId === minnow), "expected minnow");
116 assert.equal(bag.inventory.some((s) => s?.itemId === trout), false);
117 const block = worker.world.chunkData(0, 0).blocks[8 * 16 + 9];
118 assert.equal(block.state, "exhausted");
119 assert.ok(worker.world.fishingSpotRespawns.has("overworld:9,8"));
120 }
121 finally
122 { harness.close(); }
123});
124
125test("rod consumes highest useful bait for spot", () =>
126{
127 const harness = makeSimulation({ workerCount: 1 });
128 try
129 {
130 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
131 const playerId = harness.simulation.spawnCharacter(1, character);
132 const worker = workerOf(harness.simulation, playerId);
133 worker.world.rng = () => 0.999;
134 setSkillLevel(worker, playerId, SkillId.fishing, 20);
135 const trout = rawFishItemId("trout");
136 placeFishingSpot(worker, 9, 8, [trout]);
137 const worms = baitItemIdForCloth(ClothTier.cotton);
138 const maggots = baitItemIdForCloth(ClothTier.wool);
139 const crickets = baitItemIdForCloth(ClothTier.linen);
140 giveInventory(harness, playerId, [
141 { itemId: FishingToolItemId.oakRod, quantity: 1 },
142 { itemId: worms, quantity: 2 },
143 { itemId: maggots, quantity: 2 },
144 { itemId: crickets, quantity: 2 }
145 ]);
146
147 harness.simulation.enqueue(1, {
148 type: "start-fish",
149 characterId: character.character_id,
150 tileX: 9,
151 tileY: 8
152 });
153 advanceMs(harness, 200);
154
155 const bag = harness.simulation.bagOf(playerId);
156 assert.equal(bag.inventory.find((s) => s?.itemId === crickets)?.quantity ?? 0, 1);
157 assert.equal(bag.inventory.find((s) => s?.itemId === maggots)?.quantity ?? 0, 2);
158 }
159 finally
160 { harness.close(); }
161});
162
163test("shallow water fishing blocked in winter", () =>
164{
165 const harness = makeSimulation({ workerCount: 1, now: Date.UTC(2026, 0, 15) });
166 try
167 {
168 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
169 const playerId = harness.simulation.spawnCharacter(1, character);
170 const worker = workerOf(harness.simulation, playerId);
171 setSkillLevel(worker, playerId, SkillId.fishing, 1);
172 placeFishingSpot(worker, 9, 8, [rawFishItemId("minnow")], GroundType.water);
173 giveInventory(harness, playerId, [
174 { itemId: FishingToolItemId.net, quantity: 1 }
175 ]);
176
177 harness.simulation.enqueue(1, {
178 type: "start-fish",
179 characterId: character.character_id,
180 tileX: 9,
181 tileY: 8
182 });
183 const result = harness.simulation.step();
184 assert.ok(result.events.some((e) => e.type === "command-error"));
185 assert.equal(worker.world.fishSessions.size, 0);
186 }
187 finally
188 { harness.close(); }
189});
190
191test("pole + yarn recipes: log makes pole, pole+2 yarn makes rod", () =>
192{
193 const pole = itemDefinition(FishingToolItemId.birchPole);
194 const rod = itemDefinition(FishingToolItemId.birchRod);
195 assert.equal(pole?.fishingPole, true);
196 assert.equal(rod?.poleItemId, FishingToolItemId.birchPole);
197 assert.equal(rod?.equipment?.kind, EquipmentKind.fishingRod);
198 assert.equal(pole?.equipment, undefined);
199});
200
201test("failed rod fishing can snap the line into pole + yarn", () =>
202{
203 const harness = makeSimulation({ workerCount: 1 });
204 try
205 {
206 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
207 const playerId = harness.simulation.spawnCharacter(1, character);
208 const worker = workerOf(harness.simulation, playerId);
209 // Always fail skill rolls (0) and always snap the line (0 < 0.05).
210 worker.world.rng = () => 0;
211 setSkillLevel(worker, playerId, SkillId.fishing, 10);
212 const perch = rawFishItemId("perch");
213 placeFishingSpot(worker, 9, 8, [perch]);
214 const maggots = baitItemIdForCloth(ClothTier.wool);
215 giveInventory(harness, playerId, [
216 { itemId: FishingToolItemId.birchRod, quantity: 1 },
217 { itemId: maggots, quantity: 1 }
218 ]);
219
220 harness.simulation.enqueue(1, {
221 type: "start-fish",
222 characterId: character.character_id,
223 tileX: 9,
224 tileY: 8
225 });
226 const events = [];
227 const ticks = Math.ceil(12_000 / 50) + 5;
228 for (let i = 0; i < ticks; i++)
229 {
230 harness.advance(50);
231 const step = harness.simulation.step();
232 events.push(...(step.events ?? []));
233 }
234
235 assert.ok(events.some((e) => e.type === "command-error" && e.message === "The fish got away."));
236 assert.ok(events.some((e) => e.type === "command-error" && e.message === "Your fishing line broke!"));
237 const bag = harness.simulation.bagOf(playerId);
238 assert.equal(bag.inventory.some((s) => s?.itemId === FishingToolItemId.birchRod), false);
239 assert.ok(bag.inventory.some((s) => s?.itemId === FishingToolItemId.birchPole), "expected pole");
240 assert.ok(bag.inventory.some((s) => s?.itemId === yarnItemId(ClothTier.wool)), "expected wool yarn");
241 }
242 finally
243 { harness.close(); }
244});
245
246test("line-break yarn drops to ground when bag is full", () =>
247{
248 const harness = makeSimulation({ workerCount: 1 });
249 try
250 {
251 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
252 const playerId = harness.simulation.spawnCharacter(1, character);
253 const worker = workerOf(harness.simulation, playerId);
254 worker.world.rng = () => 0;
255 setSkillLevel(worker, playerId, SkillId.fishing, 10);
256 const perch = rawFishItemId("perch");
257 placeFishingSpot(worker, 9, 8, [perch]);
258 const maggots = baitItemIdForCloth(ClothTier.wool);
259 const yarn = yarnItemId(ClothTier.wool);
260 // Rod equipped; bag full except bait (consumed on start → one free slot for pole only).
261 const slots = emptySlots();
262 slots[0] = { itemId: maggots, quantity: 1 };
263 for (let i = 1; i < slots.length; i++)
264 { slots[i] = { itemId: rawFishItemId("minnow"), quantity: 1 }; }
265 const equipment = emptyEquipment();
266 equipment.slots.weapon = { itemId: FishingToolItemId.birchRod, quantity: 1 };
267 harness.simulation.applyBag(playerId, {
268 inventory: slots,
269 equipment,
270 vault: [],
271 vaultReserved: []
272 });
273
274 harness.simulation.enqueue(1, {
275 type: "start-fish",
276 characterId: character.character_id,
277 tileX: 9,
278 tileY: 8
279 });
280 const events = [];
281 const ticks = Math.ceil(12_000 / 50) + 5;
282 for (let i = 0; i < ticks; i++)
283 {
284 harness.advance(50);
285 const step = harness.simulation.step();
286 events.push(...(step.events ?? []));
287 }
288
289 assert.ok(events.some((e) => e.message === "Your fishing line broke!"));
290 const bag = harness.simulation.bagOf(playerId);
291 assert.ok(bag.inventory.some((s) => s?.itemId === FishingToolItemId.birchPole));
292 assert.equal(bag.equipment?.slots?.weapon, null);
293 assert.equal(bag.inventory.some((s) => s?.itemId === yarn), false);
294 assert.ok(events.some((e) =>
295 e.type === "ground-item-appear" && e.stack?.itemId === yarn
296 ), "yarn should appear on the ground");
297 }
298 finally
299 { harness.close(); }
300});