1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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}
|