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
+29 -25
View File
@@ -32,7 +32,7 @@
## no locking: the caller holds the global lock (4.3).
import std/[json, math, options, strutils]
import db_connector/db_sqlite
import geometry, text
import db, geometry, text
type
WallPosition* = enum
@@ -53,8 +53,9 @@ type
pwWindow = "window"
pwOneWay = "one_way"
WallSwitches* = object
## The raw switches a payload names. A field that is `none` is not patched, which is what makes
WallSettings* = object
## The raw settings a payload names (GLOSSARY: a wall *switch* is not a door/open/secret/
## locked/sight-dir setting). A field that is `none` is not patched, which is what makes
## a raw-switch `wall.update` patch-only and a raw-switch `wall.add` start from the `wall`
## defaults (DESIGN 6.1). `open` is collected only for `wall.add`; on an update it is an
## unknown field and `door.set` owns it.
@@ -110,7 +111,7 @@ type
label*: Option[string]
clearLabel*: bool
preset*: Option[Preset] ## replaces the whole switch set; never with a raw switch
switches*: WallSwitches ## the raw switches present; patches only those
settings*: WallSettings ## the raw settings present; patches only those
flags*: Option[JsonNode]
ScenePatch* = object
@@ -158,13 +159,23 @@ proc derivedKind*(w: WallRec): string =
else:
"custom"
proc markerSig*(w: WallRec): string =
## The marker cause signature of DESIGN 5.8: exactly the wall fields whose change retracts a
## player marker — endpoints, thickness, placement, the `player_marker` switch and the derived
## `window` gate. Height and label have no marker consequence and are deliberately absent. Shared
## with `server/markers.nim` so the online retraction and the write-time delete cannot drift.
$w.a.x & "," & $w.a.y & "," & $w.b.x & "," & $w.b.y & "," &
$w.thickness & "," & $w.position & "," & $w.playerMarker & "," & derivedKind(w)
type
MarkerSig* = object
## The marker cause signature of DESIGN 5.8: exactly the wall fields whose change retracts a
## player marker. Compared field by field (never as a formatted string), so two signatures are
## equal exactly when those fields are. Height and label have no marker consequence and are
## deliberately absent.
a*, b*: Vec
thickness*: float
position*: WallPosition
playerMarker*: bool
kind*: string
proc markerSig*(w: WallRec): MarkerSig =
## The marker cause signature of DESIGN 5.8, shared with `server/markers.nim` so the online
## retraction and the write-time delete cannot drift.
MarkerSig(a: w.a, b: w.b, thickness: w.thickness, position: w.position,
playerMarker: w.playerMarker, kind: derivedKind(w))
# ---------------------------------------------------------------------------------------------------
# The one expand-and-validate path for wall writes (DESIGN 5.8, 6.1 FIXED #49/#55)
@@ -183,8 +194,8 @@ proc storable*(rec: WallRec): bool =
if rec.sightDir != sdBoth and not rec.blocksSight: return false
true
proc hasAnySwitch*(sw: WallSwitches): bool =
## True when the payload named any raw switch (a preset may not appear with one).
proc hasAnySetting*(sw: WallSettings): bool =
## True when the payload named any raw setting (a preset may not appear with one).
sw.blocksSight.isSome or sw.blocksMovement.isSome or sw.playerMarker.isSome or
sw.door.isSome or sw.open.isSome or sw.secret.isSome or sw.locked.isSome or
sw.sightDir.isSome
@@ -213,7 +224,7 @@ proc expandPreset*(base: WallRec, preset: Preset): WallRec =
result.secret = false
result.locked = false
proc applySwitches(base: WallRec, sw: WallSwitches): Option[WallRec] =
proc applySettings(base: WallRec, sw: WallSettings): Option[WallRec] =
## A raw-switch write patches only the switches it names. A door-only switch set to true on a
## non-door is `invalid` (never silently dropped); when the write clears `door`, inherited
## door-only settings are closed and de-secreted (DESIGN 5.8, 6.1).
@@ -235,16 +246,16 @@ proc applySwitches(base: WallRec, sw: WallSwitches): Option[WallRec] =
some(r)
proc resolveWall*(base: WallRec, preset: Option[Preset],
sw: WallSwitches): Option[WallRec] =
sw: WallSettings): Option[WallRec] =
## THE single path shared by `wall.add`, `wall.update` and the future UVTT importer: a preset is
## expanded before validation and may not appear with any raw switch; raw switches patch the
## base; the resulting switch set is then checked by `storable`. Geometry, height and label are
## validated by `validWall`; a preset never touches them (DESIGN 5.8, 6.1 FIXED #49/#55).
if preset.isSome:
if sw.hasAnySwitch: return none(WallRec)
if sw.hasAnySetting: return none(WallRec)
result = some(expandPreset(base, preset.get))
else:
result = applySwitches(base, sw)
result = applySettings(base, sw)
if result.isSome and not storable(result.get): result = none(WallRec)
# ---------------------------------------------------------------------------------------------------
@@ -397,7 +408,7 @@ proc patched*(w: WallRec, patch: WallPatch): Option[WallRec] =
if patch.clearLabel: base.label = none(string)
elif patch.label.isSome: base.label = patch.label
if patch.flags.isSome: base.flags = patch.flags.get
result = resolveWall(base, patch.preset, patch.switches)
result = resolveWall(base, patch.preset, patch.settings)
if result.isSome:
result = some(normalize(result.get))
@@ -407,17 +418,10 @@ proc patched*(w: WallRec, patch: WallPatch): Option[WallRec] =
proc flagsText(node: JsonNode): string =
if node.isNil: "{}" else: $node
proc sqlLitText*(value: string): string =
## A SQL text literal (DESIGN 4.4): single quotes doubled. Used for `label`, which is nullable.
"'" & value.replace("'", "''") & "'"
proc sqlLitReal*(value: float): string =
## A fixed, locale-free SQL number literal. Used for `height`, which is nullable.
$value
proc sqlLitTextOrNull*(value: Option[string]): string =
if value.isSome: sqlLitText(value.get) else: "NULL"
proc sqlLitRealOrNull*(value: Option[float]): string =
if value.isSome: sqlLitReal(value.get) else: "NULL"