server: scene.set closes the 29-intent catalogue, plus a log-leak fault hook (#25)

This commit is contained in:
2026-11-15 12:52:17 +01:00
parent df2b186267
commit 70a984c55a
7 changed files with 453 additions and 11 deletions
+90 -1
View File
@@ -12,6 +12,9 @@
## owner-delegated, #19, in DESIGN 6.1/14):
## - `scene` is an integer id; in v1 it is always the one seeded scene (id 1), and any other value
## is `not-found` (no existence oracle either way: only the GM can send these intents).
## - `scene.set` (slice 10, #25): the patch rules of 6.1 ("Scene payload rules"); `w`, `h`,
## `tile_size`, `cell` and `art_version` are not patch fields. `patched`/`writeSceneRow` are pure
## and transaction-free so main.nim can commit the scene row with the tokens it resized.
## - `wall.add`: `open` omitted = false; `open` on a `kind: "wall"` is `invalid` (a wall is never
## open), and the stored row normalizes it to false regardless.
## - `wall.update` patches `a`, `b`, `kind`, `flags` only (`open` belongs to `door.set`; an `open`
@@ -26,7 +29,7 @@
## no locking: the caller holds the global lock (4.3).
import std/[json, math, options, strutils]
import db_connector/db_sqlite
import geometry
import geometry, text
type
WallKind* = enum
@@ -66,6 +69,17 @@ type
kind*: Option[WallKind]
flags*: Option[JsonNode]
ScenePatch* = object
## `scene.set` (DESIGN 6.1): every field is optional; an absent field keeps its value.
## `w`, `h`, `tile_size`, `cell` and `art_version` are not patch fields (import is the M6 CLI):
## they are unknown fields and ignored (6.2), so a patch with only one of them has no field at
## all and is `invalid`.
name*: Option[string]
gridSize*: Option[float]
visionRadiusDefault*: Option[float]
tokenRadiusDefault*: Option[float]
extra*: Option[JsonNode]
SceneError* = object of CatchableError
## The DB and this build disagree about the scene (missing seed, unreadable row).
@@ -73,6 +87,8 @@ const
DefaultSceneId* = 1
## The seeded v1 scene (migration step 3). Exactly one scene exists in v1 (Q3).
WallKinds* = [wkWall, wkDoor]
SceneNameMaxLen* = 64
## `scene.set.name`: the 6.5 text rule, same 64-code-point bound as character names (#25).
# ---------------------------------------------------------------------------------------------------
# Field parsing (DESIGN 6.1 field notes: px floats, missing or wrongly typed -> `invalid`)
@@ -113,6 +129,66 @@ proc fieldOrNull*(env: JsonNode, key: string): JsonNode =
result = env{key}
if not result.isNil and result.kind == JNull: result = nil
proc parseFinite(node: JsonNode): Option[float] =
## One finite JSON number (integers accepted; strings, booleans and null are not numbers).
if node.isNil: return none(float)
case node.kind
of JInt: result = some(float(node.getInt))
of JFloat:
let f = node.getFloat
if classify(f) in {fcNan, fcInf, fcNegInf}: return none(float)
result = some(f)
else: discard
proc parseScenePatch*(node: JsonNode): Option[ScenePatch] =
## `scene.set` payload (DESIGN 6.1, slice 10 / #25). At least one patch field is required and a
## present field must have its own shape: a `null` is a wrong type, not "absent", because the
## field was declared (the same reading `token.resize` gives a required `radius`).
## name: the 6.5 text rule (1..64 code points, no control characters)
## grid_size: a finite number >= 0 (0 = no grid)
## vision_radius_default, token_radius_default: a finite number > 0; no upper cap (#4, #25)
## extra: a JSON object, stored verbatim and never interpreted
if node.isNil or node.kind != JObject: return none(ScenePatch)
var p: ScenePatch
var any = false
if node.hasKey("name"):
let v = node["name"]
if v.kind != JString or not validText(v.getStr, SceneNameMaxLen): return none(ScenePatch)
p.name = some(v.getStr)
any = true
if node.hasKey("grid_size"):
let v = parseFinite(node["grid_size"])
if v.isNone or v.get < 0: return none(ScenePatch)
p.gridSize = v
any = true
if node.hasKey("vision_radius_default"):
let v = parseFinite(node["vision_radius_default"])
if v.isNone or v.get <= 0: return none(ScenePatch)
p.visionRadiusDefault = v
any = true
if node.hasKey("token_radius_default"):
let v = parseFinite(node["token_radius_default"])
if v.isNone or v.get <= 0: return none(ScenePatch)
p.tokenRadiusDefault = v
any = true
if node.hasKey("extra"):
let v = node["extra"]
if v.kind != JObject: return none(ScenePatch)
p.extra = some(v)
any = true
if not any: return none(ScenePatch)
some(p)
proc patched*(sc: SceneRec, patch: ScenePatch): SceneRec =
## The scene as `scene.set` would store it. The caller validates the result by construction: every
## patch field was already type- and range-checked by `parseScenePatch`.
result = sc
if patch.name.isSome: result.name = patch.name.get
if patch.gridSize.isSome: result.gridSize = patch.gridSize.get
if patch.visionRadiusDefault.isSome: result.visionRadiusDefault = patch.visionRadiusDefault.get
if patch.tokenRadiusDefault.isSome: result.tokenRadiusDefault = patch.tokenRadiusDefault.get
if patch.extra.isSome: result.extra = patch.extra.get
# ---------------------------------------------------------------------------------------------------
# In-memory rules (pure): validation, lookup and the id allocator
@@ -170,6 +246,19 @@ template authoringTransaction*(db: DbConn, body: untyped) =
finally:
db.exec(sql"PRAGMA synchronous=NORMAL")
proc writeSceneRow*(db: DbConn, sc: SceneRec) =
## The `scene.set` columns, no transaction control: the caller owns the transaction (the scene row
## and the tokens its default radius resized commit together, #25).
db.exec(sql"""UPDATE scene SET name = ?, grid_size = ?, vision_radius_default = ?,
token_radius_default = ?, extra = ? WHERE id = ?""",
sc.name, $sc.gridSize, $sc.visionRadiusDefault, $sc.tokenRadiusDefault,
flagsText(sc.extra), sc.id)
proc storeScene*(db: DbConn, sc: SceneRec) =
## `scene.set` alone in memory (tests): one FULL authoring transaction (Q12).
authoringTransaction(db):
writeSceneRow(db, sc)
proc insertRow(db: DbConn, sceneId: int, w: WallRec) =
db.exec(sql"""INSERT INTO wall (scene_id, id, ax, ay, bx, by, kind, open, flags)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",