From c96629b704687b03ab5cabde1e6c99b3f330d8d2 Mon Sep 17 00:00:00 2001 From: Georges-Leonard Prunet Date: Tue, 31 Mar 2026 15:59:02 +0200 Subject: [PATCH] Fix lobby player leaving --- Transcendence/srcs/backend/db.js | 2 +- Transcendence/srcs/backend/services/socket.js | 17 +++++++++++++++++ Transcendence/srcs/frontend/src/trans/app.js | 15 ++++++++------- .../srcs/frontend/src/windows/game_room.js | 18 ++++++++++++------ 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Transcendence/srcs/backend/db.js b/Transcendence/srcs/backend/db.js index bbb9c63..13a32fe 100644 --- a/Transcendence/srcs/backend/db.js +++ b/Transcendence/srcs/backend/db.js @@ -127,7 +127,7 @@ async function createTables() status VARCHAR(20) DEFAULT 'waiting', max_players INT DEFAULT 8, current_round INT DEFAULT 0, - max_rounds INT DEFAULT 3, + max_rounds INT DEFAULT 5, round_duration INT DEFAULT 90, created_at TIMESTAMP DEFAULT NOW(), started_at TIMESTAMP, diff --git a/Transcendence/srcs/backend/services/socket.js b/Transcendence/srcs/backend/services/socket.js index 39a0780..0e99a4f 100644 --- a/Transcendence/srcs/backend/services/socket.js +++ b/Transcendence/srcs/backend/services/socket.js @@ -202,6 +202,15 @@ function setupSocketIO(io) if (socket.gameRoomId) { const roomId = socket.gameRoomId; const dbRoomId = socket.gameRoomDbId; + const userId = socket.user.userId; + + if (dbRoomId && userId) { + try { + await gameRoomService.leaveRoom(dbRoomId, userId); + } catch (err) { + console.error('Error removing player from room on socket leave:', err.message); + } + } socket.to(roomId).emit('game-player-left', { username: socket.user.username, @@ -867,6 +876,14 @@ function setupSocketIO(io) } else { + if (dbRoomId && socket.user.userId) { + try { + await gameRoomService.leaveRoom(dbRoomId, socket.user.userId); + } catch (err) { + console.error('Error removing disconnected player from room:', err.message); + } + } + // Regular player disconnect socket.to(roomId).emit('game-player-left', { username: socket.user.username, diff --git a/Transcendence/srcs/frontend/src/trans/app.js b/Transcendence/srcs/frontend/src/trans/app.js index 7a73d79..9070d38 100644 --- a/Transcendence/srcs/frontend/src/trans/app.js +++ b/Transcendence/srcs/frontend/src/trans/app.js @@ -78,10 +78,6 @@ class App { return; } - const actionMap = { - 'gameroom': 'gameroom' - }; - // Event delegation on the menu page.addEventListener('click', (e) => { const button = e.target.closest('.page__item'); @@ -89,9 +85,14 @@ class App { const action = button.dataset.action; - // Actions with associated windows - if (actionMap[action]) { - windowRegistry.toggle(actionMap[action]); + if (action === 'gameroom') { + const gameRoomWindow = windowRegistry.get('gameroom'); + windowRegistry.toggle('gameroom'); + gameRoomWindow.loadRooms(); + + if (gameRoomWindow?.currentTab === 'browse') { + gameRoomWindow.loadRooms(); + } return; } diff --git a/Transcendence/srcs/frontend/src/windows/game_room.js b/Transcendence/srcs/frontend/src/windows/game_room.js index c507960..a39cfef 100644 --- a/Transcendence/srcs/frontend/src/windows/game_room.js +++ b/Transcendence/srcs/frontend/src/windows/game_room.js @@ -195,7 +195,8 @@ export class GameRoomWindow extends Window { currentPlayerIndex: 0, guessedLetters: [], scores: {}, - counter: 0 + counter: 0, + counterRound: 0 }; this.initDrawing(); @@ -375,10 +376,11 @@ export class GameRoomWindow extends Window { this.socket.on('game-player-left', (data) => { this.showMessage(`${data.username} a quitté le salon`, 'info'); + console.log(`${data.username} left the room`); if (this.gameState.isPlaying) { - if (this.gameState.players) + if (Array.isArray(this.gameState.players)) this.gameState.players = this.gameState.players.filter(p => p !== data.username); } @@ -494,7 +496,7 @@ export class GameRoomWindow extends Window { // If spectating, return to spectator list if (this.isSpectating) { this.resetGameUI(); - this.currentRoom = null; + // this.currentRoom = null; this.isSpectating = false; this.switchTab('spectator'); this.showMessage('La partie est terminée', 'info'); @@ -752,8 +754,7 @@ export class GameRoomWindow extends Window { return; } - this.roomsList = data || []; - this.renderRoomsList(this.roomsList); + this.renderRoomsList(data || []); } catch (error) { console.error('Load rooms error:', error); this.showMessage('Erreur de connexion', 'error'); @@ -1102,7 +1103,9 @@ export class GameRoomWindow extends Window { } async loadLobby() { + console.log('Loading lobby for room:', this.currentRoom); if (!this.currentRoom) return; + console.log('Managed to load lobby, current room:', this.currentRoom); this.gameState.scores = {}; @@ -1518,7 +1521,7 @@ export class GameRoomWindow extends Window { const pointsText = points !== 0 ? ` (${points > 0 ? '+' : ''}${points} pts)` : ''; if (success) { - item.textContent = `${username}: "${guess}" - Bonne ${typeText}!${pointsText}`; + item.textContent = `${username}: "${guess}" - Bon ${typeText}!${pointsText}`; } else { item.textContent = `${username}: "${guess}" - Mauvais ${typeText}${pointsText}`; } @@ -1568,7 +1571,10 @@ export class GameRoomWindow extends Window { this.wordDisplay.textContent = word.split('').join(' '); // Auto next round after delay + this.gameState.counterRound++; setTimeout(() => { + if (this.gameState.counterRound >= (this.gameState.players.length * 4)) + this.endGame(); if (this.gameState.isPlaying) { this.nextRound(); }