fix: periodic player count refresh via probe shard connection
Some Jackbox games (e.g. Trivia Murder Party 2) do not send client/connected events to shard connections and lack textDescriptions, leaving the player count stuck at 0 if the shard connects before players join. Fix by opening a lightweight probe shard every 20s to read the fresh here map. Also fix bc:room entity lookup in handleWelcome and a WebSocket close handler race condition. Made-with: Cursor
This commit is contained in:
@@ -113,10 +113,70 @@ class EcastShardClient {
|
||||
startStatusBroadcast() {
|
||||
this.stopStatusBroadcast();
|
||||
this.statusInterval = setInterval(() => {
|
||||
this.onEvent('game.status', this.getSnapshot());
|
||||
this._refreshPlayerCount().finally(() => {
|
||||
this.onEvent('game.status', this.getSnapshot());
|
||||
});
|
||||
}, 20000);
|
||||
}
|
||||
|
||||
_refreshPlayerCount() {
|
||||
if (!this.host || this.gameFinished || this.manuallyStopped) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
const url = `wss://${this.host}/api/v2/rooms/${this.roomCode}/play?role=shard&name=GamePickerProbe&format=json`;
|
||||
let resolved = false;
|
||||
const done = () => { if (!resolved) { resolved = true; resolve(); } };
|
||||
|
||||
try {
|
||||
const probe = new WebSocket(url, ['ecast-v0'], {
|
||||
headers: { Origin: 'https://jackbox.tv' },
|
||||
handshakeTimeout: 8000,
|
||||
});
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
try { probe.close(); } catch (_) {}
|
||||
done();
|
||||
}, 10000);
|
||||
|
||||
probe.on('message', (data) => {
|
||||
try {
|
||||
const msg = JSON.parse(data.toString());
|
||||
if (msg.opcode === 'client/welcome') {
|
||||
const { playerCount, playerNames } = EcastShardClient.parsePlayersFromHere(msg.result.here);
|
||||
if (playerCount > this.playerCount || playerNames.length !== this.playerNames.length) {
|
||||
this.playerCount = playerCount;
|
||||
this.playerNames = playerNames;
|
||||
this.onEvent('lobby.player-joined', {
|
||||
sessionId: this.sessionId,
|
||||
gameId: this.gameId,
|
||||
roomCode: this.roomCode,
|
||||
playerName: playerNames[playerNames.length - 1] || '',
|
||||
playerCount,
|
||||
players: [...playerNames],
|
||||
maxPlayers: this.maxPlayers,
|
||||
});
|
||||
} else if (playerCount !== this.playerCount) {
|
||||
this.playerCount = playerCount;
|
||||
this.playerNames = playerNames;
|
||||
}
|
||||
} else if (msg.opcode === 'error' && msg.result?.code === 2027) {
|
||||
this.gameFinished = true;
|
||||
}
|
||||
} catch (_) {}
|
||||
clearTimeout(timeout);
|
||||
try { probe.close(); } catch (_) {}
|
||||
done();
|
||||
});
|
||||
|
||||
probe.on('error', () => { clearTimeout(timeout); done(); });
|
||||
probe.on('close', () => { clearTimeout(timeout); done(); });
|
||||
} catch (_) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
stopStatusBroadcast() {
|
||||
if (this.statusInterval) {
|
||||
clearInterval(this.statusInterval);
|
||||
@@ -157,8 +217,8 @@ class EcastShardClient {
|
||||
this.playerCount = playerCount;
|
||||
this.playerNames = playerNames;
|
||||
|
||||
if (result.entities?.room) {
|
||||
const roomEntity = result.entities.room;
|
||||
const roomEntity = result.entities?.room || result.entities?.['bc:room'];
|
||||
if (roomEntity) {
|
||||
const roomVal = Array.isArray(roomEntity) ? roomEntity[1]?.val : roomEntity.val;
|
||||
if (roomVal) {
|
||||
const roomState = EcastShardClient.parseRoomEntity(roomVal);
|
||||
@@ -343,11 +403,14 @@ class EcastShardClient {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
const thisWs = this.ws;
|
||||
this.ws.on('close', (code, reason) => {
|
||||
console.log(`[Shard Monitor] Disconnected from room ${this.roomCode} (code: ${code})`);
|
||||
this.ws = null;
|
||||
if (!this.manuallyStopped && !this.gameFinished && this.secret != null && this.host != null) {
|
||||
void this.reconnectWithBackoff();
|
||||
if (this.ws === thisWs) {
|
||||
this.ws = null;
|
||||
if (!this.manuallyStopped && !this.gameFinished && this.secret != null && this.host != null) {
|
||||
void this.reconnectWithBackoff();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user