Decouple room monitoring from player count, fix Jackbox API fetch
Extracts checkRoomStatus into shared jackbox-api.js with proper User-Agent header (bare fetch was silently rejected by Jackbox API) and always-on error logging (previously gated behind DEBUG flag). Splits room-start detection (room-monitor.js) from audience-based player counting (player-count-checker.js) to eliminate circular dependency and allow immediate game.started detection. Room monitor now polls immediately instead of waiting 10 seconds for first check. Made-with: Cursor
This commit is contained in:
42
backend/utils/jackbox-api.js
Normal file
42
backend/utils/jackbox-api.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const JACKBOX_API_BASE = 'https://ecast.jackboxgames.com/api/v2';
|
||||
|
||||
const DEFAULT_HEADERS = {
|
||||
'User-Agent': 'Mozilla/5.0 (compatible; GamePicker/1.0)'
|
||||
};
|
||||
|
||||
/**
|
||||
* Check room status via the Jackbox ecast REST API.
|
||||
* Shared by room-monitor (polling for lock) and player-count-checker (room existence).
|
||||
*/
|
||||
async function checkRoomStatus(roomCode) {
|
||||
try {
|
||||
const response = await fetch(`${JACKBOX_API_BASE}/rooms/${roomCode}`, {
|
||||
headers: DEFAULT_HEADERS
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.log(`[Jackbox API] Room ${roomCode}: HTTP ${response.status}`);
|
||||
return { exists: false };
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const roomData = data.body || data;
|
||||
|
||||
if (process.env.DEBUG === 'true') {
|
||||
console.log('[Jackbox API] Room data:', JSON.stringify(roomData, null, 2));
|
||||
}
|
||||
|
||||
return {
|
||||
exists: true,
|
||||
locked: roomData.locked || false,
|
||||
full: roomData.full || false,
|
||||
maxPlayers: roomData.maxPlayers || 8,
|
||||
minPlayers: roomData.minPlayers || 0
|
||||
};
|
||||
} catch (e) {
|
||||
console.error(`[Jackbox API] Error checking room ${roomCode}:`, e.message);
|
||||
return { exists: false };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { checkRoomStatus };
|
||||
Reference in New Issue
Block a user