Fix lobby player leaving

This commit is contained in:
Georges-Leonard Prunet
2026-03-31 15:59:02 +02:00
parent 41612f5d39
commit c96629b704
4 changed files with 38 additions and 14 deletions
+1 -1
View File
@@ -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,
@@ -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,
+8 -7
View File
@@ -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;
}
@@ -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();
}