Files
FFF_vtt_frozen/client/src/slices/admin.ts
T

152 lines
5.0 KiB
TypeScript

// GM admin slice (T11, #74): the GM-only token records the token panel edits, plus the one piece of
// selection state the panel owns. Append-only registration: adding this file was enough; the
// reducer's glob picked it up and `hello` resets it with every other slice.
//
// TOKEN SELECTION FOR #71/#72: the selected token id lives here, not in the panel, so the token
// renderer (#71) and the drag module (#72) can read it without importing the panel. Read it with
// `selectedTokenId(state)` and change it only by dispatching
// `{ type: 'admin-select', token: <id> | null }`. This slice never writes selection from a `token`
// frame; a `token_gone` for the selected id clears it.
import type { ReducerEvent, RootState, Slice } from '../reducer.ts'
import type { ServerFrame } from '../protocol.ts'
export interface AdminStatus {
status: string
/** GM frames carry the source (`walls`|`GM`); a player frame cannot reach this panel. */
source?: string
}
/** The GM's view of one token frame (DESIGN 6.1 `token`, GM columns). */
export interface AdminToken {
id: string
scene: number
name: string
pos: [number, number]
radius: number
/** GM-only fields; optional so a malformed frame is ignored rather than guessed. */
kind: string | null
controller: number | null
gmOnly: boolean
statuses: AdminStatus[]
descriptiveName: string | null
actualName: string | null
visionRadius: number | null
radiusOwn: number | null
}
export interface AdminState {
tokens: AdminToken[]
/** Id of the token selected in the GM panel; `null` when nothing is selected. */
selectedToken: string | null
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
function isVec2(value: unknown): value is [number, number] {
return (
Array.isArray(value) &&
value.length === 2 &&
typeof value[0] === 'number' &&
Number.isFinite(value[0]) &&
typeof value[1] === 'number' &&
Number.isFinite(value[1])
)
}
function finiteOrNull(value: unknown): number | null {
return typeof value === 'number' && Number.isFinite(value) ? value : null
}
function parseStatuses(value: unknown): AdminStatus[] {
if (!Array.isArray(value)) return []
const out: AdminStatus[] = []
for (const entry of value) {
if (!isRecord(entry) || typeof entry['status'] !== 'string') continue
const source = entry['source']
out.push(
typeof source === 'string'
? { status: entry['status'], source }
: { status: entry['status'] },
)
}
return out
}
/** Read one GM `token` frame; `null` for a shape the panel cannot edit. */
export function tokenFromFrame(frame: ServerFrame): AdminToken | null {
const id = frame['id']
const scene = frame['scene']
const name = frame['name']
const pos = frame['pos']
const radius = frame['radius']
if (
typeof id !== 'string' ||
id.length === 0 ||
typeof scene !== 'number' ||
!Number.isInteger(scene) ||
typeof name !== 'string' ||
!isVec2(pos) ||
typeof radius !== 'number' ||
!Number.isFinite(radius)
) {
return null
}
return {
id,
scene,
name,
pos: [pos[0], pos[1]],
radius,
kind: typeof frame['kind'] === 'string' ? frame['kind'] : null,
controller: finiteOrNull(frame['controller']),
gmOnly: frame['gm_only'] === true,
statuses: parseStatuses(frame['statuses']),
descriptiveName:
typeof frame['descriptive_name'] === 'string' ? frame['descriptive_name'] : null,
actualName: typeof frame['actual_name'] === 'string' ? frame['actual_name'] : null,
visionRadius: finiteOrNull(frame['vision_radius']),
radiusOwn: finiteOrNull(frame['radius_own']),
}
}
function upsert(state: AdminState, token: AdminToken): void {
const index = state.tokens.findIndex(entry => entry.id === token.id)
if (index >= 0) state.tokens[index] = token
else state.tokens.push(token)
}
export const slice: Slice<AdminState> = {
name: 'admin',
init: () => ({ tokens: [], selectedToken: null }),
reduce(state: AdminState, event: ReducerEvent, _root: RootState): void {
if (event.type === 'admin-select') {
state.selectedToken = event.token
return
}
if (event.type !== 'frame') return
const frame = event.frame
if (frame.t === 'token') {
const token = tokenFromFrame(frame)
if (token) upsert(state, token)
} else if (frame.t === 'token_gone') {
const id = frame['id']
if (typeof id !== 'string') return
state.tokens = state.tokens.filter(token => token.id !== id)
if (state.selectedToken === id) state.selectedToken = null
}
},
}
/** The selected token id, owned by this slice for #71/#72 to read. */
export function selectedTokenId(state: RootState): string | null {
return (state.slices['admin'] as AdminState | undefined)?.selectedToken ?? null
}
/** The GM token records, in the order the server sent them. */
export function adminTokens(state: RootState): AdminToken[] {
return (state.slices['admin'] as AdminState | undefined)?.tokens ?? []
}