BlueGit

timfa questward

Files

questward / server / tests / skills.test.js

skills.test.js

spawner tweaks
timfa Tim Falken · pushed by timfa · 2 months ago
661b81e5 History
skills.test.js 7.1 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 SKILL_XP_BASE,
5 xpToAdvanceFromLevel,
6 totalXpForLevel,
7 xpProgress,
8 levelFromTotalXp,
9 combatHitXpPerSkill
10} from "../../shared/skills/progression.js";
11import {
12 normalizeSkillXp,
13 normalizePlayerSkills,
14 defaultPlayerSkills,
15 addSkillXp,
16 publicSkillsView,
17 enforceMinimumSkillLevels
18} from "../../shared/skills/state.js";
19import { skillLevel, SkillId, playerMaxHealth } from "../../shared/definitions/skills.js";
20import { resolveMeleeSwing, attackProfileOf } from "../src/combat/service.js";
21import { mobComponents } from "../src/simulation/spawners.js";
22import { MobId } from "../../shared/definitions/mobs.js";
23import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
24
25test("enforceMinimumSkillLevels raises Health to 10 and leaves higher XP alone", () =>
26{
27 const low = { xp: { health: 0, strength: 0 } };
28 assert.equal(enforceMinimumSkillLevels(low), true);
29 assert.equal(skillLevel(low, SkillId.health), 10);
30 assert.equal(skillLevel(low, SkillId.strength), 1);
31
32 const high = { xp: { health: totalXpForLevel(12), strength: totalXpForLevel(5) } };
33 assert.equal(enforceMinimumSkillLevels(high), false);
34 assert.equal(skillLevel(high, SkillId.health), 12);
35 assert.equal(skillLevel(high, SkillId.strength), 5);
36});
37
38test("login floors under-minimum skills and persists them", () =>
39{
40 const harness = makeSimulation({ workerCount: 1 });
41 try
42 {
43 const character = makeCharacter(harness.stateDb);
44 harness.stateDb.db.prepare("UPDATE character_bags SET skills_blob = ? WHERE character_id = ?")
45 .run(JSON.stringify({ xp: { health: 3, strength: 0 } }), character.character_id);
46
47 harness.simulation.spawnCharacter(1, character);
48 const stored = harness.stateDb.getSkills(character.character_id);
49 assert.equal(skillLevel(stored, SkillId.health), 10);
50 assert.equal(skillLevel(stored, SkillId.strength), 1);
51 assert.equal(skillLevel(stored, SkillId.defence), 1);
52 }
53 finally
54 { harness.close(); }
55});
56
57test("normalizeSkillXp floors Health to the level-10 starting XP", () =>
58{
59 assert.equal(normalizeSkillXp({}).health, totalXpForLevel(10));
60 assert.equal(normalizeSkillXp({ health: 14 }).health, totalXpForLevel(10));
61 assert.equal(normalizeSkillXp({ health: totalXpForLevel(11) }).health, totalXpForLevel(11));
62});
63
64test("legacy empty skills_blob loads as Health level 10 max HP", () =>
65{
66 const harness = makeSimulation({ workerCount: 1 });
67 try
68 {
69 const character = makeCharacter(harness.stateDb);
70 harness.stateDb.db.prepare("UPDATE character_bags SET skills_blob = ? WHERE character_id = ?")
71 .run("{}", character.character_id);
72 const skills = harness.stateDb.getSkills(character.character_id);
73 assert.equal(skillLevel(skills, SkillId.health), 10);
74 assert.equal(playerMaxHealth(skills), 10);
75 const stored = JSON.parse(
76 harness.stateDb.db.prepare("SELECT skills_blob FROM character_bags WHERE character_id = ?")
77 .get(character.character_id).skills_blob
78 );
79 assert.ok((stored.xp?.health ?? stored.health) >= totalXpForLevel(10));
80 }
81 finally
82 { harness.close(); }
83});
84
85test("xp to advance doubles each level starting at 50", () =>
86{
87 assert.equal(xpToAdvanceFromLevel(1), 50);
88 assert.equal(xpToAdvanceFromLevel(2), 100);
89 assert.equal(xpToAdvanceFromLevel(3), 200);
90 assert.equal(xpToAdvanceFromLevel(99), 0);
91});
92
93test("level is derived from total xp", () =>
94{
95 assert.equal(levelFromTotalXp(0), 1);
96 assert.equal(levelFromTotalXp(SKILL_XP_BASE - 1), 1);
97 assert.equal(levelFromTotalXp(SKILL_XP_BASE), 2);
98 assert.equal(totalXpForLevel(2), SKILL_XP_BASE);
99 assert.equal(totalXpForLevel(3), SKILL_XP_BASE + xpToAdvanceFromLevel(2));
100});
101
102test("skills cap at level 99", () =>
103{
104 const capped = totalXpForLevel(100) - 1;
105 assert.equal(levelFromTotalXp(capped), 99);
106 const progress = xpProgress(capped);
107 assert.equal(progress.level, 99);
108 assert.equal(progress.xpToNext, 0);
109});
110
111test("xp progress reports in-level remainder", () =>
112{
113 const progress = xpProgress(SKILL_XP_BASE + 25);
114 assert.equal(progress.level, 2);
115 assert.equal(progress.xpInLevel, 25);
116 assert.equal(progress.xpToNext, xpToAdvanceFromLevel(2));
117});
118
119test("normalizeSkillXp fills every skill id", () =>
120{
121 const xp = normalizeSkillXp({ [SkillId.strength]: 42 });
122 assert.equal(xp[SkillId.strength], 42);
123 assert.equal(xp[SkillId.magic], 0);
124});
125
126test("skillLevel reads player xp and mob levels", () =>
127{
128 assert.equal(skillLevel(normalizePlayerSkills(null), SkillId.clubfighting), 1);
129 assert.equal(skillLevel({ xp: { [SkillId.strength]: SKILL_XP_BASE } }, SkillId.strength), 2);
130 assert.equal(skillLevel({ levels: { [SkillId.unarmed]: 5 } }, SkillId.unarmed), 5);
131});
132
133test("new characters start with Health level 10", () =>
134{
135 const skills = defaultPlayerSkills();
136 assert.equal(skillLevel(skills, SkillId.health), 10);
137 assert.equal(skillLevel(skills, SkillId.strength), 1);
138 assert.equal(playerMaxHealth(skills), 10);
139});
140
141test("publicSkillsView exposes progress fields for the client", () =>
142{
143 const view = publicSkillsView(normalizePlayerSkills({ [SkillId.clubfighting]: 50 }));
144 const club = view.find((entry) => entry.skillId === SkillId.clubfighting);
145 assert.equal(club.level, 2);
146 assert.equal(club.xp, 0);
147 assert.equal(club.xpToNext, xpToAdvanceFromLevel(2));
148 assert.match(club.icon, /clubfighting\.svg$/);
149});
150
151test("addSkillXp mutates the player skills component", () =>
152{
153 const skills = normalizePlayerSkills(null);
154 addSkillXp(skills, SkillId.strength, 10);
155 assert.equal(skills.xp[SkillId.strength], 10);
156});
157
158test("combat hit xp is split equally", () =>
159{
160 assert.equal(combatHitXpPerSkill(), 5);
161});
162
163test("successful player melee hits award split xp", () =>
164{
165 const harness = makeSimulation({ workerCount: 1 });
166 const random = Math.random;
167 Math.random = () => 0;
168 try
169 {
170 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
171 const playerId = harness.simulation.spawnCharacter(1, character);
172 const ratId = harness.simulation.spawnEntity(mobComponents({
173 mobTypeId: MobId.rat,
174 tileX: 9,
175 tileY: 8
176 }));
177 const worker = harness.simulation.workers[0];
178 const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(playerId));
179 const events = resolveMeleeSwing(worker.world, playerId, ratId, profile, Date.now());
180 const updated = events.find((event) => event.type === "skills-updated");
181 assert.ok(updated);
182 assert.equal(updated.stored[profile.hitSkillId], combatHitXpPerSkill());
183 assert.equal(updated.stored[profile.damageSkillId], combatHitXpPerSkill());
184 }
185 finally
186 {
187 Math.random = random;
188 harness.close();
189 }
190});
191
192test("skills persist when a character despawns", () =>
193{
194 const harness = makeSimulation({ workerCount: 1 });
195 try
196 {
197 const character = makeCharacter(harness.stateDb);
198 const playerId = harness.simulation.spawnCharacter(1, character);
199 const worker = harness.simulation.workers[0];
200 const skills = worker.world.ecs.get(worker.world.ecs.handleOf(playerId), "skills");
201 skills.xp[SkillId.strength] = SKILL_XP_BASE + 10;
202 harness.simulation.despawnCharacter(character.character_id);
203 const stored = harness.stateDb.getSkills(character.character_id);
204 assert.equal(stored.xp[SkillId.strength], SKILL_XP_BASE + 10);
205 }
206 finally
207 { harness.close(); }
208});