diff options
Diffstat (limited to 'web/src/lib/data')
| -rw-r--r-- | web/src/lib/data/gameData.ts | 170 |
1 files changed, 158 insertions, 12 deletions
diff --git a/web/src/lib/data/gameData.ts b/web/src/lib/data/gameData.ts index 2511022..10e1c60 100644 --- a/web/src/lib/data/gameData.ts +++ b/web/src/lib/data/gameData.ts @@ -5,6 +5,11 @@ * server is the single source and the client fetches on demand and caches. */ +import type { + GraphicPositional, + MonsterAbilityCard +} from '../protocol/types'; + // --- Raw JSON shapes (the subset the UI needs) ---------------------------- interface RawClass { @@ -14,6 +19,9 @@ interface RawClass { hidden?: boolean; color: string | string[]; colorSecondary?: string | string[]; + /** `adds`/`removes` per perk slot (the reference `PerkModel`). */ + perks?: { text?: string; adds?: string[]; removes?: string[] }[]; + perksFH?: { text?: string; adds?: string[]; removes?: string[] }[]; } interface RawMonsterStats { @@ -46,6 +54,14 @@ interface RawEdition { classes: RawClass[]; monsters: Record<string, RawMonster>; scenarios: Record<string, RawScenario>; + /** Monster ability-card decks (the reference `MonsterAbilityDeckModel`). */ + monsterAbilities?: RawMonsterAbilityDeck[]; +} + +interface RawMonsterAbilityDeck { + name: string; + edition: string; + cards: unknown[]; } // --- UI-facing types ------------------------------------------------------- @@ -60,6 +76,19 @@ export interface ClassData { colorSecondary: string; /** `/images/class-icons/<name>.png` */ icon: string; + /** The `adds` graphic list per perk slot (the reference `perks`). */ + perks: string[][]; + perksFH: string[][]; + /** The full perk-slot data (text + adds + removes) for the perks menu. */ + perkDetails: PerkDetail[]; + perkDetailsFH: PerkDetail[]; +} + +/** One perk slot (the reference `PerkModel`). */ +export interface PerkDetail { + text: string; + adds: string[]; + removes: string[]; } export interface MonsterData { @@ -90,6 +119,12 @@ export interface CampaignData { monsters: MonsterData[]; /** Scenario names in file order. */ scenarios: ScenarioData[]; + /** + * Monster ability-card decks, keyed by deck name (the reference + * `MonsterAbilityDeckModel`). The client reconstructs full cards from + * these when state references a card by `{nr, deck}`. + */ + abilityDecks: Record<string, MonsterAbilityCard[]>; } export function firstColor(value: string | string[] | undefined, fallback = '000000') { @@ -101,12 +136,54 @@ function escapePathSegment(value: string): string { return encodeURIComponent(value); } +/** + * Parse one ability-card raw entry into a full {@link MonsterAbilityCard}. + * Mirrors the reference `MonsterAbilityDeckModel.fromJson`: the card is a + * JSON array of `[optionalTitle, nr, shuffle, initiative, optionalGraphics, + * ...lines]` where the title (a leading string) and the graphics list (at + * index 3, a list of `{gfx,x,y,scale?,angle?}`) are both optional. + */ +function parseAbilityCard(raw: unknown[], deckName: string): MonsterAbilityCard { + let title = deckName; + let arr = raw; + if (typeof arr[0] === 'string') { + title = arr[0] as string; + arr = arr.slice(1); + } + const nr = arr[0] as number; + const shuffle = arr[1] as boolean; + const initiative = arr[2] as number; + let rest = arr.slice(3); + let graphics: GraphicPositional[] = []; + if (Array.isArray(rest[0])) { + graphics = (rest[0] as Array<Record<string, unknown>>).map((g) => ({ + gfx: g.gfx as string, + x: g.x as number, + y: g.y as number, + scale: (g.scale as number | undefined) ?? 1, + angle: (g.angle as number | undefined) ?? 0 + })); + rest = rest.slice(1); + } + return { + title, + nr, + shuffle, + initiative, + lines: rest as string[], + deck: deckName, + graphics + }; +} + // --- The loader ------------------------------------------------------------ export class GameDataClient { private campaignsPromise: Promise<string[]> | null = null; private campaignsCache: string[] | null = null; private readonly cache = new Map<string, Promise<CampaignData>>(); + /** Resolved campaign data (populated as each fetch lands) for sync lookups. */ + private readonly loaded = new Map<string, CampaignData>(); private readonly roomsCache = new Map<string, Promise<Record<string, Record<string, unknown>[]> | null>>(); constructor(private readonly base = '/data') {} @@ -135,22 +212,66 @@ export class GameDataClient { if (!r.ok) throw new Error(`failed to load campaign ${campaign}`); return r.json(); }) - .then((raw: RawEdition) => ({ - edition: raw.edition, - classes: raw.classes.map((c) => this.toClass(c)), - monsters: Object.entries(raw.monsters).map(([name, m]) => - this.toMonster(name, m, raw.edition) - ), - scenarios: Object.entries(raw.scenarios).map(([name, s]) => ({ - name, - monsters: s.monsters ?? [] - })) - })); + .then((raw: RawEdition) => { + const abilityDecks: Record<string, MonsterAbilityCard[]> = {}; + for (const deck of raw.monsterAbilities ?? []) { + abilityDecks[deck.name] = deck.cards.map((c) => + parseAbilityCard(c as unknown[], deck.name) + ); + } + const data: CampaignData = { + edition: raw.edition, + classes: raw.classes.map((c) => this.toClass(c)), + monsters: Object.entries(raw.monsters).map(([name, m]) => + this.toMonster(name, m, raw.edition) + ), + scenarios: Object.entries(raw.scenarios).map(([name, s]) => ({ + name, + monsters: s.monsters ?? [] + })), + abilityDecks + }; + this.loaded.set(campaign, data); + return data; + }); this.cache.set(campaign, promise); return promise; } /** + * The monster model for [name] in [campaign] (by name or display name), + * or null when the campaign data is not yet loaded. Used to resolve a + * state monster (keyed by model name) to its stats, deck, and art. + */ + findMonster(campaign: string, name: string): MonsterData | null { + const data = this.loaded.get(campaign); + if (!data) return null; + return ( + data.monsters.find((m) => m.name === name || m.display === name) ?? null + ); + } + + /** The ability cards for a monster-ability [deck] in [campaign]. */ + abilityCards(campaign: string, deck: string): MonsterAbilityCard[] { + return this.loaded.get(campaign)?.abilityDecks[deck] ?? []; + } + + /** + * The full ability card for a state reference `{deck, nr}` in [campaign] — + * the client reconstructs it from the edition data (state carries only the + * reference). Null when the campaign is not loaded or the card is unknown. + */ + abilityCard( + campaign: string, + deck: string, + nr: number + ): MonsterAbilityCard | null { + const cards = this.loaded.get(campaign)?.abilityDecks[deck]; + if (!cards) return null; + return cards.find((c) => c.nr === nr) ?? null; + } + + /** * The addable section names for a scenario, mirroring the server's * `ScenarioModel`: the rooms file is keyed by the scenario number (the * `#` and name stripped — `"#0 Howling in the Snow"` → `"0"`); the first @@ -190,6 +311,14 @@ export class GameDataClient { private toClass(c: RawClass): ClassData { const name = c.name; + const toDetails = ( + perks: RawClass['perks'] + ): PerkDetail[] => + (perks ?? []).map((p) => ({ + text: p.text ?? '', + adds: p.adds ?? [], + removes: p.removes ?? [] + })); return { id: name, name, @@ -198,10 +327,27 @@ export class GameDataClient { health: c.health, color: firstColor(c.color), colorSecondary: firstColor(c.colorSecondary, firstColor(c.color)), - icon: `/images/class-icons/${escapePathSegment(name)}.png` + icon: `/images/class-icons/${escapePathSegment(name)}.png`, + perks: (c.perks ?? []).map((p) => p.adds ?? []), + perksFH: (c.perksFH ?? []).map((p) => p.adds ?? []), + perkDetails: toDetails(c.perks), + perkDetailsFH: toDetails(c.perksFH) }; } + /** + * The character class for [name] in [campaign] (by name or display + * name), or null when the campaign data is not yet loaded. Used by the + * modifier-card gfx resolver for perk (`P<n>`) graphics. + */ + classByName(campaign: string, name: string): ClassData | null { + const data = this.loaded.get(campaign); + if (!data) return null; + return ( + data.classes.find((c) => c.name === name || c.id === name) ?? null + ); + } + private toMonster(name: string, m: RawMonster, edition: string): MonsterData { const display = m.display ?? name; const gfx = m.gfx ?? display; |
