server: refresh a player's sight when a connection starts receiving state (#23)

This commit is contained in:
2026-11-15 12:11:42 +01:00
parent 1ad9f724f2
commit ea6d92bf76
2 changed files with 18 additions and 5 deletions
+9 -5
View File
@@ -304,6 +304,10 @@ proc resync(connId: int, named: bool, player: Player) =
let found = app.conns.connById(connId) let found = app.conns.connById(connId)
if found.isNone: return if found.isNone: return
let conn = found.get let conn = found.get
# This is the moment a connection starts receiving state, so the player's own sight is computed
# here: a connection opened while unnamed never had a refresh, and `player.setname` resyncs on the
# same socket (6.1). Idempotent: a refresh that changes nothing only costs the polygon pass.
if not isGm and named: refreshPlayer(player.id)
# A token visible right now is a sighting: record it before the `encountered` list is built (the # A token visible right now is a sighting: record it before the `encountered` list is built (the
# player may have been offline while something walked into their sight). # player may have been offline while something walked into their sight).
recordEncounters() recordEncounters()
@@ -634,9 +638,7 @@ proc wsUpgrade(request: Request) {.gcsafe.} =
# One Ping right away, then one every PingIntervalMs: the browser answers without JS (4.3). # One Ping right away, then one every PingIntervalMs: the browser answers without JS (4.3).
ws.send($nextPingSeq(), Ping) ws.send($nextPingSeq(), Ping)
# The 6.3 order: `roster`, walls (GM), characters, encountered, then vision/explored and the # The 6.3 order: `roster`, walls (GM), characters, encountered, then vision/explored and the
# tokens. Players never receive wall data (3.1, T2). # tokens. Players never receive wall data (3.1, T2); `resync` refreshes the player's sight.
if role != prGm and named:
refreshPlayer(pid)
resync(activated.id, named, playerRec) resync(activated.id, named, playerRec)
# --------------------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------------------
@@ -1602,7 +1604,8 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
app.db.withTransaction: app.db.withTransaction:
if subject.get == nsCharacter: revealEncountered(app.db, subjectId, actual) if subject.get == nsCharacter: revealEncountered(app.db, subjectId, actual)
else: revealEncounteredToken(app.db, subjectId, actual) else: revealEncounteredToken(app.db, subjectId, actual)
for eid, e in app.encountered.getOrDefault(player): for eid in app.encountered.getOrDefault(player).keys.toSeq:
let e = app.encountered[player][eid]
if (subject.get == nsCharacter and e.characterId == some(subjectId)) or if (subject.get == nsCharacter and e.characterId == some(subjectId)) or
(subject.get == nsToken and eid == subjectId): (subject.get == nsToken and eid == subjectId):
app.encountered[player][eid] = e.revealed(actual) app.encountered[player][eid] = e.revealed(actual)
@@ -1651,7 +1654,8 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
if subject.get == nsCharacter: revealEncountered(app.db, id, actual) if subject.get == nsCharacter: revealEncountered(app.db, id, actual)
else: revealEncounteredToken(app.db, id, actual) else: revealEncounteredToken(app.db, id, actual)
for pid in app.encountered.keys.toSeq: for pid in app.encountered.keys.toSeq:
for eid, e in app.encountered[pid]: for eid in app.encountered[pid].keys.toSeq:
let e = app.encountered[pid][eid]
if (subject.get == nsCharacter and e.characterId == some(id)) or if (subject.get == nsCharacter and e.characterId == some(id)) or
(subject.get == nsToken and eid == id): (subject.get == nsToken and eid == id):
app.encountered[pid][eid] = e.revealed(actual) app.encountered[pid][eid] = e.revealed(actual)
+9
View File
@@ -63,6 +63,11 @@ test('player.setname: the first name, the second hello, a roster to everyone, an
const alice = await createPlayer(gm.ws) const alice = await createPlayer(gm.ws)
const bob = await createPlayer(gm.ws) const bob = await createPlayer(gm.ws)
// a token Alice controls, so naming her must produce a vision polygon on the resync
const sightTok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [400, 400], descriptive_name: 'Alice',
actual_name: 'Alice', controller: alice.id,
flags: { vision: true } })
const a = await openAs_(s.origin, alice.key) const a = await openAs_(s.origin, alice.key)
const hello = await a.ws.waitText('hello') const hello = await a.ws.waitText('hello')
assert.equal(hello.me.name, null, 'a fresh key has no name yet') assert.equal(hello.me.name, null, 'a fresh key has no name yet')
@@ -74,6 +79,10 @@ test('player.setname: the first name, the second hello, a roster to everyone, an
const named = await a.ws.waitFor((m, i) => i >= from && m.t === 'hello') const named = await a.ws.waitFor((m, i) => i >= from && m.t === 'hello')
assert.equal(named.me.name, 'Alice', 'the name is trimmed before it is stored (6.5)') assert.equal(named.me.name, 'Alice', 'the name is trimmed before it is stored (6.5)')
assert.equal(named.scene.id, 1, 'a named player gets the scene block') assert.equal(named.scene.id, 1, 'a named player gets the scene block')
// the resync computes this connection's sight for the first time: an unnamed connection never
// had a refresh, so a vision token it controls must show up now
const vision = await a.ws.waitFor((m, i) => i >= from && m.t === 'vision')
assert.deepEqual(vision.polys.map(p => p.id), [sightTok.id])
const roster = await a.ws.waitFor((m, i) => i >= from && m.t === 'roster') const roster = await a.ws.waitFor((m, i) => i >= from && m.t === 'roster')
assert.deepEqual(roster.players.map(p => p.name).sort(), ['Alice', 'GM'], assert.deepEqual(roster.players.map(p => p.name).sort(), ['Alice', 'GM'],
'a player sees the named players only (6.1 leak note 4)') 'a player sees the named players only (6.1 leak note 4)')