diff options
Diffstat (limited to 'web/src/lib/ui/CharacterRow.svelte')
| -rw-r--r-- | web/src/lib/ui/CharacterRow.svelte | 195 |
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} |
