From 4f50f2008f4d7c2a635f7f26861fcfc7f7504423 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Sun, 15 Nov 2026 13:45:21 +0100 Subject: Phase 3: web UI core loop (M3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Browser game UI on top of the Phase 2 protocol: server-driven, input-only clients with per-connection secret visibility. Web (Svelte 5): - Layout: TopBar (element buttons with use/imbue long-press, title, menu), MainList (character/monster/note rows), BottomBar (draw/next-round + level widget with T/H/X/C values). - CharacterRow with the secret initiative input: own value editable, server-masked values render as ??, revealed on draw. - Per-figure StatusMenu: health/max/xp/bless/curse/empower/enfeeble steppers, condition grid, turn-done — all server commands, player-gated. - Menu dialogs: select scenario, add section, add/remove character, set level/difficulty/solo/auto-level, add/remove monster, note row, action log with rollback, settings (style theme + language). - Player vs spectator UI gated on the live seat table; reconnecting banner, error toasts, connection status plumbing. - i18n: English ported, locale store with localStorage persistence. - Design tokens + the three style themes (frosthaven/gloomhaven/original). - Unit tests: level values, game logic (elements/conditions/art/stats), i18n, the reactive player store, the data client (sections keyed by scenario number), plus the existing connection/protocol/state tests. Server: - Serve the engine's data JSON at /data/ and artwork at /images/ so the client menus and rows read the same assets the engine loads. --- web/src/lib/game/art.ts | 27 ++++++++++++++ web/src/lib/game/conditions.ts | 57 +++++++++++++++++++++++++++++ web/src/lib/game/elements.ts | 29 +++++++++++++++ web/src/lib/game/levelValues.ts | 45 +++++++++++++++++++++++ web/src/lib/game/stats.ts | 81 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 239 insertions(+) create mode 100644 web/src/lib/game/art.ts create mode 100644 web/src/lib/game/conditions.ts create mode 100644 web/src/lib/game/elements.ts create mode 100644 web/src/lib/game/levelValues.ts create mode 100644 web/src/lib/game/stats.ts (limited to 'web/src/lib/game') diff --git a/web/src/lib/game/art.ts b/web/src/lib/game/art.ts new file mode 100644 index 0000000..4be6637 --- /dev/null +++ b/web/src/lib/game/art.ts @@ -0,0 +1,27 @@ +/** + * Artwork helpers. The Dart server serves the engine's image assets under + * `/images/` (mirroring the reference app's `assets/images/`). + */ + +/** Character-class icon (the reference row art). */ +export function classIcon(className: string): string { + return `/images/class-icons/${className}.png`; +} + +/** Monster / summon artwork. */ +export function monsterImage(gfx: string): string { + return `/images/monsters/${gfx}.png`; +} + +/** + * A character-class background colour. The data stores a Flutter ARGB hex + * string (e.g. `"ffa28b7c"`); CSS wants `#rrggbb` with the alpha dropped + * (the reference uses it at full opacity as the row backdrop). + */ +export function classColor(argb: string): string { + if (/^[0-9a-f]{8}$/i.test(argb)) { + return `#${argb.slice(2)}`; + } + if (/^#[0-9a-f]{6}$/i.test(argb)) return argb; + return '#3a3f47'; +} diff --git a/web/src/lib/game/conditions.ts b/web/src/lib/game/conditions.ts new file mode 100644 index 0000000..b7579ac --- /dev/null +++ b/web/src/lib/game/conditions.ts @@ -0,0 +1,57 @@ +/** + * The `Condition` enum (server `lib/core/enums.dart`). The state serializes + * conditions as their index; the wire commands `addCondition` / + * `removeCondition` take the enum *name*. + */ + +export const CONDITIONS = [ + { name: 'stun', label: 'Stunned' }, + { name: 'immobilize', label: 'Immobilized' }, + { name: 'disarm', label: 'Disarmed' }, + { name: 'wound', label: 'Wounded' }, + { name: 'wound2', label: 'Wounded x2' }, + { name: 'muddle', label: 'Muddled' }, + { name: 'poison', label: 'Poisoned' }, + { name: 'poison2', label: 'Poisoned x2' }, + { name: 'poison3', label: 'Poisoned x3' }, + { name: 'poison4', label: 'Poisoned x4' }, + { name: 'bane', label: 'Bane' }, + { name: 'brittle', label: 'Brittle' }, + { name: 'chill', label: 'Chilled' }, + { name: 'infect', label: 'Infected' }, + { name: 'impair', label: 'Impaired' }, + { name: 'rupture', label: 'Ruptured' }, + { name: 'plague', label: 'Plagued' }, + { name: 'strengthen', label: 'Strengthened' }, + { name: 'invisible', label: 'Invisible' }, + { name: 'regenerate', label: 'Regenerating' }, + { name: 'ward', label: 'Warded' }, + { name: 'dodge', label: 'Dodging' }, + { name: 'safeguard', label: 'Safeguard' }, + { name: 'shield', label: 'Shielded' }, + { name: 'retaliate', label: 'Retaliating' }, + // character1..4 are custom per-character slots; they are managed by the + // game data, not the condition menu, so they are excluded here. +] as const; + +export type ConditionName = (typeof CONDITIONS)[number]['name']; + +/** Human label for a condition index (falling back to the index). */ +export function conditionLabel(index: number): string { + const c = CONDITIONS[index]; + return c ? c.label : `condition ${index}`; +} + +/** Wire name for a condition index (falling back to the index). */ +export function conditionName(index: number): string { + const c = CONDITIONS[index]; + return c ? c.name : `condition${index}`; +} + +/** True when [index] is chill or plague (rendered with a coloured chip). */ +export function conditionChipClass(index: number): string { + const name = CONDITIONS[index]?.name; + if (name === 'chill') return 'cond cond--chill'; + if (name === 'plague') return 'cond cond--plague'; + return 'cond'; +} diff --git a/web/src/lib/game/elements.ts b/web/src/lib/game/elements.ts new file mode 100644 index 0000000..61ecde3 --- /dev/null +++ b/web/src/lib/game/elements.ts @@ -0,0 +1,29 @@ +/** + * The six elemental abilities. The indices match the server's `Elements` + * enum (the state serializes `elementState` as element-index → + * state-index), and the wire commands `useElement` / `imbueElement` take + * the enum *name*. + */ + +export const ELEMENTS = [ + { name: 'fire', label: 'Fire', color: 'var(--el-fire)', icon: '/images/psd/element-fire.png' }, + { name: 'ice', label: 'Ice', color: 'var(--el-ice)', icon: '/images/psd/element-ice.png' }, + { name: 'air', label: 'Air', color: 'var(--el-air)', icon: '/images/psd/element-air.png' }, + { name: 'earth', label: 'Earth', color: 'var(--el-earth)', icon: '/images/psd/element-earth.png' }, + { name: 'light', label: 'Light', color: 'var(--el-light)', icon: '/images/psd/element-light.png' }, + { name: 'dark', label: 'Dark', color: 'var(--el-dark)', icon: '/images/psd/element-dark.png' }, +] as const; + +/** ElementState index: 0 full, 1 half, 2 inert. */ +export type ElementState = 0 | 1 | 2; + +/** + * The element's fill state from the game state (absent → inert), matching + * the reference `ElementButton`: a tap uses the element when it is active, + * imbues it (full) when inert; a long press imbues the half (advanced) + * amount. + */ +export function elementState(state: Record, index: number): ElementState { + const value = state[String(index)]; + return value === 0 || value === 1 || value === 2 ? value : 2; +} diff --git a/web/src/lib/game/levelValues.ts b/web/src/lib/game/levelValues.ts new file mode 100644 index 0000000..01b70a0 --- /dev/null +++ b/web/src/lib/game/levelValues.ts @@ -0,0 +1,45 @@ +/** + Scenario level values shown by the level widget (a port of the reference + `GameMethods.getTrapValue/getHazardValue/getXPValue/getCoinValue`). + */ + +export function trapValue(level: number): number { + return 2 + level; +} + +/** + The reference halves the trap value for OGGloom games unless the + "FH hazard/terrain calc in OG Gloomhaven" setting is on. + */ +export function hazardValue( + level: number, + isOgGloom: boolean, + fhHazTerrainCalcInOGGloom: boolean +): number { + if (isOgGloom && !fhHazTerrainCalcInOGGloom) { + return Math.floor(trapValue(level) / 2); + } + return 1 + Math.ceil(level / 3); +} + +export function xpValue(level: number): number { + return 4 + 2 * level; +} + +export function coinValue(level: number): number { + if (level === 7) return 6; + return 2 + Math.floor(level / 2); +} + +/** The reference `GameMethods.getRecommendedLevel`. */ +export function recommendedLevel( + characterLevels: number[], + solo: boolean +): number { + if (characterLevels.length === 0) return 1; + const total = characterLevels.reduce((a, b) => a + b, 0); + if (solo) { + return Math.ceil((total / characterLevels.length + 1) / 2); + } + return Math.ceil(total / characterLevels.length / 2); +} diff --git a/web/src/lib/game/stats.ts b/web/src/lib/game/stats.ts new file mode 100644 index 0000000..d842159 --- /dev/null +++ b/web/src/lib/game/stats.ts @@ -0,0 +1,81 @@ +/** + * Reads current stat values for the status menu from the live state. + * + * Health / max / xp / chill / conditions live on the figure; bless, curse, + * empower and enfeeble live as modifier-deck cards — the server keeps their + * counter in sync with the matching cards in the character's draw pile, so + * counting the cards here mirrors the server's own computation. + */ + +import type { + CharacterItem, + MonsterInstance +} from '$lib/protocol/types'; + +export interface ModifierCard { + gfx: string; +} + +export interface ModifierDeckJson { + drawPile: ModifierCard[]; + discardPile: ModifierCard[]; + removedPile?: ModifierCard[]; + [ key: string ]: unknown; +} + +/** + * The wire `figureId` for a standee (a monster instance or a summon): the + * reference app builds it as `name + gfx + standeeNr`. + */ +export function standeeFigureId(standee: MonsterInstance): string { + return standee.name + standee.gfx + standee.standeeNr.toString(); +} + +/** Count modifier cards matching [predicate] in the draw pile. */ +function countCards( + deck: ModifierDeckJson | undefined, + predicate: (gfx: string) => boolean +): number { + if (!deck?.drawPile) return 0; + return deck.drawPile.filter((c) => c && predicate(c.gfx)).length; +} + +/** + * The stat counters for a character, keyed by the modifier-deck removable + * name (which is also the `gfx` wire arg for empower/enfeeble). + */ +export function characterStatCounters( + character: CharacterItem +): { + bless: number; + curse: number; + empower: number; + enfeeble: number; +} { + const deck = character.characterState.modifierDeck as + | ModifierDeckJson + | undefined; + return { + bless: countCards(deck, (g) => g === 'bless'), + curse: countCards(deck, (g) => g === 'curse'), + empower: countCards(deck, (g) => g.includes('empower')), + enfeeble: countCards(deck, (g) => g.includes('enfeeble')) + }; +} + +/** The stat counters for a standee (its owner's modifier deck). */ +export function standeeStatCounters( + deck: ModifierDeckJson | undefined +): { + bless: number; + curse: number; + empower: number; + enfeeble: number; +} { + return { + bless: countCards(deck, (g) => g === 'bless'), + curse: countCards(deck, (g) => g === 'curse'), + empower: countCards(deck, (g) => g.includes('empower')), + enfeeble: countCards(deck, (g) => g.includes('enfeeble')) + }; +} -- cgit v1.3.1