feat(db): typed wall storage, marker_seen and height backfill (#53)
This commit is contained in:
+97
-18
@@ -36,12 +36,30 @@ type
|
||||
wkWall = "wall"
|
||||
wkDoor = "door"
|
||||
|
||||
WallPosition* = enum
|
||||
wpCenter = "center"
|
||||
wpLeft = "left"
|
||||
wpRight = "right"
|
||||
|
||||
SightDir* = enum
|
||||
sdBoth = "both"
|
||||
sdLeft = "left"
|
||||
sdRight = "right"
|
||||
|
||||
WallRec* = object
|
||||
## The typed wall record of DESIGN 8. The wire still carries the legacy `kind`/`open` pair in
|
||||
## this round (#53); `kind` is derived from `door` and nothing else is exposed there yet (#54).
|
||||
id*: int
|
||||
a*, b*: Vec
|
||||
kind*: WallKind
|
||||
open*: bool
|
||||
flags*: JsonNode ## JObject; never nil for a loaded or created wall
|
||||
thickness*: float
|
||||
position*: WallPosition
|
||||
blocksSight*, blocksMovement*: bool
|
||||
sightDir*: SightDir
|
||||
playerMarker*: bool
|
||||
door*, open*, secret*, locked*: bool
|
||||
height*: Option[float] ## metres; none = full height (DESIGN 5.7)
|
||||
label*: Option[string] ## GM-only free text, 1..64 code points (DESIGN 5.8)
|
||||
flags*: JsonNode ## JObject; never nil for a loaded or created wall
|
||||
|
||||
SceneRec* = object
|
||||
## The one v1 scene (Q3 FIXED). Walls are kept in id order (ids only grow, appends only).
|
||||
@@ -90,6 +108,26 @@ const
|
||||
SceneNameMaxLen* = 64
|
||||
## `scene.set.name`: the 6.5 text rule, same 64-code-point bound as character names (#25).
|
||||
|
||||
proc defaultWall*(a, b: Vec): WallRec =
|
||||
## The `wall` preset's switches: both blocks on, both-way sight, no marker, no door settings.
|
||||
WallRec(a: a, b: b, blocksSight: true, blocksMovement: true, flags: newJObject())
|
||||
|
||||
proc kind*(w: WallRec): WallKind =
|
||||
## The legacy wire label for this round: only the door axis exists on it (#53). #54 replaces
|
||||
## this with the full derived shape label.
|
||||
if w.door: wkDoor else: wkWall
|
||||
|
||||
proc `kind=`*(w: var WallRec, k: WallKind) =
|
||||
## Legacy update path (`wall.update {kind}`): a wall is never open; clearing the door switch
|
||||
## also drops the door-only settings so the DB CHECK cannot see an impossible row.
|
||||
if k == wkDoor:
|
||||
w.door = true
|
||||
else:
|
||||
w.door = false
|
||||
w.open = false
|
||||
w.secret = false
|
||||
w.locked = false
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------
|
||||
# Field parsing (DESIGN 6.1 field notes: px floats, missing or wrongly typed -> `invalid`)
|
||||
|
||||
@@ -201,9 +239,12 @@ proc validWall*(sc: SceneRec, rec: WallRec): bool =
|
||||
(rec.a.x != rec.b.x or rec.a.y != rec.b.y) and inBounds(sc, rec.a) and inBounds(sc, rec.b)
|
||||
|
||||
proc normalize*(rec: WallRec): WallRec =
|
||||
## A wall is never open; only a door can be.
|
||||
## A wall is never open; only a door can be, and a non-door stores no door-only settings.
|
||||
result = rec
|
||||
if result.kind == wkWall: result.open = false
|
||||
if not result.door:
|
||||
result.open = false
|
||||
result.secret = false
|
||||
result.locked = false
|
||||
if result.flags.isNil: result.flags = newJObject()
|
||||
|
||||
proc wallIndex*(sc: SceneRec, id: int): Option[int] =
|
||||
@@ -229,6 +270,20 @@ proc patched*(w: WallRec, patch: WallPatch): 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"
|
||||
|
||||
template authoringTransaction*(db: DbConn, body: untyped) =
|
||||
## GM authoring durability (Q12): `synchronous=FULL` for this transaction, `NORMAL` again
|
||||
## afterwards, whether the body succeeded or raised. `PRAGMA synchronous` cannot change inside a
|
||||
@@ -260,16 +315,29 @@ proc storeScene*(db: DbConn, sc: SceneRec) =
|
||||
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 (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
||||
sceneId, w.id, w.a.x, w.a.y, w.b.x, w.b.y, $w.kind,
|
||||
(if w.open: 1 else: 0), flagsText(w.flags))
|
||||
let height = sqlLitRealOrNull(w.height)
|
||||
let label = sqlLitTextOrNull(w.label)
|
||||
db.exec(sql("INSERT INTO wall (scene_id, id, ax, ay, bx, by, thickness, position, " &
|
||||
"blocks_sight, blocks_movement, sight_dir, player_marker, door, open, secret, " &
|
||||
"locked, height, label, flags) " &
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " & height & ", " &
|
||||
label & ", ?)"),
|
||||
sceneId, w.id, w.a.x, w.a.y, w.b.x, w.b.y, $w.thickness, $w.position,
|
||||
(if w.blocksSight: 1 else: 0), (if w.blocksMovement: 1 else: 0), $w.sightDir,
|
||||
(if w.playerMarker: 1 else: 0), (if w.door: 1 else: 0), (if w.open: 1 else: 0),
|
||||
(if w.secret: 1 else: 0), (if w.locked: 1 else: 0), flagsText(w.flags))
|
||||
|
||||
proc replaceRow(db: DbConn, sceneId: int, w: WallRec) =
|
||||
db.exec(sql"""UPDATE wall SET ax = ?, ay = ?, bx = ?, by = ?, kind = ?, open = ?, flags = ?
|
||||
WHERE scene_id = ? AND id = ?""",
|
||||
w.a.x, w.a.y, w.b.x, w.b.y, $w.kind, (if w.open: 1 else: 0), flagsText(w.flags),
|
||||
sceneId, w.id)
|
||||
let height = sqlLitRealOrNull(w.height)
|
||||
let label = sqlLitTextOrNull(w.label)
|
||||
db.exec(sql("UPDATE wall SET ax = ?, ay = ?, bx = ?, by = ?, thickness = ?, position = ?, " &
|
||||
"blocks_sight = ?, blocks_movement = ?, sight_dir = ?, player_marker = ?, " &
|
||||
"door = ?, open = ?, secret = ?, locked = ?, height = " & height &
|
||||
", label = " & label & ", flags = ? WHERE scene_id = ? AND id = ?"),
|
||||
w.a.x, w.a.y, w.b.x, w.b.y, $w.thickness, $w.position,
|
||||
(if w.blocksSight: 1 else: 0), (if w.blocksMovement: 1 else: 0), $w.sightDir,
|
||||
(if w.playerMarker: 1 else: 0), (if w.door: 1 else: 0), (if w.open: 1 else: 0),
|
||||
(if w.secret: 1 else: 0), (if w.locked: 1 else: 0), flagsText(w.flags), sceneId, w.id)
|
||||
|
||||
proc addWall*(db: DbConn, sc: var SceneRec, rec: WallRec): Option[int] =
|
||||
## `wall.add`: assigns the scene's next wall id, persists and then appends to memory.
|
||||
@@ -310,7 +378,7 @@ proc setDoor*(db: DbConn, sc: var SceneRec, id: int, open: bool): WallApply =
|
||||
## `door.set`: only a door can be opened or closed (`invalid` on a plain wall).
|
||||
let idx = wallIndex(sc, id)
|
||||
if idx.isNone: return waNotFound
|
||||
if sc.walls[idx.get].kind != wkDoor: return waNotDoor
|
||||
if sc.walls[idx.get].door == false: return waNotDoor
|
||||
var stored = sc.walls[idx.get]
|
||||
stored.open = open
|
||||
authoringTransaction(db):
|
||||
@@ -325,15 +393,25 @@ proc setDoor*(db: DbConn, sc: var SceneRec, id: int, open: bool): WallApply =
|
||||
proc rowToWall(row: Row): WallRec =
|
||||
var flags: JsonNode
|
||||
try:
|
||||
flags = parseJson(row[7])
|
||||
flags = parseJson(row[17])
|
||||
except CatchableError:
|
||||
flags = newJObject()
|
||||
if flags.isNil or flags.kind != JObject: flags = newJObject()
|
||||
WallRec(id: parseInt(row[0]),
|
||||
a: (parseFloat(row[1]), parseFloat(row[2])),
|
||||
b: (parseFloat(row[3]), parseFloat(row[4])),
|
||||
kind: parseEnum[WallKind](row[5]),
|
||||
open: row[6] == "1",
|
||||
thickness: parseFloat(row[5]),
|
||||
position: parseEnum[WallPosition](row[6]),
|
||||
blocksSight: row[7] == "1",
|
||||
blocksMovement: row[8] == "1",
|
||||
sightDir: parseEnum[SightDir](row[9]),
|
||||
playerMarker: row[10] == "1",
|
||||
door: row[11] == "1",
|
||||
open: row[12] == "1",
|
||||
secret: row[13] == "1",
|
||||
locked: row[14] == "1",
|
||||
height: (if row[15].len == 0: none(float) else: some(parseFloat(row[15]))),
|
||||
label: (if row[16].len == 0: none(string) else: some(row[16])),
|
||||
flags: flags)
|
||||
|
||||
proc loadScene*(db: DbConn, sceneId = DefaultSceneId): Option[SceneRec] =
|
||||
@@ -361,7 +439,8 @@ proc loadScene*(db: DbConn, sceneId = DefaultSceneId): Option[SceneRec] =
|
||||
if result.isNone: return
|
||||
var sc = result.get
|
||||
for row in db.getAllRows(sql"""
|
||||
SELECT id, ax, ay, bx, by, kind, open, flags
|
||||
SELECT id, ax, ay, bx, by, thickness, position, blocks_sight, blocks_movement, sight_dir,
|
||||
player_marker, door, open, secret, locked, height, label, flags
|
||||
FROM wall WHERE scene_id = ? ORDER BY id""", sceneId):
|
||||
sc.walls.add rowToWall(row)
|
||||
result = some(sc)
|
||||
|
||||
Reference in New Issue
Block a user