/** * Loads the campaign/character/monster/scenario data the menus read, from * the same JSON files the engine uses (served at `/data/` by the Dart * server). The reference app ships these as bundled assets; here the * server is the single source and the client fetches on demand and caches. */ // --- Raw JSON shapes (the subset the UI needs) ---------------------------- interface RawClass { name: string; edition: string; health: number[]; hidden?: boolean; color: string | string[]; colorSecondary?: string | string[]; } interface RawMonsterStats { health: number | string; [ key: string ]: unknown; } interface RawMonsterLevel { level: number; normal?: RawMonsterStats; elite?: RawMonsterStats; boss?: RawMonsterStats; } interface RawMonster { deck: string; count: number; levels: RawMonsterLevel[]; display?: string; gfx?: string; hidden?: boolean; flying?: boolean; } interface RawScenario { monsters?: string[]; lootDeck?: Record; special?: unknown[]; initialMessage?: string; } interface RawEdition { edition: string; classes: RawClass[]; monsters: Record; scenarios: Record; } // --- UI-facing types ------------------------------------------------------- export interface ClassData { id: string; name: string; edition: string; hidden: boolean; health: number[]; color: string; colorSecondary: string; /** `/images/class-icons/.png` */ icon: string; } export interface MonsterData { name: string; display: string; gfx: string; deck: string; count: number; hidden: boolean; flying: boolean; levels: RawMonsterLevel[]; maxLevel: number; /** The first level has a boss block. */ isBoss: boolean; edition: string; /** `/images/monsters/.png` */ portrait: string; } export interface ScenarioData { name: string; monsters: string[]; } export interface CampaignData { edition: string; classes: ClassData[]; monsters: MonsterData[]; /** Scenario names in file order. */ scenarios: ScenarioData[]; } export function firstColor(value: string | string[] | undefined, fallback = '000000') { const v = Array.isArray(value) ? value[0] : value; return v ?? fallback; } function escapePathSegment(value: string): string { return encodeURIComponent(value); } // --- The loader ------------------------------------------------------------ export class GameDataClient { private campaignsPromise: Promise | null = null; private campaignsCache: string[] | null = null; private readonly cache = new Map>(); private readonly roomsCache = new Map[]> | null>>(); constructor(private readonly base = '/data') {} /** The list of campaign/edition names (order from `editions.json`). */ campaigns(): Promise { if (this.campaignsCache) return Promise.resolve(this.campaignsCache); if (this.campaignsPromise) return this.campaignsPromise; this.campaignsPromise = fetch(`${this.base}/editions/editions.json`) .then((r) => r.json()) .then((d: { editions: string[] }) => { this.campaignsCache = d.editions; return d.editions; }); return this.campaignsPromise; } /** Character classes, monsters, and scenario names for a campaign. */ loadCampaign(campaign: string): Promise { const cached = this.cache.get(campaign); if (cached) return cached; const promise = fetch( `${this.base}/editions/${escapePathSegment(campaign)}.json` ) .then((r) => { 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 ?? [] })) })); this.cache.set(campaign, promise); return promise; } /** * 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 * room is the scenario start (skipped) and each remaining room's name is * an addable section. Names containing "spawn" are excluded. */ sections(campaign: string, scenario: string): Promise { const key = `${campaign}::${scenario}`; const cached = this.roomsCache.get(campaign); const roomsPromise = cached ?? fetch(`${this.base}/rooms/${escapePathSegment(campaign)}.json`) .then((r) => (r.ok ? r.json() : null)) .then( (rooms: Record[]> | null) => { this.roomsCache.set(campaign, Promise.resolve(rooms)); return rooms; } ); const promise = roomsPromise.then((rooms) => { if (!rooms) return []; // The rooms key is the scenario number after `#` (first token): // `"#1 A Town in Flames"` → `"1"`, `"#4A …"` → `"4A"`. const nr = scenario.startsWith('#') ? scenario.slice(1).split(/\s/)[0] : scenario; const entry = rooms[nr] ?? rooms[scenario] ?? null; if (!Array.isArray(entry)) return []; return entry .slice(1) // skip the scenario start room .map((room) => Object.keys(room)[0] ?? '') .filter((name) => name && !name.toLowerCase().includes('spawn')); }); this.roomsCache.set(campaign, roomsPromise); void key; // (cache key kept for a possible per-scenario variant) return promise; } private toClass(c: RawClass): ClassData { const name = c.name; return { id: name, name, edition: c.edition, hidden: c.hidden ?? false, health: c.health, color: firstColor(c.color), colorSecondary: firstColor(c.colorSecondary, firstColor(c.color)), icon: `/images/class-icons/${escapePathSegment(name)}.png` }; } private toMonster(name: string, m: RawMonster, edition: string): MonsterData { const display = m.display ?? name; const gfx = m.gfx ?? display; const levels = m.levels ?? []; const maxLevel = levels.reduce( (max, l) => Math.max(max, l.level), levels.length ? 0 : 1 ); return { name, display, gfx, deck: m.deck, count: m.count, hidden: m.hidden ?? false, flying: m.flying ?? false, levels, maxLevel, isBoss: levels.some((l) => l.boss != null), edition, portrait: `/images/monsters/${escapePathSegment(gfx)}.png` }; } } /** The default shared client. */ export const gameData = new GameDataClient();