BlueGit

timfa questward

Files

questward / client / src / play / spells.js

spells.js

spells.js 11.3 KiB Download Raw
1/**
2 * Play spells panel: known spells, favourites bar, arm / cast intents.
3 */
4import { spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "/shared/definitions/spells.js";
5import { SkillId } from "/shared/definitions/skills.js";
6import { spellMaxHitDisplay, isMagicStaffEquipped, firstFavoriteAttackSpellId } from "/shared/combat/maxHit.js";
7
8export function initSpells ({
9 socket,
10 MessageType,
11 getSkillLevel,
12 getSkillLevels = null,
13 getEquipmentSlots = null,
14 getSettings = null
15} = {})
16{
17 const panel = document.getElementById("spells-panel");
18 const list = document.getElementById("spells-list");
19 const bar = document.getElementById("spell-favorites-bar");
20 const autoCastToggle = document.getElementById("spell-autocast-toggle");
21 let spells = [];
22 /** Server favourite order (spell ids). */
23 let favoriteOrder = [];
24 let autoCast = false;
25 let armedSpellId = null;
26 let dragFromSpellId = null;
27 let suppressNextClick = false;
28
29 document.getElementById("spells-toggle")?.addEventListener("click", () =>
30 {
31 panel.hidden = !panel.hidden;
32 if (!panel.hidden)
33 { refreshCastGates(); }
34 });
35
36 autoCastToggle?.addEventListener("change", () =>
37 {
38 socket.send({
39 type: MessageType.setSpellAutoCast,
40 enabled: autoCastToggle.checked
41 });
42 });
43
44 /** Re-evaluate Magic gates from the latest client skill levels (e.g. after level-up). */
45 function refreshCastGates ()
46 {
47 if (!spells.length)
48 { return; }
49 const magicLevel = Math.max(1, getSkillLevel?.(SkillId.magic) | 0 || 1);
50 let changed = false;
51 for (const spell of spells)
52 {
53 const need = spellMinMagicLevel(spellDefinition(spell.spellId));
54 const ok = magicLevel >= need;
55 if (spell.canCast !== ok || spell.canFavorite !== ok)
56 {
57 spell.canCast = ok;
58 spell.canFavorite = ok;
59 changed = true;
60 }
61 }
62 if (changed || !panel.hidden)
63 { renderList(); }
64 /** Favourites sit outside the panel — always sync so they show after login. */
65 renderBar();
66 }
67
68 function refreshMaxHitDisplay ()
69 {
70 if (!panel.hidden)
71 { renderList(); }
72 renderBar();
73 }
74
75 /**
76 * Favourite attack spell used for staff LMB / retaliate, or null if unequipped / unavailable.
77 */
78 function getStaffAttackSpellId ()
79 {
80 if (!isMagicStaffEquipped(getEquipmentSlots?.() ?? {}))
81 { return null; }
82 const spellId = firstFavoriteAttackSpellId({ favorites: favoriteOrder });
83 if (!spellId)
84 { return null; }
85 const spell = spells.find((entry) => entry.spellId === spellId);
86 if (!spell?.canCast || spell.target !== "entity")
87 { return null; }
88 return spellId;
89 }
90
91 function disarmLocal ()
92 {
93 if (!armedSpellId)
94 { return; }
95 armedSpellId = null;
96 renderList();
97 renderBar();
98 }
99
100 function syncFavoriteOrder (payload)
101 {
102 if (Array.isArray(payload?.favorites))
103 {
104 favoriteOrder = payload.favorites.filter((id) => typeof id === "string" || typeof id === "number");
105 return;
106 }
107 favoriteOrder = spells.filter((entry) => entry.favorite).map((entry) => entry.spellId);
108 }
109
110 function render (payload)
111 {
112 spells = payload?.spells ?? [];
113 autoCast = !!payload?.autoCast;
114 if (payload?.armedSpellId !== undefined)
115 { armedSpellId = payload.armedSpellId; }
116 if (autoCastToggle)
117 { autoCastToggle.checked = autoCast; }
118 syncFavoriteOrder(payload);
119 refreshCastGates();
120 }
121
122 function renderList ()
123 {
124 list.replaceChildren();
125 for (const spell of spells)
126 {
127 list.append(renderSpellRow(spell));
128 }
129 }
130
131 function renderSpellRow (spell)
132 {
133 const row = document.createElement("div");
134 row.className = "spell-row";
135 if (spell.spellId === armedSpellId)
136 { row.classList.add("armed"); }
137
138 const icon = document.createElement("img");
139 icon.className = "spell-icon";
140 icon.src = spell.icon;
141 icon.alt = "";
142 icon.loading = "lazy";
143
144 const body = document.createElement("div");
145 body.className = "spell-body";
146
147 const title = document.createElement("div");
148 title.className = "spell-title-row";
149 const name = document.createElement("span");
150 name.className = "spell-title";
151 name.textContent = spell.displayName;
152 const meta = document.createElement("span");
153 meta.className = "spell-meta";
154 const level = spell.tier | 0 || 1;
155 const power = spell.mindPower != null
156 ? spell.mindPower | 0
157 : spellMindPowerFromTier(level);
158 meta.textContent = `Lv. ${level} Pwr: ${power}`;
159 title.append(name, meta);
160
161 const desc = document.createElement("div");
162 desc.className = "spell-desc hint";
163 desc.textContent = spell.description ?? "";
164
165 const maxHit = spellMaxHitDisplay({
166 spellId: spell.spellId,
167 skillLevels: getSkillLevels?.() ?? {},
168 equipmentSlots: getEquipmentSlots?.() ?? {},
169 settings: getSettings?.() ?? null
170 });
171 const maxHitEl = maxHit != null
172 ? (() =>
173 {
174 const line = document.createElement("div");
175 line.className = "spell-max-hit hint";
176 line.textContent = `Max hit: ${maxHit}`;
177 return line;
178 })()
179 : null;
180
181 const actions = document.createElement("div");
182 actions.className = "spell-actions";
183
184 const favorite = document.createElement("button");
185 favorite.type = "button";
186 favorite.className = "spell-favorite-btn";
187 favorite.textContent = spell.favorite ? "★" : "☆";
188 favorite.title = spell.canFavorite
189 ? (spell.favorite ? "Remove from favourites" : "Add to favourites")
190 : "Magic level too low to favourite";
191 favorite.disabled = !spell.canFavorite;
192 favorite.addEventListener("click", () =>
193 {
194 socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId });
195 });
196
197 const arm = document.createElement("button");
198 arm.type = "button";
199 arm.className = "spell-arm-btn";
200 arm.textContent = spell.spellId === armedSpellId ? "Armed" : "Arm";
201 arm.disabled = !spell.canCast;
202 arm.addEventListener("click", () =>
203 {
204 if (spell.spellId === armedSpellId)
205 { cancelTargeting(); }
206 else
207 { socket.send({ type: MessageType.armSpell, spellId: spell.spellId }); }
208 });
209
210 actions.append(favorite, arm);
211 body.append(title, desc);
212 if (maxHitEl)
213 { body.append(maxHitEl); }
214 body.append(actions);
215 row.append(icon, body);
216 return row;
217 }
218
219 function orderedFavoriteSpells ()
220 {
221 const byId = new Map(spells.map((entry) => [entry.spellId, entry]));
222 const ordered = [];
223 for (const spellId of favoriteOrder)
224 {
225 const spell = byId.get(spellId);
226 if (spell?.favorite)
227 { ordered.push(spell); }
228 }
229 for (const spell of spells)
230 {
231 if (spell.favorite && !ordered.some((entry) => entry.spellId === spell.spellId))
232 { ordered.push(spell); }
233 }
234 return ordered;
235 }
236
237 function staffAttackFavoriteId ()
238 {
239 if (!isMagicStaffEquipped(getEquipmentSlots?.() ?? {}))
240 { return null; }
241 return firstFavoriteAttackSpellId({ favorites: favoriteOrder });
242 }
243
244 function swapFavorites (fromSpellId, toSpellId)
245 {
246 if (fromSpellId == null || toSpellId == null || fromSpellId === toSpellId)
247 { return; }
248 const order = orderedFavoriteSpells().map((entry) => entry.spellId);
249 const fromIndex = order.indexOf(fromSpellId);
250 const toIndex = order.indexOf(toSpellId);
251 if (fromIndex < 0 || toIndex < 0)
252 { return; }
253 const next = [...order];
254 const tmp = next[fromIndex];
255 next[fromIndex] = next[toIndex];
256 next[toIndex] = tmp;
257 favoriteOrder = next;
258 renderBar();
259 socket.send({
260 type: MessageType.reorderSpellFavorites,
261 favoriteSpellIds: next
262 });
263 }
264
265 function renderBar ()
266 {
267 const favorites = orderedFavoriteSpells();
268 bar.replaceChildren();
269 bar.hidden = favorites.length === 0;
270 const attackId = staffAttackFavoriteId();
271 for (const spell of favorites)
272 {
273 const btn = document.createElement("button");
274 btn.type = "button";
275 btn.className = "spell-favorite-slot";
276 btn.draggable = true;
277 btn.dataset.spellId = String(spell.spellId);
278 if (spell.spellId === armedSpellId)
279 { btn.classList.add("armed"); }
280 if (spell.spellId === attackId)
281 { btn.classList.add("staff-attack"); }
282 btn.title = spell.spellId === attackId
283 ? `${spell.displayName} (staff auto-attack)`
284 : spell.displayName;
285 btn.disabled = !spell.canCast;
286
287 const icon = document.createElement("img");
288 icon.src = spell.icon;
289 icon.alt = "";
290 icon.draggable = false;
291 btn.append(icon);
292
293 btn.addEventListener("click", () =>
294 {
295 if (suppressNextClick)
296 {
297 suppressNextClick = false;
298 return;
299 }
300 if (spell.spellId === armedSpellId)
301 { return; }
302 socket.send({ type: MessageType.armSpell, spellId: spell.spellId });
303 });
304 btn.addEventListener("contextmenu", (event) =>
305 {
306 event.preventDefault();
307 socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId });
308 });
309 btn.addEventListener("dragstart", (event) =>
310 {
311 dragFromSpellId = spell.spellId;
312 btn.classList.add("dragging");
313 event.dataTransfer?.setData("text/plain", String(spell.spellId));
314 if (event.dataTransfer)
315 { event.dataTransfer.effectAllowed = "move"; }
316 });
317 btn.addEventListener("dragend", () =>
318 {
319 btn.classList.remove("dragging");
320 dragFromSpellId = null;
321 for (const el of bar.querySelectorAll(".spell-favorite-slot.drag-over"))
322 { el.classList.remove("drag-over"); }
323 });
324 btn.addEventListener("dragover", (event) =>
325 {
326 event.preventDefault();
327 if (event.dataTransfer)
328 { event.dataTransfer.dropEffect = "move"; }
329 btn.classList.add("drag-over");
330 });
331 btn.addEventListener("dragleave", () =>
332 {
333 btn.classList.remove("drag-over");
334 });
335 btn.addEventListener("drop", (event) =>
336 {
337 event.preventDefault();
338 btn.classList.remove("drag-over");
339 const rawFrom = dragFromSpellId ?? event.dataTransfer?.getData("text/plain");
340 if (rawFrom == null || rawFrom === "")
341 { return; }
342 suppressNextClick = true;
343 const fromId = favoriteOrder.find((id) => String(id) === String(rawFrom)) ?? rawFrom;
344 swapFavorites(fromId, spell.spellId);
345 });
346 bar.append(btn);
347 }
348 }
349
350 function getArmedSpell ()
351 {
352 if (!armedSpellId)
353 { return null; }
354 return spells.find((entry) => entry.spellId === armedSpellId) ?? null;
355 }
356
357 function tryCastAtTile (tileX, tileY)
358 {
359 const armed = getArmedSpell();
360 if (!armed || armed.target !== "tile" || !armed.canCast)
361 { return false; }
362 socket.send({
363 type: MessageType.castSpellTile,
364 spellId: armed.spellId,
365 tileX,
366 tileY
367 });
368 return true;
369 }
370
371 function tryCastAtEntity (targetEntityId)
372 {
373 const armed = getArmedSpell();
374 if (!armed || armed.target !== "entity" || !armed.canCast)
375 { return false; }
376 socket.send({
377 type: MessageType.castSpellEntity,
378 spellId: armed.spellId,
379 targetEntityId
380 });
381 return true;
382 }
383
384 function cancelTargeting ()
385 {
386 if (!armedSpellId)
387 { return; }
388 socket.send({ type: MessageType.cancelArmSpell });
389 disarmLocal();
390 }
391
392 return {
393 render,
394 refreshCastGates,
395 getArmedSpell,
396 tryCastAtTile,
397 tryCastAtEntity,
398 cancelTargeting,
399 isBlocking ()
400 {
401 return panel != null && !panel.hidden;
402 },
403 isTargeting ()
404 {
405 return armedSpellId != null;
406 },
407 handleSpellArmed (message)
408 {
409 armedSpellId = message.spellId ?? null;
410 renderList();
411 renderBar();
412 },
413 knowsSpell (spellId)
414 {
415 return spells.some((spell) => spell.spellId === spellId);
416 },
417 getKnownSpellIds ()
418 {
419 return spells.map((spell) => spell.spellId);
420 },
421 refreshMaxHitDisplay,
422 getStaffAttackSpellId,
423 handleSpellDisarmed ()
424 {
425 disarmLocal();
426 },
427 hide ()
428 {
429 panel.hidden = true;
430 spells = [];
431 favoriteOrder = [];
432 autoCast = false;
433 armedSpellId = null;
434 if (autoCastToggle)
435 { autoCastToggle.checked = false; }
436 list.replaceChildren();
437 bar.replaceChildren();
438 bar.hidden = true;
439 }
440 };
441}