add /server/status endpoint

This commit is contained in:
Devin Zuczek
2026-08-05 18:42:19 -04:00
parent cc43d57172
commit f185ef97df
8 changed files with 100 additions and 5 deletions
+15
View File
@@ -152,6 +152,21 @@ export async function countPlayersInInstance(
return row?.n ?? 0
}
/**
* How many players are online right now, anywhere — one row per account, so this is
* the player count a status page means. Counts unexpired presence only: rows outlive
* the player by up to the TTL until the sweep purges them, and reads elsewhere ignore
* them the same way. Lobby (null-instance) presence IS counted — those players are
* signed in and playing, they're just not in a room.
*/
export async function countOnlinePlayers(db: D1Database, now = nowSeconds()): Promise<number> {
const row = await db
.prepare('SELECT COUNT(*) AS n FROM presence WHERE expires_at > ?1')
.bind(now)
.first<{ n: number }>()
return row?.n ?? 0
}
/**
* Live head-count per ROOM, keyed by room id — the players standing in any of a
* room's instances right now. One grouped query rather than a count per room, so