feat: GM characters, reveals, who-sees-it and seen_by (#75)
Server: GM token frames gain the additive, GM-only seen_by:[player ids], computed from each player's existing sight (PlayerVision.visibleTo) with no extra polygon pass. Leakcheck BannedAlways bans seen_by; a seenBy fault build is caught as exactly WALLDATA. proto stays 2 (6.2 additive). Client: new cast slice parses character/character_gone/encountered frames and seen_by; gmcast panel adds character create/rename/assign/remove, reveal to one/all, encountered forget (grey entries only) and the who-sees-it list (0/100 in M4). Mounted beside the #74 admin panel. Docs: DESIGN 5.9/6.1/6.2/7.4 record the round-1 shape and the no-bump call. Gates: devbox run test, client-test, e2e green; bench recorded.
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
// GM cast slice (T12, #75): the character records, the per-player encountered lists and the GM-only
|
||||
// who-sees-it field of `token` frames. Append-only registration: this file is enough; the reducer's
|
||||
// glob picks it up and `hello` resets it with every other slice.
|
||||
//
|
||||
// This slice is deliberately separate from `slices/admin.ts` (#74, the token editor) so the two
|
||||
// tickets stay file-disjoint. It only reads frames; the panel in `src/panels/gmcast.ts` sends the
|
||||
// intents. `seen_by` is a GM-only additive field (DESIGN 6.1/7.4): a player frame never carries it,
|
||||
// so a player session simply leaves the map empty.
|
||||
|
||||
import type { ReducerEvent, RootState, Slice } from '../reducer.ts'
|
||||
import type { ServerFrame } from '../protocol.ts'
|
||||
|
||||
export interface CastCharacter {
|
||||
id: string
|
||||
/** The player who owns the character (the GM owns through its own player id). */
|
||||
owner: number
|
||||
descriptiveName: string
|
||||
actualName: string
|
||||
revealAll: boolean
|
||||
/** Players the actual name was revealed to (8); never revoked. */
|
||||
revealed: number[]
|
||||
props?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export interface CastEncountered {
|
||||
id: string
|
||||
name: string
|
||||
grey: boolean
|
||||
}
|
||||
|
||||
export interface CastState {
|
||||
characters: CastCharacter[]
|
||||
/** playerId -> that player's encountered list, in the order the server sent it. */
|
||||
encountered: Record<number, CastEncountered[]>
|
||||
/** tokenId -> the players who see it (GM-only `seen_by`, 0/100 in M4). */
|
||||
seenBy: Record<string, number[]>
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
function playerIds(value: unknown): number[] {
|
||||
if (!Array.isArray(value)) return []
|
||||
return value.filter(
|
||||
(entry): entry is number => typeof entry === 'number' && Number.isInteger(entry),
|
||||
)
|
||||
}
|
||||
|
||||
/** Read one GM `character` frame; `null` for a shape the panel cannot show. */
|
||||
export function characterFromFrame(frame: ServerFrame): CastCharacter | null {
|
||||
const id = frame['id']
|
||||
const owner = frame['owner']
|
||||
const descriptiveName = frame['descriptive_name']
|
||||
const actualName = frame['actual_name']
|
||||
if (
|
||||
typeof id !== 'string' ||
|
||||
id.length === 0 ||
|
||||
typeof owner !== 'number' ||
|
||||
!Number.isInteger(owner) ||
|
||||
typeof descriptiveName !== 'string' ||
|
||||
typeof actualName !== 'string'
|
||||
) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
id,
|
||||
owner,
|
||||
descriptiveName,
|
||||
actualName,
|
||||
revealAll: frame['reveal_all'] === true,
|
||||
revealed: playerIds(frame['revealed']),
|
||||
props: isRecord(frame['props']) ? frame['props'] : undefined,
|
||||
}
|
||||
}
|
||||
|
||||
function entries(value: unknown): CastEncountered[] | null {
|
||||
if (!Array.isArray(value)) return null
|
||||
const out: CastEncountered[] = []
|
||||
for (const entry of value) {
|
||||
if (!isRecord(entry) || typeof entry['id'] !== 'string' || typeof entry['name'] !== 'string') {
|
||||
continue
|
||||
}
|
||||
out.push({ id: entry['id'], name: entry['name'], grey: entry['grey'] === true })
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function upsertCharacter(state: CastState, character: CastCharacter): void {
|
||||
const index = state.characters.findIndex(entry => entry.id === character.id)
|
||||
if (index >= 0) state.characters[index] = character
|
||||
else state.characters.push(character)
|
||||
}
|
||||
|
||||
function reduceEncountered(state: CastState, frame: ServerFrame): void {
|
||||
const player = frame['player']
|
||||
if (typeof player !== 'number' || !Number.isInteger(player)) return
|
||||
const set = entries(frame['set'])
|
||||
if (set === null) return
|
||||
if (frame['full'] === true) {
|
||||
state.encountered[player] = set
|
||||
return
|
||||
}
|
||||
const current = state.encountered[player] ?? []
|
||||
for (const entry of set) {
|
||||
const index = current.findIndex(row => row.id === entry.id)
|
||||
if (index >= 0) current[index] = entry
|
||||
else current.push(entry)
|
||||
}
|
||||
const del = frame['del']
|
||||
if (Array.isArray(del)) {
|
||||
const gone = new Set(del.filter((id): id is string => typeof id === 'string'))
|
||||
state.encountered[player] = current.filter(entry => !gone.has(entry.id))
|
||||
} else {
|
||||
state.encountered[player] = current
|
||||
}
|
||||
}
|
||||
|
||||
export const slice: Slice<CastState> = {
|
||||
name: 'cast',
|
||||
init: () => ({ characters: [], encountered: {}, seenBy: {} }),
|
||||
reduce(state: CastState, event: ReducerEvent, _root: RootState): void {
|
||||
if (event.type !== 'frame') return
|
||||
const frame = event.frame
|
||||
switch (frame.t) {
|
||||
case 'character': {
|
||||
const character = characterFromFrame(frame)
|
||||
if (character) upsertCharacter(state, character)
|
||||
return
|
||||
}
|
||||
case 'character_gone': {
|
||||
const id = frame['id']
|
||||
if (typeof id !== 'string') return
|
||||
state.characters = state.characters.filter(entry => entry.id !== id)
|
||||
return
|
||||
}
|
||||
case 'encountered':
|
||||
reduceEncountered(state, frame)
|
||||
return
|
||||
case 'token': {
|
||||
const id = frame['id']
|
||||
if (typeof id !== 'string') return
|
||||
const seen = frame['seen_by']
|
||||
if (!Array.isArray(seen)) return
|
||||
state.seenBy[id] = playerIds(seen)
|
||||
return
|
||||
}
|
||||
case 'token_gone': {
|
||||
const id = frame['id']
|
||||
if (typeof id !== 'string') return
|
||||
delete state.seenBy[id]
|
||||
return
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export function castCharacters(state: RootState): CastCharacter[] {
|
||||
return (state.slices['cast'] as CastState | undefined)?.characters ?? []
|
||||
}
|
||||
|
||||
export function encounteredOf(state: RootState, player: number): CastEncountered[] {
|
||||
return (state.slices['cast'] as CastState | undefined)?.encountered[player] ?? []
|
||||
}
|
||||
|
||||
/** The players who see a token, or `null` when no GM frame has carried it yet. */
|
||||
export function seenByOf(state: RootState, token: string): number[] | null {
|
||||
const map = (state.slices['cast'] as CastState | undefined)?.seenBy
|
||||
const seen = map ? map[token] : undefined
|
||||
return seen ?? null
|
||||
}
|
||||
Reference in New Issue
Block a user