BlueGit

timfa questward

Files

questward / server / tests / skills.test.js

skills.test.js

skills.test.js 4.8 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} from "../../shared/skills/state.js";
18import { skillLevel, SkillId, playerMaxHealth } from "../../shared/definitions/skills.js";
19import { resolveMeleeSwing, attackProfileOf } from "../src/combat/service.js";
20import { mobComponents } from "../src/simulation/spawners.js";
21import { MobId } from "../../shared/definitions/mobs.js";
22import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
23
24test("xp to advance doubles each level starting at 50", () =>
25{
26 assert.equal(xpToAdvanceFromLevel(1), 50);
27 assert.equal(xpToAdvanceFromLevel(2), 100);
28 assert.equal(xpToAdvanceFromLevel(3), 200);
29 assert.equal(xpToAdvanceFromLevel(99), 0);
30});
31
32test("level is derived from total xp", () =>
33{
34 assert.equal(levelFromTotalXp(0), 1);
35 assert.equal(levelFromTotalXp(SKILL_XP_BASE - 1), 1);
36 assert.equal(levelFromTotalXp(SKILL_XP_BASE), 2);
37 assert.equal(totalXpForLevel(2), SKILL_XP_BASE);
38 assert.equal(totalXpForLevel(3), SKILL_XP_BASE + xpToAdvanceFromLevel(2));
39});
40
41test("skills cap at level 99", () =>
42{
43 const capped = totalXpForLevel(100) - 1;
44 assert.equal(levelFromTotalXp(capped), 99);
45 const progress = xpProgress(capped);
46 assert.equal(progress.level, 99);
47 assert.equal(progress.xpToNext, 0);
48});
49
50test("xp progress reports in-level remainder", () =>
51{
52 const progress = xpProgress(SKILL_XP_BASE + 25);
53 assert.equal(progress.level, 2);
54 assert.equal(progress.xpInLevel, 25);
55 assert.equal(progress.xpToNext, xpToAdvanceFromLevel(2));
56});
57
58test("normalizeSkillXp fills every skill id", () =>
59{
60 const xp = normalizeSkillXp({ [SkillId.strength]: 42 });
61 assert.equal(xp[SkillId.strength], 42);
62 assert.equal(xp[SkillId.magic], 0);
63});
64
65test("skillLevel reads player xp and mob levels", () =>
66{
67 assert.equal(skillLevel(normalizePlayerSkills(null), SkillId.clubfighting), 1);
68 assert.equal(skillLevel({ xp: { [SkillId.strength]: SKILL_XP_BASE } }, SkillId.strength), 2);
69 assert.equal(skillLevel({ levels: { [SkillId.unarmed]: 5 } }, SkillId.unarmed), 5);
70});
71
72test("new characters start with Health level 10", () =>
73{
74 const skills = defaultPlayerSkills();
75 assert.equal(skillLevel(skills, SkillId.health), 10);
76 assert.equal(skillLevel(skills, SkillId.strength), 1);
77 assert.equal(playerMaxHealth(skills), 10);
78});
79
80test("publicSkillsView exposes progress fields for the client", () =>
81{
82 const view = publicSkillsView(normalizePlayerSkills({ [SkillId.clubfighting]: 50 }));
83 const club = view.find((entry) => entry.skillId === SkillId.clubfighting);
84 assert.equal(club.level, 2);
85 assert.equal(club.xp, 0);
86 assert.equal(club.xpToNext, xpToAdvanceFromLevel(2));
87 assert.match(club.icon, /clubfighting\.svg$/);
88});
89
90test("addSkillXp mutates the player skills component", () =>
91{
92 const skills = normalizePlayerSkills(null);
93 addSkillXp(skills, SkillId.strength, 10);
94 assert.equal(skills.xp[SkillId.strength], 10);
95});
96
97test("combat hit xp is split equally", () =>
98{
99 assert.equal(combatHitXpPerSkill(), 5);
100});
101
102test("successful player melee hits award split xp", () =>
103{
104 const harness = makeSimulation({ workerCount: 1 });
105 const random = Math.random;
106 Math.random = () => 0;
107 try
108 {
109 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
110 const playerId = harness.simulation.spawnCharacter(1, character);
111 const ratId = harness.simulation.spawnEntity(mobComponents({
112 mobTypeId: MobId.rat,
113 position: { tileX: 9, tileY: 8 }
114 }));
115 const worker = harness.simulation.workers[0];
116 const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(playerId));
117 const events = resolveMeleeSwing(worker.world, playerId, ratId, profile, Date.now());
118 const updated = events.find((event) => event.type === "skills-updated");
119 assert.ok(updated);
120 assert.equal(updated.stored[profile.hitSkillId], combatHitXpPerSkill());
121 assert.equal(updated.stored[profile.damageSkillId], combatHitXpPerSkill());
122 }
123 finally
124 {
125 Math.random = random;
126 harness.close();
127 }
128});
129
130test("skills persist when a character despawns", () =>
131{
132 const harness = makeSimulation({ workerCount: 1 });
133 try
134 {
135 const character = makeCharacter(harness.stateDb);
136 const playerId = harness.simulation.spawnCharacter(1, character);
137 const worker = harness.simulation.workers[0];
138 const skills = worker.world.ecs.get(worker.world.ecs.handleOf(playerId), "skills");
139 skills.xp[SkillId.strength] = SKILL_XP_BASE + 10;
140 harness.simulation.despawnCharacter(character.character_id);
141 const stored = harness.stateDb.getSkills(character.character_id);
142 assert.equal(stored.xp[SkillId.strength], SKILL_XP_BASE + 10);
143 }
144 finally
145 { harness.close(); }
146});