| 1 | import { SpellId, spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "../definitions/spells.js"; |
| 2 | import { MAX_FAVORITE_SPELLS } from "./constants.js"; |
| 3 | import { skillLevel } from "../definitions/skills.js"; |
| 4 | import { SkillId } from "../definitions/skills.js"; |
| 5 | |
| 6 | export const DEFAULT_KNOWN_SPELLS = Object.freeze([SpellId.light]); |
| 7 | |
| 8 | export function defaultPlayerSpells () |
| 9 | { |
| 10 | return { |
| 11 | known: [...DEFAULT_KNOWN_SPELLS], |
| 12 | favorites: [], |
| 13 | autoCast: false |
| 14 | }; |
| 15 | } |
| 16 | |
| 17 | export function normalizeKnownSpells (raw) |
| 18 | { |
| 19 | const known = new Set(DEFAULT_KNOWN_SPELLS); |
| 20 | if (Array.isArray(raw)) |
| 21 | { |
| 22 | for (const id of raw) |
| 23 | { |
| 24 | if (spellDefinition(id)) |
| 25 | { known.add(id); } |
| 26 | } |
| 27 | } |
| 28 | else if (raw && typeof raw === "object" && Array.isArray(raw.known)) |
| 29 | { |
| 30 | for (const id of raw.known) |
| 31 | { |
| 32 | if (spellDefinition(id)) |
| 33 | { known.add(id); } |
| 34 | } |
| 35 | } |
| 36 | return [...known]; |
| 37 | } |
| 38 | |
| 39 | export function normalizeFavoriteSpells (raw, known) |
| 40 | { |
| 41 | const knownSet = new Set(known); |
| 42 | const favorites = []; |
| 43 | const source = Array.isArray(raw) ? raw : (raw?.favorites ?? []); |
| 44 | for (const id of source) |
| 45 | { |
| 46 | if (!knownSet.has(id) || !spellDefinition(id)) |
| 47 | { continue; } |
| 48 | if (favorites.includes(id)) |
| 49 | { continue; } |
| 50 | favorites.push(id); |
| 51 | if (favorites.length >= MAX_FAVORITE_SPELLS) |
| 52 | { break; } |
| 53 | } |
| 54 | return favorites; |
| 55 | } |
| 56 | |
| 57 | export function normalizePlayerSpells (stored) |
| 58 | { |
| 59 | const known = normalizeKnownSpells(stored); |
| 60 | return { |
| 61 | known, |
| 62 | favorites: normalizeFavoriteSpells(stored, known), |
| 63 | autoCast: !!(stored && typeof stored === "object" && stored.autoCast) |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | export function storedPlayerSpells (component) |
| 68 | { |
| 69 | const normalized = normalizePlayerSpells(component); |
| 70 | return { |
| 71 | known: normalized.known, |
| 72 | favorites: normalized.favorites, |
| 73 | autoCast: normalized.autoCast |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | export function playerKnowsSpell (spells, spellId) |
| 78 | { |
| 79 | return normalizeKnownSpells(spells).includes(spellId); |
| 80 | } |
| 81 | |
| 82 | export function canCastSpell (skills, spells, spellId) |
| 83 | { |
| 84 | const def = spellDefinition(spellId); |
| 85 | if (!def || !playerKnowsSpell(spells, spellId)) |
| 86 | { return { ok: false, reason: "You do not know that spell." }; } |
| 87 | const magicLevel = skillLevel(skills, SkillId.magic); |
| 88 | const need = spellMinMagicLevel(def); |
| 89 | if (magicLevel < need) |
| 90 | { |
| 91 | return { ok: false, reason: `Requires Magic level ${need}.` }; |
| 92 | } |
| 93 | return { ok: true }; |
| 94 | } |
| 95 | |
| 96 | export function canFavoriteSpell (skills, spells, spellId) |
| 97 | { |
| 98 | return canCastSpell(skills, spells, spellId).ok; |
| 99 | } |
| 100 | |
| 101 | export function learnSpell (spells, spellId) |
| 102 | { |
| 103 | const normalized = normalizePlayerSpells(spells); |
| 104 | if (!spellDefinition(spellId)) |
| 105 | { return { changed: false, spells: normalized }; } |
| 106 | if (normalized.known.includes(spellId)) |
| 107 | { return { changed: false, spells: normalized }; } |
| 108 | normalized.known.push(spellId); |
| 109 | return { changed: true, spells: normalized }; |
| 110 | } |
| 111 | |
| 112 | export function toggleFavorite (spells, spellId, skills) |
| 113 | { |
| 114 | const normalized = normalizePlayerSpells(spells); |
| 115 | if (!normalized.known.includes(spellId)) |
| 116 | { return { changed: false, spells: normalized, error: "Unknown spell." }; } |
| 117 | if (!canFavoriteSpell(skills, normalized, spellId)) |
| 118 | { return { changed: false, spells: normalized, error: "Magic level too low to favourite this spell." }; } |
| 119 | const idx = normalized.favorites.indexOf(spellId); |
| 120 | if (idx >= 0) |
| 121 | { |
| 122 | normalized.favorites.splice(idx, 1); |
| 123 | return { changed: true, spells: normalized }; |
| 124 | } |
| 125 | if (normalized.favorites.length >= MAX_FAVORITE_SPELLS) |
| 126 | { |
| 127 | return { changed: false, spells: normalized, error: "You can only favourite six spells." }; |
| 128 | } |
| 129 | normalized.favorites.push(spellId); |
| 130 | return { changed: true, spells: normalized }; |
| 131 | } |
| 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 | |
| 167 | export function publicSpellsView (spells, skills) |
| 168 | { |
| 169 | const normalized = normalizePlayerSpells(spells); |
| 170 | return normalized.known.map((spellId) => |
| 171 | { |
| 172 | const def = spellDefinition(spellId); |
| 173 | const cast = canCastSpell(skills, normalized, spellId); |
| 174 | return { |
| 175 | spellId, |
| 176 | displayName: def?.displayName ?? spellId, |
| 177 | tier: def?.tier ?? 1, |
| 178 | mindPower: spellMindPowerFromTier(def?.tier ?? 1), |
| 179 | description: def?.description ?? "", |
| 180 | target: def?.target ?? "tile", |
| 181 | autoRepeat: !!def?.autoRepeat, |
| 182 | icon: `/assets/spells/${spellId}.svg`, |
| 183 | canCast: cast.ok, |
| 184 | canFavorite: cast.ok, |
| 185 | favorite: normalized.favorites.includes(spellId) |
| 186 | }; |
| 187 | }); |
| 188 | } |