- 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.
178 lines
6.3 KiB
TypeScript
178 lines
6.3 KiB
TypeScript
// 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)
|
|
})
|
|
})
|