game workflow corrected

This commit is contained in:
magdo
2025-11-06 19:37:32 +01:00
parent 5a4be5b7d3
commit 5b177c77fc
3 changed files with 63 additions and 1 deletions
@@ -1139,6 +1139,36 @@ export class GameWebSocketService {
// If the socket was in a game, handle cleanup
if (socket.gameCode && socket.playerName) {
try {
// Check if this player is the gamemaster
const game = await this.gameRepository.findByGameCode(socket.gameCode);
const isGamemaster = game && socket.userId && game.createdby === socket.userId;
// If gamemaster leaves, end the game immediately
if (isGamemaster && game) {
logOther(`Gamemaster ${socket.playerName} left game ${socket.gameCode}, ending game`);
const gameRoomName = `game_${socket.gameCode}`;
// Notify all players
this.io.of('/game').to(gameRoomName).emit('game:ended', {
reason: 'gamemaster_left',
gamemasterName: socket.playerName,
message: `🎭 Gamemaster ${socket.playerName} left. Game has ended.`,
timestamp: new Date().toISOString()
});
// Update database
await this.gameRepository.update(game.id, {
state: GameState.CANCELLED,
enddate: new Date()
});
// Clean up all game data
await this.cleanupGameData(socket.gameCode, game.id);
return; // Exit early, no need for further cleanup
}
// Clean up any pending card answer
if (socket.userId) {
const pendingCard = await this.getPendingCard(socket.gameCode, socket.userId);
@@ -2236,7 +2266,7 @@ export class GameWebSocketService {
if (game) {
await this.gameRepository.update(game.id, {
state: GameState.FINISHED,
winner: winnerId,
winnerId: winnerId,
enddate: new Date()
});
}