feat(walls): proto 2 typed wall record, inert geometry, derived kind (#54)

wall.add/wall.update take thickness, position, height and the eight raw
switches; kind is output-only and the GM wall frames echo the full typed
record. Geometry is stored but inert (0/full/centre behaviour), and the
legacy kind/open wire adapter is gone. proto is bumped to 2 with the
harness default, and BannedAlways gains every wall field name with a
WALLDATA positive control.

Tests: schema-7 migrated scene vs the same walls authored via raw switches
gives byte-identical player frames, refusals and per-tile statuses.
Nim 382 / Node 113 green.
This commit is contained in:
2026-11-19 22:21:47 +01:00
parent c1b1e9cf91
commit 77fa2dfe4c
23 changed files with 852 additions and 318 deletions
+164 -58
View File
@@ -137,7 +137,15 @@ proc closeGone(conn: Conn, reason: string, code: int) =
# views.egressFrames and never enter a PlayerView at all.
proc wallObjectOf(w: WallRec): JsonNode =
wallObject(w.id, w.a, w.b, $w.kind, w.open, w.flags)
## The GM's typed wall (DESIGN 6.1 proto 2): every stored field plus the derived shape label.
wallObject(WallView(id: w.id, a: w.a, b: w.b, thickness: w.thickness, position: $w.position,
blocksSight: w.blocksSight, blocksMovement: w.blocksMovement,
sightDir: $w.sightDir, playerMarker: w.playerMarker, door: w.door,
open: w.open, secret: w.secret, locked: w.locked,
height: (if w.height.isSome: w.height.get else: 0.0),
hasHeight: w.height.isSome,
label: (if w.label.isSome: w.label.get else: ""),
hasLabel: w.label.isSome, kind: derivedKind(w), flags: w.flags))
proc wallsSnapshot(sc: SceneRec): JsonNode =
var walls: seq[JsonNode]
@@ -484,6 +492,144 @@ proc declared(env: Envelope, key: string): bool =
## `vision_radius: null` means "the scene default" (6.1), so both are real patch fields.
not env.node.isNil and env.node.hasKey(key)
# Wall payload fields (DESIGN 6.1, proto 2, #54). `kind` and (in an update) `open` are never read:
# they are unknown fields and ignored (6.2). Absent and JSON null are the same field-absent for
# every wall field except `height` and `label`, where an explicit null means "clear it".
proc parseWallGeometry(env: Envelope, rec: var WallRec): bool =
## thickness (px), position and height (metres); the ranges are checked by `validWall`.
if present(env, "thickness"):
let v = parseFinite(fieldOrNull(env.node, "thickness"))
if v.isNone: return false
rec.thickness = v.get
if present(env, "position"):
let node = env.node{"position"}
if node.kind != JString: return false
let p = parsePosition(node.getStr)
if p.isNone: return false
rec.position = p.get
if declared(env, "height"):
let node = env.node{"height"}
if node.kind == JNull:
rec.height = none(float)
else:
let v = parseFinite(node)
if v.isNone: return false
rec.height = v
true
proc parseWallLabel(env: Envelope, rec: var WallRec): bool =
## `label` absent leaves it alone on an update (the caller seeds `rec` from the stored row);
## an explicit null clears it; anything but a string is `invalid`. The 6.5 text rule is #55,
## but the schema's 1..64 bound is enforced by `validWall` so SQLite cannot raise.
if not declared(env, "label"): return true
let node = env.node{"label"}
if node.kind == JNull:
rec.label = none(string)
elif node.kind == JString:
rec.label = some(node.getStr)
else:
return false
true
proc parseWallSwitch(env: Envelope, key: string, dest: var bool): bool =
## One boolean switch: absent = leave `dest`; a non-boolean is `invalid`.
if not present(env, key): return true
boolField(env, key, dest)
proc parseWallSwitches(env: Envelope, rec: var WallRec, allowOpen: bool): bool =
## The raw 5.8 switches on a create: omitted switches keep the candidate's value (the `wall`
## defaults), `open` is accepted here. A `sight_dir` outside the three words is `invalid`.
if not parseWallSwitch(env, "blocks_sight", rec.blocksSight): return false
if not parseWallSwitch(env, "blocks_movement", rec.blocksMovement): return false
if not parseWallSwitch(env, "player_marker", rec.playerMarker): return false
if not parseWallSwitch(env, "door", rec.door): return false
if allowOpen and not parseWallSwitch(env, "open", rec.open): return false
if not parseWallSwitch(env, "secret", rec.secret): return false
if not parseWallSwitch(env, "locked", rec.locked): return false
if present(env, "sight_dir"):
let node = env.node{"sight_dir"}
if node.kind != JString: return false
let d = parseSightDir(node.getStr)
if d.isNone: return false
rec.sightDir = d.get
true
proc parseWallPatch(env: Envelope, patch: var WallPatch): bool =
## `wall.update` names only the fields it patches. `open` and `kind` are unknown here and are
## ignored: a door's open state changes only through `door.set` (DESIGN 6.1, #54).
if present(env, "a"):
let v = parseVec(env.node{"a"})
if v.isNone: return false
patch.a = v
if present(env, "b"):
let v = parseVec(env.node{"b"})
if v.isNone: return false
patch.b = v
if present(env, "thickness"):
let v = parseFinite(fieldOrNull(env.node, "thickness"))
if v.isNone: return false
patch.thickness = v
if present(env, "position"):
let node = env.node{"position"}
if node.kind != JString: return false
let p = parsePosition(node.getStr)
if p.isNone: return false
patch.position = p
if declared(env, "height"):
let node = env.node{"height"}
if node.kind == JNull:
patch.clearHeight = true
else:
let v = parseFinite(node)
if v.isNone: return false
patch.height = v
if declared(env, "label"):
let node = env.node{"label"}
if node.kind == JNull:
patch.clearLabel = true
elif node.kind == JString:
patch.label = some(node.getStr)
else:
return false
var bit: bool
if present(env, "blocks_sight"):
if not boolField(env, "blocks_sight", bit): return false
patch.blocksSight = some(bit)
if present(env, "blocks_movement"):
if not boolField(env, "blocks_movement", bit): return false
patch.blocksMovement = some(bit)
if present(env, "player_marker"):
if not boolField(env, "player_marker", bit): return false
patch.playerMarker = some(bit)
if present(env, "door"):
if not boolField(env, "door", bit): return false
patch.door = some(bit)
if present(env, "secret"):
if not boolField(env, "secret", bit): return false
patch.secret = some(bit)
if present(env, "locked"):
if not boolField(env, "locked", bit): return false
patch.locked = some(bit)
if present(env, "sight_dir"):
let node = env.node{"sight_dir"}
if node.kind != JString: return false
let d = parseSightDir(node.getStr)
if d.isNone: return false
patch.sightDir = d
if present(env, "flags"):
let f = parseFlags(env.node{"flags"})
if f.isNone: return false
patch.flags = f
true
proc wallPatchHasField(patch: WallPatch): bool =
patch.a.isSome or patch.b.isSome or patch.thickness.isSome or patch.position.isSome or
patch.height.isSome or patch.clearHeight or patch.label.isSome or patch.clearLabel or
patch.blocksSight.isSome or patch.blocksMovement.isSome or patch.sightDir.isSome or
patch.playerMarker.isSome or patch.door.isSome or patch.secret.isSome or patch.locked.isSome or
patch.flags.isSome
type
ControllerCheck = enum
ccOk
@@ -1897,20 +2043,10 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
if a.isNone or b.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
let kindNode = env.node{"kind"}
if kindNode.isNil or kindNode.kind != JString:
ackErr(conn, env.reqId, CodeInvalid)
return
let kind = parseKind(kindNode.getStr)
if kind.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
let open = parseOpen(fieldOrNull(env.node, "open"))
if open.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
if kind.get == wkWall and open.get:
# A wall is never open; only a door has an open state (DESIGN 6.1, 8).
# The raw-switch create: omitted switches keep the `wall` preset's defaults (DESIGN 6.1, #54).
var candidate = defaultWall(a.get, b.get)
if not parseWallGeometry(env, candidate) or not parseWallSwitches(env, candidate, true) or
not parseWallLabel(env, candidate):
ackErr(conn, env.reqId, CodeInvalid)
return
let flags = parseFlags(fieldOrNull(env.node, "flags"))
@@ -1923,16 +2059,15 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
var ok = false
withLock app.dbLock:
sceneId = app.scene.id # the request's one read (#31)
# `candidate` is built here so its `flags` copy and its record copy both die under the lock;
# `detachedJson` keeps the stored object independent of `env` (ANALYSIS 4.1).
let candidate = WallRec(a: a.get, b: b.get, door: kind.get == wkDoor,
open: open.get, blocksSight: true, blocksMovement: true,
flags: detachedJson(flags.get))
# The record is copied here so its `flags` object and the record copy both die under the
# lock; `detachedJson` keeps the stored object independent of `env` (ANALYSIS 4.1).
var stored = candidate
stored.flags = detachedJson(flags.get)
# 5.5 SET: the new wall sticks every token its circle overlaps. The rules run on a snapshot
# taken before the write (the id is assigned by the write), and their outcome is applied only
# after it committed, so a failed write leaves every token untouched.
var mr = moveScene(app.scene, app.tokens)
let id = addWall(app.db, app.scene, candidate)
let id = addWall(app.db, app.scene, stored)
if id.isSome:
let added = wallById(app.scene, id.get)
discard moverules.addWall(mr, ruleWall(added.get))
@@ -1940,7 +2075,7 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
refreshAll() # 4.2 step 5: a wall change moves every player's sight
persistAllExplored() # step 6: the chunks the new wall revealed, before the ack
addedId = added.get.id
addedKind = $added.get.kind
addedKind = derivedKind(added.get)
# The wall frame references `added.flags`; build it and fan it out here, before the record
# copy leaves scope, never after the lock is released (ANALYSIS 4.1).
settleWorldLocked(conn, env.reqId, wallFrame(sceneId, wallObjectOf(added.get)),
@@ -1960,41 +2095,12 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
ackErr(conn, env.reqId, CodeInvalid)
return
var patch: WallPatch
var anyField = false
if not fieldOrNull(env.node, "a").isNil:
let a = parseVec(env.node{"a"})
if a.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
patch.a = a
anyField = true
if not fieldOrNull(env.node, "b").isNil:
let b = parseVec(env.node{"b"})
if b.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
patch.b = b
anyField = true
if not fieldOrNull(env.node, "kind").isNil:
let node = env.node{"kind"}
if node.kind != JString:
ackErr(conn, env.reqId, CodeInvalid)
return
let kind = parseKind(node.getStr)
if kind.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
patch.kind = kind
anyField = true
if not fieldOrNull(env.node, "flags").isNil:
let flags = parseFlags(env.node{"flags"})
if flags.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
patch.flags = flags
anyField = true
if not anyField:
# `wall.update` changes at least one of a, b, kind, flags (6.1).
if not parseWallPatch(env, patch):
ackErr(conn, env.reqId, CodeInvalid)
return
if not wallPatchHasField(patch):
# A fieldless update (or one naming only the ignored `open`/`kind`) is `invalid`, before the
# target lookup (6.1, 6.5 steps 7 then 8).
ackErr(conn, env.reqId, CodeInvalid)
return
var outcome = waNotFound
@@ -2020,7 +2126,7 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
discard applyRuleOutcome(mr)
refreshAll()
persistAllExplored()
updatedKind = $updated.get.kind
updatedKind = derivedKind(updated.get)
# Frame and record copy both live and die inside this lock (ANALYSIS 4.1).
settleWorldLocked(conn, env.reqId, wallFrame(sceneId, wallObjectOf(updated.get)))
ok = true