// GM wall slice (T10, #73; DESIGN 6.1 `walls`/`wall`/`wall_gone`, 5.8, 7.4). Append-only // registration: adding this file was enough; the reducer's glob picked it up and `hello` resets it // with every other slice. // // Wall records are GM-only on the wire (3.1). A player connection never receives these frames and a // player-shaped frame is ignored here too, so a bug on the server side could not quietly turn this // client into a wall-data renderer. The slice keeps the server's typed record plus the editor's // selection and its single-step undo descriptor; it never derives or invents a wall field. import type { ReducerEvent, RootState, Slice } from '../reducer.ts' import type { Vec2 } from '../protocol.ts' import type { SightDir, WallKind, WallPosition, WallRestore, WallSwitches, WallUndo, } from '../wallEditor.ts' import { deriveKind } from '../wallEditor.ts' export interface WallView { id: number scene: number a: Vec2 b: Vec2 thickness: number position: WallPosition height: number | null kind: WallKind switches: WallSwitches label: string | null flags: Record } export interface WallsState { byId: Record order: number[] /** The wall selected in the GM editor, or null. */ selectedId: number | null /** The inverse of the last successful edit; single-step undo (story 52). */ undo: WallUndo | null } function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value) } function own(record: Record, key: string): unknown { return Object.prototype.hasOwnProperty.call(record, key) ? record[key] : undefined } function isVec2(value: unknown): value is Vec2 { return ( Array.isArray(value) && value.length === 2 && typeof value[0] === 'number' && Number.isFinite(value[0]) && typeof value[1] === 'number' && Number.isFinite(value[1]) ) } function isPosition(value: unknown): value is WallPosition { return value === 'center' || value === 'left' || value === 'right' } function isSightDir(value: unknown): value is SightDir { return value === 'both' || value === 'left' || value === 'right' } function isKind(value: unknown): value is WallKind { return value === 'wall' || value === 'door' || value === 'window' || value === 'one_way' || value === 'custom' } function bool(value: unknown): boolean { return value === true } function optionalLabel(value: unknown): string | null { return typeof value === 'string' ? value : null } function flagObject(value: unknown): Record { return isRecord(value) ? value : {} } /** * One GM `wall` frame (the same shape inside `walls`) as the editor stores it, or null when the * frame cannot be that shape. `kind` is the server's derived label; an absent one is derived from * the switches so an older-shaped frame still shows a label. */ export function wallViewFromFrame(frame: Record): WallView | null { const id = frame['id'] const scene = frame['scene'] const a = frame['a'] const b = frame['b'] const thickness = frame['thickness'] const position = frame['position'] const sightDir = frame['sight_dir'] if (typeof id !== 'number' || !Number.isInteger(id) || id <= 0) return null if (typeof scene !== 'number' || !Number.isInteger(scene)) return null if (!isVec2(a) || !isVec2(b)) return null if (typeof thickness !== 'number' || !Number.isFinite(thickness)) return null if (!isPosition(position) || !isSightDir(sightDir)) return null const switches: WallSwitches = { blocksSight: bool(frame['blocks_sight']), blocksMovement: bool(frame['blocks_movement']), sightDir, playerMarker: bool(frame['player_marker']), door: bool(frame['door']), open: bool(frame['open']), secret: bool(frame['secret']), locked: bool(frame['locked']), } const height = frame['height'] const kind = own(frame, 'kind') const label = own(frame, 'label') return { id, scene, a: [a[0], a[1]], b: [b[0], b[1]], thickness, position, height: typeof height === 'number' && Number.isFinite(height) ? height : null, kind: isKind(kind) ? kind : deriveKind(switches), switches, label: optionalLabel(label), flags: flagObject(frame['flags']), } } function upsert(state: WallsState, wall: WallView): void { if (state.byId[wall.id] === undefined) state.order = [...state.order, wall.id] state.byId = { ...state.byId, [wall.id]: wall } } function remove(state: WallsState, id: number): void { if (state.byId[id] === undefined) return const byId = { ...state.byId } delete byId[id] state.byId = byId state.order = state.order.filter(entry => entry !== id) if (state.selectedId === id) state.selectedId = null } /** The editor's restore payload for one wall: everything `wall.add` needs to bring it back. */ export function restoreOf(wall: WallView): WallRestore { return { a: [wall.a[0], wall.a[1]], b: [wall.b[0], wall.b[1]], thickness: wall.thickness, position: wall.position, height: wall.height, switches: { ...wall.switches }, label: wall.label, flags: { ...wall.flags }, } } export const slice: Slice = { name: 'walls', init: () => ({ byId: {}, order: [], selectedId: null, undo: null }), reduce(state: WallsState, event: ReducerEvent, root: RootState): void { if (event.type === 'wall-select') { state.selectedId = event.id return } if (event.type === 'wall-record-undo') { state.undo = event.undo return } if (event.type !== 'frame') return const frame = event.frame // Wall data is GM-only (DESIGN 3.1); never accept it on a player-shaped session. if (root.session.role !== 'gm') return if (frame.t === 'walls') { const list = frame['walls'] if (!Array.isArray(list)) return const byId: Record = {} const order: number[] = [] for (const entry of list) { if (!isRecord(entry)) continue const wall = wallViewFromFrame(entry) if (!wall) continue byId[wall.id] = wall order.push(wall.id) } state.byId = byId state.order = order if (state.selectedId !== null && byId[state.selectedId] === undefined) { state.selectedId = null } return } if (frame.t === 'wall') { const wall = wallViewFromFrame(frame) if (wall) upsert(state, wall) return } if (frame.t === 'wall_gone') { const id = frame['id'] if (typeof id === 'number' && Number.isInteger(id)) remove(state, id) } }, } /** Every wall the GM's connection knows, in the server's id order. */ export function wallsOf(state: RootState): WallView[] { const walls = state.slices['walls'] as WallsState | undefined if (!walls) return [] return walls.order .map(id => walls.byId[id]) .filter((wall): wall is WallView => wall !== undefined) } export function wallOf(state: RootState, id: number): WallView | null { const walls = state.slices['walls'] as WallsState | undefined if (!walls) return null return walls.byId[id] ?? null } export function selectedWallId(state: RootState): number | null { return (state.slices['walls'] as WallsState | undefined)?.selectedId ?? null } export function selectedWallOf(state: RootState): WallView | null { const id = selectedWallId(state) return id === null ? null : wallOf(state, id) } export function undoOf(state: RootState): WallUndo | null { return (state.slices['walls'] as WallsState | undefined)?.undo ?? null }