BlueGit

timfa questward

Files

questward / client / src / play / skills.js

skills.js

skills.js 5.0 KiB Download Raw
1import { equipmentBonusesFromSlots } from "/shared/equipment/stats.js";
2
3const XP_GAIN_VISIBLE_MS = 5000;
4
5export function initSkills ({ getEquipmentSlots, getSettings } = {})
6{
7 const panel = document.getElementById("skills-panel");
8 const list = document.getElementById("skills-list");
9 const gainHud = document.getElementById("xp-gain-hud");
10 let current = [];
11 /** @type {Map<string, { skill: object, gainedAt: number }>} */
12 const recentGains = new Map();
13 let hideTimer = null;
14
15 document.getElementById("skills-toggle").addEventListener("click", () =>
16 {
17 panel.hidden = !panel.hidden;
18 if (!panel.hidden)
19 { renderPanel(current); }
20 });
21
22 function equipmentBonuses ()
23 {
24 return equipmentBonusesFromSlots(
25 getEquipmentSlots?.() ?? {},
26 getSettings?.() ?? null
27 );
28 }
29
30 function clearHideTimer ()
31 {
32 if (hideTimer != null)
33 {
34 window.clearTimeout(hideTimer);
35 hideTimer = null;
36 }
37 }
38
39 function scheduleHide ()
40 {
41 clearHideTimer();
42 hideTimer = window.setTimeout(() =>
43 {
44 hideTimer = null;
45 recentGains.clear();
46 renderGainHud();
47 }, XP_GAIN_VISIBLE_MS);
48 }
49
50 function skillGainedXp (prev, next)
51 {
52 if (!prev)
53 { return false; }
54 if ((next.level | 0) > (prev.level | 0))
55 { return true; }
56 if ((next.level | 0) === (prev.level | 0) && (next.xp | 0) > (prev.xp | 0))
57 { return true; }
58 return false;
59 }
60
61 function noteXpGains (previous, nextSkills)
62 {
63 if (!gainHud)
64 { return; }
65 const prevById = new Map((previous ?? []).map((skill) => [skill.skillId, skill]));
66 const now = performance.now();
67 let anyGain = false;
68 for (const skill of nextSkills ?? [])
69 {
70 const prev = prevById.get(skill.skillId);
71 if (skillGainedXp(prev, skill))
72 {
73 recentGains.set(skill.skillId, { skill: { ...skill }, gainedAt: now });
74 anyGain = true;
75 }
76 else if (recentGains.has(skill.skillId))
77 { recentGains.get(skill.skillId).skill = { ...skill }; }
78 }
79 if (anyGain)
80 { scheduleHide(); }
81 if (anyGain || recentGains.size)
82 { renderGainHud(); }
83 }
84
85 function renderGainHud ()
86 {
87 if (!gainHud)
88 { return; }
89 if (!recentGains.size)
90 {
91 gainHud.hidden = true;
92 gainHud.replaceChildren();
93 return;
94 }
95 const rows = [...recentGains.values()]
96 .sort((a, b) => b.gainedAt - a.gainedAt);
97 gainHud.replaceChildren();
98 for (const entry of rows)
99 { gainHud.append(renderGainRow(entry.skill)); }
100 gainHud.hidden = false;
101 }
102
103 function renderPanel (skills)
104 {
105 const bonuses = equipmentBonuses();
106 list.replaceChildren();
107 for (const skill of skills)
108 {
109 list.append(renderSkillRow(skill, bonuses[skill.skillId] | 0));
110 }
111 }
112
113 function render (skills)
114 {
115 const next = skills ?? [];
116 const previous = current;
117 noteXpGains(previous, next);
118 current = next;
119 if (!panel.hidden)
120 { renderPanel(current); }
121 }
122
123 return {
124 render,
125 refresh ()
126 {
127 if (current.length && !panel.hidden)
128 { renderPanel(current); }
129 if (recentGains.size)
130 {
131 for (const skill of current)
132 {
133 if (recentGains.has(skill.skillId))
134 { recentGains.get(skill.skillId).skill = { ...skill }; }
135 }
136 renderGainHud();
137 }
138 },
139 skillLevel (skillId)
140 {
141 return current.find((entry) => entry.skillId === skillId)?.level ?? 1;
142 },
143 hide ()
144 {
145 panel.hidden = true;
146 clearHideTimer();
147 recentGains.clear();
148 if (gainHud)
149 {
150 gainHud.hidden = true;
151 gainHud.replaceChildren();
152 }
153 }
154 };
155}
156
157function xpPercent (skill)
158{
159 return skill.xpToNext > 0 ? Math.min(100, (skill.xp / skill.xpToNext) * 100) : 100;
160}
161
162function renderGainRow (skill)
163{
164 const row = document.createElement("div");
165 row.className = "xp-gain-row";
166
167 const icon = document.createElement("img");
168 icon.className = "xp-gain-icon";
169 icon.src = skill.icon;
170 icon.alt = skill.displayName ?? "";
171 icon.loading = "lazy";
172 icon.title = `${skill.displayName} — Level ${skill.level}`;
173
174 const track = document.createElement("div");
175 track.className = "xp-gain-track";
176 track.title = `${skill.xp} / ${skill.xpToNext} XP to next level`;
177 const fill = document.createElement("div");
178 fill.className = "xp-gain-fill";
179 fill.style.width = `${xpPercent(skill)}%`;
180 track.append(fill);
181
182 row.append(icon, track);
183 return row;
184}
185
186function renderSkillRow (skill, bonus)
187{
188 const row = document.createElement("div");
189 row.className = "skill-row";
190
191 const icon = document.createElement("img");
192 icon.className = "skill-icon";
193 icon.src = skill.icon;
194 icon.alt = "";
195 icon.loading = "lazy";
196
197 const body = document.createElement("div");
198 body.className = "skill-body";
199
200 const title = document.createElement("div");
201 title.className = "skill-title";
202 const bonusText = bonus > 0 ? ` (+${bonus})` : "";
203 title.textContent = `${skill.displayName} — Level ${skill.level}${bonusText}`;
204
205 const track = document.createElement("div");
206 track.className = "skill-xp-track";
207 const fill = document.createElement("div");
208 fill.className = "skill-xp-fill";
209 fill.style.width = `${xpPercent(skill)}%`;
210 track.title = `${skill.xp} / ${skill.xpToNext} XP to next level`;
211 track.append(fill);
212
213 body.append(title, track);
214 row.append(icon, body);
215 return row;
216}