import { Component } from "../ecs/components.js"; import { SkillId, playerMaxHealth, playerMaxStamina } from "../../../shared/definitions/skills.js"; import { combatHitXpPerSkill } from "../../../shared/skills/progression.js"; import { applySkillXp, publicSkillsView, storedSkillXp } from "../../../shared/skills/state.js"; export function syncPlayerHealth (ecs, handle) { const character = ecs.get(handle, Component.character); if (!character) { return; } const skills = ecs.get(handle, Component.skills); const health = ecs.get(handle, Component.health); if (!skills || !health) { return; } const max = playerMaxHealth(skills); health.max = max; health.current = Math.min(health.current | 0, max); } export function syncPlayerStamina (ecs, handle) { const character = ecs.get(handle, Component.character); if (!character) { return; } const skills = ecs.get(handle, Component.skills); const stamina = ecs.get(handle, Component.stamina); if (!skills || !stamina) { return; } const max = playerMaxStamina(skills); stamina.max = max; stamina.current = Math.min(Number(stamina.current) || 0, max); } export function awardCombatHitXp (ecs, attackerHandle, profile) { const skills = ecs.get(attackerHandle, Component.skills); if (!skills?.xp) { return null; } const perSkill = combatHitXpPerSkill(); const levelUps = []; const hit = applySkillXp(skills, profile.hitSkillId, perSkill); if (hit.levelUp) { levelUps.push(hit.levelUp); } const damage = applySkillXp(skills, profile.damageSkillId, perSkill); if (damage.levelUp) { levelUps.push(damage.levelUp); } if (!hit.changed && !damage.changed) { return null; } return { view: publicSkillsView(skills), stored: storedSkillXp(skills), levelUps }; } export function awardDamageHealthXp (ecs, targetHandle, damage) { const amount = Math.max(0, Math.floor(Number(damage) || 0)); if (!amount) { return null; } const character = ecs.get(targetHandle, Component.character); if (!character) { return null; } const skills = ecs.get(targetHandle, Component.skills); if (!skills?.xp) { return null; } const result = applySkillXp(skills, SkillId.health, amount); if (!result.changed) { return null; } syncPlayerHealth(ecs, targetHandle); return { characterId: character.characterId, view: publicSkillsView(skills), stored: storedSkillXp(skills), levelUps: result.levelUp ? [result.levelUp] : [] }; }