| 1 | import { SkillId, SKILL_DEFINITIONS, ALL_SKILL_IDS, DEFAULT_PLAYER_SKILL_LEVELS } from "../definitions/skills.js"; |
| 2 | import { combatLevelFromSkills } from "../combat/combatLevel.js"; |
| 3 | import { maxTotalSkillXp, totalXpForLevel, xpProgress, MAX_SKILL_LEVEL } from "./progression.js"; |
| 4 | import { skillXpMultiplier } from "../equipment/jewelryEffects.js"; |
| 5 | |
| 6 | /** Health starts at level 5; Stamina at 5; other skills at level 1. */ |
| 7 | export const STARTING_HEALTH_XP = totalXpForLevel(DEFAULT_PLAYER_SKILL_LEVELS[SkillId.health] ?? 5); |
| 8 | |
| 9 | export function emptySkillXp () |
| 10 | { |
| 11 | const xp = {}; |
| 12 | for (const skillId of ALL_SKILL_IDS) |
| 13 | { xp[skillId] = 0; } |
| 14 | return xp; |
| 15 | } |
| 16 | |
| 17 | /** Raise any skill below its default starting level up to that floor. Returns whether XP changed. */ |
| 18 | export function enforceMinimumSkillLevels (skillsComponent) |
| 19 | { |
| 20 | if (!skillsComponent?.xp) |
| 21 | { return false; } |
| 22 | let raised = false; |
| 23 | for (const skillId of ALL_SKILL_IDS) |
| 24 | { |
| 25 | const minLevel = DEFAULT_PLAYER_SKILL_LEVELS[skillId] ?? 1; |
| 26 | const minXp = totalXpForLevel(minLevel); |
| 27 | const current = Math.max(0, Math.floor(Number(skillsComponent.xp[skillId]) || 0)); |
| 28 | if (current < minXp) |
| 29 | { |
| 30 | skillsComponent.xp[skillId] = minXp; |
| 31 | raised = true; |
| 32 | } |
| 33 | } |
| 34 | return raised; |
| 35 | } |
| 36 | |
| 37 | export function normalizeSkillXp (raw) |
| 38 | { |
| 39 | const xp = emptySkillXp(); |
| 40 | if (raw && typeof raw === "object") |
| 41 | { |
| 42 | for (const skillId of ALL_SKILL_IDS) |
| 43 | { |
| 44 | const value = Math.max(0, Math.floor(Number(raw[skillId]) || 0)); |
| 45 | xp[skillId] = Number.isSafeInteger(value) ? value : 0; |
| 46 | } |
| 47 | } |
| 48 | enforceMinimumSkillLevels({ xp }); |
| 49 | return xp; |
| 50 | } |
| 51 | |
| 52 | export function normalizePlayerSkills (stored) |
| 53 | { |
| 54 | return { xp: normalizeSkillXp(stored?.xp ?? stored) }; |
| 55 | } |
| 56 | |
| 57 | /** Starting skills for a new character (Health 5, Stamina 5, others 1). */ |
| 58 | export function defaultPlayerSkills () |
| 59 | { |
| 60 | return normalizePlayerSkills({ |
| 61 | [SkillId.health]: STARTING_HEALTH_XP, |
| 62 | [SkillId.stamina]: totalXpForLevel(DEFAULT_PLAYER_SKILL_LEVELS[SkillId.stamina] ?? 5) |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | export function addSkillXp (skillsComponent, skillId, amount) |
| 67 | { |
| 68 | if (!skillsComponent?.xp) |
| 69 | { return false; } |
| 70 | const add = Math.max(0, Math.floor(Number(amount) || 0)); |
| 71 | if (!add) |
| 72 | { return false; } |
| 73 | const current = skillsComponent.xp[skillId] ?? 0; |
| 74 | if (xpProgress(current).level >= MAX_SKILL_LEVEL) |
| 75 | { return false; } |
| 76 | skillsComponent.xp[skillId] = Math.min(maxTotalSkillXp(), current + add); |
| 77 | return skillsComponent.xp[skillId] !== current; |
| 78 | } |
| 79 | |
| 80 | export function skillLevelUpPayload (skillId, level, previousLevel = null) |
| 81 | { |
| 82 | const def = SKILL_DEFINITIONS[skillId]; |
| 83 | const prev = previousLevel == null ? Math.max(0, (level | 0) - 1) : previousLevel | 0; |
| 84 | return { |
| 85 | skillId, |
| 86 | level, |
| 87 | previousLevel: prev, |
| 88 | displayName: def?.displayName ?? skillId, |
| 89 | icon: `/assets/skills/${skillId}.svg` |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | /** Apply XP and report whether the skill leveled up. */ |
| 94 | export function applySkillXp (skillsComponent, skillId, amount, equipment = null) |
| 95 | { |
| 96 | if (!skillsComponent?.xp) |
| 97 | { return { changed: false, levelUp: null }; } |
| 98 | const mult = skillXpMultiplier(equipment, skillsComponent, skillId); |
| 99 | const adjusted = Math.max(0, Math.floor((Number(amount) || 0) * mult)); |
| 100 | const before = skillsComponent.xp[skillId] ?? 0; |
| 101 | const beforeLevel = xpProgress(before).level; |
| 102 | const changed = addSkillXp(skillsComponent, skillId, adjusted); |
| 103 | if (!changed) |
| 104 | { return { changed: false, levelUp: null }; } |
| 105 | const afterLevel = xpProgress(skillsComponent.xp[skillId] ?? 0).level; |
| 106 | if (afterLevel <= beforeLevel) |
| 107 | { return { changed: true, levelUp: null }; } |
| 108 | return { |
| 109 | changed: true, |
| 110 | levelUp: skillLevelUpPayload(skillId, afterLevel, beforeLevel) |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | export function publicSkillsView (skillsComponent) |
| 115 | { |
| 116 | const xpMap = skillsComponent?.xp ?? null; |
| 117 | if (!xpMap) |
| 118 | { return []; } |
| 119 | return ALL_SKILL_IDS.map((skillId) => |
| 120 | { |
| 121 | const def = SKILL_DEFINITIONS[skillId]; |
| 122 | const progress = xpProgress(xpMap[skillId] ?? 0); |
| 123 | return { |
| 124 | skillId, |
| 125 | displayName: def?.displayName ?? skillId, |
| 126 | icon: `/assets/skills/${skillId}.svg`, |
| 127 | level: progress.level, |
| 128 | xp: progress.xpInLevel, |
| 129 | xpToNext: progress.xpToNext |
| 130 | }; |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | export function publicSkillsPayload (skillsComponent) |
| 135 | { |
| 136 | return { |
| 137 | combatLevel: combatLevelFromSkills(skillsComponent), |
| 138 | skills: publicSkillsView(skillsComponent) |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | export function storedSkillXp (skillsComponent) |
| 143 | { |
| 144 | return normalizeSkillXp(skillsComponent?.xp); |
| 145 | } |