play-locally: a player who leaves on purpose reaches the game server as "lost", so onLeave waits 30 seconds
Bug
What I was doing: testing a game server on the local test bench (npx @zaisma/cli play-locally) with scripted players: each gets a pass from the bench's API, joins the room over the WebSocket, and leaves by closing with code 1000 and reason "Left", as the SDK does.
Expected: a close with 1000 (or 1001) is a player leaving on purpose: onLeave runs at once with reason left, and the others see them gone from room.players.
What happened: every departure is treated as a dropped connection. The others get a players message with the leaver still listed, "awayReason":"lost", and onLeave runs only after reconnectTimeoutSec (30 seconds by default), with reason lost.
{"type":"players","players":[{"id":"player_…","name":"Wild Gecko 27","kind":"guest","awayReason":null},{"id":"player_…","name":"Calm Yak 42","kind":"guest","awayReason":"lost"}],"hostPlayerId":null}To make it happen again:
- Start the bench with
--no-open --json. POST /bench/api/open-playerstwice, thenPOST /bench/api/open-players/<number>/passfor each.- Join both to one room at
<rooms_url as ws>/v1/rooms/<game>/<code>withOrigin: <game_site_url>and thehellomessage. - Close the second socket with
close(1000, "Left"). - The first player's next
playersmessage shows the second aslost, not gone.
Cause, as far as I can tell: the bench's stand-in rooms pass each connection on to the game server, but drop the close code on the way. In bin/local-play.js:
client.on("close", () => {
forget();
upstream.close();
});The game server then sees a close with no code, and bin/node.js counts anything but 1000 or 1001 as lost:
room.core.leave(playerId, closeCode === 1000 || closeCode === 1001 ? "left" : "lost");Not checked: I saw this with scripted players. A browser tab that closes goes through the same code, so I expect the same, but I didn't test it. Connecting straight to the game server's own port should not have the problem.
Suggestion: pass the player's close code and reason on, falling back to a plain close for codes that can't be sent (1005, 1006):
client.on("close", (clientCloseCode, reason) => {
forget();
if (clientCloseCode === 1000 || clientCloseCode === 1001) upstream.close(clientCloseCode, reason);
else upstream.close();
});Why it matters: on the bench a game can't test what happens when a player leaves, only what happens when one drops, and anything tied to onLeave (freeing a slot, ending a round) lags 30 seconds behind what Zaisma's rooms would do.