diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2026-11-16 22:19:39 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2026-11-16 22:19:39 +0100 |
| commit | 94e2850fb73005f3fb30af8489905e5cdfb6fac5 (patch) | |
| tree | 1aea8e7e6683af770f0c57c0fb654003778c9f16 /web/src/lib/data | |
| parent | 4f50f2008f4d7c2a635f7f26861fcfc7f7504423 (diff) | |
Phase 4: cards and decks (M4)
Full card play in the browser: ability-line rendering, monster ability
decks, the full modifier-deck (AMD) suite, and the loot deck — all
server commands, faithful to the reference flows and layout.
Web (Svelte 5):
- Line renderer + in-game translations: $lib/game/line/ renders the
reference line_builder output (emphasis, range markers, element
styling); gameI18n.ts loads the /i18n/ in-game ability-text tables
(the client-side TranslationService equivalent)
- Card components: stat cards, monster ability cards (OG + FH styles,
rendered lines, element/flying tokens), modifier cards (gfx resolver
incl. P<n> perk graphics), loot cards (party-size value rules,
owner, enhancements)
- Monster ability decks: AbilityCardSlot in each monster row (front of
the current card while active in playTurns, else the back with the
draw-pile count); AbilityCardsMenu (reveal top cards, draw extra,
extra shuffle, activate/inactivate, drag-reorder, remove); card zoom
- Modifier decks: ModifierDeckWidget (tap to draw, face-up revealed top
card, empty-box class icon for the current character's own deck,
discard stack); ModifierDeckHeader (bad omen, corrosive spew,
±1/−2/+0, imbue 1/2, reveal with the Cassandra 8-button cap,
save/don't-save revealed cards, removed count, Hail perk, CS
sanctuary/party cards, faction icons, bless/curse and the
empower/enfeeble counters); ModifierDeckMenu (draw-pile
drag-reorder + send-to-bottom/shuffle/remove, discard
remove/return-top, removed pile with return, faction grid with
gray-out, perks menu with the Frosthaven opt-in); card zoom
- Loot deck: LootDeckWidget (player-gated draw, current-character
icon, discard stack, hidden when both piles empty); LootCardsMenu
(set owner, per-character loot totals, add card, enhance with the
reference material/herb/coin splits, special cards 1418/1419,
return top/bottom)
- Bottom DeckArea: loot deck left; the Character Decks toggle with the
reference open-state cycling (oneOpen/allOpen/noOpen per round
state; one-deck view for the current character during playTurns);
ally deck behind shouldShowAlliesDeck; global deck
- Pure helper modules ported from the reference: rules.ts (FH card
styling, inactive-for-rule, ally-deck gate, deck resolution),
modifierDeck.ts (pile derivations, faction cards), perks.ts (active
perk set, card-text tokens, descriptions, add/remove gating),
cards/lootCard.ts (values + per-character totals)
- App wiring: campaign data loaded on currentCampaign change (guarded
against out-of-order fetches); ability/AMD/loot menus and zooms
driven by live-derived state so open menus track pile changes;
Escape closes every dialog
Server:
- /i18n/ static mount: the in-game ability-text translation files the
card renderer resolves (the reference TranslationService data)
- Loot deck JSON gains pools (pool card ids per pool — the client
derives card gfx from pool key + index and reads the enhancements
map; ids only, since pool cards are the same live instances that
mutate in the piles)
- Modifier deck JSON gains removables (the counter totals the header
reads) plus load-time round-trip for saves written before the field
existed
Docs:
- PLAN.md: Phase 3 and Phase 4 marked complete (corrected the
'character ability decks' mislabel — ability decks are monster-only),
progress log entries for M3 and M4
svelte-check 0 errors; web tests 177/177; build OK; dart analyze
0 errors; dart test 592/592; scripts/ci.sh green.
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; |
