feat(dashboard): show round info and results for active and finished games
Some checks failed
CI/CD - Francia Ocupada (La Resistencia) / build-and-deploy (push) Failing after 10s
Some checks failed
CI/CD - Francia Ocupada (La Resistencia) / build-and-deploy (push) Failing after 10s
This commit is contained in:
@@ -25,6 +25,14 @@ export const initDb = async () => {
|
||||
status TEXT DEFAULT 'active' -- 'active', 'finished', 'aborted'
|
||||
);
|
||||
`);
|
||||
|
||||
// Migration: Add new columns if they don't exist
|
||||
await client.query(`
|
||||
ALTER TABLE game_logs
|
||||
ADD COLUMN IF NOT EXISTS rounds_played INTEGER DEFAULT 0,
|
||||
ADD COLUMN IF NOT EXISTS round_results TEXT DEFAULT '';
|
||||
`);
|
||||
|
||||
console.log('[DB] Base de datos inicializada correctamente');
|
||||
} catch (err) {
|
||||
console.error('[DB] Error al inicializar base de datos:', err);
|
||||
@@ -58,11 +66,20 @@ export const updateGamePlayers = async (roomId: string, players: string[]) => {
|
||||
};
|
||||
|
||||
// Registrar fin de partida
|
||||
export const logGameEnd = async (roomId: string, winner: string | null = null, aborted: boolean = false) => {
|
||||
export const logGameEnd = async (
|
||||
roomId: string,
|
||||
winner: string | null = null,
|
||||
aborted: boolean = false,
|
||||
roundsPlayed: number = 0,
|
||||
roundResults: boolean[] = []
|
||||
) => {
|
||||
try {
|
||||
// Convert boolean[] to string "true,false,true" or simplified "W,L,W"
|
||||
const resultsStr = roundResults.join(',');
|
||||
|
||||
await pool.query(
|
||||
'UPDATE game_logs SET finished_at = CURRENT_TIMESTAMP, winner = $1, status = $2 WHERE room_id = $3 AND status = $4',
|
||||
[winner, aborted ? 'aborted' : 'finished', roomId, 'active']
|
||||
'UPDATE game_logs SET finished_at = CURRENT_TIMESTAMP, winner = $1, status = $2, rounds_played = $3, round_results = $4 WHERE room_id = $5 AND status = $6',
|
||||
[winner, aborted ? 'aborted' : 'finished', roundsPlayed, resultsStr, roomId, 'active']
|
||||
);
|
||||
} catch (err) {
|
||||
console.error('[DB] Error al registrar fin de partida:', err);
|
||||
|
||||
@@ -90,6 +90,7 @@ const getAdminData = async () => {
|
||||
status: g.state.phase,
|
||||
currentPlayers: g.state.players.length,
|
||||
maxPlayers: g.maxPlayers,
|
||||
currentRound: g.state.currentRound,
|
||||
players: g.state.players.map(p => ({ id: p.id, name: p.name }))
|
||||
}));
|
||||
|
||||
@@ -348,7 +349,7 @@ io.on('connection', (socket) => {
|
||||
io.in(roomId).socketsLeave(roomId);
|
||||
|
||||
// LOG EN DB
|
||||
logGameEnd(roomId, game.state.winner, false);
|
||||
logGameEnd(roomId, game.state.winner, false, game.state.currentRound, game.state.questResults);
|
||||
broadcastAdminUpdate();
|
||||
}
|
||||
});
|
||||
@@ -381,7 +382,7 @@ io.on('connection', (socket) => {
|
||||
io.in(roomId).socketsLeave(roomId);
|
||||
|
||||
// LOG EN DB COMO ABORTADA
|
||||
logGameEnd(roomId, null, true);
|
||||
logGameEnd(roomId, null, true, game.state.currentRound, game.state.questResults);
|
||||
broadcastAdminUpdate();
|
||||
|
||||
console.log(`[LEAVE_GAME] ${playerName} abandonó la partida ${roomId}. Partida eliminada.`);
|
||||
@@ -469,7 +470,7 @@ io.on('connection', (socket) => {
|
||||
io.in(roomId).socketsLeave(roomId);
|
||||
|
||||
// Log como abortada por admin
|
||||
await logGameEnd(roomId, null, true);
|
||||
await logGameEnd(roomId, null, true, game.state.currentRound, game.state.questResults);
|
||||
socket.emit('admin_action_success');
|
||||
broadcastAdminUpdate();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user