BlueGit

timfa questward

Files

questward / server / src / skills / service.js

service.js

service.js 4.0 KiB Download Raw
1import { Component } from "../ecs/components.js";
2import { SkillId, playerMaxHealth, playerMaxStamina } from "../../../shared/definitions/skills.js";
3import { combatDamageXp, spellCastXpFromDifficulty } from "../../../shared/skills/progression.js";
4import { applySkillXp, publicSkillsView, storedSkillXp } from "../../../shared/skills/state.js";
5
6export 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
20export 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
34export 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. */
59export 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
77export 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 health = ecs.get(targetHandle, Component.health);
83 // Lethal hits grant no Health XP — only damage you survive counts.
84 if (!health || (health.current | 0) <= 0)
85 { return null; }
86 const character = ecs.get(targetHandle, Component.character);
87 if (!character)
88 { return null; }
89 const skills = ecs.get(targetHandle, Component.skills);
90 if (!skills?.xp)
91 { return null; }
92 const result = applySkillXp(skills, SkillId.health, amount);
93 if (!result.changed)
94 { return null; }
95 syncPlayerHealth(ecs, targetHandle);
96 return {
97 characterId: character.characterId,
98 view: publicSkillsView(skills),
99 stored: storedSkillXp(skills),
100 levelUps: result.levelUp ? [result.levelUp] : []
101 };
102}
103
104/** XP for avoiding damage — Defence on block, Agility on dodge (players only). */
105export function awardCombatAvoidanceXp (ecs, targetHandle, skillId, avoidedDamage)
106{
107 const amount = combatDamageXp(avoidedDamage);
108 if (!amount)
109 { return null; }
110 const character = ecs.get(targetHandle, Component.character);
111 if (!character)
112 { return null; }
113 const skills = ecs.get(targetHandle, Component.skills);
114 if (!skills?.xp)
115 { return null; }
116 const result = applySkillXp(skills, skillId, amount);
117 if (!result.changed)
118 { return null; }
119 return {
120 characterId: character.characterId,
121 view: publicSkillsView(skills),
122 stored: storedSkillXp(skills),
123 levelUps: result.levelUp ? [result.levelUp] : []
124 };
125}