BlueGit

timfa questward

Files

questward / server / tests / stamina.test.js

stamina.test.js

stamina.test.js 6.3 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { GAIT } from "../../shared/protocol/constants.js";
4import { SkillId, skillLevel } from "../../shared/definitions/skills.js";
5import { totalXpForLevel } from "../../shared/skills/progression.js";
6import {
7 staminaCostPerRunStep,
8 STAMINA_REGEN_PER_SECOND,
9 PATH_RUN_STAMINA_FACTOR
10} from "../../shared/stamina/rules.js";
11import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js";
12
13function workerOf (simulation, entityId)
14{
15 const record = simulation.entityRecord(entityId);
16 assert.ok(record);
17 return simulation.workers[record.workerId];
18}
19
20function componentsOf (simulation, entityId)
21{
22 const worker = workerOf(simulation, entityId);
23 const handle = worker.world.ecs.handleOf(entityId);
24 assert.ok(handle != null);
25 return worker.world.ecs.componentsOf(handle);
26}
27
28function setStaminaXp (harness, characterId, level)
29{
30 const skills = harness.stateDb.getSkills(characterId);
31 skills.xp.stamina = totalXpForLevel(level);
32 harness.stateDb.saveSkills(characterId, skills);
33}
34
35test("staminaCostPerRunStep drains one point per second of continuous running", () =>
36{
37 assert.equal(staminaCostPerRunStep(2), 0.25);
38 assert.equal(staminaCostPerRunStep(1), 0.5);
39});
40
41test("path tiles cost half stamina while running", () =>
42{
43 assert.equal(staminaCostPerRunStep(2, { onPath: true }), 0.25 * PATH_RUN_STAMINA_FACTOR);
44 assert.equal(PATH_RUN_STAMINA_FACTOR, 0.5);
45});
46
47test("set-gait to run is rejected when stamina is empty", () =>
48{
49 const harness = makeSimulation({ workerCount: 1 });
50 try
51 {
52 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
53 const playerId = harness.simulation.spawnCharacter(1, character);
54 const comps = componentsOf(harness.simulation, playerId);
55 comps.stamina.current = 0;
56
57 harness.simulation.enqueue(1, {
58 type: "set-gait",
59 characterId: character.character_id,
60 gait: GAIT.run
61 });
62 const result = harness.simulation.step();
63 assert.ok(result.events.some((event) =>
64 event.type === "command-error" && /exhausted/i.test(event.message)
65 ));
66 assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.walk);
67 }
68 finally
69 { harness.close(); }
70});
71
72test("running drains stamina, awards XP, and forces walk at zero", () =>
73{
74 const harness = makeSimulation({ workerCount: 1 });
75 try
76 {
77 const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 });
78 setStaminaXp(harness, character.character_id, 6);
79 const playerId = harness.simulation.spawnCharacter(1, character);
80 assert.equal(harness.simulation.staminaOf(playerId).max, 6);
81 // Drain a short bar so the test finishes quickly.
82 componentsOf(harness.simulation, playerId).stamina.current = 2;
83
84 harness.simulation.enqueue(1, {
85 type: "set-gait",
86 characterId: character.character_id,
87 gait: GAIT.run
88 });
89 harness.simulation.enqueue(1, {
90 type: "move-to",
91 characterId: character.character_id,
92 tileX: 30,
93 tileY: 8
94 });
95
96 const xpBefore = harness.stateDb.getSkills(character.character_id).xp.stamina;
97 let forcedWalk = false;
98 for (let i = 0; i < 120; i++)
99 {
100 harness.advance(50);
101 const events = harness.simulation.step().events;
102 const stamina = harness.simulation.staminaOf(playerId);
103 if (events.some((event) => event.type === "stamina" && event.gait === GAIT.walk && event.current <= 0))
104 { forcedWalk = true; }
105 if (stamina.current <= 0)
106 {
107 assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.walk);
108 forcedWalk = true;
109 break;
110 }
111 }
112 assert.equal(forcedWalk, true);
113 assert.ok(harness.simulation.staminaOf(playerId).current <= 0);
114 assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.walk);
115
116 const xpAfter = harness.stateDb.getSkills(character.character_id).xp.stamina;
117 assert.ok(xpAfter > xpBefore, "running should award Stamina XP");
118 assert.equal(skillLevel({ xp: { stamina: xpAfter } }, SkillId.stamina) >= 6, true);
119 }
120 finally
121 { harness.close(); }
122});
123
124test("walking regenerates stamina over time", () =>
125{
126 const harness = makeSimulation({ workerCount: 1 });
127 try
128 {
129 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
130 setStaminaXp(harness, character.character_id, 3);
131 const playerId = harness.simulation.spawnCharacter(1, character);
132 const comps = componentsOf(harness.simulation, playerId);
133 comps.stamina.current = 0;
134 comps.movement.gait = GAIT.walk;
135
136 const seconds = 2.2;
137 const ticks = Math.ceil(seconds * 20);
138 for (let i = 0; i < ticks; i++)
139 {
140 harness.advance(50);
141 harness.simulation.step();
142 }
143 const stamina = harness.simulation.staminaOf(playerId);
144 assert.ok(
145 stamina.current >= seconds * STAMINA_REGEN_PER_SECOND - 0.2,
146 `expected ~${seconds} regen, got ${stamina.current}`
147 );
148 assert.ok(stamina.current <= 3);
149 }
150 finally
151 { harness.close(); }
152});
153
154test("running does not regenerate stamina while moving", () =>
155{
156 const harness = makeSimulation({ workerCount: 1 });
157 try
158 {
159 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
160 setStaminaXp(harness, character.character_id, 5);
161 const playerId = harness.simulation.spawnCharacter(1, character);
162 const comps = componentsOf(harness.simulation, playerId);
163 comps.stamina.current = 2;
164 comps.movement.gait = GAIT.run;
165
166 harness.simulation.enqueue(1, {
167 type: "move-to",
168 characterId: character.character_id,
169 tileX: 30,
170 tileY: 8
171 });
172
173 stepTimes(harness.simulation, 40);
174 assert.ok(harness.simulation.staminaOf(playerId).current <= 2.01);
175 }
176 finally
177 { harness.close(); }
178});
179
180test("run gait while idle still regenerates stamina", () =>
181{
182 const harness = makeSimulation({ workerCount: 1 });
183 try
184 {
185 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
186 setStaminaXp(harness, character.character_id, 5);
187 const playerId = harness.simulation.spawnCharacter(1, character);
188 const comps = componentsOf(harness.simulation, playerId);
189 comps.stamina.current = 1;
190 comps.movement.gait = GAIT.run;
191
192 const seconds = 1.5;
193 const ticks = Math.ceil(seconds * 20);
194 for (let i = 0; i < ticks; i++)
195 {
196 harness.advance(50);
197 harness.simulation.step();
198 }
199 const stamina = harness.simulation.staminaOf(playerId);
200 assert.ok(
201 stamina.current >= 1 + seconds * STAMINA_REGEN_PER_SECOND - 0.2,
202 `expected idle run regen, got ${stamina.current}`
203 );
204 }
205 finally
206 { harness.close(); }
207});