feat(m4): playtest friction: perf overlay, fresh import, token seed (#82)

Opt-in ?perf=1 overlay (fps, frame p50/p95, JS p50/p95, tokens, zoom; no node or ticker work when off). fff-vtt import creates and migrates a fresh/empty state dir through the shared server startup path; devbox run serve gains --image. devbox run seed creates deterministic labelled NPC tokens over the real WS (idempotent rerun, --replace), and the GM token form defaults X/Y to the viewport centre.
This commit is contained in:
2026-11-21 17:20:05 +01:00
parent 356705823b
commit 9c7f9de910
20 changed files with 933 additions and 53 deletions
+11
View File
@@ -347,3 +347,14 @@ proc migrate*(db: DbConn): int =
except CatchableError: discard # the original error is the one worth reporting
raise newException(MigrationError,
"migration " & $m.version & " (" & m.name & ") failed: " & e.msg)
proc openMigratedDatabase*(path: string): tuple[db: DbConn, applied: int] =
## The one startup path shared by the server, `gm-key` and `import` (#82): open the DB with the
## engine settings above, then apply every pending migration. A failure closes the connection
## before re-raising, so a caller that handles the error never leaks the handle.
result.db = openDatabase(path)
try:
result.applied = migrate(result.db)
except CatchableError:
close(result.db)
raise
+22 -12
View File
@@ -32,8 +32,10 @@ Options:
positive multiple of the scene's explored cell (32 px), so 64 is the
designed fallback.
--scene=<id> Target scene. v1 has exactly one scene, id 1.
--state-dir=<path> Directory holding state.db and tiles (default: run). The server must be
stopped: the state-dir lock is refused (exit 3), like gm-key --new.
--state-dir=<path> Directory holding state.db and tiles (default: run). A missing or empty
directory is created and migrated through the server's startup path, so
an import works before the first serve. The server must be stopped: the
state-dir lock is refused (exit 3), like gm-key --new.
Tiles land in <state-dir>/tiles/<scene>/v<art_version>/<x>_<y>.png, origin at the world origin.
A re-import for a scene that already has art grows art_version and writes a new v directory; the
@@ -143,13 +145,7 @@ proc runImport*(rawArgs: seq[string]): int =
if a.scene != DefaultSceneId:
return failImport("scene " & $a.scene & ": v1 has exactly one scene, id " & $DefaultSceneId, 2)
# Validate the state dir and the source before touching either one: an error changes nothing.
if not dirExists(a.cfg.stateDir):
return failImport("state dir \"" & a.cfg.stateDir &
"\" does not exist or is not readable; create it by running the server or gm-key once", 1)
if not fileExists(a.cfg.dbPath):
return failImport("no state.db in \"" & a.cfg.stateDir &
"\"; create it by running the server or gm-key once", 1)
# Validate the source before touching the state dir: an error creates nothing.
if not fileExists(a.image):
return failImport("source image \"" & a.image & "\" does not exist", 1)
let imagePath = expandFilename(a.image)
@@ -175,22 +171,36 @@ proc runImport*(rawArgs: seq[string]): int =
if imgW <= 0 or imgH <= 0:
return failImport("image \"" & a.image & "\" has no pixels", 1)
# A fresh or empty state dir is the normal case (#82): create and migrate through the same
# startup path as the server, under the same state-dir lock. The lock is taken before the DB is
# created, so a running server can never race an import.
if not dirExists(a.cfg.stateDir):
try: createDir(a.cfg.stateDir)
except OSError as e:
return failImport("cannot create state dir \"" & a.cfg.stateDir & "\": " & e.msg, 1)
var lock = tryAcquire(a.cfg.stateDir)
if not lock.held:
return failImport("the server is running on \"" & a.cfg.stateDir &
"\" (state-dir lock held); stop it first, then restart it after the import", 3)
var conn: DbConn
var applied: int
try:
conn = openDatabase(a.cfg.dbPath)
let opened = openMigratedDatabase(a.cfg.dbPath)
conn = opened.db
applied = opened.applied
except CatchableError as e:
return failImport("cannot open " & a.cfg.dbPath & ": " & e.msg, 1)
return failImport("cannot open or migrate " & a.cfg.dbPath & ": " & e.msg, 1)
defer: close(conn)
if applied > 0:
stderr.writeLine "fff-vtt import: created/migrated " & a.cfg.dbPath &
" (" & $applied & " schema step" & (if applied == 1: "" else: "s") & ")"
let loaded = loadScene(conn)
if loaded.isNone:
return failImport("state.db has no scene " & $DefaultSceneId &
"; start the server once to migrate and seed it", 1)
" after migration; the database is inconsistent", 1)
let scene = loaded.get
var tileSize = if a.hasTileSize: a.tileSize else: scene.tileSize
+11 -9
View File
@@ -2380,7 +2380,8 @@ Commands:
gm-key --new Rotate the GM key and revoke all GM sessions. Refused while the server
holds the state-dir lock (stop the server first).
import Slice a PNG/WebP map into the scene's tile tree and update the scene row.
Stop the server first (it reads the scene at startup); restart it after.
A fresh/empty state dir is created and migrated first. Stop the server
first (it reads the scene at startup); restart it after.
Run `fff-vtt import --help` for the import arguments.
The bind address is fixed at 127.0.0.1 and is not a key: put nginx (or another TLS proxy) in front.
@@ -2425,13 +2426,12 @@ proc runGmKey(rawArgs: seq[string]): int =
var conn: DbConn
try:
conn = openDatabase(cfg.dbPath)
conn = openMigratedDatabase(cfg.dbPath).db
except CatchableError as e:
stderr.writeLine "fff-vtt gm-key: cannot open " & cfg.dbPath & ": " & e.msg
stderr.writeLine "fff-vtt gm-key: cannot open or migrate " & cfg.dbPath & ": " & e.msg
return 1
defer: close(conn)
try:
discard migrate(conn)
let created = ensureGm(conn, rotate)
if created.rotated:
stdout.writeLine "GM key (new): " & created.key
@@ -2466,15 +2466,17 @@ proc runServer(cfg: Config): int =
return 1
var conn: DbConn
var applied: int
try:
conn = openDatabase(cfg.dbPath)
let opened = openMigratedDatabase(cfg.dbPath)
conn = opened.db
applied = opened.applied
except MigrationError as e:
lg.log(lvError, "db_migrate_failed", [("path", cfg.dbPath), ("error", e.msg)])
return 1
except CatchableError as e:
lg.log(lvError, "db_open_failed", [("path", cfg.dbPath), ("error", e.msg)])
return 1
let applied = try: migrate(conn)
except CatchableError as e:
lg.log(lvError, "db_migrate_failed", [("path", cfg.dbPath), ("error", e.msg)])
return 1
lg.log(lvInfo, "db_ready", [("path", cfg.dbPath), ("schema_version", $schemaVersion(conn)),
("applied", $applied)])