Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 91 additions & 53 deletions server/modules/Events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,65 @@ const GameStateCurator = require('./GameStateCurator');
const GameCreationRequest = require('../model/GameCreationRequest');
const { EVENT_IDS, STATUS, USER_TYPES, GAME_PROCESS_COMMANDS, REDIS_CHANNELS, PRIMITIVES } = require('../config/globals');

// Helper function to handle timer commands
async function handleTimerCommand (timerEventSubtype, game, socketId, vars) {
switch (timerEventSubtype) {
case GAME_PROCESS_COMMANDS.PAUSE_TIMER:
const pauseTimeRemaining = await vars.gameManager.pauseTimer(game);
if (pauseTimeRemaining !== null) {
await vars.eventManager.handleEventById(
EVENT_IDS.PAUSE_TIMER,
null,
game,
null,
game.accessCode,
{ timeRemaining: pauseTimeRemaining },
null,
false
);
await vars.gameManager.refreshGame(game);
await vars.eventManager.publisher.publish(
REDIS_CHANNELS.ACTIVE_GAME_STREAM,
vars.eventManager.createMessageToPublish(
game.accessCode,
EVENT_IDS.PAUSE_TIMER,
vars.instanceId,
JSON.stringify({ timeRemaining: pauseTimeRemaining })
)
);
}
break;
case GAME_PROCESS_COMMANDS.RESUME_TIMER:
const resumeTimeRemaining = await vars.gameManager.resumeTimer(game);
if (resumeTimeRemaining !== null) {
await vars.eventManager.handleEventById(
EVENT_IDS.RESUME_TIMER,
null,
game,
null,
game.accessCode,
{ timeRemaining: resumeTimeRemaining },
null,
false
);
await vars.gameManager.refreshGame(game);
await vars.eventManager.publisher.publish(
REDIS_CHANNELS.ACTIVE_GAME_STREAM,
vars.eventManager.createMessageToPublish(
game.accessCode,
EVENT_IDS.RESUME_TIMER,
vars.instanceId,
JSON.stringify({ timeRemaining: resumeTimeRemaining })
)
);
}
break;
case GAME_PROCESS_COMMANDS.GET_TIME_REMAINING:
await vars.gameManager.getTimeRemaining(game, socketId);
break;
}
}

const Events = [
{
id: EVENT_IDS.PLAYER_JOINED,
Expand Down Expand Up @@ -164,7 +223,7 @@ const Events = [
vars.gameManager.deal(game);
if (game.hasTimer) {
game.timerParams.paused = true;
await vars.timerManager.runTimer(game, vars.gameManager.namespace, vars.eventManager, vars.gameManager);
await vars.gameManager.runTimer(game);
}
}
},
Expand Down Expand Up @@ -220,9 +279,10 @@ const Events = [
id: EVENT_IDS.END_GAME,
stateChange: async (game, socketArgs, vars) => {
game.status = STATUS.ENDED;
if (game.hasTimer && vars.timerManager.timerThreads[game.accessCode]) {
vars.logger.trace('KILLING TIMER PROCESS FOR ENDED GAME ' + game.accessCode);
vars.timerManager.timerThreads[game.accessCode].kill();
if (game.hasTimer && vars.gameManager.timers[game.accessCode]) {
vars.logger.trace('STOPPING TIMER FOR ENDED GAME ' + game.accessCode);
vars.gameManager.timers[game.accessCode].stopTimer();
delete vars.gameManager.timers[game.accessCode];
}
for (const person of game.people) {
person.revealed = true;
Expand Down Expand Up @@ -297,12 +357,10 @@ const Events = [
id: EVENT_IDS.RESTART_GAME,
stateChange: async (game, socketArgs, vars) => {
if (vars.instanceId !== vars.senderInstanceId
&& vars.timerManager.timerThreads[game.accessCode]
&& vars.gameManager.timers[game.accessCode]
) {
if (!vars.timerManager.timerThreads[game.accessCode].killed) {
vars.timerManager.timerThreads[game.accessCode].kill();
}
delete vars.timerManager.timerThreads[game.accessCode];
vars.gameManager.timers[game.accessCode].stopTimer();
delete vars.gameManager.timers[game.accessCode];
}
},
communicate: async (game, socketArgs, vars) => {
Expand All @@ -316,25 +374,10 @@ const Events = [
id: EVENT_IDS.TIMER_EVENT,
stateChange: async (game, socketArgs, vars) => {},
communicate: async (game, socketArgs, vars) => {
const thread = vars.timerManager.timerThreads[game.accessCode];
if (thread) {
if (!thread.killed && thread.exitCode === null) {
thread.send({
command: vars.timerEventSubtype,
accessCode: game.accessCode,
socketId: vars.requestingSocketId,
logLevel: vars.logger.logLevel
});
} else {
const socket = vars.gameManager.namespace.sockets.get(vars.requestingSocketId);
if (socket) {
vars.gameManager.namespace.to(socket.id).emit(
GAME_PROCESS_COMMANDS.GET_TIME_REMAINING,
game.timerParams.timeRemaining,
game.timerParams.paused
);
}
}
const timer = vars.gameManager.timers[game.accessCode];
if (timer) {
// Timer is running on this instance, handle the request directly
await handleTimerCommand(vars.timerEventSubtype, game, vars.requestingSocketId, vars);
} else { // we need to consult another container for the timer data
await vars.eventManager.publisher?.publish(
REDIS_CHANNELS.ACTIVE_GAME_STREAM,
Expand All @@ -350,34 +393,29 @@ 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 thread. */
* 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) => {},
communicate: async (game, socketArgs, vars) => {
const thread = vars.timerManager.timerThreads[game.accessCode];
if (thread) {
if (!thread.killed && thread.exitCode === null) {
thread.send({
command: socketArgs.timerEventSubtype,
accessCode: game.accessCode,
socketId: socketArgs.socketId,
logLevel: vars.logger.logLevel
});
} else {
await vars.eventManager.publisher.publish(
REDIS_CHANNELS.ACTIVE_GAME_STREAM,
vars.eventManager.createMessageToPublish(
game.accessCode,
socketArgs.timerEventSubtype,
vars.instanceId,
JSON.stringify({
socketId: socketArgs.socketId,
timeRemaining: game.timerParams.timeRemaining,
paused: game.timerParams.paused
})
)
);
}
const timer = vars.gameManager.timers[game.accessCode];
if (timer) {
// Timer is running on this instance, handle the request
await handleTimerCommand(socketArgs.timerEventSubtype, game, socketArgs.socketId, vars);
} else {
// Timer not running here, publish stored timer state
await vars.eventManager.publisher.publish(
REDIS_CHANNELS.ACTIVE_GAME_STREAM,
vars.eventManager.createMessageToPublish(
game.accessCode,
socketArgs.timerEventSubtype,
vars.instanceId,
JSON.stringify({
socketId: socketArgs.socketId,
timeRemaining: game.timerParams.timeRemaining,
paused: game.timerParams.paused
})
)
);
}
}
},
Expand Down
53 changes: 0 additions & 53 deletions server/modules/GameProcess.js

This file was deleted.

Loading