BlueGit

timfa questward

Files

questward / shared / spells / state.js

state.js

state.js 4.1 KiB Download Raw
1import { SpellId, spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "../definitions/spells.js";
2import { MAX_FAVORITE_SPELLS } from "./constants.js";
3import { skillLevel } from "../definitions/skills.js";
4import { SkillId } from "../definitions/skills.js";
5
6export const DEFAULT_KNOWN_SPELLS = Object.freeze([SpellId.light]);
7
8export function defaultPlayerSpells ()
9{
10 return {
11 known: [...DEFAULT_KNOWN_SPELLS],
12 favorites: [],
13 autoCast: false
14 };
15}
16
17export 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
39export 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
57export 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
67export 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
77export function playerKnowsSpell (spells, spellId)
78{
79 return normalizeKnownSpells(spells).includes(spellId);
80}
81
82export 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
96export function canFavoriteSpell (skills, spells, spellId)
97{
98 return canCastSpell(skills, spells, spellId).ok;
99}
100
101export 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
112export 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
133export function publicSpellsView (spells, skills)
134{
135 const normalized = normalizePlayerSpells(spells);
136 return normalized.known.map((spellId) =>
137 {
138 const def = spellDefinition(spellId);
139 const cast = canCastSpell(skills, normalized, spellId);
140 return {
141 spellId,
142 displayName: def?.displayName ?? spellId,
143 tier: def?.tier ?? 1,
144 mindPower: spellMindPowerFromTier(def?.tier ?? 1),
145 description: def?.description ?? "",
146 target: def?.target ?? "tile",
147 autoRepeat: !!def?.autoRepeat,
148 icon: `/assets/spells/${spellId}.svg`,
149 canCast: cast.ok,
150 canFavorite: cast.ok,
151 favorite: normalized.favorites.includes(spellId)
152 };
153 });
154}