import { SpellId, spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "../definitions/spells.js"; import { MAX_FAVORITE_SPELLS } from "./constants.js"; import { skillLevel } from "../definitions/skills.js"; import { SkillId } from "../definitions/skills.js"; export const DEFAULT_KNOWN_SPELLS = Object.freeze([SpellId.light]); export function defaultPlayerSpells () { return { known: [...DEFAULT_KNOWN_SPELLS], favorites: [], autoCast: false }; } export function normalizeKnownSpells (raw) { const known = new Set(DEFAULT_KNOWN_SPELLS); if (Array.isArray(raw)) { for (const id of raw) { if (spellDefinition(id)) { known.add(id); } } } else if (raw && typeof raw === "object" && Array.isArray(raw.known)) { for (const id of raw.known) { if (spellDefinition(id)) { known.add(id); } } } return [...known]; } export function normalizeFavoriteSpells (raw, known) { const knownSet = new Set(known); const favorites = []; const source = Array.isArray(raw) ? raw : (raw?.favorites ?? []); for (const id of source) { if (!knownSet.has(id) || !spellDefinition(id)) { continue; } if (favorites.includes(id)) { continue; } favorites.push(id); if (favorites.length >= MAX_FAVORITE_SPELLS) { break; } } return favorites; } export function normalizePlayerSpells (stored) { const known = normalizeKnownSpells(stored); return { known, favorites: normalizeFavoriteSpells(stored, known), autoCast: !!(stored && typeof stored === "object" && stored.autoCast) }; } export function storedPlayerSpells (component) { const normalized = normalizePlayerSpells(component); return { known: normalized.known, favorites: normalized.favorites, autoCast: normalized.autoCast }; } export function playerKnowsSpell (spells, spellId) { return normalizeKnownSpells(spells).includes(spellId); } export function canCastSpell (skills, spells, spellId) { const def = spellDefinition(spellId); if (!def || !playerKnowsSpell(spells, spellId)) { return { ok: false, reason: "You do not know that spell." }; } const magicLevel = skillLevel(skills, SkillId.magic); const need = spellMinMagicLevel(def); if (magicLevel < need) { return { ok: false, reason: `Requires Magic level ${need}.` }; } return { ok: true }; } export function canFavoriteSpell (skills, spells, spellId) { return canCastSpell(skills, spells, spellId).ok; } export function learnSpell (spells, spellId) { const normalized = normalizePlayerSpells(spells); if (!spellDefinition(spellId)) { return { changed: false, spells: normalized }; } if (normalized.known.includes(spellId)) { return { changed: false, spells: normalized }; } normalized.known.push(spellId); return { changed: true, spells: normalized }; } export function toggleFavorite (spells, spellId, skills) { const normalized = normalizePlayerSpells(spells); if (!normalized.known.includes(spellId)) { return { changed: false, spells: normalized, error: "Unknown spell." }; } if (!canFavoriteSpell(skills, normalized, spellId)) { return { changed: false, spells: normalized, error: "Magic level too low to favourite this spell." }; } const idx = normalized.favorites.indexOf(spellId); if (idx >= 0) { normalized.favorites.splice(idx, 1); return { changed: true, spells: normalized }; } if (normalized.favorites.length >= MAX_FAVORITE_SPELLS) { return { changed: false, spells: normalized, error: "You can only favourite six spells." }; } normalized.favorites.push(spellId); return { changed: true, spells: normalized }; } /** * Reorder favourites by swapping/moving to match `orderedIds` (must be a permutation of current favourites). */ export function reorderFavorites (spells, orderedIds) { const normalized = normalizePlayerSpells(spells); const current = normalized.favorites; if (!Array.isArray(orderedIds) || orderedIds.length !== current.length) { return { changed: false, spells: normalized, error: "Invalid favourite order." }; } const currentSet = new Set(current); const next = []; for (const id of orderedIds) { if (!currentSet.has(id) || next.includes(id)) { return { changed: false, spells: normalized, error: "Invalid favourite order." }; } next.push(id); } if (next.length !== current.length) { return { changed: false, spells: normalized, error: "Invalid favourite order." }; } let same = true; for (let i = 0; i < next.length; i++) { if (next[i] !== current[i]) { same = false; break; } } if (same) { return { changed: false, spells: normalized }; } normalized.favorites = next; return { changed: true, spells: normalized }; } export function publicSpellsView (spells, skills) { const normalized = normalizePlayerSpells(spells); return normalized.known.map((spellId) => { const def = spellDefinition(spellId); const cast = canCastSpell(skills, normalized, spellId); return { spellId, displayName: def?.displayName ?? spellId, tier: def?.tier ?? 1, mindPower: spellMindPowerFromTier(def?.tier ?? 1), description: def?.description ?? "", target: def?.target ?? "tile", autoRepeat: !!def?.autoRepeat, icon: `/assets/spells/${spellId}.svg`, canCast: cast.ok, canFavorite: cast.ok, favorite: normalized.favorites.includes(spellId) }; }); }