server: token records and the entity migration step (#20)

Migration step 4 ("tokens") creates the entity table: the token id, scene, kind,
both names, position, nullable radius/owner/vision_radius, gm_only and flags. A
NULL owner is GM-controlled, so player.remove hands that player's tokens to the GM
through the FK; radius, props and character_id stay out until slices 7-8.

server/tokens.nim holds the record, the 6.1 field rules (12 CSPRNG characters from
a 64-symbol alphabet, trimmed names, kind, positive vision_radius), the write-through
persistence (FULL for GM authoring, NORMAL for a move) and the loader. db_connector
quotes every argument, so the three nullable columns are emitted as numeric literals
while text still goes through ? arguments.
This commit is contained in:
2026-11-15 09:35:05 +01:00
parent 8b595156f0
commit d38b871dfa
6 changed files with 554 additions and 12 deletions
+19
View File
@@ -10,6 +10,9 @@
## 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).
## Step 4 ("tokens", M3 slice 5 / #20) creates `entity` for the map figures (DESIGN 4.4, 8). The
## character link, the name-reveal flags and the three property buckets are slices 7-8 (#22, #23) and
## are not columns yet; `entity_status` and `explored_chunk` arrive with slice 7 and slice 6.
## 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
@@ -90,6 +93,22 @@ CREATE TABLE wall (
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, '{}')"""]),
Migration(version: 4, name: "tokens", stmts: @["""
CREATE TABLE entity (
id TEXT PRIMARY KEY, -- 12 random chars from [A-Za-z0-9_-] (#3, DESIGN 8)
scene_id INTEGER NOT NULL REFERENCES scene(id) ON DELETE CASCADE,
kind TEXT NOT NULL, -- free string (pc, npc, item...); GM-only on the wire
descriptive_name TEXT NOT NULL, -- the name players see until the GM reveals (8)
actual_name TEXT NOT NULL,
x REAL NOT NULL,
y REAL NOT NULL,
radius REAL, -- NULL = scene token_radius_default (#4, slice 7)
owner_player_id INTEGER REFERENCES player(id) ON DELETE SET NULL, -- NULL = GM-controlled
gm_only INTEGER NOT NULL DEFAULT 0 CHECK (gm_only IN (0, 1)),
vision_radius REAL, -- NULL = scene vision_radius_default (Q7)
flags TEXT NOT NULL DEFAULT '{}', -- JSON object; the `vision` flag lives here (8)
created_at TEXT NOT NULL
)"""]),
]
LatestVersion* = migrations[^1].version
## Derived, so adding a step cannot leave it stale.