BlueGit

timfa questward

Add favourite spell bar drag-swap and staff auto-attack outline.

Players can reorder favourites by dragging slots; with a staff equipped the first attack favourite gets a thicker cyan rim.

timfa Tim Falken · pushed by timfa

Authored 2026-09-02 · 2 months ago

Hash
06b69a7007bab87fa63ca84d1644197ed8a576b3
Parent
988ba1da
9 files changed +246 −3
client/src/play/spells.js +116 −3
@@ -3,7 +3,7 @@
3 3 */
4 4 import { spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "/shared/definitions/spells.js";
5 5 import { SkillId } from "/shared/definitions/skills.js";
6 import { spellMaxHitDisplay } from "/shared/combat/maxHit.js";
6 import { spellMaxHitDisplay, isMagicStaffEquipped, firstFavoriteAttackSpellId } from "/shared/combat/maxHit.js";
7 7
8 8 export function initSpells ({
9 9 socket,
@@ -19,8 +19,12 @@ export function initSpells ({
19 19 const bar = document.getElementById("spell-favorites-bar");
20 20 const autoCastToggle = document.getElementById("spell-autocast-toggle");
21 21 let spells = [];
22 /** Server favourite order (spell ids). */
23 let favoriteOrder = [];
22 24 let autoCast = false;
23 25 let armedSpellId = null;
26 let dragFromSpellId = null;
27 let suppressNextClick = false;
24 28
25 29 document.getElementById("spells-toggle")?.addEventListener("click", () =>
26 30 {
@@ -65,6 +69,7 @@ export function initSpells ({
65 69 {
66 70 if (!panel.hidden)
67 71 { renderList(); }
72 renderBar();
68 73 }
69 74
70 75 function disarmLocal ()
@@ -76,6 +81,16 @@ export function initSpells ({
76 81 renderBar();
77 82 }
78 83
84 function syncFavoriteOrder (payload)
85 {
86 if (Array.isArray(payload?.favorites))
87 {
88 favoriteOrder = payload.favorites.filter((id) => typeof id === "string" || typeof id === "number");
89 return;
90 }
91 favoriteOrder = spells.filter((entry) => entry.favorite).map((entry) => entry.spellId);
92 }
93
79 94 function render (payload)
80 95 {
81 96 spells = payload?.spells ?? [];
@@ -84,6 +99,7 @@ export function initSpells ({
84 99 { armedSpellId = payload.armedSpellId; }
85 100 if (autoCastToggle)
86 101 { autoCastToggle.checked = autoCast; }
102 syncFavoriteOrder(payload);
87 103 refreshCastGates();
88 104 }
89 105
@@ -184,28 +200,87 @@ export function initSpells ({
184 200 return row;
185 201 }
186 202
203 function orderedFavoriteSpells ()
204 {
205 const byId = new Map(spells.map((entry) => [entry.spellId, entry]));
206 const ordered = [];
207 for (const spellId of favoriteOrder)
208 {
209 const spell = byId.get(spellId);
210 if (spell?.favorite)
211 { ordered.push(spell); }
212 }
213 for (const spell of spells)
214 {
215 if (spell.favorite && !ordered.some((entry) => entry.spellId === spell.spellId))
216 { ordered.push(spell); }
217 }
218 return ordered;
219 }
220
221 function staffAttackFavoriteId ()
222 {
223 if (!isMagicStaffEquipped(getEquipmentSlots?.() ?? {}))
224 { return null; }
225 return firstFavoriteAttackSpellId({ favorites: favoriteOrder });
226 }
227
228 function swapFavorites (fromSpellId, toSpellId)
229 {
230 if (fromSpellId == null || toSpellId == null || fromSpellId === toSpellId)
231 { return; }
232 const order = orderedFavoriteSpells().map((entry) => entry.spellId);
233 const fromIndex = order.indexOf(fromSpellId);
234 const toIndex = order.indexOf(toSpellId);
235 if (fromIndex < 0 || toIndex < 0)
236 { return; }
237 const next = [...order];
238 const tmp = next[fromIndex];
239 next[fromIndex] = next[toIndex];
240 next[toIndex] = tmp;
241 favoriteOrder = next;
242 renderBar();
243 socket.send({
244 type: MessageType.reorderSpellFavorites,
245 favoriteSpellIds: next
246 });
247 }
248
187 249 function renderBar ()
188 250 {
189 const favorites = spells.filter((entry) => entry.favorite);
251 const favorites = orderedFavoriteSpells();
190 252 bar.replaceChildren();
191 253 bar.hidden = favorites.length === 0;
254 const attackId = staffAttackFavoriteId();
192 255 for (const spell of favorites)
193 256 {
194 257 const btn = document.createElement("button");
195 258 btn.type = "button";
196 259 btn.className = "spell-favorite-slot";
260 btn.draggable = true;
261 btn.dataset.spellId = String(spell.spellId);
197 262 if (spell.spellId === armedSpellId)
198 263 { btn.classList.add("armed"); }
199 btn.title = spell.displayName;
264 if (spell.spellId === attackId)
265 { btn.classList.add("staff-attack"); }
266 btn.title = spell.spellId === attackId
267 ? `${spell.displayName} (staff auto-attack)`
268 : spell.displayName;
200 269 btn.disabled = !spell.canCast;
201 270
202 271 const icon = document.createElement("img");
203 272 icon.src = spell.icon;
204 273 icon.alt = "";
274 icon.draggable = false;
205 275 btn.append(icon);
206 276
207 277 btn.addEventListener("click", () =>
208 278 {
279 if (suppressNextClick)
280 {
281 suppressNextClick = false;
282 return;
283 }
209 284 if (spell.spellId === armedSpellId)
210 285 { return; }
211 286 socket.send({ type: MessageType.armSpell, spellId: spell.spellId });
@@ -215,6 +290,43 @@ export function initSpells ({
215 290 event.preventDefault();
216 291 socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId });
217 292 });
293 btn.addEventListener("dragstart", (event) =>
294 {
295 dragFromSpellId = spell.spellId;
296 btn.classList.add("dragging");
297 event.dataTransfer?.setData("text/plain", String(spell.spellId));
298 if (event.dataTransfer)
299 { event.dataTransfer.effectAllowed = "move"; }
300 });
301 btn.addEventListener("dragend", () =>
302 {
303 btn.classList.remove("dragging");
304 dragFromSpellId = null;
305 for (const el of bar.querySelectorAll(".spell-favorite-slot.drag-over"))
306 { el.classList.remove("drag-over"); }
307 });
308 btn.addEventListener("dragover", (event) =>
309 {
310 event.preventDefault();
311 if (event.dataTransfer)
312 { event.dataTransfer.dropEffect = "move"; }
313 btn.classList.add("drag-over");
314 });
315 btn.addEventListener("dragleave", () =>
316 {
317 btn.classList.remove("drag-over");
318 });
319 btn.addEventListener("drop", (event) =>
320 {
321 event.preventDefault();
322 btn.classList.remove("drag-over");
323 const rawFrom = dragFromSpellId ?? event.dataTransfer?.getData("text/plain");
324 if (rawFrom == null || rawFrom === "")
325 { return; }
326 suppressNextClick = true;
327 const fromId = favoriteOrder.find((id) => String(id) === String(rawFrom)) ?? rawFrom;
328 swapFavorites(fromId, spell.spellId);
329 });
218 330 bar.append(btn);
219 331 }
220 332 }
@@ -299,6 +411,7 @@ export function initSpells ({
299 411 {
300 412 panel.hidden = true;
301 413 spells = [];
414 favoriteOrder = [];
302 415 autoCast = false;
303 416 armedSpellId = null;
304 417 if (autoCastToggle)
@@ -3,7 +3,7 @@
3 */
3 */
4import { spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "/shared/definitions/spells.js";
4import { spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "/shared/definitions/spells.js";
5import { SkillId } from "/shared/definitions/skills.js";
5import { SkillId } from "/shared/definitions/skills.js";
6import { spellMaxHitDisplay } from "/shared/combat/maxHit.js";
6import { spellMaxHitDisplay, isMagicStaffEquipped, firstFavoriteAttackSpellId } from "/shared/combat/maxHit.js";
7
7
8export function initSpells ({
8export function initSpells ({
9 socket,
9 socket,
@@ -19,8 +19,12 @@ export function initSpells ({
19 const bar = document.getElementById("spell-favorites-bar");
19 const bar = document.getElementById("spell-favorites-bar");
20 const autoCastToggle = document.getElementById("spell-autocast-toggle");
20 const autoCastToggle = document.getElementById("spell-autocast-toggle");
21 let spells = [];
21 let spells = [];
22 /** Server favourite order (spell ids). */
23 let favoriteOrder = [];
22 let autoCast = false;
24 let autoCast = false;
23 let armedSpellId = null;
25 let armedSpellId = null;
26 let dragFromSpellId = null;
27 let suppressNextClick = false;
24
28
25 document.getElementById("spells-toggle")?.addEventListener("click", () =>
29 document.getElementById("spells-toggle")?.addEventListener("click", () =>
26 {
30 {
@@ -65,6 +69,7 @@ export function initSpells ({
65 {
69 {
66 if (!panel.hidden)
70 if (!panel.hidden)
67 { renderList(); }
71 { renderList(); }
72 renderBar();
68 }
73 }
69
74
70 function disarmLocal ()
75 function disarmLocal ()
@@ -76,6 +81,16 @@ export function initSpells ({
76 renderBar();
81 renderBar();
77 }
82 }
78
83
84 function syncFavoriteOrder (payload)
85 {
86 if (Array.isArray(payload?.favorites))
87 {
88 favoriteOrder = payload.favorites.filter((id) => typeof id === "string" || typeof id === "number");
89 return;
90 }
91 favoriteOrder = spells.filter((entry) => entry.favorite).map((entry) => entry.spellId);
92 }
93
79 function render (payload)
94 function render (payload)
80 {
95 {
81 spells = payload?.spells ?? [];
96 spells = payload?.spells ?? [];
@@ -84,6 +99,7 @@ export function initSpells ({
84 { armedSpellId = payload.armedSpellId; }
99 { armedSpellId = payload.armedSpellId; }
85 if (autoCastToggle)
100 if (autoCastToggle)
86 { autoCastToggle.checked = autoCast; }
101 { autoCastToggle.checked = autoCast; }
102 syncFavoriteOrder(payload);
87 refreshCastGates();
103 refreshCastGates();
88 }
104 }
89
105
@@ -184,28 +200,87 @@ export function initSpells ({
184 return row;
200 return row;
185 }
201 }
186
202
203 function orderedFavoriteSpells ()
204 {
205 const byId = new Map(spells.map((entry) => [entry.spellId, entry]));
206 const ordered = [];
207 for (const spellId of favoriteOrder)
208 {
209 const spell = byId.get(spellId);
210 if (spell?.favorite)
211 { ordered.push(spell); }
212 }
213 for (const spell of spells)
214 {
215 if (spell.favorite && !ordered.some((entry) => entry.spellId === spell.spellId))
216 { ordered.push(spell); }
217 }
218 return ordered;
219 }
220
221 function staffAttackFavoriteId ()
222 {
223 if (!isMagicStaffEquipped(getEquipmentSlots?.() ?? {}))
224 { return null; }
225 return firstFavoriteAttackSpellId({ favorites: favoriteOrder });
226 }
227
228 function swapFavorites (fromSpellId, toSpellId)
229 {
230 if (fromSpellId == null || toSpellId == null || fromSpellId === toSpellId)
231 { return; }
232 const order = orderedFavoriteSpells().map((entry) => entry.spellId);
233 const fromIndex = order.indexOf(fromSpellId);
234 const toIndex = order.indexOf(toSpellId);
235 if (fromIndex < 0 || toIndex < 0)
236 { return; }
237 const next = [...order];
238 const tmp = next[fromIndex];
239 next[fromIndex] = next[toIndex];
240 next[toIndex] = tmp;
241 favoriteOrder = next;
242 renderBar();
243 socket.send({
244 type: MessageType.reorderSpellFavorites,
245 favoriteSpellIds: next
246 });
247 }
248
187 function renderBar ()
249 function renderBar ()
188 {
250 {
189 const favorites = spells.filter((entry) => entry.favorite);
251 const favorites = orderedFavoriteSpells();
190 bar.replaceChildren();
252 bar.replaceChildren();
191 bar.hidden = favorites.length === 0;
253 bar.hidden = favorites.length === 0;
254 const attackId = staffAttackFavoriteId();
192 for (const spell of favorites)
255 for (const spell of favorites)
193 {
256 {
194 const btn = document.createElement("button");
257 const btn = document.createElement("button");
195 btn.type = "button";
258 btn.type = "button";
196 btn.className = "spell-favorite-slot";
259 btn.className = "spell-favorite-slot";
260 btn.draggable = true;
261 btn.dataset.spellId = String(spell.spellId);
197 if (spell.spellId === armedSpellId)
262 if (spell.spellId === armedSpellId)
198 { btn.classList.add("armed"); }
263 { btn.classList.add("armed"); }
199 btn.title = spell.displayName;
264 if (spell.spellId === attackId)
265 { btn.classList.add("staff-attack"); }
266 btn.title = spell.spellId === attackId
267 ? `${spell.displayName} (staff auto-attack)`
268 : spell.displayName;
200 btn.disabled = !spell.canCast;
269 btn.disabled = !spell.canCast;
201
270
202 const icon = document.createElement("img");
271 const icon = document.createElement("img");
203 icon.src = spell.icon;
272 icon.src = spell.icon;
204 icon.alt = "";
273 icon.alt = "";
274 icon.draggable = false;
205 btn.append(icon);
275 btn.append(icon);
206
276
207 btn.addEventListener("click", () =>
277 btn.addEventListener("click", () =>
208 {
278 {
279 if (suppressNextClick)
280 {
281 suppressNextClick = false;
282 return;
283 }
209 if (spell.spellId === armedSpellId)
284 if (spell.spellId === armedSpellId)
210 { return; }
285 { return; }
211 socket.send({ type: MessageType.armSpell, spellId: spell.spellId });
286 socket.send({ type: MessageType.armSpell, spellId: spell.spellId });
@@ -215,6 +290,43 @@ export function initSpells ({
215 event.preventDefault();
290 event.preventDefault();
216 socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId });
291 socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId });
217 });
292 });
293 btn.addEventListener("dragstart", (event) =>
294 {
295 dragFromSpellId = spell.spellId;
296 btn.classList.add("dragging");
297 event.dataTransfer?.setData("text/plain", String(spell.spellId));
298 if (event.dataTransfer)
299 { event.dataTransfer.effectAllowed = "move"; }
300 });
301 btn.addEventListener("dragend", () =>
302 {
303 btn.classList.remove("dragging");
304 dragFromSpellId = null;
305 for (const el of bar.querySelectorAll(".spell-favorite-slot.drag-over"))
306 { el.classList.remove("drag-over"); }
307 });
308 btn.addEventListener("dragover", (event) =>
309 {
310 event.preventDefault();
311 if (event.dataTransfer)
312 { event.dataTransfer.dropEffect = "move"; }
313 btn.classList.add("drag-over");
314 });
315 btn.addEventListener("dragleave", () =>
316 {
317 btn.classList.remove("drag-over");
318 });
319 btn.addEventListener("drop", (event) =>
320 {
321 event.preventDefault();
322 btn.classList.remove("drag-over");
323 const rawFrom = dragFromSpellId ?? event.dataTransfer?.getData("text/plain");
324 if (rawFrom == null || rawFrom === "")
325 { return; }
326 suppressNextClick = true;
327 const fromId = favoriteOrder.find((id) => String(id) === String(rawFrom)) ?? rawFrom;
328 swapFavorites(fromId, spell.spellId);
329 });
218 bar.append(btn);
330 bar.append(btn);
219 }
331 }
220 }
332 }
@@ -299,6 +411,7 @@ export function initSpells ({
299 {
411 {
300 panel.hidden = true;
412 panel.hidden = true;
301 spells = [];
413 spells = [];
414 favoriteOrder = [];
302 autoCast = false;
415 autoCast = false;
303 armedSpellId = null;
416 armedSpellId = null;
304 if (autoCastToggle)
417 if (autoCastToggle)
client/ui/play.css +25 −0
@@ -924,6 +924,31 @@ button.friend-account-name:hover {
924 924 transform: translateY(-2px);
925 925 }
926 926
927 .spell-favorite-slot.staff-attack {
928 border-width: 3px;
929 border-color: #3de0d0;
930 box-shadow:
931 0 0 0 2px rgba(61, 224, 208, 0.55),
932 0 0 12px rgba(40, 190, 180, 0.45);
933 }
934
935 .spell-favorite-slot.staff-attack.armed {
936 border-color: #5ef0e2;
937 box-shadow:
938 0 0 0 2px rgba(94, 240, 226, 0.6),
939 0 0 14px rgba(61, 224, 208, 0.55),
940 0 0 14px rgba(110, 181, 255, 0.35);
941 }
942
943 .spell-favorite-slot.dragging {
944 opacity: 0.55;
945 }
946
947 .spell-favorite-slot.drag-over {
948 border-color: #c9d4e8;
949 transform: translateY(-2px);
950 }
951
927 952 .spell-favorite-slot img {
928 953 width: 100%;
929 954 height: 100%;
@@ -924,6 +924,31 @@ button.friend-account-name:hover {
924 transform: translateY(-2px);
924 transform: translateY(-2px);
925}
925}
926
926
927.spell-favorite-slot.staff-attack {
928 border-width: 3px;
929 border-color: #3de0d0;
930 box-shadow:
931 0 0 0 2px rgba(61, 224, 208, 0.55),
932 0 0 12px rgba(40, 190, 180, 0.45);
933}
934
935.spell-favorite-slot.staff-attack.armed {
936 border-color: #5ef0e2;
937 box-shadow:
938 0 0 0 2px rgba(94, 240, 226, 0.6),
939 0 0 14px rgba(61, 224, 208, 0.55),
940 0 0 14px rgba(110, 181, 255, 0.35);
941}
942
943.spell-favorite-slot.dragging {
944 opacity: 0.55;
945}
946
947.spell-favorite-slot.drag-over {
948 border-color: #c9d4e8;
949 transform: translateY(-2px);
950}
951
927.spell-favorite-slot img {
952.spell-favorite-slot img {
928 width: 100%;
953 width: 100%;
929 height: 100%;
954 height: 100%;
server/src/gateway/websocket.js +12 −0
@@ -646,6 +646,16 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
646 646 spellId: message.spellId
647 647 });
648 648 break;
649 case MessageType.reorderSpellFavorites:
650 requirePlaying(session);
651 simulation.enqueue(session.id, {
652 type: "reorder-spell-favorites",
653 characterId: session.characterId,
654 favoriteSpellIds: Array.isArray(message.favoriteSpellIds)
655 ? message.favoriteSpellIds
656 : (message.favorites ?? [])
657 });
658 break;
649 659 case MessageType.setSpellAutoCast:
650 660 requirePlaying(session);
651 661 simulation.enqueue(session.id, {
@@ -1197,6 +1207,7 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1197 1207 send(session, {
1198 1208 type: MessageType.spells,
1199 1209 spells: spellView.spells,
1210 favorites: spellView.favorites ?? [],
1200 1211 autoCast: spellView.autoCast,
1201 1212 armedSpellId: spellView.armedSpellId ?? null
1202 1213 });
@@ -1547,6 +1558,7 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1547 1558 send(session, {
1548 1559 type: MessageType.spellsUpdated,
1549 1560 spells: event.spells ?? [],
1561 favorites: event.favorites ?? [],
1550 1562 autoCast: !!event.autoCast,
1551 1563 armedSpellId: event.armedSpellId ?? null
1552 1564 });
@@ -646,6 +646,16 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
646 spellId: message.spellId
646 spellId: message.spellId
647 });
647 });
648 break;
648 break;
649 case MessageType.reorderSpellFavorites:
650 requirePlaying(session);
651 simulation.enqueue(session.id, {
652 type: "reorder-spell-favorites",
653 characterId: session.characterId,
654 favoriteSpellIds: Array.isArray(message.favoriteSpellIds)
655 ? message.favoriteSpellIds
656 : (message.favorites ?? [])
657 });
658 break;
649 case MessageType.setSpellAutoCast:
659 case MessageType.setSpellAutoCast:
650 requirePlaying(session);
660 requirePlaying(session);
651 simulation.enqueue(session.id, {
661 simulation.enqueue(session.id, {
@@ -1197,6 +1207,7 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1197 send(session, {
1207 send(session, {
1198 type: MessageType.spells,
1208 type: MessageType.spells,
1199 spells: spellView.spells,
1209 spells: spellView.spells,
1210 favorites: spellView.favorites ?? [],
1200 autoCast: spellView.autoCast,
1211 autoCast: spellView.autoCast,
1201 armedSpellId: spellView.armedSpellId ?? null
1212 armedSpellId: spellView.armedSpellId ?? null
1202 });
1213 });
@@ -1547,6 +1558,7 @@ export function attachGateway ({ httpServer, auth, stateDb, characters, friends,
1547 send(session, {
1558 send(session, {
1548 type: MessageType.spellsUpdated,
1559 type: MessageType.spellsUpdated,
1549 spells: event.spells ?? [],
1560 spells: event.spells ?? [],
1561 favorites: event.favorites ?? [],
1550 autoCast: !!event.autoCast,
1562 autoCast: !!event.autoCast,
1551 armedSpellId: event.armedSpellId ?? null
1563 armedSpellId: event.armedSpellId ?? null
1552 });
1564 });
server/src/simulation/simulation.js +1 −0
@@ -2711,6 +2711,7 @@ export class Simulation
2711 2711 this.stateDb.saveSpells(event.characterId, event.stored);
2712 2712 this.pushSpells(event.characterId, {
2713 2713 spells: event.spells ?? [],
2714 favorites: event.favorites ?? [],
2714 2715 autoCast: !!event.autoCast
2715 2716 });
2716 2717 }
@@ -2711,6 +2711,7 @@ export class Simulation
2711 this.stateDb.saveSpells(event.characterId, event.stored);
2711 this.stateDb.saveSpells(event.characterId, event.stored);
2712 this.pushSpells(event.characterId, {
2712 this.pushSpells(event.characterId, {
2713 spells: event.spells ?? [],
2713 spells: event.spells ?? [],
2714 favorites: event.favorites ?? [],
2714 autoCast: !!event.autoCast
2715 autoCast: !!event.autoCast
2715 });
2716 });
2716 }
2717 }
server/src/simulation/workerWorld.js +2 −0
@@ -516,6 +516,7 @@ export class WorkerWorld
516 516 return { events: [] };
517 517 }
518 518 case "set-spell-favorite":
519 case "reorder-spell-favorites":
519 520 case "set-spell-autocast":
520 521 case "arm-spell":
521 522 case "cancel-arm-spell":
@@ -803,6 +804,7 @@ export class WorkerWorld
803 804 { return null; }
804 805 return {
805 806 spells: publicSpellsView(spells, skills),
807 favorites: Array.isArray(spells.favorites) ? [...spells.favorites] : [],
806 808 autoCast: !!spells.autoCast,
807 809 armedSpellId: casting?.armedSpellId ?? null
808 810 };
@@ -516,6 +516,7 @@ export class WorkerWorld
516 return { events: [] };
516 return { events: [] };
517 }
517 }
518 case "set-spell-favorite":
518 case "set-spell-favorite":
519 case "reorder-spell-favorites":
519 case "set-spell-autocast":
520 case "set-spell-autocast":
520 case "arm-spell":
521 case "arm-spell":
521 case "cancel-arm-spell":
522 case "cancel-arm-spell":
@@ -803,6 +804,7 @@ export class WorkerWorld
803 { return null; }
804 { return null; }
804 return {
805 return {
805 spells: publicSpellsView(spells, skills),
806 spells: publicSpellsView(spells, skills),
807 favorites: Array.isArray(spells.favorites) ? [...spells.favorites] : [],
806 autoCast: !!spells.autoCast,
808 autoCast: !!spells.autoCast,
807 armedSpellId: casting?.armedSpellId ?? null
809 armedSpellId: casting?.armedSpellId ?? null
808 };
810 };
server/src/spells/service.js +25 −0
@@ -9,6 +9,7 @@ import
9 9 publicSpellsView,
10 10 storedPlayerSpells,
11 11 toggleFavorite,
12 reorderFavorites,
12 13 learnSpell
13 14 } from "../../../shared/spells/state.js";
14 15 import
@@ -130,6 +131,8 @@ export function handleSpellCommand (world, sessionId, command, now = Date.now())
130 131 {
131 132 case "set-spell-favorite":
132 133 return setSpellFavorite(world, sessionId, command, now);
134 case "reorder-spell-favorites":
135 return reorderSpellFavorites(world, sessionId, command, now);
133 136 case "set-spell-autocast":
134 137 return setSpellAutoCast(world, sessionId, command, now);
135 138 case "arm-spell":
@@ -170,11 +173,13 @@ function requirePlayer (world, sessionId, characterId)
170 173
171 174 function spellsUpdatedEvent (entityId, characterId, spells, skills, levelUps = [])
172 175 {
176 const normalized = normalizePlayerSpells(spells);
173 177 return {
174 178 type: "spells-updated",
175 179 entityId,
176 180 characterId,
177 181 spells: publicSpellsView(spells, skills),
182 favorites: [...normalized.favorites],
178 183 stored: storedPlayerSpells(spells),
179 184 autoCast: !!spells.autoCast,
180 185 levelUps
@@ -203,6 +208,26 @@ function setSpellFavorite (world, sessionId, command, now)
203 208 return { events };
204 209 }
205 210
211 function reorderSpellFavorites (world, sessionId, command, now)
212 {
213 const { handle, entityId, character } = requirePlayer(world, sessionId, command.characterId);
214 ensureSpellComponents(world.ecs, handle);
215 const skills = world.ecs.get(handle, Component.skills);
216 const spells = world.ecs.get(handle, Component.spells);
217 const result = reorderFavorites(spells, command.favoriteSpellIds ?? command.favorites);
218 if (result.error)
219 {
220 const error = new Error(result.error);
221 error.userFacing = true;
222 throw error;
223 }
224 Object.assign(spells, result.spells);
225 const events = [];
226 if (result.changed)
227 { events.push(spellsUpdatedEvent(entityId, character.characterId, spells, skills)); }
228 return { events };
229 }
230
206 231 function setSpellAutoCast (world, sessionId, command, now)
207 232 {
208 233 const { handle, entityId, character } = requirePlayer(world, sessionId, command.characterId);
@@ -9,6 +9,7 @@ import
9 publicSpellsView,
9 publicSpellsView,
10 storedPlayerSpells,
10 storedPlayerSpells,
11 toggleFavorite,
11 toggleFavorite,
12 reorderFavorites,
12 learnSpell
13 learnSpell
13} from "../../../shared/spells/state.js";
14} from "../../../shared/spells/state.js";
14import
15import
@@ -130,6 +131,8 @@ export function handleSpellCommand (world, sessionId, command, now = Date.now())
130 {
131 {
131 case "set-spell-favorite":
132 case "set-spell-favorite":
132 return setSpellFavorite(world, sessionId, command, now);
133 return setSpellFavorite(world, sessionId, command, now);
134 case "reorder-spell-favorites":
135 return reorderSpellFavorites(world, sessionId, command, now);
133 case "set-spell-autocast":
136 case "set-spell-autocast":
134 return setSpellAutoCast(world, sessionId, command, now);
137 return setSpellAutoCast(world, sessionId, command, now);
135 case "arm-spell":
138 case "arm-spell":
@@ -170,11 +173,13 @@ function requirePlayer (world, sessionId, characterId)
170
173
171function spellsUpdatedEvent (entityId, characterId, spells, skills, levelUps = [])
174function spellsUpdatedEvent (entityId, characterId, spells, skills, levelUps = [])
172{
175{
176 const normalized = normalizePlayerSpells(spells);
173 return {
177 return {
174 type: "spells-updated",
178 type: "spells-updated",
175 entityId,
179 entityId,
176 characterId,
180 characterId,
177 spells: publicSpellsView(spells, skills),
181 spells: publicSpellsView(spells, skills),
182 favorites: [...normalized.favorites],
178 stored: storedPlayerSpells(spells),
183 stored: storedPlayerSpells(spells),
179 autoCast: !!spells.autoCast,
184 autoCast: !!spells.autoCast,
180 levelUps
185 levelUps
@@ -203,6 +208,26 @@ function setSpellFavorite (world, sessionId, command, now)
203 return { events };
208 return { events };
204}
209}
205
210
211function reorderSpellFavorites (world, sessionId, command, now)
212{
213 const { handle, entityId, character } = requirePlayer(world, sessionId, command.characterId);
214 ensureSpellComponents(world.ecs, handle);
215 const skills = world.ecs.get(handle, Component.skills);
216 const spells = world.ecs.get(handle, Component.spells);
217 const result = reorderFavorites(spells, command.favoriteSpellIds ?? command.favorites);
218 if (result.error)
219 {
220 const error = new Error(result.error);
221 error.userFacing = true;
222 throw error;
223 }
224 Object.assign(spells, result.spells);
225 const events = [];
226 if (result.changed)
227 { events.push(spellsUpdatedEvent(entityId, character.characterId, spells, skills)); }
228 return { events };
229}
230
206function setSpellAutoCast (world, sessionId, command, now)
231function setSpellAutoCast (world, sessionId, command, now)
207{
232{
208 const { handle, entityId, character } = requirePlayer(world, sessionId, command.characterId);
233 const { handle, entityId, character } = requirePlayer(world, sessionId, command.characterId);
server/tests/spells.test.js +30 −0
@@ -679,3 +679,33 @@ test("death clears active auto-repeat but keeps auto-cast for respawn", () =>
679 679 harness.close();
680 680 }
681 681 });
682
683 test("reorder-spell-favorites swaps favourite order", () =>
684 {
685 const harness = makeSimulation({ workerCount: 1 });
686 try
687 {
688 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
689 const playerId = harness.simulation.spawnCharacter(1, character);
690 const worker = workerOf(harness.simulation, playerId);
691 const handle = worker.world.ecs.handleOf(playerId);
692 const spells = worker.world.ecs.get(handle, Component.spells);
693 spells.known.push(SpellId.manaBullet);
694 spells.favorites = [SpellId.light, SpellId.manaBullet];
695 const skills = worker.world.ecs.get(handle, Component.skills);
696 skills.xp[SkillId.magic] = totalXpForLevel(20);
697
698 harness.simulation.enqueue(1, {
699 type: "reorder-spell-favorites",
700 characterId: character.character_id,
701 favoriteSpellIds: [SpellId.manaBullet, SpellId.light]
702 });
703 harness.simulation.step();
704 assert.deepEqual(
705 worker.world.ecs.get(handle, Component.spells).favorites,
706 [SpellId.manaBullet, SpellId.light]
707 );
708 }
709 finally
710 { harness.close(); }
711 });
@@ -679,3 +679,33 @@ test("death clears active auto-repeat but keeps auto-cast for respawn", () =>
679 harness.close();
679 harness.close();
680 }
680 }
681});
681});
682
683test("reorder-spell-favorites swaps favourite order", () =>
684{
685 const harness = makeSimulation({ workerCount: 1 });
686 try
687 {
688 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
689 const playerId = harness.simulation.spawnCharacter(1, character);
690 const worker = workerOf(harness.simulation, playerId);
691 const handle = worker.world.ecs.handleOf(playerId);
692 const spells = worker.world.ecs.get(handle, Component.spells);
693 spells.known.push(SpellId.manaBullet);
694 spells.favorites = [SpellId.light, SpellId.manaBullet];
695 const skills = worker.world.ecs.get(handle, Component.skills);
696 skills.xp[SkillId.magic] = totalXpForLevel(20);
697
698 harness.simulation.enqueue(1, {
699 type: "reorder-spell-favorites",
700 characterId: character.character_id,
701 favoriteSpellIds: [SpellId.manaBullet, SpellId.light]
702 });
703 harness.simulation.step();
704 assert.deepEqual(
705 worker.world.ecs.get(handle, Component.spells).favorites,
706 [SpellId.manaBullet, SpellId.light]
707 );
708 }
709 finally
710 { harness.close(); }
711});
shared/protocol/constants.js +1 −0
@@ -230,6 +230,7 @@ export const MessageType = Object.freeze({
230 230 spells: "spells",
231 231 setSpellFavorite: "SET_SPELL_FAVORITE",
232 232 setSpellAutoCast: "SET_SPELL_AUTOCAST",
233 reorderSpellFavorites: "REORDER_SPELL_FAVORITES",
233 234 armSpell: "ARM_SPELL",
234 235 cancelArmSpell: "CANCEL_ARM_SPELL",
235 236 castSpellTile: "CAST_SPELL_TILE",
@@ -230,6 +230,7 @@ export const MessageType = Object.freeze({
230 spells: "spells",
230 spells: "spells",
231 setSpellFavorite: "SET_SPELL_FAVORITE",
231 setSpellFavorite: "SET_SPELL_FAVORITE",
232 setSpellAutoCast: "SET_SPELL_AUTOCAST",
232 setSpellAutoCast: "SET_SPELL_AUTOCAST",
233 reorderSpellFavorites: "REORDER_SPELL_FAVORITES",
233 armSpell: "ARM_SPELL",
234 armSpell: "ARM_SPELL",
234 cancelArmSpell: "CANCEL_ARM_SPELL",
235 cancelArmSpell: "CANCEL_ARM_SPELL",
235 castSpellTile: "CAST_SPELL_TILE",
236 castSpellTile: "CAST_SPELL_TILE",
shared/spells/state.js +34 −0
@@ -130,6 +130,40 @@ export function toggleFavorite (spells, spellId, skills)
130 130 return { changed: true, spells: normalized };
131 131 }
132 132
133 /**
134 * Reorder favourites by swapping/moving to match `orderedIds` (must be a permutation of current favourites).
135 */
136 export function reorderFavorites (spells, orderedIds)
137 {
138 const normalized = normalizePlayerSpells(spells);
139 const current = normalized.favorites;
140 if (!Array.isArray(orderedIds) || orderedIds.length !== current.length)
141 { return { changed: false, spells: normalized, error: "Invalid favourite order." }; }
142 const currentSet = new Set(current);
143 const next = [];
144 for (const id of orderedIds)
145 {
146 if (!currentSet.has(id) || next.includes(id))
147 { return { changed: false, spells: normalized, error: "Invalid favourite order." }; }
148 next.push(id);
149 }
150 if (next.length !== current.length)
151 { return { changed: false, spells: normalized, error: "Invalid favourite order." }; }
152 let same = true;
153 for (let i = 0; i < next.length; i++)
154 {
155 if (next[i] !== current[i])
156 {
157 same = false;
158 break;
159 }
160 }
161 if (same)
162 { return { changed: false, spells: normalized }; }
163 normalized.favorites = next;
164 return { changed: true, spells: normalized };
165 }
166
133 167 export function publicSpellsView (spells, skills)
134 168 {
135 169 const normalized = normalizePlayerSpells(spells);
@@ -130,6 +130,40 @@ export function toggleFavorite (spells, spellId, skills)
130 return { changed: true, spells: normalized };
130 return { changed: true, spells: normalized };
131}
131}
132
132
133/**
134 * Reorder favourites by swapping/moving to match `orderedIds` (must be a permutation of current favourites).
135 */
136export function reorderFavorites (spells, orderedIds)
137{
138 const normalized = normalizePlayerSpells(spells);
139 const current = normalized.favorites;
140 if (!Array.isArray(orderedIds) || orderedIds.length !== current.length)
141 { return { changed: false, spells: normalized, error: "Invalid favourite order." }; }
142 const currentSet = new Set(current);
143 const next = [];
144 for (const id of orderedIds)
145 {
146 if (!currentSet.has(id) || next.includes(id))
147 { return { changed: false, spells: normalized, error: "Invalid favourite order." }; }
148 next.push(id);
149 }
150 if (next.length !== current.length)
151 { return { changed: false, spells: normalized, error: "Invalid favourite order." }; }
152 let same = true;
153 for (let i = 0; i < next.length; i++)
154 {
155 if (next[i] !== current[i])
156 {
157 same = false;
158 break;
159 }
160 }
161 if (same)
162 { return { changed: false, spells: normalized }; }
163 normalized.favorites = next;
164 return { changed: true, spells: normalized };
165}
166
133export function publicSpellsView (spells, skills)
167export function publicSpellsView (spells, skills)
134{
168{
135 const normalized = normalizePlayerSpells(spells);
169 const normalized = normalizePlayerSpells(spells);