BlueGit

timfa questward

Files

questward / client / src / play / skills.js

skills.js

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