server: the character, reveal and encountered records and migration step 7 (#23)

This commit is contained in:
2026-11-15 12:01:21 +01:00
parent 34a33b5ead
commit 75c3e046f8
4 changed files with 441 additions and 15 deletions
+35 -10
View File
@@ -53,8 +53,10 @@ type
id*: string
scene*: int
kind*: string ## free string (pc, npc, item...); GM-only on the wire (6.1 leak note 1)
descriptiveName*: string
actualName*: string
descriptiveName*: string ## "" when `characterId` is set: the character carries the names (8)
actualName*: string ## "" when `characterId` is set
characterId*: Option[string] ## the character this token figures (8); none = a plain token
revealAll*: bool ## plain tokens only: the actual name is revealed to everyone (8)
pos*: Vec
radius*: Option[float] ## px; none = the scene default (5.6, #4)
owner*: int ## controller: player id, GmControlled = the GM
@@ -77,6 +79,7 @@ type
kind*: Option[string]
descriptiveName*: Option[string]
actualName*: Option[string]
characterId*: Option[Option[string]] ## some(none) = unlink, some(some(id)) = link
controller*: Option[int] ## GmControlled = GM
gmOnly*: Option[bool]
flags*: Option[JsonNode]
@@ -224,7 +227,12 @@ proc validPlacement*(sc: SceneRec, pos: Vec): bool = inBounds(sc, pos)
proc validToken*(sc: SceneRec, t: TokenRec): bool =
## 6.1 validation. The props are checked for structure and caps; the per-key schema is the wire
## layer's job (props.checkWrite), because only the intent knows the registry.
validName(t.descriptiveName) and validName(t.actualName) and validKind(t.kind) and
## Names (8): a plain token carries both of its own and they follow the name rule; a token linked
## to a character carries neither, because the character's names are the ones that reach a client.
let namesOk =
if t.characterId.isSome: t.descriptiveName.len == 0 and t.actualName.len == 0
else: validName(t.descriptiveName) and validName(t.actualName)
namesOk and validKind(t.kind) and
validPlacement(sc, t.pos) and props.valid(t.props) and
(t.radius.isNone or t.radius.get > 0)
@@ -234,6 +242,7 @@ proc patched*(t: TokenRec, patch: TokenPatch): TokenRec =
if patch.kind.isSome: result.kind = patch.kind.get
if patch.descriptiveName.isSome: result.descriptiveName = patch.descriptiveName.get
if patch.actualName.isSome: result.actualName = patch.actualName.get
if patch.characterId.isSome: result.characterId = patch.characterId.get
if patch.controller.isSome: result.owner = patch.controller.get
if patch.gmOnly.isSome: result.gmOnly = patch.gmOnly.get
if patch.flags.isSome: result.flags = patch.flags.get
@@ -251,6 +260,17 @@ proc sqlLit(v: Option[float]): string =
proc sqlLitOwner(owner: int): string =
if owner == GmControlled: "NULL" else: $owner
proc sqlLitId(id: Option[string]): string =
## `character_id` is NULL for a plain token, and db_connector cannot bind a NULL argument
## (it quotes every parameter as text), so the value rides as a literal. Only ids from the token
## alphabet (8) ever reach here - a value is checked on the way in - so a quote cannot appear.
if id.isNone: return "NULL"
let v = id.get
for c in v:
if c notin {'A'..'Z', 'a'..'z', '0'..'9', '_', '-'}:
raise newException(ValueError, "character id outside the id alphabet")
"'" & v & "'"
proc writeStatuses*(db: DbConn, t: TokenRec) =
## Rewrites the token's `entity_status` rows to exactly `t.statuses`. No transaction control: the
## caller is inside the row's transaction (a status change and its row commit together).
@@ -267,23 +287,25 @@ proc writeStatuses*(db: DbConn, id: string, statuses: openArray[StatusEntry]) =
proc insertRow(db: DbConn, t: TokenRec) =
db.exec(sql("INSERT INTO entity (id, scene_id, kind, descriptive_name, actual_name, x, y, radius," &
" owner_player_id, gm_only, vision_radius, flags, props_public, props_owner," &
" props_gm, created_at) VALUES (" &
" props_gm, created_at, character_id, reveal_all) VALUES (" &
"?, ?, ?, ?, ?, ?, ?, " & sqlLit(t.radius) & ", " & sqlLitOwner(t.owner) & ", ?, " &
sqlLit(t.visionRadius) & ", ?, ?, ?, ?, ?)"),
sqlLit(t.visionRadius) & ", ?, ?, ?, ?, ?, " & sqlLitId(t.characterId) & ", ?)"),
t.id, t.scene, t.kind, t.descriptiveName, t.actualName, $t.pos.x, $t.pos.y,
(if t.gmOnly: 1 else: 0), $t.flags,
$t.props[pbPublic], $t.props[pbOwner], $t.props[pbGm],
now().utc.format("yyyy-MM-dd'T'HH:mm:ss'Z'"))
now().utc.format("yyyy-MM-dd'T'HH:mm:ss'Z'"), (if t.revealAll: 1 else: 0))
writeStatuses(db, t)
proc replaceRow(db: DbConn, t: TokenRec) =
db.exec(sql("UPDATE entity SET kind = ?, descriptive_name = ?, actual_name = ?, x = ?, y = ?," &
" radius = " & sqlLit(t.radius) & ", owner_player_id = " & sqlLitOwner(t.owner) &
", gm_only = ?, vision_radius = " & sqlLit(t.visionRadius) & ", flags = ?," &
" props_public = ?, props_owner = ?, props_gm = ? WHERE id = ?"),
" props_public = ?, props_owner = ?, props_gm = ?, reveal_all = ?," &
" character_id = " & sqlLitId(t.characterId) & " WHERE id = ?"),
t.kind, t.descriptiveName, t.actualName, $t.pos.x, $t.pos.y,
(if t.gmOnly: 1 else: 0), $t.flags,
$t.props[pbPublic], $t.props[pbOwner], $t.props[pbGm], t.id)
$t.props[pbPublic], $t.props[pbOwner], $t.props[pbGm],
(if t.revealAll: 1 else: 0), t.id)
writeStatuses(db, t)
@@ -349,7 +371,9 @@ proc rowToToken(row: Row): TokenRec =
radius: optFloat(row[7]),
owner: (if row[8].len == 0: GmControlled else: parseInt(row[8])),
gmOnly: row[9] == "1",
visionRadius: optFloat(row[10]), flags: parseObject(row[11]), props: p)
visionRadius: optFloat(row[10]), flags: parseObject(row[11]), props: p,
characterId: (if row[15].len == 0: none(string) else: some(row[15])),
revealAll: row[16] == "1")
proc loadStatuses(db: DbConn, sceneId: int): Table[string, seq[StatusEntry]] =
## The `(status, source)` entries of every token of the scene, keyed by token id (5.5).
@@ -364,7 +388,8 @@ proc loadTokens*(db: DbConn, sceneId = DefaultSceneId): seq[TokenRec] =
## Every token of the scene, in creation order (rowid), which is the order egress walks.
for row in db.getAllRows(sql"""
SELECT id, scene_id, kind, descriptive_name, actual_name, x, y, radius, owner_player_id,
gm_only, vision_radius, flags, props_public, props_owner, props_gm
gm_only, vision_radius, flags, props_public, props_owner, props_gm, character_id,
reveal_all
FROM entity WHERE scene_id = ? ORDER BY rowid""", sceneId):
result.add rowToToken(row)
let statuses = loadStatuses(db, sceneId)