server: scene and wall state, slice-4 migration (#19)
Migration step 3 ("world") creates scene and wall and seeds the single v1 scene
(id 1, 4096x4096, tile = cell 32, vision 400, token radius 20, art_version 1,
next_wall_id 1). server/world.nim holds the in-memory scene with SQLite as
write-through persistence: per-scene wall ids from next_wall_id (monotonic,
never reused, #3), 6.1 validation (a != b, in bounds, a wall is never open),
flags as an opaque JSON object, and one transaction with synchronous=FULL
around every GM authoring write (Q12); memory follows COMMIT, so a failed
write leaves both sides unchanged.
Tests: tests/tworld.nim (ids, reload, doors, validation, pragma, failed write),
tests/tdb.nim (version 3, history, a slice-2 DB migrating to slice 3).
This commit is contained in:
@@ -7,6 +7,9 @@
|
||||
## - One transaction per step: DDL + history row + user_version together. A failing step leaves
|
||||
## nothing behind; a fully migrated DB is not written to at all (re-open is a no-op).
|
||||
## - Engine settings: WAL, synchronous=NORMAL (Q12), busy_timeout=5000, foreign_keys=ON.
|
||||
## Step 3 ("world", M3 slice 4 / #19) creates `scene` and `wall` and seeds the single v1 scene
|
||||
## (DESIGN 4.4, 8; Q3 FIXED: exactly one scene). Wall ids are per scene and come from
|
||||
## `scene.next_wall_id`, which never decreases (#3 FIXED).
|
||||
## One connection, used under the server's global lock (4.3); this module does no locking itself.
|
||||
import std/[strutils, times]
|
||||
import db_connector/db_sqlite
|
||||
@@ -58,6 +61,35 @@ CREATE TABLE session (
|
||||
last_seen TEXT NOT NULL
|
||||
)""", """
|
||||
CREATE INDEX session_player ON session(player_id)"""]),
|
||||
Migration(version: 3, name: "world", stmts: @["""
|
||||
CREATE TABLE scene (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
w REAL NOT NULL,
|
||||
h REAL NOT NULL,
|
||||
tile_size INTEGER NOT NULL, -- px; == cell in v1 (Q4)
|
||||
cell INTEGER NOT NULL, -- explored cell px; tile_size % cell == 0
|
||||
grid_size REAL NOT NULL, -- px; 0 = no grid
|
||||
vision_radius_default REAL NOT NULL, -- px; also the player step cap (5.5)
|
||||
token_radius_default REAL NOT NULL, -- px; token size when the token sets none (#4)
|
||||
art_version INTEGER NOT NULL, -- tiles URL version; 1 in M3 (3.3)
|
||||
active INTEGER NOT NULL DEFAULT 1,
|
||||
next_wall_id INTEGER NOT NULL DEFAULT 1, -- monotonic, never reused (#3, 8)
|
||||
extra TEXT NOT NULL DEFAULT '{}'
|
||||
)""", """
|
||||
CREATE TABLE wall (
|
||||
scene_id INTEGER NOT NULL REFERENCES scene(id) ON DELETE CASCADE,
|
||||
id INTEGER NOT NULL, -- per scene, from next_wall_id (#3, 8)
|
||||
ax REAL NOT NULL, ay REAL NOT NULL,
|
||||
bx REAL NOT NULL, by REAL NOT NULL,
|
||||
kind TEXT NOT NULL CHECK (kind IN ('wall', 'door')),
|
||||
open INTEGER NOT NULL DEFAULT 0 CHECK (open IN (0, 1)),
|
||||
flags TEXT NOT NULL DEFAULT '{}', -- JSON object, GM-defined, never interpreted
|
||||
PRIMARY KEY (scene_id, id)
|
||||
)""", """
|
||||
INSERT INTO scene (id, name, w, h, tile_size, cell, grid_size, vision_radius_default,
|
||||
token_radius_default, art_version, active, next_wall_id, extra)
|
||||
VALUES (1, 'Scene', 4096, 4096, 32, 32, 0, 400, 20, 1, 1, 1, '{}')"""]),
|
||||
]
|
||||
LatestVersion* = migrations[^1].version
|
||||
## Derived, so adding a step cannot leave it stale.
|
||||
|
||||
Reference in New Issue
Block a user