BlueGit

timfa questward

Files

questward / client / src / play / skills.js

skills.js

skills.js 5.9 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 getSkillLevels ()
178 {
179 const out = {};
180 for (const skill of current)
181 { out[skill.skillId] = skill.level | 0 || 1; }
182 return out;
183 },
184 hide ()
185 {
186 panel.hidden = true;
187 clearAllGainTimers();
188 recentGains.clear();
189 if (gainHud)
190 {
191 gainHud.hidden = true;
192 gainHud.replaceChildren();
193 }
194 }
195 };
196}
197
198function xpPercent (skill)
199{
200 return skill.xpToNext > 0 ? Math.min(100, (skill.xp / skill.xpToNext) * 100) : 100;
201}
202
203function renderGainRow (skill)
204{
205 const row = document.createElement("div");
206 row.className = "xp-gain-row";
207
208 const icon = document.createElement("img");
209 icon.className = "xp-gain-icon";
210 icon.src = skill.icon;
211 icon.alt = skill.displayName ?? "";
212 icon.loading = "lazy";
213 icon.title = `${skill.displayName} — Level ${skill.level}`;
214
215 const track = document.createElement("div");
216 track.className = "xp-gain-track";
217 track.title = `${skill.xp} / ${skill.xpToNext} XP to next level`;
218 const fill = document.createElement("div");
219 fill.className = "xp-gain-fill";
220 fill.style.width = `${xpPercent(skill)}%`;
221 track.append(fill);
222
223 row.append(icon, track);
224 return row;
225}
226
227function renderSkillRow (skill, bonus)
228{
229 const row = document.createElement("div");
230 row.className = "skill-row";
231
232 const icon = document.createElement("img");
233 icon.className = "skill-icon";
234 icon.src = skill.icon;
235 icon.alt = "";
236 icon.loading = "lazy";
237
238 const body = document.createElement("div");
239 body.className = "skill-body";
240
241 const title = document.createElement("div");
242 title.className = "skill-title";
243 const bonusText = bonus > 0 ? ` (+${bonus})` : "";
244 title.textContent = `${skill.displayName} — Level ${skill.level}${bonusText}`;
245
246 const track = document.createElement("div");
247 track.className = "skill-xp-track";
248 const fill = document.createElement("div");
249 fill.className = "skill-xp-fill";
250 fill.style.width = `${xpPercent(skill)}%`;
251 track.title = `${skill.xp} / ${skill.xpToNext} XP to next level`;
252 track.append(fill);
253
254 body.append(title, track);
255 row.append(icon, body);
256 return row;
257}