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)
if found.isNone: return
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
# player may have been offline while something walked into their sight).
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).
ws.send($nextPingSeq(), Ping)
# The 6.3 order: `roster`, walls (GM), characters, encountered, then vision/explored and the
# tokens. Players never receive wall data (3.1, T2).
if role != prGm and named:
refreshPlayer(pid)
# tokens. Players never receive wall data (3.1, T2); `resync` refreshes the player's sight.
resync(activated.id, named, playerRec)
# ---------------------------------------------------------------------------------------------------
@@ -1602,7 +1604,8 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
app.db.withTransaction:
if subject.get == nsCharacter: revealEncountered(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
(subject.get == nsToken and eid == subjectId):
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)
else: revealEncounteredToken(app.db, id, actual)
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
(subject.get == nsToken and eid == id):
app.encountered[pid][eid] = e.revealed(actual)