This commit is contained in:
cottongin
2025-11-03 17:56:15 -05:00
parent 140988d01d
commit f52754ac87
8 changed files with 1194 additions and 45 deletions

View File

@@ -4,6 +4,7 @@ const { authenticateToken } = require('../middleware/auth');
const db = require('../database');
const { triggerWebhook } = require('../utils/webhooks');
const { getWebSocketManager } = require('../utils/websocket-manager');
const { startPlayerCountCheck, stopPlayerCountCheck } = require('../utils/player-count-checker');
const router = express.Router();
@@ -355,6 +356,16 @@ router.post('/:id/games', authenticateToken, (req, res) => {
console.error('Error triggering notifications:', error);
}
// Automatically start player count check if room code was provided
if (room_code) {
try {
startPlayerCountCheck(req.params.id, result.lastInsertRowid, room_code, game.max_players);
} catch (error) {
console.error('Error starting player count check:', error);
// Don't fail the request if player count check fails
}
}
res.status(201).json(sessionGame);
} catch (error) {
res.status(500).json({ error: error.message });
@@ -569,6 +580,15 @@ router.patch('/:sessionId/games/:gameId/status', authenticateToken, (req, res) =
return res.status(404).json({ error: 'Session game not found' });
}
// Stop player count check if game is no longer playing
if (status !== 'playing') {
try {
stopPlayerCountCheck(sessionId, gameId);
} catch (error) {
console.error('Error stopping player count check:', error);
}
}
res.json({ message: 'Status updated successfully', status });
} catch (error) {
res.status(500).json({ error: error.message });
@@ -580,6 +600,13 @@ router.delete('/:sessionId/games/:gameId', authenticateToken, (req, res) => {
try {
const { sessionId, gameId } = req.params;
// Stop player count check before deleting
try {
stopPlayerCountCheck(sessionId, gameId);
} catch (error) {
console.error('Error stopping player count check:', error);
}
const result = db.prepare(`
DELETE FROM session_games
WHERE session_id = ? AND id = ?
@@ -778,5 +805,101 @@ router.get('/:id/export', authenticateToken, (req, res) => {
}
});
// Start player count check for a session game (admin only)
router.post('/:sessionId/games/:gameId/start-player-check', authenticateToken, (req, res) => {
try {
const { sessionId, gameId } = req.params;
// Get the game to verify it exists and has a room code
const game = db.prepare(`
SELECT sg.*, g.max_players
FROM session_games sg
JOIN games g ON sg.game_id = g.id
WHERE sg.session_id = ? AND sg.id = ?
`).get(sessionId, gameId);
if (!game) {
return res.status(404).json({ error: 'Session game not found' });
}
if (!game.room_code) {
return res.status(400).json({ error: 'Game does not have a room code' });
}
// Start the check
startPlayerCountCheck(sessionId, gameId, game.room_code, game.max_players);
res.json({
message: 'Player count check started',
status: 'waiting'
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Stop player count check for a session game (admin only)
router.post('/:sessionId/games/:gameId/stop-player-check', authenticateToken, (req, res) => {
try {
const { sessionId, gameId } = req.params;
// Stop the check
stopPlayerCountCheck(sessionId, gameId);
res.json({
message: 'Player count check stopped',
status: 'stopped'
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Manually update player count for a session game (admin only)
router.patch('/:sessionId/games/:gameId/player-count', authenticateToken, (req, res) => {
try {
const { sessionId, gameId } = req.params;
const { player_count } = req.body;
if (player_count === undefined || player_count === null) {
return res.status(400).json({ error: 'player_count is required' });
}
const count = parseInt(player_count);
if (isNaN(count) || count < 0) {
return res.status(400).json({ error: 'player_count must be a positive number' });
}
// Update the player count
const result = db.prepare(`
UPDATE session_games
SET player_count = ?, player_count_check_status = 'completed'
WHERE session_id = ? AND id = ?
`).run(count, sessionId, gameId);
if (result.changes === 0) {
return res.status(404).json({ error: 'Session game not found' });
}
// Broadcast via WebSocket
const wsManager = getWebSocketManager();
if (wsManager) {
wsManager.broadcastEvent('player-count.updated', {
sessionId,
gameId,
playerCount: count,
status: 'completed'
}, parseInt(sessionId));
}
res.json({
message: 'Player count updated successfully',
player_count: count
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
module.exports = router;