summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/CharacterRow.svelte
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-15 13:45:21 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-15 13:45:21 +0100
commit4f50f2008f4d7c2a635f7f26861fcfc7f7504423 (patch)
tree94e6286e7e22e0467a61b99c18b9ab582fbbe656 /web/src/lib/ui/CharacterRow.svelte
parent3ebbdf6ffa16f751c82e3bd2e74294f4fe843155 (diff)
Phase 3: web UI core loop (M3)
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.
Diffstat (limited to 'web/src/lib/ui/CharacterRow.svelte')
-rw-r--r--web/src/lib/ui/CharacterRow.svelte195
1 files changed, 195 insertions, 0 deletions
diff --git a/web/src/lib/ui/CharacterRow.svelte b/web/src/lib/ui/CharacterRow.svelte
new file mode 100644
index 0000000..f4fdd76
--- /dev/null
+++ b/web/src/lib/ui/CharacterRow.svelte
@@ -0,0 +1,195 @@
+<!--
+ A character row (the reference `CharacterWidget`): class-coloured
+ backdrop, class art, the initiative slot, the name and health line with
+ condition chips, and the XP / level on the right.
+
+ Initiative (the server-gated secret, PLAN.md section 3.4):
+ - during chooseInitiative the owning player sees an editable field;
+ every other connection sees `??` (the server masks the value to
+ `null` for them)
+ - a value set by the owner is visible to the owner; after the draw the
+ number stays visible to everyone
+ The right-hand tap zone focuses the initiative field (chooseInitiative)
+ or ends the character's turn (playTurns), like the reference; tapping
+ the row opens the stat menu.
+-->
+<script lang="ts">
+ import type { GameSession } from '$lib/state/session';
+ import type { CharacterItem } from '$lib/protocol/types';
+ import { classInfo, isObjectiveOrEscort, type ClassData } from '$lib/data/classInfo';
+ import { classColor, classIcon } from '$lib/game/art';
+ import { conditionChipClass, conditionLabel } from '$lib/game/conditions';
+ import type { FigureRef } from './StatusMenu.svelte';
+
+ let {
+ session,
+ item,
+ onOpenMenu
+ }: {
+ session: GameSession;
+ item: CharacterItem;
+ onOpenMenu: (figure: FigureRef) => void;
+ } = $props();
+
+ const seats = session.seats;
+ const player = session.player;
+ const gameState = session.state;
+ const mySession = session.session; // my own {id, role} (store)
+
+ let cls = $state<ClassData | null>(null);
+ $effect(() => {
+ let alive = true;
+ classInfo(item.edition, item.characterClass)
+ .then((c) => {
+ if (alive) cls = c;
+ })
+ .catch(() => {});
+ return () => {
+ alive = false;
+ };
+ });
+
+ const cs = $derived(item.characterState);
+ const alive = $derived(cs.health > 0);
+ const roundState = $derived($gameState?.roundState ?? 0);
+ const turnState = $derived(item.turnState);
+ const isObjective = $derived(isObjectiveOrEscort(item.characterClass));
+
+ const owner = $derived($seats[item.id] ?? null);
+ const mine = $derived(!!$player && owner != null && owner === $mySession?.id);
+ const unclaimed = $derived(owner === null);
+
+ const currentTurn = $derived(roundState === 1 && turnState === 1);
+ const done = $derived(roundState === 1 && turnState === 2);
+
+ const canEnterInit = $derived(
+ roundState === 0 && alive && !isObjective && mine && $gameState != null
+ );
+
+ let initInput = $state<HTMLInputElement | null>(null);
+ let initDraft = $state('');
+
+ // `??` for a value masked by the server; the revealed number otherwise.
+ const initDisplay = $derived(
+ cs.initiative === null ? '??' : cs.initiative > 0 ? String(cs.initiative) : ''
+ );
+ const showInitNumber = $derived(
+ roundState === 0 ? alive && !canEnterInit : alive
+ );
+
+ function onInitInput() {
+ const value = initDraft.trim();
+ if (
+ /^\d{1,2}$/.test(value) &&
+ value !== '0' &&
+ cs.initiative !== Number(value)
+ ) {
+ session.command('setInit', { characterId: item.id, init: Number(value) });
+ }
+ }
+
+ function onTurnTap() {
+ if (roundState === 0) {
+ initInput?.focus();
+ return;
+ }
+ if (roundState === 1 && alive && $player) {
+ session.command('turnDone', { id: item.id });
+ }
+ }
+
+ const figure: FigureRef = $derived({
+ title: cs.display,
+ figureId: item.id,
+ ownerId: null,
+ kind: 'character',
+ rowId: item.id,
+ isObjective
+ });
+</script>
+
+<div
+ class="row row--character"
+ class:row--current={currentTurn}
+ class:row--done={done}
+ class:row--dead={!alive}
+>
+ <div
+ class="row__bg"
+ style:background-color={cls ? classColor(cls.color) : '#3a3f47'}
+ ></div>
+
+ <div class="row__inner">
+ <div
+ class="row__art"
+ role="button"
+ tabindex="0"
+ onclick={() => onOpenMenu(figure)}
+ onkeydown={(e) => {
+ if (e.key === 'Enter' || e.key === ' ') onOpenMenu(figure);
+ }}
+ >
+ <img src={cls ? classIcon(cls.name) : ''} alt={cs.display} loading="lazy" />
+ </div>
+
+ {#if canEnterInit}
+ <input
+ class="init__input"
+ bind:this={initInput}
+ bind:value={initDraft}
+ oninput={onInitInput}
+ onclick={() => (initDraft = '')}
+ maxlength={2}
+ inputmode="numeric"
+ autocomplete="off"
+ aria-label="Initiative"
+ />
+ {:else if showInitNumber}
+ <div class="init" class:init__secret={cs.initiative === null}>{initDisplay}</div>
+ {/if}
+
+ <div
+ style="overflow: hidden; min-width: 0; flex: 1"
+ role="button"
+ tabindex="0"
+ onclick={() => onOpenMenu(figure)}
+ onkeydown={(e) => {
+ if (e.key === 'Enter' || e.key === ' ') onOpenMenu(figure);
+ }}
+ >
+ <div class="row__name">{cs.display}</div>
+ <div class="row__hp">
+ {cs.health} / {cs.maxHealth}
+ <span class="conds">
+ {#each cs.conditions as condIndex}
+ <span class={conditionChipClass(condIndex)}>{conditionLabel(condIndex)}</span>
+ {/each}
+ </span>
+ </div>
+ </div>
+
+ {#if !isObjective}
+ <span class="row__xp">XP {cs.xp}</span>
+ {/if}
+ <span class="row__level">{cs.level}</span>
+ {#if cs.summonList.length > 0}
+ <span class="row__summon">⚔ {cs.summonList.length}</span>
+ {/if}
+
+ <!-- Right-hand tap zone: initiative focus / turn done (reference). -->
+ <div
+ style="width: 70px; height: 100%; flex: 0 0 auto"
+ role="button"
+ tabindex="0"
+ aria-label={roundState === 0 ? 'Initiative' : 'Turn done'}
+ onclick={onTurnTap}
+ onkeydown={(e) => {
+ if (e.key === 'Enter' || e.key === ' ') onTurnTap();
+ }}
+ ></div>
+ </div>
+</div>
+
+{#if unclaimed}
+ <button class="chip chip--claim" onclick={() => session.claim(item.id)}>Claim</button>
+{/if}