feat(walls): player markers — emission, opaque ids, storage, resync (#59)

A wall that is player_marker:true and derives window sends a player one
anchor (the drawn midpoint, integer px) and an opaque per-player 12-char id
once any point of its segment enters that player's vision polygon. Markers
live in PlayerVision like explored, persist to marker_seen in the same
derived-memory transaction, load before the first connection and resync as a
full set after explored; the GM and other players never receive one.

- markers.nim: emission gate, visible-extent test, id mint, anchor, marker_seen I/O.
- views/main: PlayerView markers + full/diff frames, refresh sync, startup load.
- markers.test.mjs: window preset, switch off, solid wall, any-part anchor,
  walk-away/door/lost sight, full-after-explored, per-player privacy, GM none.
- leakcheck: marker allowed type + independent visible-extent oracle with a
  positive control (a marked window the subject sees).
- restart: marked window in the fixture; marker set and marker_seen rows
  identical before/after SIGTERM and SIGKILL.
- protocol/tviews: catalogue and frame order pin the 16th message.

Retraction, re-earning and MARKER_* faults are #60.

Ref #59
This commit is contained in:
2026-11-19 23:27:26 +01:00
parent e989100f50
commit edd4437f7e
11 changed files with 599 additions and 47 deletions
+58 -2
View File
@@ -104,6 +104,12 @@ type
name*: string
grey*: bool
MarkerView* = object
## One player marker (DESIGN 5.8, 6.1): the player's own opaque id and the wall's drawn midpoint
## in integer px. There is deliberately no wall id, kind, state, label or switch here.
id*: string
x*, y*: int
GmEncounteredView* = object
player*: int
entries*: seq[EncounteredView]
@@ -117,6 +123,7 @@ type
tokens*: seq[PlayerTokenView]
characters*: seq[PlayerCharacterView]
encountered*: seq[EncounteredView]
markers*: seq[MarkerView]
polys*: seq[Poly]
fresh*: seq[(int, int)]
@@ -151,6 +158,7 @@ type
tokens*: Table[string, JsonNode]
characters*: Table[string, JsonNode]
encountered*: Table[string, JsonNode] ## GM: "<player>/<token>"; player: "<token>"
markers*: Table[string, JsonNode] ## player: marker id -> the `{id,pos}` entry sent
vision*: string ## the last `vision` payload sent, "" = never
visionSent*: bool
@@ -291,8 +299,7 @@ proc playerView*(ctx: Context, playerId: int): PlayerView =
result.encountered.add EncounteredView(id: eid, name: e.entryName, grey: eid notin visible)
when defined(leakfault):
# Negative controls (DESIGN 10.3): add what the honest rule just refused, so the harness proves
# it would notice. Never a second copy of the visibility rule.
# The two "add a token the honest rule refused" controls build the frame exactly as the honest
# it would notice. Never a second copy of the visibility rule. # The two "add a token the honest rule refused" controls build the frame exactly as the honest
# path would, so the class they report stays the one they were built for (a bucket mismatch here
# would add an OWNER_PROP/CHAR_OWNER_PROP class and blunt the assertion).
proc leakedView(t: TokenRec): PlayerTokenView =
@@ -367,6 +374,12 @@ proc playerView*(ctx: Context, playerId: int): PlayerView =
if ch.isNone or ch.get.owner == playerId: continue
for v in result.tokens.mitems:
if v.id == t.id: v.cpropsPublic = objectOf(ch.get.props[pbOwner])
result.markers = newSeqOfCap[MarkerView](pv[].markers.len)
for _, marker in pv[].markers.pairs:
result.markers.add MarkerView(id: marker.id, x: marker.x, y: marker.y)
# Deterministic order by the opaque id: the full set is compared byte-for-byte on resync, and the
# order must not reveal wall order or count (the ids are random, 5.8).
result.markers.sort(proc (a, b: MarkerView): int = cmp(a.id, b.id))
result.polys = pv[].polys
result.fresh = pv[].fresh
@@ -504,6 +517,22 @@ proc exploredFreshFrame*(cells: seq[(int, int)]): JsonNode =
for (cx, cy) in cells: list.add %*[cx, cy]
%*{"t": "explored", "full": false, "cells": list}
proc markerEntryJson(m: MarkerView): JsonNode =
## One entry of a `marker` set: only the opaque id and the integer-px anchor (5.8, 6.1).
%*{"id": m.id, "pos": [m.x, m.y]}
proc markerFrame*(entries: seq[MarkerView], full: bool, del: seq[string]): JsonNode =
## `marker {full, set:[{id, pos}], del:[id]}` (DESIGN 6.1). `full:true` replaces the whole set on
## connect/resync and carries no `del`; a diff carries additions in `set` and retractions in `del`,
## never both for one id. Retraction emission is #60; this builder already has the shape.
var set: JsonNode = newJArray()
for m in entries: set.add markerEntryJson(m)
result = %*{"t": "marker", "full": full, "set": set}
if not full:
var removals = newJArray()
for id in del: removals.add %id
result["del"] = removals
proc sceneBlock*(sc: SceneRec): JsonNode =
## The `scene` object inside `hello` (DESIGN 6.1, 3.1: no counts, no wall data, no owned ids).
%*{"id": sc.id, "name": sc.name, "w": sc.w, "h": sc.h, "tile_size": sc.tileSize,
@@ -562,6 +591,27 @@ proc diffEncountered(known: var Known, key: string, entries: seq[EncounteredView
del.sort()
encounteredFrame(set, full, del, player)
proc diffMarkers(known: var Known, markers: seq[MarkerView], full: bool): JsonNode =
## One `marker` message or nil (DESIGN 5.8, 6.1): on connect/resync `full:true` with the whole set
## (after `explored`, 6.3); afterwards only new ids in `set`. The `del` path exists in the frame
## shape but the r1 map is add-only: retraction is #60, so it stays empty here.
var seen = initHashSet[string]()
var set: seq[MarkerView]
var del: seq[string]
for m in markers:
seen.incl m.id
let frame = markerEntryJson(m)
if full or known.markers.getOrDefault(m.id) != frame:
known.markers[m.id] = frame
set.add m
for id in known.markers.keys.toSeq:
if id in seen: continue
known.markers.del id
if not full: del.add id
if full or set.len > 0 or del.len > 0:
del.sort()
return markerFrame(set, full, del)
proc egressFrames*(r: Recipient, ctx: Context, known: var Known, resync = false): seq[JsonNode] =
## Ordered like the 6.3 resync sequence: characters, encountered, then vision/explored and the
## tokens. Empty = nothing changed for this recipient, which is what keeps hidden mutations
@@ -624,6 +674,12 @@ proc egressFrames*(r: Recipient, ctx: Context, known: var Known, resync = false)
fresh.add other.exploredFull(ctx.scene)
result.add exploredFreshFrame(fresh)
# 6.3: `marker` (full) sits after `explored` because it is the same kind of memory and the client
# draws it over the fog `explored` just rebuilt. A diff follows the explored diff for the same
# reason. The GM never reaches this branch (3.1, 5.8).
let markerMsg = diffMarkers(known, view.markers, resync)
if not markerMsg.isNil: result.add markerMsg
var seen = initHashSet[string]()
for v in view.tokens:
seen.incl v.id