| 1 | /** |
| 2 | * Play spells panel: known spells, favourites bar, arm / cast intents. |
| 3 | */ |
| 4 | import { spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "/shared/definitions/spells.js"; |
| 5 | import { SkillId } from "/shared/definitions/skills.js"; |
| 6 | |
| 7 | export function initSpells ({ socket, MessageType, getSkillLevel } = {}) |
| 8 | { |
| 9 | const panel = document.getElementById("spells-panel"); |
| 10 | const list = document.getElementById("spells-list"); |
| 11 | const bar = document.getElementById("spell-favorites-bar"); |
| 12 | const autoCastToggle = document.getElementById("spell-autocast-toggle"); |
| 13 | let spells = []; |
| 14 | let autoCast = false; |
| 15 | let armedSpellId = null; |
| 16 | |
| 17 | document.getElementById("spells-toggle")?.addEventListener("click", () => |
| 18 | { |
| 19 | panel.hidden = !panel.hidden; |
| 20 | if (!panel.hidden) |
| 21 | { refreshCastGates(); } |
| 22 | }); |
| 23 | |
| 24 | autoCastToggle?.addEventListener("change", () => |
| 25 | { |
| 26 | socket.send({ |
| 27 | type: MessageType.setSpellAutoCast, |
| 28 | enabled: autoCastToggle.checked |
| 29 | }); |
| 30 | }); |
| 31 | |
| 32 | /** Re-evaluate Magic gates from the latest client skill levels (e.g. after level-up). */ |
| 33 | function refreshCastGates () |
| 34 | { |
| 35 | if (!spells.length) |
| 36 | { return; } |
| 37 | const magicLevel = Math.max(1, getSkillLevel?.(SkillId.magic) | 0 || 1); |
| 38 | let changed = false; |
| 39 | for (const spell of spells) |
| 40 | { |
| 41 | const need = spellMinMagicLevel(spellDefinition(spell.spellId)); |
| 42 | const ok = magicLevel >= need; |
| 43 | if (spell.canCast !== ok || spell.canFavorite !== ok) |
| 44 | { |
| 45 | spell.canCast = ok; |
| 46 | spell.canFavorite = ok; |
| 47 | changed = true; |
| 48 | } |
| 49 | } |
| 50 | if (changed || !panel.hidden) |
| 51 | { |
| 52 | renderList(); |
| 53 | renderBar(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | function disarmLocal () |
| 58 | { |
| 59 | if (!armedSpellId) |
| 60 | { return; } |
| 61 | armedSpellId = null; |
| 62 | renderList(); |
| 63 | renderBar(); |
| 64 | } |
| 65 | |
| 66 | function render (payload) |
| 67 | { |
| 68 | spells = payload?.spells ?? []; |
| 69 | autoCast = !!payload?.autoCast; |
| 70 | if (payload?.armedSpellId !== undefined) |
| 71 | { armedSpellId = payload.armedSpellId; } |
| 72 | if (autoCastToggle) |
| 73 | { autoCastToggle.checked = autoCast; } |
| 74 | refreshCastGates(); |
| 75 | } |
| 76 | |
| 77 | function renderList () |
| 78 | { |
| 79 | list.replaceChildren(); |
| 80 | for (const spell of spells) |
| 81 | { |
| 82 | list.append(renderSpellRow(spell)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | function renderSpellRow (spell) |
| 87 | { |
| 88 | const row = document.createElement("div"); |
| 89 | row.className = "spell-row"; |
| 90 | if (spell.spellId === armedSpellId) |
| 91 | { row.classList.add("armed"); } |
| 92 | |
| 93 | const icon = document.createElement("img"); |
| 94 | icon.className = "spell-icon"; |
| 95 | icon.src = spell.icon; |
| 96 | icon.alt = ""; |
| 97 | icon.loading = "lazy"; |
| 98 | |
| 99 | const body = document.createElement("div"); |
| 100 | body.className = "spell-body"; |
| 101 | |
| 102 | const title = document.createElement("div"); |
| 103 | title.className = "spell-title-row"; |
| 104 | const name = document.createElement("span"); |
| 105 | name.className = "spell-title"; |
| 106 | name.textContent = spell.displayName; |
| 107 | const meta = document.createElement("span"); |
| 108 | meta.className = "spell-meta"; |
| 109 | const level = spell.tier | 0 || 1; |
| 110 | const power = spell.mindPower != null |
| 111 | ? spell.mindPower | 0 |
| 112 | : spellMindPowerFromTier(level); |
| 113 | meta.textContent = `Lv. ${level} Pwr: ${power}`; |
| 114 | title.append(name, meta); |
| 115 | |
| 116 | const desc = document.createElement("div"); |
| 117 | desc.className = "spell-desc hint"; |
| 118 | desc.textContent = spell.description ?? ""; |
| 119 | |
| 120 | const actions = document.createElement("div"); |
| 121 | actions.className = "spell-actions"; |
| 122 | |
| 123 | const favorite = document.createElement("button"); |
| 124 | favorite.type = "button"; |
| 125 | favorite.className = "spell-favorite-btn"; |
| 126 | favorite.textContent = spell.favorite ? "★" : "☆"; |
| 127 | favorite.title = spell.canFavorite |
| 128 | ? (spell.favorite ? "Remove from favourites" : "Add to favourites") |
| 129 | : "Magic level too low to favourite"; |
| 130 | favorite.disabled = !spell.canFavorite; |
| 131 | favorite.addEventListener("click", () => |
| 132 | { |
| 133 | socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId }); |
| 134 | }); |
| 135 | |
| 136 | const arm = document.createElement("button"); |
| 137 | arm.type = "button"; |
| 138 | arm.className = "spell-arm-btn"; |
| 139 | arm.textContent = spell.spellId === armedSpellId ? "Armed" : "Arm"; |
| 140 | arm.disabled = !spell.canCast; |
| 141 | arm.addEventListener("click", () => |
| 142 | { |
| 143 | if (spell.spellId === armedSpellId) |
| 144 | { cancelTargeting(); } |
| 145 | else |
| 146 | { socket.send({ type: MessageType.armSpell, spellId: spell.spellId }); } |
| 147 | }); |
| 148 | |
| 149 | actions.append(favorite, arm); |
| 150 | body.append(title, desc, actions); |
| 151 | row.append(icon, body); |
| 152 | return row; |
| 153 | } |
| 154 | |
| 155 | function renderBar () |
| 156 | { |
| 157 | const favorites = spells.filter((entry) => entry.favorite); |
| 158 | bar.replaceChildren(); |
| 159 | bar.hidden = favorites.length === 0; |
| 160 | for (const spell of favorites) |
| 161 | { |
| 162 | const btn = document.createElement("button"); |
| 163 | btn.type = "button"; |
| 164 | btn.className = "spell-favorite-slot"; |
| 165 | if (spell.spellId === armedSpellId) |
| 166 | { btn.classList.add("armed"); } |
| 167 | btn.title = spell.displayName; |
| 168 | btn.disabled = !spell.canCast; |
| 169 | |
| 170 | const icon = document.createElement("img"); |
| 171 | icon.src = spell.icon; |
| 172 | icon.alt = ""; |
| 173 | btn.append(icon); |
| 174 | |
| 175 | btn.addEventListener("click", () => |
| 176 | { |
| 177 | if (spell.spellId === armedSpellId) |
| 178 | { return; } |
| 179 | socket.send({ type: MessageType.armSpell, spellId: spell.spellId }); |
| 180 | }); |
| 181 | btn.addEventListener("contextmenu", (event) => |
| 182 | { |
| 183 | event.preventDefault(); |
| 184 | socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId }); |
| 185 | }); |
| 186 | bar.append(btn); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | function getArmedSpell () |
| 191 | { |
| 192 | if (!armedSpellId) |
| 193 | { return null; } |
| 194 | return spells.find((entry) => entry.spellId === armedSpellId) ?? null; |
| 195 | } |
| 196 | |
| 197 | function tryCastAtTile (tileX, tileY) |
| 198 | { |
| 199 | const armed = getArmedSpell(); |
| 200 | if (!armed || armed.target !== "tile" || !armed.canCast) |
| 201 | { return false; } |
| 202 | socket.send({ |
| 203 | type: MessageType.castSpellTile, |
| 204 | spellId: armed.spellId, |
| 205 | tileX, |
| 206 | tileY |
| 207 | }); |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | function tryCastAtEntity (targetEntityId) |
| 212 | { |
| 213 | const armed = getArmedSpell(); |
| 214 | if (!armed || armed.target !== "entity" || !armed.canCast) |
| 215 | { return false; } |
| 216 | socket.send({ |
| 217 | type: MessageType.castSpellEntity, |
| 218 | spellId: armed.spellId, |
| 219 | targetEntityId |
| 220 | }); |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | function cancelTargeting () |
| 225 | { |
| 226 | if (!armedSpellId) |
| 227 | { return; } |
| 228 | socket.send({ type: MessageType.cancelArmSpell }); |
| 229 | disarmLocal(); |
| 230 | } |
| 231 | |
| 232 | return { |
| 233 | render, |
| 234 | refreshCastGates, |
| 235 | getArmedSpell, |
| 236 | tryCastAtTile, |
| 237 | tryCastAtEntity, |
| 238 | cancelTargeting, |
| 239 | isBlocking () |
| 240 | { |
| 241 | return panel != null && !panel.hidden; |
| 242 | }, |
| 243 | isTargeting () |
| 244 | { |
| 245 | return armedSpellId != null; |
| 246 | }, |
| 247 | handleSpellArmed (message) |
| 248 | { |
| 249 | armedSpellId = message.spellId ?? null; |
| 250 | renderList(); |
| 251 | renderBar(); |
| 252 | }, |
| 253 | handleSpellDisarmed () |
| 254 | { |
| 255 | disarmLocal(); |
| 256 | }, |
| 257 | hide () |
| 258 | { |
| 259 | panel.hidden = true; |
| 260 | spells = []; |
| 261 | autoCast = false; |
| 262 | armedSpellId = null; |
| 263 | if (autoCastToggle) |
| 264 | { autoCastToggle.checked = false; } |
| 265 | list.replaceChildren(); |
| 266 | bar.replaceChildren(); |
| 267 | bar.hidden = true; |
| 268 | } |
| 269 | }; |
| 270 | } |