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([])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,177 @@
|
||||
// T8 (#71): the token renderer's pure seam. The board turns store events into the exact set of
|
||||
// entities a renderer has to touch — entered, updated, left — so a frame that moves one token costs
|
||||
// one node update, never a full redraw (DESIGN 7.2, spec #63 decision 11).
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { initialState, reduce } from '../src/reducer.ts'
|
||||
import type { ReducerEvent } from '../src/reducer.ts'
|
||||
import { parseServerFrame } from '../src/protocol.ts'
|
||||
import { TokenBoard } from '../src/render/tokenBoard.ts'
|
||||
import { tokenViewFromFrame } from '../src/slices/tokens.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 = 1): ReducerEvent =>
|
||||
frame({ t: 'hello', n, proto: 2, role: 'player', me: { id: 2, name: 'Ann' } })
|
||||
|
||||
const token = (n: number, id: string, overrides: Record<string, unknown> = {}): ReducerEvent =>
|
||||
frame({
|
||||
t: 'token',
|
||||
n,
|
||||
id,
|
||||
scene: 1,
|
||||
name: 'Rat',
|
||||
pos: [400, 400],
|
||||
radius: 20,
|
||||
mine: false,
|
||||
statuses: [],
|
||||
props: {},
|
||||
...overrides,
|
||||
})
|
||||
|
||||
/** Apply every event, draining after each one the way the renderer does. */
|
||||
function feed(board: TokenBoard, events: ReducerEvent[]) {
|
||||
const patches = []
|
||||
for (const event of events) {
|
||||
board.apply(event)
|
||||
patches.push(board.drain())
|
||||
}
|
||||
return patches
|
||||
}
|
||||
|
||||
describe('the token board (stories 28, 29; DESIGN 7.2)', () => {
|
||||
it('reports one entity entered per new token', () => {
|
||||
const board = new TokenBoard()
|
||||
const patches = feed(board, [hello(), token(2, 'aaa'), token(3, 'bbb')])
|
||||
expect(patches[0]).toMatchObject({ reset: true, entered: [], updated: [], left: [] })
|
||||
expect(patches[1]?.entered.map(view => view.id)).toEqual(['aaa'])
|
||||
expect(patches[1]?.updated).toEqual([])
|
||||
// The second token arrives on its own frame; nothing earlier is re-entered.
|
||||
expect(board.size).toBe(2)
|
||||
})
|
||||
|
||||
it('moves only the changed token, never the whole set (the instrumented count)', () => {
|
||||
const board = new TokenBoard()
|
||||
const events: ReducerEvent[] = [hello()]
|
||||
for (let i = 0; i < 50; i += 1) events.push(token(2 + i, `t${i}`))
|
||||
feed(board, events)
|
||||
expect(board.size).toBe(50)
|
||||
|
||||
board.apply(token(100, 't7', { pos: [420, 400] }))
|
||||
const patch = board.drain()
|
||||
// One token moved -> one entity updated. 49 untouched tokens are not touched again.
|
||||
expect(patch.updated.map(view => view.id)).toEqual(['t7'])
|
||||
expect(patch.entered).toEqual([])
|
||||
expect(patch.left).toEqual([])
|
||||
expect(board.size).toBe(50)
|
||||
})
|
||||
|
||||
it('reports a token leaving once, with no stale entered entry', () => {
|
||||
const board = new TokenBoard()
|
||||
feed(board, [hello(), token(2, 'aaa'), token(3, 'bbb')])
|
||||
board.apply(frame({ t: 'token_gone', n: 4, id: 'aaa' }))
|
||||
const patch = board.drain()
|
||||
expect(patch.left).toEqual(['aaa'])
|
||||
expect(patch.entered).toEqual([])
|
||||
expect(patch.updated).toEqual([])
|
||||
expect(board.size).toBe(1)
|
||||
})
|
||||
|
||||
it('never both enters and leaves one token between drains', () => {
|
||||
const board = new TokenBoard()
|
||||
board.apply(hello())
|
||||
board.drain()
|
||||
board.apply(token(2, 'aaa'))
|
||||
board.apply(frame({ t: 'token_gone', n: 3, id: 'aaa' }))
|
||||
const patch = board.drain()
|
||||
expect(patch.entered).toEqual([])
|
||||
expect(patch.updated).toEqual([])
|
||||
expect(patch.left).toEqual(['aaa'])
|
||||
expect(board.size).toBe(0)
|
||||
})
|
||||
|
||||
it('asks for a reset on hello, and clears anything still pending', () => {
|
||||
const board = new TokenBoard()
|
||||
feed(board, [hello(), token(2, 'aaa')])
|
||||
board.apply(token(3, 'bbb'))
|
||||
board.apply(hello(4))
|
||||
const patch = board.drain()
|
||||
expect(patch.reset).toBe(true)
|
||||
expect(patch.entered).toEqual([])
|
||||
expect(patch.updated).toEqual([])
|
||||
expect(patch.left).toEqual([])
|
||||
expect(board.size).toBe(0)
|
||||
})
|
||||
|
||||
it('ignores a close: the map stays on screen, the resync arrives after the next hello', () => {
|
||||
const board = new TokenBoard()
|
||||
feed(board, [hello(), token(2, 'aaa')])
|
||||
board.apply({ type: 'close', reason: null, code: 1006 })
|
||||
const patch = board.drain()
|
||||
expect(patch).toMatchObject({ reset: false, entered: [], updated: [], left: [] })
|
||||
expect(board.size).toBe(1)
|
||||
})
|
||||
|
||||
it('keeps the view an entity was last given, for the renderer to compare against', () => { const board = new TokenBoard()
|
||||
board.apply(hello())
|
||||
board.apply(token(2, 'aaa', { name: 'Rat' }))
|
||||
expect(board.views.get('aaa')?.name).toBe('Rat')
|
||||
board.apply(token(3, 'aaa', { name: 'Giant Rat' }))
|
||||
expect(board.views.get('aaa')?.name).toBe('Giant Rat')
|
||||
})
|
||||
|
||||
it('seeds the current state when a stage mounts after the resync', () => {
|
||||
const board = new TokenBoard()
|
||||
const view = parseServerFrame(
|
||||
JSON.stringify({
|
||||
t: 'token',
|
||||
n: 1,
|
||||
id: 'aaa',
|
||||
scene: 1,
|
||||
name: 'Rat',
|
||||
pos: [400, 400],
|
||||
radius: 20,
|
||||
mine: false,
|
||||
statuses: [],
|
||||
props: {},
|
||||
}),
|
||||
)
|
||||
if (!view) throw new Error('bad test frame')
|
||||
const parsed = tokenViewFromFrame(view)
|
||||
if (!parsed) throw new Error('bad test token')
|
||||
board.seed([parsed])
|
||||
const patch = board.drain()
|
||||
expect(patch.entered.map(entry => entry.id)).toEqual(['aaa'])
|
||||
expect(patch.reset).toBe(false)
|
||||
expect(board.size).toBe(1)
|
||||
})
|
||||
|
||||
it('is a pure reader of reducer state too: the slice agrees with the board', () => {
|
||||
let state = reduce(initialState(), { type: 'connecting' })
|
||||
const board = new TokenBoard()
|
||||
for (const event of [hello(), token(2, 'aaa'), token(3, 'bbb')]) {
|
||||
state = reduce(state, event)
|
||||
board.apply(event)
|
||||
board.drain()
|
||||
}
|
||||
expect([...board.views.keys()]).toEqual(['aaa', 'bbb'])
|
||||
expect(state.slices['tokens']).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('the token board keeps one pending entry per entity', () => {
|
||||
it('draws a token once when it enters and changes before the drain', () => {
|
||||
const board = new TokenBoard()
|
||||
board.apply(hello())
|
||||
board.drain()
|
||||
board.apply(token(2, 'aaa', { name: 'Rat' }))
|
||||
board.apply(token(3, 'aaa', { name: 'Giant Rat' }))
|
||||
const patch = board.drain()
|
||||
expect(patch.entered.map(view => [view.id, view.name])).toEqual([['aaa', 'Giant Rat']])
|
||||
expect(patch.updated).toEqual([])
|
||||
expect(board.size).toBe(1)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,142 @@
|
||||
// T8 (#71): the token slice is the reducer seam for stories 28-30 and 34. Server frames in, the
|
||||
// token view out; `token` upserts one token, `token_gone` removes it, `hello` resets the view.
|
||||
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 { tokenOf, tokensOf } from '../src/slices/tokens.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): ReducerEvent =>
|
||||
frame({ t: 'hello', n, proto: 2, role: 'player', me: { id: 2, name: 'Ann' } })
|
||||
|
||||
/** A player-shaped token frame: `statuses` are names only, no `source` (6.1 leak note 6). */
|
||||
const playerToken = (n: number, id: string, overrides: Record<string, unknown> = {}): ReducerEvent =>
|
||||
frame({
|
||||
t: 'token',
|
||||
n,
|
||||
id,
|
||||
scene: 1,
|
||||
name: 'Rat',
|
||||
pos: [400, 400],
|
||||
radius: 20,
|
||||
mine: false,
|
||||
statuses: [],
|
||||
props: { public: {}, cprops: { public: {} } },
|
||||
...overrides,
|
||||
})
|
||||
|
||||
const run = (...events: ReducerEvent[]): RootState =>
|
||||
events.reduce((state, event) => reduce(state, event), initialState())
|
||||
|
||||
describe('token enters, updates and leaves (stories 28, 29)', () => {
|
||||
it('enters on a token frame and replaces the whole view on a later one', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
playerToken(2, 'aaa'),
|
||||
playerToken(3, 'bbb'),
|
||||
)
|
||||
expect(tokensOf(state).map(token => token.id)).toEqual(['aaa', 'bbb'])
|
||||
|
||||
// 6.1: every `token` message is the whole view of one token; a later one replaces it in place.
|
||||
const moved = reduce(state, playerToken(4, 'aaa', { pos: [420, 400], radius: 26, name: 'Giant Rat' }))
|
||||
expect(tokensOf(moved)).toHaveLength(2)
|
||||
expect(tokensOf(moved).map(token => token.id)).toEqual(['aaa', 'bbb'])
|
||||
expect(tokenOf(moved, 'aaa')).toMatchObject({ name: 'Giant Rat', pos: [420, 400], radius: 26 })
|
||||
expect(tokenOf(moved, 'bbb')).toMatchObject({ name: 'Rat', pos: [400, 400] })
|
||||
})
|
||||
|
||||
it('removes a token on token_gone, and a later frame re-enters it', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
playerToken(2, 'aaa'),
|
||||
playerToken(3, 'bbb'),
|
||||
frame({ t: 'token_gone', n: 4, id: 'aaa' }),
|
||||
)
|
||||
expect(tokensOf(state).map(token => token.id)).toEqual(['bbb'])
|
||||
expect(tokenOf(state, 'aaa')).toBeNull()
|
||||
|
||||
const back = reduce(state, playerToken(5, 'aaa'))
|
||||
expect(tokensOf(back).map(token => token.id)).toEqual(['bbb', 'aaa'])
|
||||
})
|
||||
|
||||
it('clears every token on hello: a token that left the view must not survive a reconnect', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
playerToken(2, 'aaa'),
|
||||
playerToken(3, 'bbb'),
|
||||
hello(4),
|
||||
)
|
||||
expect(tokensOf(state)).toEqual([])
|
||||
})
|
||||
|
||||
it('keeps the status names and drops the GM-only source (6.1 leak note 6)', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
playerToken(2, 'aaa', { statuses: [{ status: 'stuck', source: 'walls' }] }),
|
||||
)
|
||||
expect(tokenOf(state, 'aaa')?.statuses).toEqual(['stuck'])
|
||||
})
|
||||
|
||||
it('keeps the GM-only fields when the server sends them (story 34)', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
playerToken(2, 'aaa', {
|
||||
statuses: [{ status: 'stuck', source: 'walls' }, { status: 'squeezed', source: 'GM' }],
|
||||
gm_only: true,
|
||||
kind: 'npc',
|
||||
controller: null,
|
||||
character_id: 'char1',
|
||||
flags: {},
|
||||
vision_radius: 320,
|
||||
radius_own: null,
|
||||
}),
|
||||
)
|
||||
const token = tokenOf(state, 'aaa')
|
||||
expect(token).toMatchObject({
|
||||
gmOnly: true,
|
||||
kind: 'npc',
|
||||
controller: null,
|
||||
characterId: 'char1',
|
||||
statuses: ['stuck', 'squeezed'],
|
||||
})
|
||||
})
|
||||
|
||||
it('marks the tokens the recipient controls (mine)', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
playerToken(2, 'aaa', { mine: true }),
|
||||
playerToken(3, 'bbb'),
|
||||
)
|
||||
expect(tokensOf(state).filter(token => token.mine).map(token => token.id)).toEqual(['aaa'])
|
||||
})
|
||||
|
||||
it('ignores a malformed token frame instead of inventing a token (6.2)', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
frame({ t: 'token', n: 2, id: 'aaa', name: 'Rat' }),
|
||||
)
|
||||
expect(tokensOf(state)).toEqual([])
|
||||
})
|
||||
|
||||
it('does not let a token_gone for an unknown id create anything', () => {
|
||||
const state = run(
|
||||
{ type: 'connecting' },
|
||||
hello(1),
|
||||
frame({ t: 'token_gone', n: 2, id: 'nope' }),
|
||||
)
|
||||
expect(tokensOf(state)).toEqual([])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user