Fix lobby player leaving
This commit is contained in:
@@ -127,7 +127,7 @@ async function createTables()
|
|||||||
status VARCHAR(20) DEFAULT 'waiting',
|
status VARCHAR(20) DEFAULT 'waiting',
|
||||||
max_players INT DEFAULT 8,
|
max_players INT DEFAULT 8,
|
||||||
current_round INT DEFAULT 0,
|
current_round INT DEFAULT 0,
|
||||||
max_rounds INT DEFAULT 3,
|
max_rounds INT DEFAULT 5,
|
||||||
round_duration INT DEFAULT 90,
|
round_duration INT DEFAULT 90,
|
||||||
created_at TIMESTAMP DEFAULT NOW(),
|
created_at TIMESTAMP DEFAULT NOW(),
|
||||||
started_at TIMESTAMP,
|
started_at TIMESTAMP,
|
||||||
|
|||||||
@@ -202,6 +202,15 @@ function setupSocketIO(io)
|
|||||||
if (socket.gameRoomId) {
|
if (socket.gameRoomId) {
|
||||||
const roomId = socket.gameRoomId;
|
const roomId = socket.gameRoomId;
|
||||||
const dbRoomId = socket.gameRoomDbId;
|
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', {
|
socket.to(roomId).emit('game-player-left', {
|
||||||
username: socket.user.username,
|
username: socket.user.username,
|
||||||
@@ -867,6 +876,14 @@ function setupSocketIO(io)
|
|||||||
}
|
}
|
||||||
else
|
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
|
// Regular player disconnect
|
||||||
socket.to(roomId).emit('game-player-left', {
|
socket.to(roomId).emit('game-player-left', {
|
||||||
username: socket.user.username,
|
username: socket.user.username,
|
||||||
|
|||||||
@@ -78,10 +78,6 @@ class App {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const actionMap = {
|
|
||||||
'gameroom': 'gameroom'
|
|
||||||
};
|
|
||||||
|
|
||||||
// Event delegation on the menu
|
// Event delegation on the menu
|
||||||
page.addEventListener('click', (e) => {
|
page.addEventListener('click', (e) => {
|
||||||
const button = e.target.closest('.page__item');
|
const button = e.target.closest('.page__item');
|
||||||
@@ -89,9 +85,14 @@ class App {
|
|||||||
|
|
||||||
const action = button.dataset.action;
|
const action = button.dataset.action;
|
||||||
|
|
||||||
// Actions with associated windows
|
if (action === 'gameroom') {
|
||||||
if (actionMap[action]) {
|
const gameRoomWindow = windowRegistry.get('gameroom');
|
||||||
windowRegistry.toggle(actionMap[action]);
|
windowRegistry.toggle('gameroom');
|
||||||
|
gameRoomWindow.loadRooms();
|
||||||
|
|
||||||
|
if (gameRoomWindow?.currentTab === 'browse') {
|
||||||
|
gameRoomWindow.loadRooms();
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -195,7 +195,8 @@ export class GameRoomWindow extends Window {
|
|||||||
currentPlayerIndex: 0,
|
currentPlayerIndex: 0,
|
||||||
guessedLetters: [],
|
guessedLetters: [],
|
||||||
scores: {},
|
scores: {},
|
||||||
counter: 0
|
counter: 0,
|
||||||
|
counterRound: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
this.initDrawing();
|
this.initDrawing();
|
||||||
@@ -375,10 +376,11 @@ export class GameRoomWindow extends Window {
|
|||||||
|
|
||||||
this.socket.on('game-player-left', (data) => {
|
this.socket.on('game-player-left', (data) => {
|
||||||
this.showMessage(`${data.username} a quitté le salon`, 'info');
|
this.showMessage(`${data.username} a quitté le salon`, 'info');
|
||||||
|
console.log(`${data.username} left the room`);
|
||||||
|
|
||||||
if (this.gameState.isPlaying)
|
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);
|
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 spectating, return to spectator list
|
||||||
if (this.isSpectating) {
|
if (this.isSpectating) {
|
||||||
this.resetGameUI();
|
this.resetGameUI();
|
||||||
this.currentRoom = null;
|
// this.currentRoom = null;
|
||||||
this.isSpectating = false;
|
this.isSpectating = false;
|
||||||
this.switchTab('spectator');
|
this.switchTab('spectator');
|
||||||
this.showMessage('La partie est terminée', 'info');
|
this.showMessage('La partie est terminée', 'info');
|
||||||
@@ -752,8 +754,7 @@ export class GameRoomWindow extends Window {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.roomsList = data || [];
|
this.renderRoomsList(data || []);
|
||||||
this.renderRoomsList(this.roomsList);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Load rooms error:', error);
|
console.error('Load rooms error:', error);
|
||||||
this.showMessage('Erreur de connexion', 'error');
|
this.showMessage('Erreur de connexion', 'error');
|
||||||
@@ -1102,7 +1103,9 @@ export class GameRoomWindow extends Window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async loadLobby() {
|
async loadLobby() {
|
||||||
|
console.log('Loading lobby for room:', this.currentRoom);
|
||||||
if (!this.currentRoom) return;
|
if (!this.currentRoom) return;
|
||||||
|
console.log('Managed to load lobby, current room:', this.currentRoom);
|
||||||
|
|
||||||
this.gameState.scores = {};
|
this.gameState.scores = {};
|
||||||
|
|
||||||
@@ -1518,7 +1521,7 @@ export class GameRoomWindow extends Window {
|
|||||||
const pointsText = points !== 0 ? ` (${points > 0 ? '+' : ''}${points} pts)` : '';
|
const pointsText = points !== 0 ? ` (${points > 0 ? '+' : ''}${points} pts)` : '';
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
item.textContent = `${username}: "${guess}" - Bonne ${typeText}!${pointsText}`;
|
item.textContent = `${username}: "${guess}" - Bon ${typeText}!${pointsText}`;
|
||||||
} else {
|
} else {
|
||||||
item.textContent = `${username}: "${guess}" - Mauvais ${typeText}${pointsText}`;
|
item.textContent = `${username}: "${guess}" - Mauvais ${typeText}${pointsText}`;
|
||||||
}
|
}
|
||||||
@@ -1568,7 +1571,10 @@ export class GameRoomWindow extends Window {
|
|||||||
this.wordDisplay.textContent = word.split('').join(' ');
|
this.wordDisplay.textContent = word.split('').join(' ');
|
||||||
|
|
||||||
// Auto next round after delay
|
// Auto next round after delay
|
||||||
|
this.gameState.counterRound++;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
if (this.gameState.counterRound >= (this.gameState.players.length * 4))
|
||||||
|
this.endGame();
|
||||||
if (this.gameState.isPlaying) {
|
if (this.gameState.isPlaying) {
|
||||||
this.nextRound();
|
this.nextRound();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user