From 94e2850fb73005f3fb30af8489905e5cdfb6fac5 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Mon, 16 Nov 2026 22:19:39 +0100 Subject: Phase 4: cards and decks (M4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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. --- web/src/lib/game/line/frosthavenConverter.ts | 199 +++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 web/src/lib/game/line/frosthavenConverter.ts (limited to 'web/src/lib/game/line/frosthavenConverter.ts') diff --git a/web/src/lib/game/line/frosthavenConverter.ts b/web/src/lib/game/line/frosthavenConverter.ts new file mode 100644 index 0000000..534efd9 --- /dev/null +++ b/web/src/lib/game/line/frosthavenConverter.ts @@ -0,0 +1,199 @@ +/** + * Frosthaven-style line conversion (client port of the reference + * `FrosthavenConverter`, PORTING.md §8b). Pure string → string: expands + * embedded newlines, rewrites keywords into token/line markers, and tracks the + * sub-line / conditional / element-use state so the line parser can lay lines + * out as the reference does (sub-lines boxed, element-use blocks, …). + */ + +const LOOKBACK = 2; + +/** Split lines containing `\n`, propagating the `^` / `^^` prefix. */ +export function expandEmbeddedNewlines(lines: string[]): string[] { + const result: string[] = []; + for (const line of lines) { + if (!line.includes('\n')) { + result.push(line); + continue; + } + const parts = line.split('\n'); + result.push(parts[0]); + const outerPrefix = parts[0].startsWith('^^') + ? '^^' + : parts[0].startsWith('^') + ? '^' + : ''; + for (let i = 1; i < parts.length; i++) result.push(outerPrefix + parts[i]); + } + return result; +} + +/** The keyword → token/line-marker rewrite + sub-line state machine. */ +export function convertLinesToFH(lines: string[], applyStats: boolean): string[] { + const retVal: string[] = []; + let isSubLine = false; + let isReallySubLine = false; + let isConditional = false; + let isElementUse = false; + + for (let i = 0; i < lines.length; i++) { + let startOfConditional = false; + let line = lines[i]; + + if (line === '[newLine]') line = ''; + + if ( + (line === '[r]' || line === '[s]') && + lines[i + 1]?.includes('%use') + ) { + isElementUse = true; + isConditional = true; + startOfConditional = true; + } + if ((line === '[/r]' || line === '[/s]') && isConditional) { + isConditional = false; + isElementUse = false; + } + + if (i > 0 && lines[i - 1].includes('%use')) startOfConditional = true; + if ( + i > 1 && + lines[i - LOOKBACK].includes('%use') && + (lines[i - 1] === '[r]' || lines[i - 1] === '[s]' || lines[i - 1] === '[c]') + ) { + startOfConditional = true; + } + + line = line.replaceAll('Affect', 'Target'); + line = line.replaceAll('damage', '%damage%'); + line = line.replaceAll('%damage%d', 'damaged'); + if (!applyStats) { + line = line.replaceAll(' - ', '-'); + line = line.replaceAll(' + ', '+'); + } + line = line.replaceAll('% ', '%'); + + if (line.startsWith('*')) { + if (isReallySubLine) retVal.push('[subLineEnd]'); + isReallySubLine = false; + isSubLine = false; + isConditional = false; + isElementUse = false; + } + + const isSubLineCandidate = + line.startsWith('^') && + (line[1] === '%' || + line.startsWith('^Self') || + line.startsWith('^-') || + line.startsWith('^+') || + line.startsWith('^Advantage') || + line.startsWith('^Target') || + line.startsWith('^%target%') || + line.startsWith('^Normal') || + line.startsWith('^all') || + (line.startsWith('^All') && + !line.startsWith('^All attacks') && + !line.startsWith('^All targets'))); + + if (line.startsWith('^') && isSubLine && !startOfConditional) { + if (isSubLineCandidate) { + if ( + !isReallySubLine && + (!isConditional || + (isElementUse && + !line.startsWith('^Perform') && + !line.startsWith('^^for each'))) + ) { + retVal.push('[subLineStart]'); + isReallySubLine = true; + } + + if ( + isElementUse && + (lines[i - 1]?.includes('use') || + (lines[i - LOOKBACK]?.includes('use') && + lines[i - 1]?.includes('[c]'))) && + !line.startsWith('^Target') && + !line.startsWith('^all') && + !line.startsWith('^All') && + !line.includes('instead') + ) { + line = line.substring(1); + if (retVal[retVal.length - 1] === '[subLineStart]') { + retVal.pop(); + } + isReallySubLine = false; + } + line = '!' + line; + line = line.replace(/Self/, 'self').replace(/All/, 'all'); + + if (retVal[retVal.length - 1] === '[subLineStart]') { + retVal[retVal.length - 1] = '![subLineStart]'; + } + } else { + isSubLine = false; + } + } + + if (line.startsWith('^') && isReallySubLine) { + if (retVal[retVal.length - 1] !== '[subLineStart]') { + retVal.push('!^[lineBreak]'); + } + line = '!' + line; + } else if ( + !line.startsWith('!') && + !line.startsWith('*') && + !line.startsWith('^') + ) { + if (!isSubLine && !line.includes('%use%')) { + isSubLine = true; + } else if (isReallySubLine) { + retVal.push('[subLineEnd]'); + isReallySubLine = false; + isSubLine = false; + } + } + + // Use iconography instead of words (applied to every line, last). + line = line.replaceAll('Target', '%target%'); + retVal.push(line); + } + + if (isReallySubLine && (!isConditional || isElementUse)) { + retVal.push('[subLineEnd]'); + } + return retVal; +} + +/** Whether the token's graphic should overflow its line (FH condition icons). */ +export function shouldOverflow(frosthavenStyle: boolean, iconToken: string): boolean { + if (!frosthavenStyle) return false; + const exact = [ + 'pierce', + 'brittle', + 'curse', + 'enfeeble', + 'bless', + 'invisible', + 'strengthen', + 'bane', + 'push', + 'pull', + 'poison', + 'wound', + 'infect', + 'chill', + 'disarm', + 'immobilize', + 'stun', + 'impair', + 'safeguard', + 'muddle' + ]; + return ( + exact.includes(iconToken) || + iconToken.includes('poison') || + iconToken.includes('wound') + ); +} -- cgit v1.3.1