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,135 @@
|
||||
// The lists panel (T8, #71; DESIGN 7.1 layer 6): story 31's own characters and story 32's
|
||||
// encountered list. Plain DOM, every server string through `textContent` (decision 15): a token
|
||||
// name can never become markup.
|
||||
//
|
||||
// Mounted by `render/modules/lists.ts` under the shell's existing `#panels` host, so the shell's
|
||||
// sign-in flow is untouched and the panel exists only while a live stage does. The panel is
|
||||
// pointer-transparent (see style.css): it must never eat a pan or a token drag.
|
||||
|
||||
import type { RootState } from '../reducer.ts'
|
||||
import type { Store } from '../store.ts'
|
||||
import { ownCharactersOf } from '../slices/characters.ts'
|
||||
import type { CharacterView } from '../slices/characters.ts'
|
||||
import { encounteredOf } from '../slices/encountered.ts'
|
||||
import type { EncounteredEntry } from '../slices/encountered.ts'
|
||||
|
||||
const NAME_FALLBACK = '(unnamed)'
|
||||
|
||||
function text(tag: keyof HTMLElementTagNameMap, className: string, value: string): HTMLElement {
|
||||
const node = document.createElement(tag)
|
||||
node.className = className
|
||||
node.textContent = value
|
||||
return node
|
||||
}
|
||||
|
||||
/** A prop value as text: scalars plainly, anything else as bounded JSON. */
|
||||
function valueText(value: unknown): string {
|
||||
if (value === null) return 'null'
|
||||
if (typeof value === 'string') return value
|
||||
if (typeof value === 'number' || typeof value === 'boolean') return String(value)
|
||||
try {
|
||||
const json = JSON.stringify(value)
|
||||
return json.length > 80 ? `${json.slice(0, 77)}…` : json
|
||||
} catch {
|
||||
return '?'
|
||||
}
|
||||
}
|
||||
|
||||
/** Story 31: the values this player may read, public first then the owner bucket on top. */
|
||||
function valuesOf(character: CharacterView): Array<[string, string]> {
|
||||
const merged: Record<string, unknown> = { ...character.props['public'], ...character.props['owner'] }
|
||||
return Object.entries(merged).map(([key, value]) => [key, valueText(value)])
|
||||
}
|
||||
|
||||
function characterRow(character: CharacterView): HTMLLIElement {
|
||||
const item = document.createElement('li')
|
||||
item.dataset['character'] = character.id
|
||||
const label = character.descriptiveName.trim() || NAME_FALLBACK
|
||||
const name = text('span', 'entry-name', label)
|
||||
if (character.actualName && character.actualName !== character.descriptiveName) {
|
||||
name.title = character.actualName
|
||||
}
|
||||
item.append(name)
|
||||
const values = valuesOf(character)
|
||||
if (values.length > 0) {
|
||||
item.append(text('span', 'entry-values', values.map(([key, value]) => `${key} ${value}`).join(' · ')))
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
function encounteredRow(entry: EncounteredEntry): HTMLLIElement {
|
||||
const item = document.createElement('li')
|
||||
item.dataset['token'] = entry.id
|
||||
item.dataset['grey'] = entry.grey ? 'true' : 'false'
|
||||
item.classList.toggle('grey', entry.grey)
|
||||
item.append(text('span', 'entry-name', entry.name.trim() || NAME_FALLBACK))
|
||||
// Greyed means "met before, whereabouts unknown" (story 32); the text says it without colour.
|
||||
item.append(text('span', 'entry-state', entry.grey ? 'out of sight' : 'in sight'))
|
||||
return item
|
||||
}
|
||||
|
||||
function listSignature(characters: readonly CharacterView[], encountered: readonly EncounteredEntry[]): string {
|
||||
return JSON.stringify([
|
||||
characters.map(character => [
|
||||
character.id,
|
||||
character.descriptiveName,
|
||||
character.actualName,
|
||||
valuesOf(character),
|
||||
]),
|
||||
encountered.map(entry => [entry.id, entry.name, entry.grey]),
|
||||
])
|
||||
}
|
||||
|
||||
export function mountListsPanel(host: HTMLElement, store: Store): () => void {
|
||||
const section = document.createElement('section')
|
||||
section.id = 'lists'
|
||||
section.hidden = true
|
||||
|
||||
const charactersSection = document.createElement('section')
|
||||
charactersSection.id = 'characters-panel'
|
||||
const charactersTitle = text('h2', 'panel-title', 'My characters')
|
||||
const charactersList = document.createElement('ul')
|
||||
charactersList.id = 'characters'
|
||||
const charactersEmpty = text('p', 'empty', 'No characters yet.')
|
||||
charactersSection.append(charactersTitle, charactersEmpty, charactersList)
|
||||
|
||||
const encounteredSection = document.createElement('section')
|
||||
encounteredSection.id = 'encountered-panel'
|
||||
const encounteredTitle = text('h2', 'panel-title', 'Encountered')
|
||||
const encounteredList = document.createElement('ul')
|
||||
encounteredList.id = 'encountered'
|
||||
const encounteredEmpty = text('p', 'empty', 'Nobody met yet.')
|
||||
encounteredSection.append(encounteredTitle, encounteredEmpty, encounteredList)
|
||||
|
||||
section.append(charactersSection, encounteredSection)
|
||||
host.append(section)
|
||||
|
||||
let signature = ''
|
||||
|
||||
function render(state: RootState): void {
|
||||
const visible = state.session.role !== null &&
|
||||
(state.connection.phase === 'live' || state.connection.phase === 'reconnecting')
|
||||
section.hidden = !visible
|
||||
if (!visible) return
|
||||
|
||||
const characters = ownCharactersOf(state)
|
||||
const encountered = encounteredOf(state)
|
||||
const next = listSignature(characters, encountered)
|
||||
// The lists change on frames, not per frame: rebuild rows only when their content changed.
|
||||
if (next !== signature) {
|
||||
signature = next
|
||||
charactersList.replaceChildren(...characters.map(characterRow))
|
||||
encounteredList.replaceChildren(...encountered.map(encounteredRow))
|
||||
charactersEmpty.hidden = characters.length > 0
|
||||
encounteredEmpty.hidden = encountered.length > 0
|
||||
}
|
||||
}
|
||||
|
||||
const unsubscribe = store.subscribe(render)
|
||||
render(store.state)
|
||||
|
||||
return () => {
|
||||
unsubscribe()
|
||||
section.remove()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user