diff --git a/client/src/app/dashboard/page.tsx b/client/src/app/dashboard/page.tsx index 301f613..8559b1a 100644 --- a/client/src/app/dashboard/page.tsx +++ b/client/src/app/dashboard/page.tsx @@ -388,24 +388,26 @@ export default function Dashboard() { AGENTES: {entry.players.split(',').length} - {entry.rounds_played > 0 && ( + {entry.matches_played > 0 && ( <>
- RONDAS: - {entry.rounds_played} + PARTIDAS: + {entry.matches_played}
-
- RESULTADOS: -
- {entry.round_results && entry.round_results.split(',').map((res: string, idx: number) => ( -
- ))} + {entry.match_results && entry.match_results.length > 0 && ( +
+ RESULTADOS: +
+ {entry.match_results.split(',').map((res: string, idx: number) => ( +
+ ))} +
-
+ )} )} {entry.players && ( diff --git a/server/src/db.ts b/server/src/db.ts index 1fe06b3..c7daff3 100644 --- a/server/src/db.ts +++ b/server/src/db.ts @@ -30,7 +30,9 @@ export const initDb = async () => { 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 ''; + ADD COLUMN IF NOT EXISTS round_results TEXT DEFAULT '', + ADD COLUMN IF NOT EXISTS matches_played INTEGER DEFAULT 1, + ADD COLUMN IF NOT EXISTS match_results TEXT DEFAULT ''; `); console.log('[DB] Base de datos inicializada correctamente'); @@ -71,15 +73,19 @@ export const logGameEnd = async ( winner: string | null = null, aborted: boolean = false, roundsPlayed: number = 0, - roundResults: boolean[] = [] + roundResults: boolean[] = [], + matchesPlayed: number = 1, + matchResults: string[] = [] ) => { try { - // Convert boolean[] to string "true,false,true" or simplified "W,L,W" - const resultsStr = roundResults.join(','); + // Convert boolean[] to string "true,false,true" + const roundResultsStr = roundResults.map(r => r === true ? 'true' : r === false ? 'false' : '').filter(r => r).join(','); + // matchResults is already string[] like ["aliados", "alemanes"] + const matchResultsStr = matchResults.join(','); await pool.query( - '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'] + 'UPDATE game_logs SET finished_at = CURRENT_TIMESTAMP, winner = $1, status = $2, rounds_played = $3, round_results = $4, matches_played = $5, match_results = $6 WHERE room_id = $7 AND status = $8', + [winner, aborted ? 'aborted' : 'finished', roundsPlayed, roundResultsStr, matchesPlayed, matchResultsStr, roomId, 'active'] ); } catch (err) { console.error('[DB] Error al registrar fin de partida:', err); diff --git a/server/src/index.ts b/server/src/index.ts index c1a829b..c630429 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -349,8 +349,10 @@ io.on('connection', (socket) => { // Desconectar a todos los jugadores de la sala io.in(roomId).socketsLeave(roomId); - // LOG EN DB - logGameEnd(roomId, game.state.winner, false, game.state.currentRound, game.state.questResults); + // LOG EN DB - Incluir resultado de la partida actual en matchResults + const finalMatchResults = [...game.state.matchResults]; + if (game.state.winner) finalMatchResults.push(game.state.winner); + logGameEnd(roomId, game.state.winner, false, game.state.currentRound, game.state.questResults, game.state.matchNumber, finalMatchResults); broadcastAdminUpdate(); } }); @@ -383,7 +385,9 @@ io.on('connection', (socket) => { io.in(roomId).socketsLeave(roomId); // LOG EN DB COMO ABORTADA - logGameEnd(roomId, null, true, game.state.currentRound, game.state.questResults); + const finalMatchResults = [...game.state.matchResults]; + if (game.state.winner) finalMatchResults.push(game.state.winner); + logGameEnd(roomId, null, true, game.state.currentRound, game.state.questResults, game.state.matchNumber, finalMatchResults); broadcastAdminUpdate(); console.log(`[LEAVE_GAME] ${playerName} abandonó la partida ${roomId}. Partida eliminada.`); @@ -471,7 +475,9 @@ io.on('connection', (socket) => { io.in(roomId).socketsLeave(roomId); // Log como abortada por admin - await logGameEnd(roomId, null, true, game.state.currentRound, game.state.questResults); + const finalMatchResults = [...game.state.matchResults]; + if (game.state.winner) finalMatchResults.push(game.state.winner); + await logGameEnd(roomId, null, true, game.state.currentRound, game.state.questResults, game.state.matchNumber, finalMatchResults); socket.emit('admin_action_success'); broadcastAdminUpdate(); } diff --git a/server/src/models/Game.ts b/server/src/models/Game.ts index 719b973..f47e7ff 100644 --- a/server/src/models/Game.ts +++ b/server/src/models/Game.ts @@ -44,7 +44,8 @@ export class Game { revealedVotes: [], history: [], hostId: hostId, - matchNumber: 1 + matchNumber: 1, + matchResults: [] }; } @@ -370,6 +371,11 @@ export class Game { restartGame() { this.log('=== REINICIANDO PARTIDA ==='); + // Guardar resultado de la partida actual antes de resetear + if (this.state.winner) { + this.state.matchResults.push(this.state.winner); + } + // Incrementar contador de partidas y resetear variables de juego this.state.matchNumber++; this.state.currentRound = 1; diff --git a/shared/types.ts b/shared/types.ts index 84041ce..55ba777 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -90,6 +90,7 @@ export interface GameState { winner?: Faction; history: string[]; // Log de acciones para mostrar en pantalla matchNumber: number; // Número de partida en esta operación (se incrementa con cada restart) + matchResults: Faction[]; // Resultados de cada partida (quién ganó) } // Configuración de jugadores por partida (según tus reglas)