A wall that is player_marker:true and derives window sends a player one anchor (the drawn midpoint, integer px) and an opaque per-player 12-char id once any point of its segment enters that player's vision polygon. Markers live in PlayerVision like explored, persist to marker_seen in the same derived-memory transaction, load before the first connection and resync as a full set after explored; the GM and other players never receive one. - markers.nim: emission gate, visible-extent test, id mint, anchor, marker_seen I/O. - views/main: PlayerView markers + full/diff frames, refresh sync, startup load. - markers.test.mjs: window preset, switch off, solid wall, any-part anchor, walk-away/door/lost sight, full-after-explored, per-player privacy, GM none. - leakcheck: marker allowed type + independent visible-extent oracle with a positive control (a marked window the subject sees). - restart: marked window in the fixture; marker set and marker_seen rows identical before/after SIGTERM and SIGKILL. - protocol/tviews: catalogue and frame order pin the 16th message. Retraction, re-earning and MARKER_* faults are #60. Ref #59
183 lines
9.3 KiB
JavaScript
183 lines
9.3 KiB
JavaScript
// Walls r1 (#59): player markers over the real wire. A marker exists only for a wall that is both
|
|
// `player_marker:true` and derives `window`; it carries one opaque per-player id and the drawn
|
|
// midpoint in integer px, nothing else. It is memory like explored: it survives walking away, a door
|
|
// closing and lost sight, the full set comes after explored on connect/resync, and a player never
|
|
// sees another player's marker (DESIGN 3.1, 5.8, 6.1, 6.3, 8; ADR-0015).
|
|
//
|
|
// Retraction, re-earning and the MARKER_* fault classes are #60 and are deliberately not asserted
|
|
// here: this group proves the emission gate, the anchor, storage/resync and privacy.
|
|
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { withGmServer, openAs } from './withserver.mjs'
|
|
import { createPlayer, namePlayer, sleep } from './lib.mjs'
|
|
|
|
const ID_RE = /^[A-Za-z0-9_-]{12}$/
|
|
|
|
async function gmConnect(origin, gmKey) {
|
|
const conn = await openAs(origin, gmKey)
|
|
await conn.ws.waitText('hello')
|
|
const walls = await conn.ws.waitText('walls')
|
|
return { ...conn, scene: walls.scene }
|
|
}
|
|
|
|
async function addWall(gm, fields) {
|
|
const ack = await gm.ws.request({ t: 'wall.add', scene: gm.scene, ...fields })
|
|
assert.equal(ack.ok, true, JSON.stringify(ack))
|
|
return ack
|
|
}
|
|
|
|
async function namedPlayer(gm, origin, name) {
|
|
const player = await createPlayer(gm.ws)
|
|
await namePlayer(origin, player.key, name)
|
|
const conn = await openAs(origin, player.key)
|
|
await conn.ws.waitText('hello')
|
|
return { ...player, conn }
|
|
}
|
|
|
|
async function addSightToken(gm, player, pos) {
|
|
const ack = await gm.ws.request({ t: 'token.create', scene: gm.scene, kind: 'pc', pos,
|
|
descriptive_name: 'sight', actual_name: 'Sight',
|
|
controller: player.id, flags: { vision: true } })
|
|
assert.equal(ack.ok, true, JSON.stringify(ack))
|
|
return ack.id
|
|
}
|
|
|
|
const markerFrames = ws => ws.messages.filter(m => m && m.t === 'marker')
|
|
|
|
function assertMarkerShape(frame) {
|
|
const keys = frame.full ? ['full', 'n', 'set', 't'] : ['del', 'full', 'n', 'set', 't']
|
|
assert.deepEqual(Object.keys(frame).sort(), keys)
|
|
assert.equal(Array.isArray(frame.del ?? []), true)
|
|
for (const entry of frame.set) {
|
|
assert.deepEqual(Object.keys(entry).sort(), ['id', 'pos'], 'a marker carries only id and pos')
|
|
assert.match(entry.id, ID_RE, 'the marker id is 12 characters from the token alphabet')
|
|
assert.equal(entry.pos.length, 2)
|
|
assert.equal(Number.isInteger(entry.pos[0]) && Number.isInteger(entry.pos[1]), true,
|
|
'the anchor is integer px (6.1)')
|
|
}
|
|
}
|
|
|
|
const markersAfter = (ws, from) =>
|
|
ws.messages.filter((m, i) => i >= from && m && m.t === 'marker')
|
|
|
|
test('marker emission: only a marked window, exact anchor and count, no wall field (#59)', async () => {
|
|
await withGmServer(async (s, gmKey) => {
|
|
const gm = await gmConnect(s.origin, gmKey)
|
|
// The first window is in view of where the subject's token will stand: (200,600) midpoint.
|
|
await addWall(gm, { preset: 'window', a: [200, 500], b: [200, 700] })
|
|
const player = await namedPlayer(gm, s.origin, 'Marker One')
|
|
|
|
const tokenId = await addSightToken(gm, player, [300, 300])
|
|
const first = await player.conn.ws.waitFor(m => m.t === 'marker' && m.set.length > 0)
|
|
assert.equal(first.full, false, 'a mutation sends a diff, not a full set')
|
|
assertMarkerShape(first)
|
|
assert.equal(first.set.length, 1, 'the window preset emits exactly one marker')
|
|
assert.deepEqual(first.set[0].pos, [200, 600], 'the anchor is the drawn midpoint, integer px')
|
|
assert.match(first.set[0].id, ID_RE)
|
|
// The wall id (an integer) and any wall field are absent from the whole frame.
|
|
for (const banned of ['wall', 'walls', 'a', 'b', 'kind', 'player_marker', 'door']) {
|
|
assert.equal(JSON.stringify(first).includes(`"${banned}"`), false,
|
|
`a marker frame carries no wall field ${banned}`)
|
|
}
|
|
|
|
// A solid wall with the switch on stores the switch and sends nothing (5.8 gate).
|
|
let from = player.conn.ws.messages.length
|
|
await addWall(gm, { a: [100, 700], b: [400, 700], player_marker: true })
|
|
await sleep(120)
|
|
assert.equal(markersAfter(player.conn.ws, from).length, 0,
|
|
'a solid wall with player_marker:true emits no marker')
|
|
|
|
// The same see-through shape with the switch off is an invisible barrier.
|
|
from = player.conn.ws.messages.length
|
|
await addWall(gm, { blocks_sight: false, blocks_movement: true, door: true,
|
|
player_marker: false, a: [500, 500], b: [500, 700] })
|
|
await sleep(120)
|
|
assert.equal(markersAfter(player.conn.ws, from).length, 0,
|
|
'a window with its marker off emits nothing')
|
|
|
|
// Any part of the extent qualifies, and the anchor stays the midpoint even when the midpoint
|
|
// is outside the vision radius: (300,650) is 350 px from the token, the midpoint (300,950) 650.
|
|
from = player.conn.ws.messages.length
|
|
await addWall(gm, { preset: 'window', a: [300, 650], b: [300, 1250] })
|
|
const second = await player.conn.ws.waitFor(m => m.t === 'marker' &&
|
|
m.set.some(e => e.pos[0] === 300 && e.pos[1] === 950))
|
|
assertMarkerShape(second)
|
|
assert.equal(second.set.length, 1, 'one newly seen window yields exactly one marker')
|
|
assert.notEqual(second.set[0].id, first.set[0].id, 'each wall gets its own marker id')
|
|
const all = markerFrames(player.conn.ws).flatMap(m => m.set)
|
|
assert.equal(new Set(all.map(e => e.id)).size, 2, 'the player holds exactly two markers')
|
|
|
|
// The GM never receives a marker frame (5.8: a player's memory stays a player's).
|
|
assert.equal(markerFrames(gm.ws).length, 0)
|
|
gm.ws.close(); player.conn.ws.close()
|
|
})
|
|
})
|
|
|
|
test('marker lifetime and resync: survives walk-away and doors, full after explored, private (#59)',
|
|
async () => {
|
|
await withGmServer(async (s, gmKey) => {
|
|
const gm = await gmConnect(s.origin, gmKey)
|
|
const window = await addWall(gm, { preset: 'window', a: [200, 500], b: [200, 700] })
|
|
const alice = await namedPlayer(gm, s.origin, 'Marker Alice')
|
|
const aliceToken = await addSightToken(gm, alice, [300, 300])
|
|
const seen = await alice.conn.ws.waitFor(m => m.t === 'marker' && m.set.length > 0)
|
|
const aliceMarker = seen.set[0]
|
|
assert.equal(seen.set.length, 1)
|
|
|
|
// Walk away: the marker is memory, so no retraction and no new frame.
|
|
let from = alice.conn.ws.messages.length
|
|
assert.equal((await gm.ws.request({ t: 'token.move', id: aliceToken,
|
|
pos: [2000, 2000] })).ok, true)
|
|
await sleep(120)
|
|
assert.equal(markersAfter(alice.conn.ws, from).length, 0,
|
|
'walking away never retracts or re-sends a marker')
|
|
|
|
// A door closing is not a cause either (the window preset is an opening window).
|
|
for (const open of [true, false]) {
|
|
from = alice.conn.ws.messages.length
|
|
assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id: window.id,
|
|
open })).ok, true)
|
|
await sleep(80)
|
|
assert.equal(markersAfter(alice.conn.ws, from).length, 0,
|
|
`door.set open:${open} leaves the marker alone`)
|
|
}
|
|
|
|
// A fresh connection gets the full set after the explored snapshot (6.3).
|
|
const again = await openAs(s.origin, alice.key)
|
|
await again.ws.waitText('hello')
|
|
const explored = await again.ws.waitFor(m => m.t === 'explored' && m.full === true)
|
|
const full = await again.ws.waitFor(m => m.t === 'marker' && m.full === true)
|
|
assertMarkerShape(full)
|
|
assert.ok(again.ws.messages.indexOf(explored) < again.ws.messages.indexOf(full),
|
|
'the marker full set comes after explored')
|
|
assert.equal(full.set.length, 1)
|
|
assert.equal(full.set[0].id, aliceMarker.id, 'the same opaque id survives the reconnect')
|
|
assert.deepEqual(full.set[0].pos, aliceMarker.pos)
|
|
|
|
// A second player sees the same window with their own, different id; neither receives the
|
|
// other's (5.8: never another player's marker).
|
|
const bob = await namedPlayer(gm, s.origin, 'Marker Bob')
|
|
await addSightToken(gm, bob, [350, 350])
|
|
const bobSeen = await bob.conn.ws.waitFor(m => m.t === 'marker' && m.set.length > 0)
|
|
const bobMarker = bobSeen.set[0]
|
|
assert.notEqual(bobMarker.id, aliceMarker.id, 'ids are per player')
|
|
assert.deepEqual(bobMarker.pos, aliceMarker.pos)
|
|
assert.equal(JSON.stringify(alice.conn.ws.messages).includes(bobMarker.id), false,
|
|
"Alice never receives Bob's marker id")
|
|
assert.equal(JSON.stringify(bob.conn.ws.messages).includes(aliceMarker.id), false,
|
|
"Bob never receives Alice's marker id")
|
|
|
|
// Turning the switch off makes the barrier invisible: a player who sees it now gets no marker.
|
|
assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: window.id,
|
|
player_marker: false })).ok, true)
|
|
const carol = await namedPlayer(gm, s.origin, 'Marker Carol')
|
|
await addSightToken(gm, carol, [300, 300])
|
|
await sleep(150)
|
|
const carolMarkers = markerFrames(carol.conn.ws).flatMap(m => m.set)
|
|
assert.equal(carolMarkers.length, 0, 'a window with the switch off is an invisible barrier')
|
|
|
|
assert.equal(markerFrames(gm.ws).length, 0, 'the GM receives no marker')
|
|
gm.ws.close(); alice.conn.ws.close(); again.ws.close(); bob.conn.ws.close(); carol.conn.ws.close()
|
|
})
|
|
})
|