show how many players in lobby

This commit is contained in:
evanpelle
2024-08-19 14:52:38 -07:00
parent 7d48dd1cba
commit e92dc8ba36
8 changed files with 55 additions and 12 deletions
+13
View File
@@ -42,6 +42,15 @@ export class GameServer {
console.warn(`client ${clientMsg.clientID} sent to wrong game`)
}
}
if (clientMsg.type == "leave") {
const toRemove = this.clients.filter(c => c.id)
if (toRemove.length == 0) {
return
}
toRemove[0].ws.close()
console.log(`client ${toRemove[0].id} left game`)
this.clients = this.clients.filter(c => c.id != clientMsg.clientID)
}
})
// In case a client joined the game late and missed the start message.
@@ -50,6 +59,10 @@ export class GameServer {
}
}
public numClients(): number {
return this.clients.length
}
public startTime(): number {
return this.createdAt + this.config.lobbyLifetime()
}
+1 -1
View File
@@ -26,7 +26,7 @@ const gm = new GameManager(getConfig())
// New GET endpoint to list lobbies
app.get('/lobbies', (req, res) => {
res.json({
lobbies: gm.gamesByPhase(GamePhase.Lobby).map(g => ({id: g.id, startTime: g.startTime()})),
lobbies: gm.gamesByPhase(GamePhase.Lobby).map(g => ({id: g.id, startTime: g.startTime(), numClients: g.numClients()})),
});
});