BlueGit

timfa questward

Files

questward / server / tests / fishing.test.js

fishing.test.js

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