diff --git a/server/modules/Events.js b/server/modules/Events.js index 3b46dc9..e6797da 100644 --- a/server/modules/Events.js +++ b/server/modules/Events.js @@ -313,32 +313,14 @@ const Events = [ }, { id: EVENT_IDS.TIMER_EVENT, - stateChange: async (game, socketArgs, vars) => { - const timer = vars.timerManager.timers[game.accessCode]; - if (timer) { - // Update game state based on timer command - switch (vars.timerEventSubtype) { - case GAME_PROCESS_COMMANDS.PAUSE_TIMER: - timer.stopTimer(); - game.timerParams.paused = true; - game.timerParams.timeRemaining = timer.currentTimeInMillis; - break; - case GAME_PROCESS_COMMANDS.RESUME_TIMER: - // resumeTimer() returns the timesUpPromise, but we don't await it here - // because timer completion is already handled by TimerManager.runTimer() - timer.resumeTimer(); - game.timerParams.paused = false; - game.timerParams.timeRemaining = timer.currentTimeInMillis; - break; - } - } - }, + stateChange: async (game, socketArgs, vars) => {}, communicate: async (game, socketArgs, vars) => { const timer = vars.timerManager.timers[game.accessCode]; if (timer) { - // Communicate timer state to clients and other instances + // Handle timer operations and communicate state to clients and other instances switch (vars.timerEventSubtype) { case GAME_PROCESS_COMMANDS.PAUSE_TIMER: + timer.stopTimer(); vars.gameManager.namespace.in(game.accessCode).emit( GAME_PROCESS_COMMANDS.PAUSE_TIMER, timer.currentTimeInMillis @@ -354,6 +336,9 @@ const Events = [ ); break; case GAME_PROCESS_COMMANDS.RESUME_TIMER: + // resumeTimer() returns the timesUpPromise, but we don't await it here + // because timer completion is already handled by TimerManager.runTimer() + timer.resumeTimer(); vars.gameManager.namespace.in(game.accessCode).emit( GAME_PROCESS_COMMANDS.RESUME_TIMER, timer.currentTimeInMillis @@ -397,32 +382,14 @@ const Events = [ /* This event is a request from another instance to consult its timer data. In response * to this event, this instance will check if it is home to a particular timer. */ id: EVENT_IDS.SOURCE_TIMER_EVENT, - stateChange: async (game, socketArgs, vars) => { - const timer = vars.timerManager.timers[game.accessCode]; - if (timer) { - // Update game state based on timer command - switch (socketArgs.timerEventSubtype) { - case GAME_PROCESS_COMMANDS.PAUSE_TIMER: - timer.stopTimer(); - game.timerParams.paused = true; - game.timerParams.timeRemaining = timer.currentTimeInMillis; - break; - case GAME_PROCESS_COMMANDS.RESUME_TIMER: - // resumeTimer() returns the timesUpPromise, but we don't await it here - // because timer completion is already handled by TimerManager.runTimer() - timer.resumeTimer(); - game.timerParams.paused = false; - game.timerParams.timeRemaining = timer.currentTimeInMillis; - break; - } - } - }, + stateChange: async (game, socketArgs, vars) => {}, communicate: async (game, socketArgs, vars) => { const timer = vars.timerManager.timers[game.accessCode]; if (timer) { - // Publish timer state to other instances + // Handle timer operations and publish timer state to other instances switch (socketArgs.timerEventSubtype) { case GAME_PROCESS_COMMANDS.PAUSE_TIMER: + timer.stopTimer(); await vars.eventManager.publisher.publish( REDIS_CHANNELS.ACTIVE_GAME_STREAM, vars.eventManager.createMessageToPublish( @@ -434,6 +401,9 @@ const Events = [ ); break; case GAME_PROCESS_COMMANDS.RESUME_TIMER: + // resumeTimer() returns the timesUpPromise, but we don't await it here + // because timer completion is already handled by TimerManager.runTimer() + timer.resumeTimer(); await vars.eventManager.publisher.publish( REDIS_CHANNELS.ACTIVE_GAME_STREAM, vars.eventManager.createMessageToPublish( diff --git a/server/modules/singletons/EventManager.js b/server/modules/singletons/EventManager.js index 3231311..933d69e 100644 --- a/server/modules/singletons/EventManager.js +++ b/server/modules/singletons/EventManager.js @@ -146,15 +146,9 @@ class EventManager { game.accessCode, args, ackFn, - false, + true, eventId ); - // Refresh and sync game state after timer events - await gameManager.refreshGame(game); - await this.publisher?.publish( - REDIS_CHANNELS.ACTIVE_GAME_STREAM, - this.createMessageToPublish(game.accessCode, EVENT_IDS.TIMER_EVENT, this.instanceId, JSON.stringify({ timerEventSubtype: eventId, socketId: socket.id })) - ); } else { await this.handleAndSyncSocketEvent(eventId, game, socket, args, ackFn); }