feat(walls): proto 2 typed wall record, inert geometry, derived kind (#54)
wall.add/wall.update take thickness, position, height and the eight raw switches; kind is output-only and the GM wall frames echo the full typed record. Geometry is stored but inert (0/full/centre behaviour), and the legacy kind/open wire adapter is gone. proto is bumped to 2 with the harness default, and BannedAlways gains every wall field name with a WALLDATA positive control. Tests: schema-7 migrated scene vs the same walls authored via raw switches gives byte-identical player frames, refusals and per-tile statuses. Nim 382 / Node 113 green.
This commit is contained in:
+39
-8
@@ -148,7 +148,7 @@ suite "server frames (6.1)":
|
||||
check not h.hasKey("scene") # an unnamed player gets proto, role, me only (6.1)
|
||||
check not h.hasKey("extra")
|
||||
check h["t"].getStr == "hello"
|
||||
check h["proto"].getInt == 1
|
||||
check h["proto"].getInt == 2
|
||||
check h["role"].getStr == "player"
|
||||
check h["me"]["id"].getInt == 4
|
||||
check h["me"]["name"].kind == JNull
|
||||
@@ -211,25 +211,54 @@ suite "server frames (6.1)":
|
||||
["revoked", "evicted", "proto-mismatch", "malformed-flood"]
|
||||
|
||||
suite "wall frames (6.1 `walls`, `wall`, `wall_gone`; GM only)":
|
||||
test "one wall carries id, a, b, kind, open and flags in the 6.1 shape":
|
||||
let w = wallObject(7, (10.0, 20.5), (300.0, 20.5), "door", true, %*{"locked": false})
|
||||
test "one wall carries the typed record plus the derived kind in the proto-2 shape":
|
||||
let w = wallObject(WallView(id: 7, a: (10.0, 20.5), b: (300.0, 20.5), thickness: 12.0,
|
||||
position: "left", blocksSight: true, blocksMovement: true,
|
||||
sightDir: "both", playerMarker: false, door: true, open: true,
|
||||
secret: false, locked: true, height: 2.5, hasHeight: true,
|
||||
label: "the king's window", hasLabel: true, kind: "door",
|
||||
flags: %*{"locked": false}))
|
||||
check w["id"].getInt == 7
|
||||
check w["a"][0].getFloat == 10.0
|
||||
check w["a"][1].getFloat == 20.5
|
||||
check w["b"][0].getFloat == 300.0
|
||||
check w["thickness"].getFloat == 12.0
|
||||
check w["position"].getStr == "left"
|
||||
check w["height"].getFloat == 2.5
|
||||
check w["kind"].getStr == "door"
|
||||
check w["blocks_sight"].getBool
|
||||
check w["blocks_movement"].getBool
|
||||
check w["sight_dir"].getStr == "both"
|
||||
check w["player_marker"].getBool == false
|
||||
check w["door"].getBool
|
||||
check w["open"].getBool
|
||||
check w["secret"].getBool == false
|
||||
check w["locked"].getBool
|
||||
check w["label"].getStr == "the king's window"
|
||||
check w["flags"]["locked"].getBool == false
|
||||
# an absent flags object is still an object on the wire
|
||||
check wallObject(1, (0.0, 0.0), (1.0, 0.0), "wall", false, nil)["flags"].kind == JObject
|
||||
# unset height/label are present as JSON null, and an absent flags object is still an object
|
||||
let plain = wallObject(WallView(id: 1, a: (0.0, 0.0), b: (1.0, 0.0), position: "center",
|
||||
blocksSight: true, blocksMovement: true, sightDir: "both",
|
||||
kind: "wall"))
|
||||
check plain["height"].kind == JNull
|
||||
check plain["label"].kind == JNull
|
||||
check plain["flags"].kind == JObject
|
||||
|
||||
test "`walls` is the full list plus the scene, `wall` is one upsert, `wall_gone` an id":
|
||||
let snap = wallsFrame(1, @[wallObject(1, (0.0, 0.0), (9.0, 0.0), "wall", false, nil)])
|
||||
let plain = WallView(id: 1, a: (0.0, 0.0), b: (9.0, 0.0), position: "center",
|
||||
blocksSight: true, blocksMovement: true, sightDir: "both", kind: "wall")
|
||||
let snap = wallsFrame(1, @[wallObject(plain)])
|
||||
check snap["t"].getStr == "walls"
|
||||
check snap["scene"].getInt == 1
|
||||
check snap["walls"].len == 1
|
||||
check snap["walls"][0]["id"].getInt == 1
|
||||
let one = wallFrame(1, wallObject(2, (0.0, 5.0), (9.0, 5.0), "door", false, nil))
|
||||
var doorView = plain
|
||||
doorView.id = 2
|
||||
doorView.a = (0.0, 5.0)
|
||||
doorView.b = (9.0, 5.0)
|
||||
doorView.door = true
|
||||
doorView.kind = "door"
|
||||
let one = wallFrame(1, wallObject(doorView))
|
||||
check one["t"].getStr == "wall"
|
||||
check one["scene"].getInt == 1
|
||||
check one["id"].getInt == 2
|
||||
@@ -240,7 +269,9 @@ suite "wall frames (6.1 `walls`, `wall`, `wall_gone`; GM only)":
|
||||
check gone["id"].getInt == 2
|
||||
|
||||
test "the fan-out frames carry no n: the sender stamps it per recipient":
|
||||
let plain = WallView(id: 1, a: (0.0, 0.0), b: (1.0, 1.0), position: "center",
|
||||
blocksSight: true, blocksMovement: true, sightDir: "both", kind: "wall")
|
||||
check not wallsFrame(1, @[]).hasKey("n")
|
||||
check not wallFrame(1, wallObject(1, (0.0, 0.0), (1.0, 1.0), "wall", false, nil)).hasKey("n")
|
||||
check not wallFrame(1, wallObject(plain)).hasKey("n")
|
||||
check not wallGoneFrame(1, 1).hasKey("n")
|
||||
check withN(wallGoneFrame(1, 1), 12)["n"].getInt == 12
|
||||
|
||||
+5
-5
@@ -9,9 +9,9 @@ proc sceneWith(walls: seq[WallRec]): SceneRec =
|
||||
visionRadiusDefault: 400, tokenRadiusDefault: 20, artVersion: 1, nextWallId: 1,
|
||||
extra: newJObject(), walls: walls)
|
||||
|
||||
proc wall(ax, ay, bx, by: float, kind = wkWall, open = false): WallRec =
|
||||
proc wall(ax, ay, bx, by: float, door = false, open = false): WallRec =
|
||||
result = defaultWall((ax, ay), (bx, by))
|
||||
result.kind = kind
|
||||
result.door = door
|
||||
result.open = open
|
||||
|
||||
proc tok(id: string, x, y: float, owner: int, visionFlag = true, gmOnly = false): TokenRec =
|
||||
@@ -55,8 +55,8 @@ suite "polygons (5.1: one per controlled token with the `vision` flag)":
|
||||
for p in pts: check hypot(p.x - 400, p.y - 400) <= 121
|
||||
|
||||
test "an open door does not block; closing it does (5.1, 5.5)":
|
||||
let closed = sceneWith(@[wall(700, 0, 700, 4096, wkDoor, open = false)])
|
||||
let open = sceneWith(@[wall(700, 0, 700, 4096, wkDoor, open = true)])
|
||||
let closed = sceneWith(@[wall(700, 0, 700, 4096, door = true, open = false)])
|
||||
let open = sceneWith(@[wall(700, 0, 700, 4096, door = true, open = true)])
|
||||
let tokens = @[tok("me", 400, 400, 2)]
|
||||
check computePolys(closed, tokens, 2)[0].pts.allIt(it.x <= 701)
|
||||
check computePolys(open, tokens, 2)[0].pts.anyIt(it.x > 790)
|
||||
@@ -90,7 +90,7 @@ suite "explored (5.1, 8: strictly per player, only grows)":
|
||||
check not pv.insideAny((400.0, 400.0))
|
||||
|
||||
test "a closed door hides the cells behind it; opening it adds them":
|
||||
var sc = sceneWith(@[wall(700, 0, 700, 4096, wkDoor, open = false)])
|
||||
var sc = sceneWith(@[wall(700, 0, 700, 4096, door = true, open = false)])
|
||||
var pv: PlayerVision
|
||||
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
||||
let behind = sc.cellIndex(23, 12) # x ~ 750, past the closed door
|
||||
|
||||
+40
-21
@@ -12,9 +12,9 @@ proc tmpDir(): string =
|
||||
removeDir(result)
|
||||
createDir(result)
|
||||
|
||||
proc wall(ax, ay, bx, by: float, kind = wkWall, open = false): WallRec =
|
||||
proc wall(ax, ay, bx, by: float, door = false, open = false): WallRec =
|
||||
result = defaultWall((ax, ay), (bx, by))
|
||||
result.kind = kind
|
||||
result.door = door
|
||||
result.open = open
|
||||
|
||||
proc freshScene(): tuple[db: DbConn, dir: string] =
|
||||
@@ -65,19 +65,37 @@ suite "field parsing (6.1 field notes)":
|
||||
check parseVec(newJNull()).isNone
|
||||
check parseVec(nil).isNone
|
||||
|
||||
test "kind, open and flags accept only their own shape":
|
||||
check parseKind("wall").get == wkWall
|
||||
check parseKind("door").get == wkDoor
|
||||
check parseKind("window").isNone
|
||||
check parseKind("").isNone
|
||||
check parseKind("WALL").isNone
|
||||
check parseOpen(nil).get == false # omitted = closed
|
||||
check parseOpen(%true).get == true
|
||||
check parseOpen(%1).isNone
|
||||
test "position, sight_dir and flags accept only their own shape":
|
||||
check parsePosition("center").get == wpCenter
|
||||
check parsePosition("left").get == wpLeft
|
||||
check parsePosition("right").get == wpRight
|
||||
check parsePosition("middle").isNone
|
||||
check parsePosition("").isNone
|
||||
check parseSightDir("both").get == sdBoth
|
||||
check parseSightDir("left").get == sdLeft
|
||||
check parseSightDir("right").get == sdRight
|
||||
check parseSightDir("any").isNone
|
||||
check parseFlags(nil).get.kind == JObject # omitted = {}
|
||||
check parseFlags(%*{"a": 1}).get["a"].getInt == 1
|
||||
check parseFlags(%*[1]).isNone
|
||||
|
||||
test "kind is derived from the switches, never stored (5.8)":
|
||||
check derivedKind(defaultWall((0.0, 0.0), (1.0, 0.0))) == "wall"
|
||||
var door = defaultWall((0.0, 0.0), (1.0, 0.0))
|
||||
door.door = true
|
||||
check derivedKind(door) == "door"
|
||||
var window = defaultWall((0.0, 0.0), (1.0, 0.0))
|
||||
window.blocksSight = false
|
||||
check derivedKind(window) == "window"
|
||||
window.door = true
|
||||
check derivedKind(window) == "window" # a fixed or opening window is still a window
|
||||
var oneWay = defaultWall((0.0, 0.0), (1.0, 0.0))
|
||||
oneWay.sightDir = sdLeft
|
||||
check derivedKind(oneWay) == "one_way"
|
||||
var custom = defaultWall((0.0, 0.0), (1.0, 0.0))
|
||||
custom.blocksMovement = false
|
||||
check derivedKind(custom) == "custom"
|
||||
|
||||
test "undefined and null are the same as an absent field":
|
||||
check fieldOrNull(%*{"open": true}, "open").getBool
|
||||
var withNull = newJObject()
|
||||
@@ -141,11 +159,11 @@ suite "persistence (write-through, restart-identical)":
|
||||
var north = defaultWall((0.0, 0.0), (4096.0, 0.0))
|
||||
north.flags = %*{"note": "north"}
|
||||
let id1 = addWall(conn, sc, north).get
|
||||
let id2 = addWall(conn, sc, wall(100, 25, 300, 25, wkDoor)).get
|
||||
let id2 = addWall(conn, sc, wall(100, 25, 300, 25, door = true)).get
|
||||
check setDoor(conn, sc, id2, true) == waOk
|
||||
var changed = defaultWall((0.0, 0.0), (2000.0, 0.0))
|
||||
changed.id = id1
|
||||
changed.kind = wkDoor
|
||||
changed.door = true
|
||||
changed.flags = %*{"note": "north wall"}
|
||||
check updateWall(conn, sc, changed) == waOk
|
||||
|
||||
@@ -155,11 +173,11 @@ suite "persistence (write-through, restart-identical)":
|
||||
check reloaded.walls[0].id == id1
|
||||
check reloaded.walls[0].a == (0.0, 0.0)
|
||||
check reloaded.walls[0].b == (2000.0, 0.0)
|
||||
check reloaded.walls[0].kind == wkDoor # kind is updatable
|
||||
check reloaded.walls[0].door # the door switch is updatable
|
||||
check reloaded.walls[0].open == false
|
||||
check reloaded.walls[0].flags["note"].getStr == "north wall"
|
||||
check reloaded.walls[1].id == id2
|
||||
check reloaded.walls[1].kind == wkDoor
|
||||
check reloaded.walls[1].door
|
||||
check reloaded.walls[1].open == true # an open door stays open
|
||||
check reloaded.walls[1].flags.kind == JObject # flags default to {}
|
||||
|
||||
@@ -264,25 +282,25 @@ suite "validation (DESIGN 6.1 `wall.*`)":
|
||||
close(conn)
|
||||
removeDir(d)
|
||||
var sc = loadScene(conn).get
|
||||
let id = addWall(conn, sc, wall(10, 10, 100, 10, wkWall, open = true)).get
|
||||
let id = addWall(conn, sc, wall(10, 10, 100, 10, open = true)).get
|
||||
check wallById(sc, id).get.open == false
|
||||
check loadScene(conn).get.walls[0].open == false
|
||||
|
||||
test "update keeps the row's id and open flag unless the kind changes":
|
||||
test "update keeps the row's id and open flag unless the door switch is cleared":
|
||||
let (conn, d) = freshScene()
|
||||
defer:
|
||||
close(conn)
|
||||
removeDir(d)
|
||||
var sc = loadScene(conn).get
|
||||
let id = addWall(conn, sc, wall(10, 10, 100, 10, wkDoor)).get
|
||||
let id = addWall(conn, sc, wall(10, 10, 100, 10, door = true)).get
|
||||
check setDoor(conn, sc, id, true) == waOk
|
||||
var w = wallById(sc, id).get
|
||||
w.a = (20.0, 20.0)
|
||||
check updateWall(conn, sc, w) == waOk
|
||||
check wallById(sc, id).get.open == true # untouched by update
|
||||
# door -> wall closes it (a wall is never open)
|
||||
# clearing the door switch closes it (a non-door stores no door settings)
|
||||
var back = wallById(sc, id).get
|
||||
back.kind = wkWall
|
||||
back.door = false
|
||||
check updateWall(conn, sc, back) == waOk
|
||||
check wallById(sc, id).get.open == false
|
||||
check loadScene(conn).get.walls[0].open == false
|
||||
@@ -298,7 +316,8 @@ suite "validation (DESIGN 6.1 `wall.*`)":
|
||||
let after = patched(before, WallPatch(b: some((300.0, 400.0))))
|
||||
check after.a == before.a
|
||||
check after.b == (300.0, 400.0)
|
||||
check after.kind == before.kind
|
||||
check after.door == before.door
|
||||
check after.thickness == before.thickness
|
||||
check after.flags.kind == JObject
|
||||
|
||||
suite "lookup outcomes (main.nim's 6.5 mapping)":
|
||||
|
||||
Reference in New Issue
Block a user