BlueGit

timfa questward

Files

questward / server / src / skills / service.js

service.js

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