feat(db): typed wall storage, marker_seen and height backfill (#53)
This commit is contained in:
+73
-1
@@ -34,8 +34,14 @@
|
||||
## the GM, and `player.remove` reassigns them before the DELETE (a raw delete raises instead of
|
||||
## orphaning a record). `name_reveal` and `encountered` DO cascade on the player, because 6.1 deletes
|
||||
## that player's reveals and encountered list with them.
|
||||
## Step 8 ("wall record", Walls r1 / #53) rebuilds `wall` into the typed record of DESIGN 8 and adds
|
||||
## `marker_seen`. The legacy `kind` column is dropped, not preserved: every row is mapped to today's
|
||||
## behaviour (thickness 0, centre, both blocks on, both-way sight, no marker/door settings; a legacy
|
||||
## door keeps its `open`). `height` and `label` are nullable and written as SQL literals because
|
||||
## db_connector cannot bind NULL (4.4). The step's `data` hook is the height backfill: every entity
|
||||
## and character whose `props_gm` lacks a `height` key gets `1.8`, and nothing else is touched (5.7).
|
||||
## One connection, used under the server's global lock (4.3); this module does no locking itself.
|
||||
import std/[strutils, times]
|
||||
import std/[json, strutils, times]
|
||||
import db_connector/db_sqlite
|
||||
|
||||
type
|
||||
@@ -48,8 +54,27 @@ type
|
||||
stmts*: seq[string]
|
||||
## One or more DDL statements, run in order inside the step's transaction.
|
||||
## db_connector's exec() prepares a single statement, so they cannot be one string.
|
||||
data*: proc(db: DbConn) {.nimcall.}
|
||||
## Optional in-transaction data hook for what SQL cannot express portably (step 8's height
|
||||
## backfill). Runs after `stmts` and before the history row, inside the same transaction.
|
||||
MigrationRow* = tuple[version: int, name: string]
|
||||
|
||||
proc backfillHeights(db: DbConn) =
|
||||
## Migration step 8's `data` hook (DESIGN 4.4, 5.7): every existing token and character gets
|
||||
## `height: 1.8` in its GM bucket when that bucket is a JSON object without a `height` key.
|
||||
## `eye_height` is never written (the 93% rule derives it) and a hand-set height is untouched.
|
||||
for table in ["entity", "character"]:
|
||||
for row in db.getAllRows(sql("SELECT id, props_gm FROM " & table)):
|
||||
var props: JsonNode
|
||||
try:
|
||||
props = parseJson(row[1])
|
||||
except CatchableError:
|
||||
continue # unreadable props are left exactly as they are
|
||||
if props.isNil or props.kind != JObject or props.hasKey("height"):
|
||||
continue
|
||||
props["height"] = newJFloat(1.8)
|
||||
db.exec(sql("UPDATE " & table & " SET props_gm = ? WHERE id = ?"), $props, row[0])
|
||||
|
||||
const
|
||||
MigrationHistoryDdl* = """
|
||||
CREATE TABLE IF NOT EXISTS schema_migration (
|
||||
@@ -180,6 +205,51 @@ CREATE TABLE encountered (
|
||||
CREATE INDEX encountered_character ON encountered(character_id)""", """
|
||||
ALTER TABLE entity ADD COLUMN character_id TEXT REFERENCES character(id)""", """
|
||||
ALTER TABLE entity ADD COLUMN reveal_all INTEGER NOT NULL DEFAULT 0 CHECK (reveal_all IN (0, 1))"""]),
|
||||
Migration(version: 8, name: "wall record", stmts: @["""
|
||||
ALTER TABLE wall RENAME TO wall_v1""", """
|
||||
CREATE TABLE wall (
|
||||
scene_id INTEGER NOT NULL REFERENCES scene(id) ON DELETE CASCADE,
|
||||
id INTEGER NOT NULL,
|
||||
ax REAL NOT NULL, ay REAL NOT NULL,
|
||||
bx REAL NOT NULL, by REAL NOT NULL,
|
||||
thickness REAL NOT NULL DEFAULT 0 CHECK (thickness >= 0 AND thickness <= 256),
|
||||
position TEXT NOT NULL DEFAULT 'center' CHECK (position IN ('center', 'left', 'right')),
|
||||
blocks_sight INTEGER NOT NULL DEFAULT 1 CHECK (blocks_sight IN (0, 1)),
|
||||
blocks_movement INTEGER NOT NULL DEFAULT 1 CHECK (blocks_movement IN (0, 1)),
|
||||
sight_dir TEXT NOT NULL DEFAULT 'both' CHECK (sight_dir IN ('both', 'left', 'right')),
|
||||
player_marker INTEGER NOT NULL DEFAULT 0 CHECK (player_marker IN (0, 1)),
|
||||
door INTEGER NOT NULL DEFAULT 0 CHECK (door IN (0, 1)),
|
||||
open INTEGER NOT NULL DEFAULT 0 CHECK (open IN (0, 1)),
|
||||
secret INTEGER NOT NULL DEFAULT 0 CHECK (secret IN (0, 1)),
|
||||
locked INTEGER NOT NULL DEFAULT 0 CHECK (locked IN (0, 1)),
|
||||
height REAL NULL CHECK (height IS NULL OR (height > 0 AND height <= 100)),
|
||||
label TEXT NULL CHECK (label IS NULL OR (length(label) >= 1 AND length(label) <= 64)),
|
||||
flags TEXT NOT NULL DEFAULT '{}', -- JSON object, GM-defined, never interpreted
|
||||
PRIMARY KEY (scene_id, id),
|
||||
CHECK (open = 0 OR door = 1),
|
||||
CHECK (secret = 0 OR (door = 1 AND open = 0 AND blocks_sight = 1 AND blocks_movement = 1
|
||||
AND sight_dir = 'both')),
|
||||
CHECK (locked = 0 OR door = 1),
|
||||
CHECK (sight_dir = 'both' OR blocks_sight = 1)
|
||||
)""", """
|
||||
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)
|
||||
SELECT scene_id, id, ax, ay, bx, by, 0, 'center', 1, 1, 'both', 0,
|
||||
CASE kind WHEN 'door' THEN 1 ELSE 0 END,
|
||||
CASE kind WHEN 'door' THEN open ELSE 0 END,
|
||||
0, 0, NULL, NULL, flags
|
||||
FROM wall_v1""", """
|
||||
DROP TABLE wall_v1""", """
|
||||
CREATE TABLE marker_seen (
|
||||
player_id INTEGER NOT NULL REFERENCES player(id) ON DELETE CASCADE,
|
||||
scene_id INTEGER NOT NULL,
|
||||
wall_id INTEGER NOT NULL,
|
||||
marker_id TEXT NOT NULL, -- the opaque per-player 12-character id on the wire
|
||||
PRIMARY KEY (player_id, scene_id, wall_id),
|
||||
UNIQUE (player_id, marker_id),
|
||||
FOREIGN KEY (scene_id, wall_id) REFERENCES wall(scene_id, id) ON DELETE CASCADE
|
||||
)"""], data: backfillHeights),
|
||||
]
|
||||
LatestVersion* = migrations[^1].version
|
||||
## Derived, so adding a step cannot leave it stale.
|
||||
@@ -238,6 +308,8 @@ proc migrate*(db: DbConn): int =
|
||||
try:
|
||||
for stmt in m.stmts:
|
||||
db.exec(sql(stmt))
|
||||
if m.data != nil:
|
||||
m.data(db)
|
||||
let stamp = now().utc.format("yyyy-MM-dd'T'HH:mm:ss'Z'")
|
||||
db.exec(sql"INSERT INTO schema_migration (version, name, applied_at) VALUES (?, ?, ?)",
|
||||
m.version, m.name, stamp)
|
||||
|
||||
Reference in New Issue
Block a user