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,93 @@
|
||||
// Characters slice (DESIGN 6.1 `character`/`character_gone`, story 31). The server sends a character
|
||||
// record only to the GM and to the player who owns it, so a player's slice *is* that player's list;
|
||||
// the GM's slice is the whole cast and the list panel filters it to the GM's own.
|
||||
|
||||
import type { ReducerEvent, RootState, Slice } from '../reducer.ts'
|
||||
import type { ServerFrame } from '../protocol.ts'
|
||||
|
||||
export interface CharacterView {
|
||||
id: string
|
||||
/** Absent on the owner's own view (6.1); the GM's copy carries it. */
|
||||
owner: number | null
|
||||
descriptiveName: string
|
||||
actualName: string
|
||||
props: Record<string, Record<string, unknown>>
|
||||
}
|
||||
|
||||
function own(record: Record<string, unknown>, key: string): unknown {
|
||||
return Object.prototype.hasOwnProperty.call(record, key) ? record[key] : undefined
|
||||
}
|
||||
|
||||
export interface CharactersState {
|
||||
byId: Record<string, CharacterView>
|
||||
order: string[]
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
function propBuckets(value: unknown): Record<string, Record<string, unknown>> {
|
||||
if (!isRecord(value)) return {}
|
||||
const buckets: Record<string, Record<string, unknown>> = {}
|
||||
for (const [bucket, contents] of Object.entries(value)) {
|
||||
if (isRecord(contents)) buckets[bucket] = contents
|
||||
}
|
||||
return buckets
|
||||
}
|
||||
|
||||
export function characterViewFromFrame(frame: ServerFrame): CharacterView | null {
|
||||
const id = frame['id']
|
||||
if (typeof id !== 'string' || id.length === 0) return null
|
||||
if (typeof frame['descriptive_name'] !== 'string') return null
|
||||
if (typeof frame['actual_name'] !== 'string') return null
|
||||
const owner = frame['owner']
|
||||
return {
|
||||
id,
|
||||
owner: typeof owner === 'number' && Number.isFinite(owner) ? owner : null,
|
||||
descriptiveName: frame['descriptive_name'],
|
||||
actualName: frame['actual_name'],
|
||||
props: propBuckets(frame['props']),
|
||||
}
|
||||
}
|
||||
|
||||
export const slice: Slice<CharactersState> = {
|
||||
name: 'characters',
|
||||
init: () => ({ byId: {}, order: [] }),
|
||||
reduce(state: CharactersState, event: ReducerEvent, _root: RootState): void {
|
||||
if (event.type !== 'frame') return
|
||||
const frame = event.frame
|
||||
if (frame.t === 'character') {
|
||||
const character = characterViewFromFrame(frame)
|
||||
if (!character) return
|
||||
if (own(state.byId, character.id) === undefined) state.order.push(character.id)
|
||||
state.byId = { ...state.byId, [character.id]: character }
|
||||
} else if (frame.t === 'character_gone') {
|
||||
const id = frame['id']
|
||||
if (typeof id !== 'string' || own(state.byId, id) === undefined) return
|
||||
const byId = { ...state.byId }
|
||||
delete byId[id]
|
||||
state.byId = byId
|
||||
state.order = state.order.filter(entry => entry !== id)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export function charactersOf(state: RootState): CharacterView[] {
|
||||
const characters = state.slices['characters'] as CharactersState | undefined
|
||||
if (!characters) return []
|
||||
return characters.order
|
||||
.map(id => characters.byId[id])
|
||||
.filter((character): character is CharacterView => character !== undefined)
|
||||
}
|
||||
|
||||
/** Story 31: the characters this player may call their own. */
|
||||
export function ownCharactersOf(state: RootState): CharacterView[] {
|
||||
const me = state.session.me
|
||||
const characters = charactersOf(state)
|
||||
// A player only ever receives their own records (6.1), so everything is theirs.
|
||||
if (state.session.role !== 'gm') return characters
|
||||
// The GM's copy of the whole cast carries `owner`; the list is the GM's own figures.
|
||||
if (me === null) return []
|
||||
return characters.filter(character => character.owner === me.id)
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// Encountered slice (DESIGN 6.1 `encountered`, story 32). Per player: the figures that player has
|
||||
// met, with `grey` marking one that is no longer in view ("met before, whereabouts unknown"). The GM
|
||||
// receives one message per player; those are not the GM's own list and are ignored here.
|
||||
|
||||
import type { ReducerEvent, RootState, Slice } from '../reducer.ts'
|
||||
import type { ServerFrame } from '../protocol.ts'
|
||||
|
||||
export interface EncounteredEntry {
|
||||
id: string
|
||||
name: string
|
||||
grey: boolean
|
||||
}
|
||||
|
||||
export interface EncounteredState {
|
||||
entries: EncounteredEntry[]
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
function entry(value: unknown): EncounteredEntry | null {
|
||||
if (!isRecord(value)) return null
|
||||
const id = value['id']
|
||||
const name = value['name']
|
||||
if (typeof id !== 'string' || typeof name !== 'string') return null
|
||||
return { id, name, grey: value['grey'] === true }
|
||||
}
|
||||
|
||||
function upsert(entries: EncounteredEntry[], next: EncounteredEntry): EncounteredEntry[] {
|
||||
const index = entries.findIndex(existing => existing.id === next.id)
|
||||
if (index < 0) return [...entries, next]
|
||||
const copy = entries.slice()
|
||||
copy[index] = next
|
||||
return copy
|
||||
}
|
||||
|
||||
export const slice: Slice<EncounteredState> = {
|
||||
name: 'encountered',
|
||||
init: () => ({ entries: [] }),
|
||||
reduce(state: EncounteredState, event: ReducerEvent, root: RootState): void {
|
||||
if (event.type !== 'frame') return
|
||||
const frame: ServerFrame = event.frame
|
||||
if (frame.t !== 'encountered') return
|
||||
// The GM's copies carry `player` (6.1): one list per player, never the GM's own.
|
||||
if (root.session.role === 'gm' || typeof frame['player'] === 'number') return
|
||||
const set = frame['set']
|
||||
if (!Array.isArray(set)) return
|
||||
const incoming: EncounteredEntry[] = []
|
||||
for (const item of set) {
|
||||
const parsed = entry(item)
|
||||
if (!parsed) return
|
||||
incoming.push(parsed)
|
||||
}
|
||||
if (frame['full'] === true) {
|
||||
state.entries = incoming
|
||||
return
|
||||
}
|
||||
let entries = state.entries
|
||||
const del = frame['del']
|
||||
if (Array.isArray(del)) {
|
||||
const removed = new Set(del.filter((id): id is string => typeof id === 'string'))
|
||||
if (removed.size > 0) entries = entries.filter(existing => !removed.has(existing.id))
|
||||
}
|
||||
for (const item of incoming) entries = upsert(entries, item)
|
||||
state.entries = entries
|
||||
},
|
||||
}
|
||||
|
||||
export function encounteredOf(state: RootState): EncounteredEntry[] {
|
||||
const encountered = state.slices['encountered'] as EncounteredState | undefined
|
||||
return encountered?.entries ?? []
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
// Tokens slice (DESIGN 6.1 `token`/`token_gone`, stories 28-30 and 34). Append-only registration:
|
||||
// adding this file was enough; the reducer's glob picked it up and `hello` resets it with every
|
||||
// other slice.
|
||||
//
|
||||
// A token frame is *the whole view of one token* for this recipient (6.1), built by the server at
|
||||
// egress: a player only ever receives tokens they control plus tokens inside their own line of
|
||||
// sight and not `gm_only`, and the GM receives every token. The client therefore never filters for
|
||||
// secrecy; it only keeps what arrived and forgets a token on `token_gone` (the server's "you may
|
||||
// not see it any more", story 29).
|
||||
|
||||
import type { ReducerEvent, RootState, Slice } from '../reducer.ts'
|
||||
import type { ServerFrame, Vec2 } from '../protocol.ts'
|
||||
|
||||
export interface TokenView {
|
||||
id: string
|
||||
scene: number | null
|
||||
/** The one name this recipient may see (8); never a reveal state. */
|
||||
name: string
|
||||
pos: Vec2
|
||||
/** Effective radius in px (5.6). */
|
||||
radius: number
|
||||
/** True when this recipient controls the token (players only; GM frames say false). */
|
||||
mine: boolean
|
||||
/** Status names only; the cause (`source`) is GM-only (5.5, 6.1 leak note 6). */
|
||||
statuses: string[]
|
||||
/** The prop buckets this recipient may read: public, plus owner for the controller. */
|
||||
props: Record<string, Record<string, unknown>>
|
||||
/** GM-only fields (6.1): present on a GM connection, absent for a player. */
|
||||
gmOnly: boolean
|
||||
kind: string | null
|
||||
controller: number | null
|
||||
characterId: string | null
|
||||
}
|
||||
|
||||
function own(record: Record<string, unknown>, key: string): unknown {
|
||||
return Object.prototype.hasOwnProperty.call(record, key) ? record[key] : undefined
|
||||
}
|
||||
|
||||
export interface TokensState {
|
||||
byId: Record<string, TokenView>
|
||||
/** Render order: entry order, kept across updates so a token never jumps layers on a move. */
|
||||
order: string[]
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
function isVec2(value: unknown): value is Vec2 {
|
||||
return (
|
||||
Array.isArray(value) &&
|
||||
value.length === 2 &&
|
||||
typeof value[0] === 'number' &&
|
||||
typeof value[1] === 'number' &&
|
||||
Number.isFinite(value[0]) &&
|
||||
Number.isFinite(value[1])
|
||||
)
|
||||
}
|
||||
|
||||
function statusNames(value: unknown): string[] | null {
|
||||
if (!Array.isArray(value)) return null
|
||||
const names: string[] = []
|
||||
for (const entry of value) {
|
||||
if (!isRecord(entry) || typeof entry['status'] !== 'string') return null
|
||||
// `source` is deliberately not stored: players never receive it, and a GM's own renderer has
|
||||
// no use for it yet (6.1 leak note 6).
|
||||
names.push(entry['status'])
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
function propBuckets(value: unknown): Record<string, Record<string, unknown>> {
|
||||
if (!isRecord(value)) return {}
|
||||
const buckets: Record<string, Record<string, unknown>> = {}
|
||||
for (const [bucket, contents] of Object.entries(value)) {
|
||||
if (isRecord(contents)) buckets[bucket] = contents
|
||||
}
|
||||
return buckets
|
||||
}
|
||||
|
||||
function optionalString(value: unknown): string | null {
|
||||
return typeof value === 'string' ? value : null
|
||||
}
|
||||
|
||||
function optionalNumber(value: unknown): number | null {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? value : null
|
||||
}
|
||||
|
||||
/**
|
||||
* One `token` frame as the slice stores it, or null when the frame cannot be that shape. An
|
||||
* unreadable frame is ignored (6.2: unknown shapes never become state), not half-applied.
|
||||
*/
|
||||
export function tokenViewFromFrame(frame: ServerFrame): TokenView | null {
|
||||
const id = frame['id']
|
||||
const pos = frame['pos']
|
||||
const radius = frame['radius']
|
||||
const statuses = statusNames(frame['statuses'])
|
||||
if (typeof id !== 'string' || id.length === 0) return null
|
||||
if (!isVec2(pos)) return null
|
||||
if (typeof radius !== 'number' || !Number.isFinite(radius)) return null
|
||||
if (statuses === null) return null
|
||||
return {
|
||||
id,
|
||||
scene: optionalNumber(frame['scene']),
|
||||
name: typeof frame['name'] === 'string' ? frame['name'] : '',
|
||||
pos,
|
||||
radius,
|
||||
mine: frame['mine'] === true,
|
||||
statuses,
|
||||
props: propBuckets(frame['props']),
|
||||
gmOnly: frame['gm_only'] === true,
|
||||
kind: optionalString(frame['kind']),
|
||||
controller: optionalNumber(frame['controller']),
|
||||
characterId: optionalString(frame['character_id']),
|
||||
}
|
||||
}
|
||||
|
||||
export const slice: Slice<TokensState> = {
|
||||
name: 'tokens',
|
||||
init: () => ({ byId: {}, order: [] }),
|
||||
reduce(state: TokensState, event: ReducerEvent, _root: RootState): void {
|
||||
if (event.type !== 'frame') return
|
||||
const frame = event.frame
|
||||
if (frame.t === 'token') {
|
||||
const token = tokenViewFromFrame(frame)
|
||||
if (!token) return
|
||||
if (own(state.byId, token.id) === undefined) state.order.push(token.id)
|
||||
state.byId = { ...state.byId, [token.id]: token }
|
||||
} else if (frame.t === 'token_gone') {
|
||||
const id = frame['id']
|
||||
if (typeof id !== 'string' || own(state.byId, id) === undefined) return
|
||||
const byId = { ...state.byId }
|
||||
delete byId[id]
|
||||
state.byId = byId
|
||||
state.order = state.order.filter(entry => entry !== id)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
/** Every token this recipient may see, in entry order. */
|
||||
export function tokensOf(state: RootState): TokenView[] {
|
||||
const tokens = state.slices['tokens'] as TokensState | undefined
|
||||
if (!tokens) return []
|
||||
return tokens.order.map(id => tokens.byId[id]).filter((token): token is TokenView => token !== undefined)
|
||||
}
|
||||
|
||||
export function tokenOf(state: RootState, id: string): TokenView | null {
|
||||
const tokens = state.slices['tokens'] as TokensState | undefined
|
||||
if (!tokens) return null
|
||||
const token = own(tokens.byId, id)
|
||||
return (token as TokenView | undefined) ?? null
|
||||
}
|
||||
Reference in New Issue
Block a user