| 1 | import { SkillId, SKILL_DEFINITIONS, ALL_SKILL_IDS, DEFAULT_PLAYER_SKILL_LEVELS } from "../definitions/skills.js"; |
| 2 | import { maxTotalSkillXp, totalXpForLevel, xpProgress, MAX_SKILL_LEVEL } from "./progression.js"; |
| 3 | |
| 4 | /** Health starts at level 10; Stamina at 5; other skills at level 1. */ |
| 5 | export const STARTING_HEALTH_XP = totalXpForLevel(DEFAULT_PLAYER_SKILL_LEVELS[SkillId.health] ?? 10); |
| 6 | |
| 7 | export function emptySkillXp () |
| 8 | { |
| 9 | const xp = {}; |
| 10 | for (const skillId of ALL_SKILL_IDS) |
| 11 | { xp[skillId] = 0; } |
| 12 | return xp; |
| 13 | } |
| 14 | |
| 15 | /** Raise any skill below its default starting level up to that floor. Returns whether XP changed. */ |
| 16 | export function enforceMinimumSkillLevels (skillsComponent) |
| 17 | { |
| 18 | if (!skillsComponent?.xp) |
| 19 | { return false; } |
| 20 | let raised = false; |
| 21 | for (const skillId of ALL_SKILL_IDS) |
| 22 | { |
| 23 | const minLevel = DEFAULT_PLAYER_SKILL_LEVELS[skillId] ?? 1; |
| 24 | const minXp = totalXpForLevel(minLevel); |
| 25 | const current = Math.max(0, Math.floor(Number(skillsComponent.xp[skillId]) || 0)); |
| 26 | if (current < minXp) |
| 27 | { |
| 28 | skillsComponent.xp[skillId] = minXp; |
| 29 | raised = true; |
| 30 | } |
| 31 | } |
| 32 | return raised; |
| 33 | } |
| 34 | |
| 35 | export function normalizeSkillXp (raw) |
| 36 | { |
| 37 | const xp = emptySkillXp(); |
| 38 | if (raw && typeof raw === "object") |
| 39 | { |
| 40 | for (const skillId of ALL_SKILL_IDS) |
| 41 | { |
| 42 | const value = Math.max(0, Math.floor(Number(raw[skillId]) || 0)); |
| 43 | xp[skillId] = Number.isSafeInteger(value) ? value : 0; |
| 44 | } |
| 45 | } |
| 46 | enforceMinimumSkillLevels({ xp }); |
| 47 | return xp; |
| 48 | } |
| 49 | |
| 50 | export function normalizePlayerSkills (stored) |
| 51 | { |
| 52 | return { xp: normalizeSkillXp(stored?.xp ?? stored) }; |
| 53 | } |
| 54 | |
| 55 | /** Starting skills for a new character (Health 10, Stamina 5, others 1). */ |
| 56 | export function defaultPlayerSkills () |
| 57 | { |
| 58 | return normalizePlayerSkills({ |
| 59 | [SkillId.health]: STARTING_HEALTH_XP, |
| 60 | [SkillId.stamina]: totalXpForLevel(DEFAULT_PLAYER_SKILL_LEVELS[SkillId.stamina] ?? 5) |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | export function addSkillXp (skillsComponent, skillId, amount) |
| 65 | { |
| 66 | if (!skillsComponent?.xp) |
| 67 | { return false; } |
| 68 | const add = Math.max(0, Math.floor(Number(amount) || 0)); |
| 69 | if (!add) |
| 70 | { return false; } |
| 71 | const current = skillsComponent.xp[skillId] ?? 0; |
| 72 | if (xpProgress(current).level >= MAX_SKILL_LEVEL) |
| 73 | { return false; } |
| 74 | skillsComponent.xp[skillId] = Math.min(maxTotalSkillXp(), current + add); |
| 75 | return skillsComponent.xp[skillId] !== current; |
| 76 | } |
| 77 | |
| 78 | export function publicSkillsView (skillsComponent) |
| 79 | { |
| 80 | const xpMap = skillsComponent?.xp ?? null; |
| 81 | if (!xpMap) |
| 82 | { return []; } |
| 83 | return ALL_SKILL_IDS.map((skillId) => |
| 84 | { |
| 85 | const def = SKILL_DEFINITIONS[skillId]; |
| 86 | const progress = xpProgress(xpMap[skillId] ?? 0); |
| 87 | return { |
| 88 | skillId, |
| 89 | displayName: def?.displayName ?? skillId, |
| 90 | icon: `/assets/skills/${skillId}.svg`, |
| 91 | level: progress.level, |
| 92 | xp: progress.xpInLevel, |
| 93 | xpToNext: progress.xpToNext |
| 94 | }; |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | export function storedSkillXp (skillsComponent) |
| 99 | { |
| 100 | return normalizeSkillXp(skillsComponent?.xp); |
| 101 | } |