fix: walls r1 review — server (ref #52)

This commit is contained in:
2026-11-20 08:42:50 +01:00
parent f9f26a4a01
commit e1a5b297e4
11 changed files with 198 additions and 167 deletions
+76 -103
View File
@@ -355,9 +355,12 @@ proc resync(connId: int, named: bool, player: Player) =
refreshPlayer(player.id)
# A marker minted here (a window the player had not seen before connecting) must be durable like
# any other derived memory before the resync sends it (5.8, 4.2 step 6). Only markers: the
# explored grid keeps its own existing connect behaviour (nothing is stored here).
app.db.withTransaction:
persistMarkers(@[player.id])
# explored grid keeps its own existing connect behaviour (nothing is stored here). No
# transaction when there is nothing to write: the common connect must not open one.
if app.visions.mgetOrPut(player.id, PlayerVision()).markerDead.len > 0 or
app.visions.mgetOrPut(player.id, PlayerVision()).markerFresh.len > 0:
app.db.withTransaction:
persistMarkers(@[player.id])
# A token visible right now is a sighting: record it before the `encountered` list is built (the
# player may have been offline while something walked into their sight).
recordEncounters()
@@ -522,83 +525,12 @@ proc declared(env: Envelope, key: string): bool =
# 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 (trimmed,
## 1..64 code points, no control characters) is enforced by `validWall` before the write (#55).
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 parseWallPreset(env: Envelope, dest: var Option[Preset]): bool =
## `preset` is a 5.8 name, never JSON null; anything else is `invalid`.
if not declared(env, "preset"): return true
let node = env.node{"preset"}
if node.kind != JString: return false
let p = parsePreset(node.getStr)
if p.isNone: return false
dest = p
true
proc parseWallSwitch(env: Envelope, key: string, dest: var Option[bool]): bool =
## One raw switch: absent = not patched; a non-boolean is `invalid`.
if not present(env, key): return true
var bit: bool
if not boolField(env, key, bit): return false
dest = some(bit)
true
proc parseWallSwitches(env: Envelope, sw: var WallSwitches, allowOpen: bool): bool =
## Collects the raw 5.8 switches the payload names. `open` is collected only on a create; in an
## update it is an unknown field and is ignored (`door.set` owns it, DESIGN 6.1).
if not parseWallSwitch(env, "blocks_sight", sw.blocksSight): return false
if not parseWallSwitch(env, "blocks_movement", sw.blocksMovement): return false
if not parseWallSwitch(env, "player_marker", sw.playerMarker): return false
if not parseWallSwitch(env, "door", sw.door): return false
if allowOpen and not parseWallSwitch(env, "open", sw.open): return false
if not parseWallSwitch(env, "secret", sw.secret): return false
if not parseWallSwitch(env, "locked", sw.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
sw.sightDir = d
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). A preset
## replaces the whole switch set and may not appear with any raw switch, `open` included (#55).
if not parseWallPreset(env, patch.preset): return false
proc parseWallGeometryAndLabel(env: Envelope, patch: var WallPatch): bool =
## THE one parser for the geometry and label fields of `wall.add` and `wall.update` (6.1).
## One rule for both payloads: `a`, `b`, `thickness` and `position` treat absent and JSON null as
## the same field-absent, while `height` and `label` treat an explicit null as a real patch
## ("clear it"), carried by `clearHeight`/`clearLabel`. Ranges and the 6.5 text rule are checked
## once on the resulting wall by `validWall`.
if present(env, "a"):
let v = parseVec(env.node{"a"})
if v.isNone: return false
@@ -633,9 +565,54 @@ proc parseWallPatch(env: Envelope, patch: var WallPatch): bool =
patch.label = some(node.getStr)
else:
return false
if not parseWallSwitches(env, patch.switches, false): return false
if patch.preset.isSome and (patch.switches.hasAnySwitch or declared(env, "open")):
# preset xor raw switches; an `open` field alongside a preset is `invalid` even though open is
true
proc parseWallPreset(env: Envelope, dest: var Option[Preset]): bool =
## `preset` is a 5.8 name, never JSON null; anything else is `invalid`.
if not declared(env, "preset"): return true
let node = env.node{"preset"}
if node.kind != JString: return false
let p = parsePreset(node.getStr)
if p.isNone: return false
dest = p
true
proc parseWallSetting(env: Envelope, key: string, dest: var Option[bool]): bool =
## One raw setting: absent = not patched; a non-boolean is `invalid`.
if not present(env, key): return true
var bit: bool
if not boolField(env, key, bit): return false
dest = some(bit)
true
proc parseWallSettings(env: Envelope, sw: var WallSettings, allowOpen: bool): bool =
## Collects the raw 5.8 settings the payload names. `open` is collected only on a create; in an
## update it is an unknown field and is ignored (`door.set` owns it, DESIGN 6.1).
if not parseWallSetting(env, "blocks_sight", sw.blocksSight): return false
if not parseWallSetting(env, "blocks_movement", sw.blocksMovement): return false
if not parseWallSetting(env, "player_marker", sw.playerMarker): return false
if not parseWallSetting(env, "door", sw.door): return false
if allowOpen and not parseWallSetting(env, "open", sw.open): return false
if not parseWallSetting(env, "secret", sw.secret): return false
if not parseWallSetting(env, "locked", sw.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
sw.sightDir = d
true
proc parseWallPatch(env: Envelope, patch: var WallPatch, allowOpen = false): bool =
## The one wall-field parser: `wall.update` and, with `allowOpen`, `wall.add`. `open` and `kind`
## are unknown on an update and are ignored: a door's open state changes only through `door.set`
## (DESIGN 6.1, #54). A preset replaces the whole setting set and may not appear with any raw
## setting, `open` included (`open:false` too, #55).
if not parseWallGeometryAndLabel(env, patch): return false
if not parseWallPreset(env, patch.preset): return false
if not parseWallSettings(env, patch.settings, allowOpen): return false
if patch.preset.isSome and (patch.settings.hasAnySetting or declared(env, "open")):
# preset xor raw settings; an `open` field alongside a preset is `invalid` even though open is
# otherwise an ignored unknown field on an update (DESIGN 6.1, #55).
return false
if present(env, "flags"):
@@ -647,7 +624,7 @@ proc parseWallPatch(env: Envelope, patch: var WallPatch): bool =
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.preset.isSome or patch.switches.hasAnySwitch or patch.flags.isSome
patch.preset.isSome or patch.settings.hasAnySetting or patch.flags.isSome
type
ControllerCheck = enum
@@ -2057,29 +2034,24 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
of "wall.add":
if not sceneOk(conn, env): return
let a = parseVec(fieldOrNull(env.node, "a"))
let b = parseVec(fieldOrNull(env.node, "b"))
if a.isNone or b.isNone:
# One parser for add and update. A raw-setting create starts from the `wall` defaults; a preset
# replaces the setting set. Both go through the same expand-and-validate path (DESIGN 6.1,
# #55); `a` and `b` are the only required fields here.
var patch: WallPatch
if not parseWallPatch(env, patch, allowOpen = true):
ackErr(conn, env.reqId, CodeInvalid)
return
# A raw-switch create starts from the `wall` defaults; a preset replaces the switch set. Both
# go through the same expand-and-validate path (DESIGN 6.1, #55).
var candidate = defaultWall(a.get, b.get)
var preset: Option[Preset]
var switches: WallSwitches
if not parseWallGeometry(env, candidate) or not parseWallPreset(env, preset) or
not parseWallSwitches(env, switches, true) or not parseWallLabel(env, candidate):
if patch.a.isNone or patch.b.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
let resolved = resolveWall(candidate, preset, switches)
let resolved = patched(defaultWall(patch.a.get, patch.b.get), patch)
if resolved.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
candidate = resolved.get
let flags = parseFlags(fieldOrNull(env.node, "flags"))
if flags.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
var candidate = resolved.get
# `label` is the intent's string: copy it at the boundary so the stored wall shares no payload
# with the request (ANALYSIS 4.1, jsonbound).
if candidate.label.isSome: candidate.label = some(detachedString(candidate.label.get))
var addedId = 0
var addedKind = ""
var sceneId = 0
@@ -2089,7 +2061,7 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
# 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)
stored.flags = detachedJson(candidate.flags)
# 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.
@@ -2136,9 +2108,10 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
var ok = false
withLock app.dbLock:
sceneId = app.scene.id # the request's one read (#31)
# The flags object goes into the wall record verbatim: detach it here so both the record and
# this local die under the lock (ANALYSIS 4.1).
# The label string and flags object go into the wall record verbatim: detach them here so
# both the record and this local die under the lock (ANALYSIS 4.1).
var storedPatch = patch
if patch.label.isSome: storedPatch.label = some(detachedString(patch.label.get))
if patch.flags.isSome: storedPatch.flags = some(detachedJson(patch.flags.get))
let current = wallById(app.scene, id)
if current.isSome: