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