feat(walls): presets, label rule and one switch-set validation path (#55)
wall.add/wall.update now take a preset or raw switches, never both:
resolveWall expands the four 5.8 presets to switches before validating, so
a preset never touches geometry and preset+switch or preset+open is invalid.
A raw create starts from the wall defaults; a raw update patches only the
switches it names and {door:false} closes and de-secrets.
The switch-set invariants are enforced by that one path for add and update:
open/secret/locked without a door, a secret door that is open/one-way/not
opaque, open with secret or locked, and a direction with no sight to block
are all invalid. The DB gains the missing open+locked CHECK as the backstop,
and the now-redundant open/door parity test is gone from the server path.
A wall label is now the 6.5 text rule: trimmed, 1..64 code points, no control
characters, null clears, stored verbatim and GM-only. window/one_way presets
are accepted and expanded to their switch sets per DESIGN 5.8/6.1; their
player-facing behaviour stays with #57/#58.
Tests: WS black-box presets/patch semantics/every refusal/label round-trip;
Nim shape pins for expansion, invariants and the label rule.
Nim 385 / Node 117 green.
This commit is contained in:
+66
-61
@@ -520,8 +520,8 @@ proc parseWallGeometry(env: Envelope, rec: var WallRec): bool =
|
||||
|
||||
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.
|
||||
## 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:
|
||||
@@ -532,32 +532,47 @@ proc parseWallLabel(env: Envelope, rec: var WallRec): bool =
|
||||
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 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 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
|
||||
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
|
||||
rec.sightDir = d.get
|
||||
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).
|
||||
## 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
|
||||
if present(env, "a"):
|
||||
let v = parseVec(env.node{"a"})
|
||||
if v.isNone: return false
|
||||
@@ -592,31 +607,11 @@ proc parseWallPatch(env: Envelope, patch: var WallPatch): bool =
|
||||
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 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
|
||||
# otherwise an ignored unknown field on an update (DESIGN 6.1, #55).
|
||||
return false
|
||||
if present(env, "flags"):
|
||||
let f = parseFlags(env.node{"flags"})
|
||||
if f.isNone: return false
|
||||
@@ -626,9 +621,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.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
|
||||
patch.preset.isSome or patch.switches.hasAnySwitch or patch.flags.isSome
|
||||
|
||||
type
|
||||
ControllerCheck = enum
|
||||
@@ -2043,12 +2036,20 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
|
||||
if a.isNone or b.isNone:
|
||||
ackErr(conn, env.reqId, CodeInvalid)
|
||||
return
|
||||
# The raw-switch create: omitted switches keep the `wall` preset's defaults (DESIGN 6.1, #54).
|
||||
# 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)
|
||||
if not parseWallGeometry(env, candidate) or not parseWallSwitches(env, candidate, true) or
|
||||
not parseWallLabel(env, candidate):
|
||||
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):
|
||||
ackErr(conn, env.reqId, CodeInvalid)
|
||||
return
|
||||
let resolved = resolveWall(candidate, preset, switches)
|
||||
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)
|
||||
@@ -2118,18 +2119,22 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
|
||||
# `wall.update` = remove then add for the status rules (5.5): the rules run on the snapshot
|
||||
# with the patched wall at the same index, so a move away clears and a move onto a token sticks.
|
||||
let idx = wallIndex(app.scene, id).get
|
||||
var mr = moveScene(app.scene, app.tokens)
|
||||
outcome = updateWall(app.db, app.scene, patched(current.get, storedPatch))
|
||||
if outcome == waOk:
|
||||
let updated = wallById(app.scene, id)
|
||||
discard moverules.updateWall(mr, idx, ruleWall(updated.get))
|
||||
discard applyRuleOutcome(mr)
|
||||
refreshAll()
|
||||
persistAllExplored()
|
||||
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
|
||||
let candidate = patched(current.get, storedPatch)
|
||||
if candidate.isNone:
|
||||
outcome = waInvalid # an impossible resulting switch set (5.8)
|
||||
else:
|
||||
var mr = moveScene(app.scene, app.tokens)
|
||||
outcome = updateWall(app.db, app.scene, candidate.get)
|
||||
if outcome == waOk:
|
||||
let updated = wallById(app.scene, id)
|
||||
discard moverules.updateWall(mr, idx, ruleWall(updated.get))
|
||||
discard applyRuleOutcome(mr)
|
||||
refreshAll()
|
||||
persistAllExplored()
|
||||
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
|
||||
if outcome == waNotFound:
|
||||
ackErr(conn, env.reqId, CodeNotFound)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user