feat(client): GM token panel sets vision and vision radius (#78)
Token create and edit now carry flags.vision and vision_radius; edits merge
only the vision key into the stored flags. A new token keeps DESIGN 8's
flags '{}' default (vision off, radius = scene default). The fog e2e authors
its sighted token through the GM panel, and the admin e2e proves the panel
round-trip plus an explored 200 / unexplored 403 tile probe.
Refs #78
This commit is contained in:
@@ -16,6 +16,7 @@ import type { AckFrame, RosterEntry } from '../protocol.ts'
|
||||
import { rosterOf } from '../reducer.ts'
|
||||
import type { AdminToken } from '../slices/admin.ts'
|
||||
import { adminTokens, selectedTokenId } from '../slices/admin.ts'
|
||||
import { createVisionFields, updateVisionFields } from '../tokenPanel.ts'
|
||||
|
||||
export interface AdminPanelHandlers {
|
||||
send(t: string, fields?: Record<string, unknown>): Promise<AckFrame>
|
||||
@@ -101,6 +102,17 @@ export function mountAdminPanel(host: HTMLElement, handlers: AdminPanelHandlers)
|
||||
radiusInput.node.type = 'number'
|
||||
radiusInput.node.min = '0.1'
|
||||
radiusInput.node.step = 'any'
|
||||
// DESIGN 8 defaults a new token's `flags` to `{}`: vision is off until the GM turns it on.
|
||||
const createVision = field('input', 'token-vision', 'Vision')
|
||||
createVision.node.type = 'checkbox'
|
||||
const createVisionRadius = field(
|
||||
'input',
|
||||
'token-vision-radius',
|
||||
'Vision radius (blank = scene default)',
|
||||
)
|
||||
createVisionRadius.node.type = 'number'
|
||||
createVisionRadius.node.min = '0.1'
|
||||
createVisionRadius.node.step = 'any'
|
||||
const createGmOnly = field('input', 'token-gm-only', 'GM only')
|
||||
createGmOnly.node.type = 'checkbox'
|
||||
const createController = field('select', 'token-controller', 'Controller')
|
||||
@@ -120,6 +132,10 @@ export function mountAdminPanel(host: HTMLElement, handlers: AdminPanelHandlers)
|
||||
yInput.node,
|
||||
radiusInput.label,
|
||||
radiusInput.node,
|
||||
createVision.label,
|
||||
createVision.node,
|
||||
createVisionRadius.label,
|
||||
createVisionRadius.node,
|
||||
createGmOnly.label,
|
||||
createGmOnly.node,
|
||||
createControllerLabel,
|
||||
@@ -138,6 +154,16 @@ export function mountAdminPanel(host: HTMLElement, handlers: AdminPanelHandlers)
|
||||
editActual.node.type = 'text'
|
||||
const editKind = field('input', 'token-edit-kind', 'Kind')
|
||||
editKind.node.type = 'text'
|
||||
const editVision = field('input', 'token-edit-vision', 'Vision')
|
||||
editVision.node.type = 'checkbox'
|
||||
const editVisionRadius = field(
|
||||
'input',
|
||||
'token-edit-vision-radius',
|
||||
'Vision radius (blank = scene default)',
|
||||
)
|
||||
editVisionRadius.node.type = 'number'
|
||||
editVisionRadius.node.min = '0.1'
|
||||
editVisionRadius.node.step = 'any'
|
||||
const editGmOnly = field('input', 'token-edit-gm-only', 'GM only')
|
||||
editGmOnly.node.type = 'checkbox'
|
||||
const editController = field('select', 'token-edit-controller', 'Controller')
|
||||
@@ -176,6 +202,10 @@ export function mountAdminPanel(host: HTMLElement, handlers: AdminPanelHandlers)
|
||||
editActual.node,
|
||||
editKind.label,
|
||||
editKind.node,
|
||||
editVision.label,
|
||||
editVision.node,
|
||||
editVisionRadius.label,
|
||||
editVisionRadius.node,
|
||||
editGmOnly.label,
|
||||
editGmOnly.node,
|
||||
editController.label,
|
||||
@@ -328,6 +358,8 @@ export function mountAdminPanel(host: HTMLElement, handlers: AdminPanelHandlers)
|
||||
editName.node.value = token.descriptiveName ?? token.name
|
||||
editActual.node.value = token.actualName ?? token.name
|
||||
editKind.node.value = token.kind ?? ''
|
||||
editVision.node.checked = token.flags['vision'] === true
|
||||
editVisionRadius.node.value = token.visionRadius === null ? '' : String(token.visionRadius)
|
||||
editGmOnly.node.checked = token.gmOnly
|
||||
renderControllerOptions(
|
||||
editController.node,
|
||||
@@ -385,6 +417,12 @@ export function mountAdminPanel(host: HTMLElement, handlers: AdminPanelHandlers)
|
||||
return current ? selectedTokenId(current) : null
|
||||
}
|
||||
|
||||
function currentToken(): AdminToken | null {
|
||||
const selected = currentSelection()
|
||||
if (selected === null || !current) return null
|
||||
return adminTokens(current).find(token => token.id === selected) ?? null
|
||||
}
|
||||
|
||||
async function request(t: string, fields: Record<string, unknown> = {}): Promise<AckFrame | null> {
|
||||
try {
|
||||
const ack = await handlers.send(t, fields)
|
||||
@@ -480,6 +518,11 @@ export function mountAdminPanel(host: HTMLElement, handlers: AdminPanelHandlers)
|
||||
setError('Radius must be a positive number or blank.')
|
||||
return
|
||||
}
|
||||
const vision = createVisionFields(createVision.node.checked, createVisionRadius.node.value)
|
||||
if (!vision.ok) {
|
||||
setError(vision.error)
|
||||
return
|
||||
}
|
||||
const controllerValue = createController.node.value
|
||||
const controller = controllerValue === '' ? null : Number(controllerValue)
|
||||
const ack = await request('token.create', {
|
||||
@@ -491,6 +534,7 @@ export function mountAdminPanel(host: HTMLElement, handlers: AdminPanelHandlers)
|
||||
radius,
|
||||
gm_only: createGmOnly.node.checked,
|
||||
controller,
|
||||
...vision.fields,
|
||||
})
|
||||
if (ack?.ok) {
|
||||
const created = ack as { id?: unknown }
|
||||
@@ -499,8 +543,8 @@ export function mountAdminPanel(host: HTMLElement, handlers: AdminPanelHandlers)
|
||||
}
|
||||
|
||||
async function saveToken(): Promise<void> {
|
||||
const selected = currentSelection()
|
||||
if (selected === null) return
|
||||
const token = currentToken()
|
||||
if (token === null) return
|
||||
setError(null)
|
||||
const name = editName.node.value.trim()
|
||||
const actual = editActual.node.value.trim()
|
||||
@@ -509,9 +553,19 @@ export function mountAdminPanel(host: HTMLElement, handlers: AdminPanelHandlers)
|
||||
setError('Kind and both names are required.')
|
||||
return
|
||||
}
|
||||
const vision = updateVisionFields(
|
||||
token.id,
|
||||
token.flags,
|
||||
editVision.node.checked,
|
||||
editVisionRadius.node.value,
|
||||
)
|
||||
if (!vision.ok) {
|
||||
setError(vision.error)
|
||||
return
|
||||
}
|
||||
const controllerValue = editController.node.value
|
||||
await request('token.update', {
|
||||
id: selected,
|
||||
...vision.fields,
|
||||
kind,
|
||||
descriptive_name: name,
|
||||
actual_name: actual,
|
||||
|
||||
@@ -31,6 +31,8 @@ export interface AdminToken {
|
||||
statuses: AdminStatus[]
|
||||
descriptiveName: string | null
|
||||
actualName: string | null
|
||||
/** Free-form GM flags (DESIGN 8); the panel merges `vision` into this object on save. */
|
||||
flags: Record<string, unknown>
|
||||
visionRadius: number | null
|
||||
radiusOwn: number | null
|
||||
}
|
||||
@@ -60,6 +62,10 @@ function finiteOrNull(value: unknown): number | null {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? value : null
|
||||
}
|
||||
|
||||
function flagsObject(value: unknown): Record<string, unknown> {
|
||||
return isRecord(value) ? { ...value } : {}
|
||||
}
|
||||
|
||||
function parseStatuses(value: unknown): AdminStatus[] {
|
||||
if (!Array.isArray(value)) return []
|
||||
const out: AdminStatus[] = []
|
||||
@@ -107,6 +113,7 @@ export function tokenFromFrame(frame: ServerFrame): AdminToken | null {
|
||||
descriptiveName:
|
||||
typeof frame['descriptive_name'] === 'string' ? frame['descriptive_name'] : null,
|
||||
actualName: typeof frame['actual_name'] === 'string' ? frame['actual_name'] : null,
|
||||
flags: flagsObject(frame['flags']),
|
||||
visionRadius: finiteOrNull(frame['vision_radius']),
|
||||
radiusOwn: finiteOrNull(frame['radius_own']),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// The GM token panel's pure half (T11/#74, #78): local validation of the vision controls and the
|
||||
// fields the panel sends for `token.create` / `token.update` (DESIGN 6.1, 8). No DOM, no socket.
|
||||
//
|
||||
// `flags` is free-form (DESIGN 8): an edit writes only the `vision` key and copies every other key
|
||||
// from the current GM `token` frame, so future GM flags survive a save. A new token keeps the
|
||||
// DESIGN 8 default (`flags: '{}'`): vision off means no `flags` field is sent at all.
|
||||
//
|
||||
// `vision_radius` is also the player's move step cap (DESIGN 5.5), so the panel validates it like
|
||||
// the token radius: blank = scene default (`null`), anything else must be > 0 and finite.
|
||||
|
||||
/** The vision part of a `token.create` / `token.update` payload. */
|
||||
export interface VisionFields {
|
||||
/** Present only when the panel writes a flag (create with vision on, or any edit). */
|
||||
flags?: Record<string, unknown>
|
||||
vision_radius: number | null
|
||||
}
|
||||
|
||||
export type VisionFieldsResult =
|
||||
| { ok: true; fields: VisionFields }
|
||||
| { ok: false; error: string }
|
||||
|
||||
export type VisionUpdateResult =
|
||||
| { ok: true; fields: { id: string; flags: Record<string, unknown>; vision_radius: number | null } }
|
||||
| { ok: false; error: string }
|
||||
|
||||
type RadiusResult = { ok: true; value: number | null } | { ok: false; error: string }
|
||||
|
||||
/** Blank is the scene default; non-finite and non-positive values are refused before the wire. */
|
||||
export function parseVisionRadius(raw: string): RadiusResult {
|
||||
const text = raw.trim()
|
||||
if (text === '') return { ok: true, value: null }
|
||||
const value = Number(text)
|
||||
if (!Number.isFinite(value) || value <= 0) {
|
||||
return {
|
||||
ok: false,
|
||||
error: 'Vision radius must be a positive number or blank for the scene default.',
|
||||
}
|
||||
}
|
||||
return { ok: true, value }
|
||||
}
|
||||
|
||||
/** Copy `flags` and set only `vision`; every other GM flag survives (DESIGN 8). */
|
||||
export function withVisionFlag(
|
||||
flags: Record<string, unknown>,
|
||||
vision: boolean,
|
||||
): Record<string, unknown> {
|
||||
return { ...flags, vision }
|
||||
}
|
||||
|
||||
/**
|
||||
* `token.create` vision fields. Vision off sends no `flags`, so the server keeps its DESIGN 8
|
||||
* default (`{}`); vision on carries only the one key.
|
||||
*/
|
||||
export function createVisionFields(vision: boolean, rawRadius: string): VisionFieldsResult {
|
||||
const radius = parseVisionRadius(rawRadius)
|
||||
if (!radius.ok) return radius
|
||||
return {
|
||||
ok: true,
|
||||
fields: vision
|
||||
? { flags: { vision: true }, vision_radius: radius.value }
|
||||
: { vision_radius: radius.value },
|
||||
}
|
||||
}
|
||||
|
||||
/** `token.update` vision fields: the stored flags are the base, never a rebuilt object. */
|
||||
export function updateVisionFields(
|
||||
id: string,
|
||||
flags: Record<string, unknown>,
|
||||
vision: boolean,
|
||||
rawRadius: string,
|
||||
): VisionUpdateResult {
|
||||
const radius = parseVisionRadius(rawRadius)
|
||||
if (!radius.ok) return radius
|
||||
return {
|
||||
ok: true,
|
||||
fields: { id, flags: withVisionFlag(flags, vision), vision_radius: radius.value },
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user