| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { SkillId, playerMaxHealth, playerMaxStamina } from "../../../shared/definitions/skills.js"; |
| 3 | import { combatDamageXp, spellCastXpFromDifficulty } from "../../../shared/skills/progression.js"; |
| 4 | import { applySkillXp, publicSkillsView, storedSkillXp } from "../../../shared/skills/state.js"; |
| 5 | |
| 6 | export function syncPlayerHealth (ecs, handle) |
| 7 | { |
| 8 | const character = ecs.get(handle, Component.character); |
| 9 | if (!character) |
| 10 | { return; } |
| 11 | const skills = ecs.get(handle, Component.skills); |
| 12 | const health = ecs.get(handle, Component.health); |
| 13 | if (!skills || !health) |
| 14 | { return; } |
| 15 | const max = playerMaxHealth(skills); |
| 16 | health.max = max; |
| 17 | health.current = Math.min(health.current | 0, max); |
| 18 | } |
| 19 | |
| 20 | export function syncPlayerStamina (ecs, handle) |
| 21 | { |
| 22 | const character = ecs.get(handle, Component.character); |
| 23 | if (!character) |
| 24 | { return; } |
| 25 | const skills = ecs.get(handle, Component.skills); |
| 26 | const stamina = ecs.get(handle, Component.stamina); |
| 27 | if (!skills || !stamina) |
| 28 | { return; } |
| 29 | const max = playerMaxStamina(skills); |
| 30 | stamina.max = max; |
| 31 | stamina.current = Math.min(Number(stamina.current) || 0, max); |
| 32 | } |
| 33 | |
| 34 | export function awardCombatHitXp (ecs, attackerHandle, profile, damage) |
| 35 | { |
| 36 | const amount = combatDamageXp(damage); |
| 37 | if (!amount) |
| 38 | { return null; } |
| 39 | const skills = ecs.get(attackerHandle, Component.skills); |
| 40 | if (!skills?.xp) |
| 41 | { return null; } |
| 42 | const levelUps = []; |
| 43 | const hit = applySkillXp(skills, profile.hitSkillId, amount); |
| 44 | if (hit.levelUp) |
| 45 | { levelUps.push(hit.levelUp); } |
| 46 | const damageSkill = applySkillXp(skills, profile.damageSkillId, amount); |
| 47 | if (damageSkill.levelUp) |
| 48 | { levelUps.push(damageSkill.levelUp); } |
| 49 | if (!hit.changed && !damageSkill.changed) |
| 50 | { return null; } |
| 51 | return { |
| 52 | view: publicSkillsView(skills), |
| 53 | stored: storedSkillXp(skills), |
| 54 | levelUps |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | /** Magic XP on a successful cast; damage XP uses {@link awardCombatHitXp} when the spell lands. */ |
| 59 | export function awardSpellCastXp (ecs, casterHandle, spellDifficulty) |
| 60 | { |
| 61 | const amount = spellCastXpFromDifficulty(spellDifficulty); |
| 62 | const skills = ecs.get(casterHandle, Component.skills); |
| 63 | if (!skills?.xp) |
| 64 | { return null; } |
| 65 | const result = applySkillXp(skills, SkillId.magic, amount); |
| 66 | if (!result.changed) |
| 67 | { return null; } |
| 68 | const character = ecs.get(casterHandle, Component.character); |
| 69 | return { |
| 70 | characterId: character?.characterId ?? null, |
| 71 | view: publicSkillsView(skills), |
| 72 | stored: storedSkillXp(skills), |
| 73 | levelUps: result.levelUp ? [result.levelUp] : [] |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | export function awardDamageHealthXp (ecs, targetHandle, damage) |
| 78 | { |
| 79 | const amount = Math.max(0, Math.floor(Number(damage) || 0)); |
| 80 | if (!amount) |
| 81 | { return null; } |
| 82 | const character = ecs.get(targetHandle, Component.character); |
| 83 | if (!character) |
| 84 | { return null; } |
| 85 | const skills = ecs.get(targetHandle, Component.skills); |
| 86 | if (!skills?.xp) |
| 87 | { return null; } |
| 88 | const result = applySkillXp(skills, SkillId.health, amount); |
| 89 | if (!result.changed) |
| 90 | { return null; } |
| 91 | syncPlayerHealth(ecs, targetHandle); |
| 92 | return { |
| 93 | characterId: character.characterId, |
| 94 | view: publicSkillsView(skills), |
| 95 | stored: storedSkillXp(skills), |
| 96 | levelUps: result.levelUp ? [result.levelUp] : [] |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | /** XP for avoiding damage — Defence on block, Agility on dodge (players only). */ |
| 101 | export function awardCombatAvoidanceXp (ecs, targetHandle, skillId, avoidedDamage) |
| 102 | { |
| 103 | const amount = combatDamageXp(avoidedDamage); |
| 104 | if (!amount) |
| 105 | { return null; } |
| 106 | const character = ecs.get(targetHandle, Component.character); |
| 107 | if (!character) |
| 108 | { return null; } |
| 109 | const skills = ecs.get(targetHandle, Component.skills); |
| 110 | if (!skills?.xp) |
| 111 | { return null; } |
| 112 | const result = applySkillXp(skills, skillId, amount); |
| 113 | if (!result.changed) |
| 114 | { return null; } |
| 115 | return { |
| 116 | characterId: character.characterId, |
| 117 | view: publicSkillsView(skills), |
| 118 | stored: storedSkillXp(skills), |
| 119 | levelUps: result.levelUp ? [result.levelUp] : [] |
| 120 | }; |
| 121 | } |