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