import { Component } from "../ecs/components.js"; import { SkillId, playerMaxHealth } from "../../../shared/definitions/skills.js"; import { combatHitXpPerSkill } from "../../../shared/skills/progression.js"; import { addSkillXp, 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 awardCombatHitXp (ecs, attackerHandle, profile) { const skills = ecs.get(attackerHandle, Component.skills); if (!skills?.xp) { return null; } const perSkill = combatHitXpPerSkill(); const hit = addSkillXp(skills, profile.hitSkillId, perSkill); const damage = addSkillXp(skills, profile.damageSkillId, perSkill); if (!hit && !damage) { return null; } return { view: publicSkillsView(skills), stored: storedSkillXp(skills) }; } 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; } if (!addSkillXp(skills, SkillId.health, amount)) { return null; } syncPlayerHealth(ecs, targetHandle); return { characterId: character.characterId, view: publicSkillsView(skills), stored: storedSkillXp(skills) }; }