feat(client): tokens, BitmapText labels, statuses and lists (#71)
- slices/tokens.ts: token upsert, token_gone, hello reset; status names only. - slices/characters.ts + slices/encountered.ts: own characters, encountered list with grey entries; the GM's per-player copies are ignored. - render/tokenBoard.ts (pure): event -> entered/updated/left patches, so one changed token costs one container; seeds from the reducer on mount. - render/modules/tokens.ts: pooled world-coordinate nodes on the tokens layer, one shared unit-circle context per kind, BitmapText from one pre-installed BitmapFont assigned only on a name change, stuck/squeezed badges, canvas dataset instrumentation for the "only changed entities" criterion. - panels/lists.ts + render/modules/lists.ts: #lists under #panels, textContent only, greyed encountered rows; mounted by a render module so no shared bootstrap file changed.
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
// T8 (#71): the lists' reducer seam. Story 31 (my characters with my own values) and story 32 (an
|
||||
// encountered list with greyed entries). `character`/`character_gone` and `encountered` in, the
|
||||
// two lists out; `hello` resets both with every other slice.
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { initialState, reduce } from '../src/reducer.ts'
|
||||
import type { ReducerEvent, RootState } from '../src/reducer.ts'
|
||||
import { parseServerFrame } from '../src/protocol.ts'
|
||||
import { charactersOf, ownCharactersOf } from '../src/slices/characters.ts'
|
||||
import { encounteredOf } from '../src/slices/encountered.ts'
|
||||
|
||||
const frame = (body: Record<string, unknown>): ReducerEvent => {
|
||||
const parsed = parseServerFrame(JSON.stringify(body))
|
||||
if (!parsed) throw new Error(`bad test frame ${JSON.stringify(body)}`)
|
||||
return { type: 'frame', frame: parsed }
|
||||
}
|
||||
|
||||
const hello = (n: number, role: 'gm' | 'player' = 'player', id = 2): ReducerEvent =>
|
||||
frame({ t: 'hello', n, proto: 2, role, me: { id, name: role === 'gm' ? 'GM' : 'Ann' } })
|
||||
|
||||
/** The owner's view has no `owner`/`reveal_all`/`revealed` field (6.1). */
|
||||
const ownerCharacter = (n: number, id: string, overrides: Record<string, unknown> = {}): ReducerEvent =>
|
||||
frame({
|
||||
t: 'character',
|
||||
n,
|
||||
id,
|
||||
descriptive_name: 'Ann',
|
||||
actual_name: 'Ann of the Vale',
|
||||
props: { public: {}, owner: { hp: 12 } },
|
||||
...overrides,
|
||||
})
|
||||
|
||||
const gmCharacter = (n: number, id: string, owner: number, overrides: Record<string, unknown> = {}): ReducerEvent =>
|
||||
frame({
|
||||
t: 'character',
|
||||
n,
|
||||
id,
|
||||
owner,
|
||||
descriptive_name: 'Ann',
|
||||
actual_name: 'Ann of the Vale',
|
||||
reveal_all: false,
|
||||
revealed: [],
|
||||
props: { public: {}, owner: { hp: 12 }, gm: { secret: true } },
|
||||
...overrides,
|
||||
})
|
||||
|
||||
const encountered = (n: number, body: Record<string, unknown>): ReducerEvent =>
|
||||
frame({ t: 'encountered', n, ...body })
|
||||
|
||||
const run = (...events: ReducerEvent[]): RootState =>
|
||||
events.reduce((state, event) => reduce(state, event), initialState())
|
||||
|
||||
describe('my characters (story 31)', () => {
|
||||
it('lists the characters the server sent the owner, with their own values', () => {
|
||||
const state = run({ type: 'connecting' }, hello(1), ownerCharacter(2, 'char1'))
|
||||
expect(charactersOf(state)).toHaveLength(1)
|
||||
expect(ownCharactersOf(state)).toEqual([
|
||||
expect.objectContaining({
|
||||
id: 'char1',
|
||||
descriptiveName: 'Ann',
|
||||
actualName: 'Ann of the Vale',
|
||||
props: { public: {}, owner: { hp: 12 } },
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it('replaces a character on a later frame and drops it on character_gone', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
ownerCharacter(2, 'char1'),
|
||||
ownerCharacter(3, 'char1', { descriptive_name: 'Ann the Bold' }),
|
||||
)
|
||||
expect(ownCharactersOf(state)).toEqual([expect.objectContaining({ descriptiveName: 'Ann the Bold' })])
|
||||
|
||||
const gone = reduce(state, frame({ t: 'character_gone', n: 4, id: 'char1' }))
|
||||
expect(ownCharactersOf(gone)).toEqual([])
|
||||
expect(charactersOf(gone)).toEqual([])
|
||||
})
|
||||
|
||||
it('shows a GM only the characters that GM owns, never the whole cast', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1, 'gm', 1),
|
||||
gmCharacter(2, 'char1', 2),
|
||||
gmCharacter(3, 'char2', 1),
|
||||
)
|
||||
expect(ownCharactersOf(state).map(character => character.id)).toEqual(['char2'])
|
||||
})
|
||||
|
||||
it('clears the list on hello', () => {
|
||||
const state = run({ type: 'connecting' }, hello(1), ownerCharacter(2, 'char1'), hello(3))
|
||||
expect(ownCharactersOf(state)).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('encountered list (story 32)', () => {
|
||||
it('replaces the whole list on full:true and keeps the grey flag', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
encountered(2, {
|
||||
full: true,
|
||||
set: [
|
||||
{ id: 'aaa', name: 'Rat', grey: false },
|
||||
{ id: 'bbb', name: 'Bat', grey: true },
|
||||
],
|
||||
}),
|
||||
)
|
||||
expect(encounteredOf(state)).toEqual([
|
||||
{ id: 'aaa', name: 'Rat', grey: false },
|
||||
{ id: 'bbb', name: 'Bat', grey: true },
|
||||
])
|
||||
})
|
||||
|
||||
it('applies a diff: set upserts and del removes, without touching anything else', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
encountered(2, {
|
||||
full: true,
|
||||
set: [
|
||||
{ id: 'aaa', name: 'Rat', grey: false },
|
||||
{ id: 'bbb', name: 'Bat', grey: false },
|
||||
],
|
||||
}),
|
||||
encountered(3, { full: false, set: [{ id: 'aaa', name: 'Rat', grey: true }], del: ['bbb'] }),
|
||||
)
|
||||
expect(encounteredOf(state)).toEqual([{ id: 'aaa', name: 'Rat', grey: true }])
|
||||
})
|
||||
|
||||
it('ignores the GM copy, which is one message per player and not the GM own list', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1, 'gm', 1),
|
||||
encountered(2, { full: true, set: [{ id: 'aaa', name: 'Rat', grey: false }], player: 2 }),
|
||||
)
|
||||
expect(encounteredOf(state)).toEqual([])
|
||||
})
|
||||
|
||||
it('clears on hello', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
encountered(2, { full: true, set: [{ id: 'aaa', name: 'Rat', grey: false }] }),
|
||||
hello(3),
|
||||
)
|
||||
expect(encounteredOf(state)).toEqual([])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user