mirror of
https://github.com/AlecM33/Werewolf.git
synced 2026-02-10 04:03:33 +01:00
Update tests and remove GameProcess.js
Co-authored-by: AlecM33 <24642328+AlecM33@users.noreply.github.com>
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
const { GAME_PROCESS_COMMANDS, PRIMITIVES } = require('../config/globals');
|
||||
const ServerTimer = require('./ServerTimer.js');
|
||||
|
||||
let timer;
|
||||
|
||||
// This is a subprocess spawned by logic in the TimerManager module.
|
||||
process.on('message', (msg) => {
|
||||
const logger = require('./Logger')(msg.logLevel);
|
||||
switch (msg.command) {
|
||||
case GAME_PROCESS_COMMANDS.START_TIMER:
|
||||
logger.trace('CHILD PROCESS ' + msg.accessCode + ': START TIMER');
|
||||
timer = new ServerTimer(
|
||||
msg.hours,
|
||||
msg.minutes,
|
||||
PRIMITIVES.CLOCK_TICK_INTERVAL_MILLIS,
|
||||
logger
|
||||
);
|
||||
timer.runTimer().then(() => {
|
||||
logger.debug('Timer finished for ' + msg.accessCode);
|
||||
process.send({ command: GAME_PROCESS_COMMANDS.END_TIMER });
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
break;
|
||||
case GAME_PROCESS_COMMANDS.PAUSE_TIMER:
|
||||
timer.stopTimer();
|
||||
logger.trace('CHILD PROCESS ' + msg.accessCode + ': PAUSE TIMER');
|
||||
process.send({ command: GAME_PROCESS_COMMANDS.PAUSE_TIMER, timeRemaining: timer.currentTimeInMillis });
|
||||
|
||||
break;
|
||||
|
||||
case GAME_PROCESS_COMMANDS.RESUME_TIMER:
|
||||
timer.resumeTimer().then(() => {
|
||||
logger.debug('Timer finished for ' + msg.accessCode);
|
||||
process.send({ command: GAME_PROCESS_COMMANDS.END_TIMER });
|
||||
process.exit(0);
|
||||
});
|
||||
logger.trace('CHILD PROCESS ' + msg.accessCode + ': RESUME TIMER');
|
||||
process.send({ command: GAME_PROCESS_COMMANDS.RESUME_TIMER, timeRemaining: timer.currentTimeInMillis });
|
||||
|
||||
break;
|
||||
|
||||
case GAME_PROCESS_COMMANDS.GET_TIME_REMAINING:
|
||||
logger.trace('CHILD PROCESS ' + msg.accessCode + ': GET TIME REMAINING');
|
||||
process.send({
|
||||
command: GAME_PROCESS_COMMANDS.GET_TIME_REMAINING,
|
||||
timeRemaining: timer.currentTimeInMillis,
|
||||
socketId: msg.socketId
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
// TODO: clean up these deep relative paths? jsconfig.json is not working...
|
||||
const Game = require('../../../../server/model/Game');
|
||||
const { ENVIRONMENTS, EVENT_IDS, USER_TYPES, STATUS } = require('../../../../server/config/globals.js');
|
||||
const { ENVIRONMENTS, EVENT_IDS, USER_TYPES, STATUS, GAME_PROCESS_COMMANDS } = require('../../../../server/config/globals.js');
|
||||
const GameManager = require('../../../../server/modules/singletons/GameManager.js');
|
||||
const TimerManager = require('../../../../server/modules/singletons/TimerManager.js');
|
||||
const EventManager = require('../../../../server/modules/singletons/EventManager.js');
|
||||
@@ -55,7 +55,7 @@ describe('Events', () => {
|
||||
spyOn(eventManager.publisher, 'publish').and.callThrough();
|
||||
spyOn(eventManager, 'createMessageToPublish').and.stub();
|
||||
namespace.sockets = new Map();
|
||||
timerManager.timerThreads = {};
|
||||
timerManager.timers = {};
|
||||
});
|
||||
|
||||
describe(EVENT_IDS.PLAYER_JOINED, () => {
|
||||
@@ -529,23 +529,23 @@ describe('Events', () => {
|
||||
|
||||
describe(EVENT_IDS.RESTART_GAME, () => {
|
||||
describe('stateChange', () => {
|
||||
it('should kill any alive timer thread if the instance is home to it', async () => {
|
||||
const mockThread = { kill: () => {}, killed: false };
|
||||
timerManager.timerThreads = { ABCD: mockThread };
|
||||
spyOn(timerManager.timerThreads.ABCD, 'kill').and.callThrough();
|
||||
it('should stop any timer if the instance is home to it', async () => {
|
||||
const mockTimer = { stopTimer: () => {} };
|
||||
timerManager.timers = { ABCD: mockTimer };
|
||||
spyOn(timerManager.timers.ABCD, 'stopTimer').and.callThrough();
|
||||
await Events.find((e) => e.id === EVENT_IDS.RESTART_GAME)
|
||||
.stateChange(game, { personId: 'b' }, { gameManager: gameManager, timerManager: timerManager, instanceId: '111', senderInstanceId: '222' });
|
||||
expect(mockThread.kill).toHaveBeenCalled();
|
||||
expect(Object.keys(timerManager.timerThreads).length).toEqual(0);
|
||||
expect(mockTimer.stopTimer).toHaveBeenCalled();
|
||||
expect(Object.keys(timerManager.timers).length).toEqual(0);
|
||||
});
|
||||
it('should not kill the timer thread if the instance sent the event', async () => {
|
||||
const mockThread = { kill: () => {}, killed: false };
|
||||
timerManager.timerThreads = { ABCD: mockThread };
|
||||
spyOn(timerManager.timerThreads.ABCD, 'kill').and.callThrough();
|
||||
it('should not stop the timer if the instance sent the event', async () => {
|
||||
const mockTimer = { stopTimer: () => {} };
|
||||
timerManager.timers = { ABCD: mockTimer };
|
||||
spyOn(timerManager.timers.ABCD, 'stopTimer').and.callThrough();
|
||||
await Events.find((e) => e.id === EVENT_IDS.RESTART_GAME)
|
||||
.stateChange(game, { personId: 'b' }, { gameManager: gameManager, timerManager: timerManager, instanceId: '111', senderInstanceId: '111' });
|
||||
expect(mockThread.kill).not.toHaveBeenCalled();
|
||||
expect(Object.keys(timerManager.timerThreads).length).toEqual(1);
|
||||
expect(mockTimer.stopTimer).not.toHaveBeenCalled();
|
||||
expect(Object.keys(timerManager.timers).length).toEqual(1);
|
||||
});
|
||||
});
|
||||
describe('communicate', () => {
|
||||
@@ -569,30 +569,41 @@ describe('Events', () => {
|
||||
|
||||
describe(EVENT_IDS.TIMER_EVENT, () => {
|
||||
describe('communicate', () => {
|
||||
it('should publish an event to source timer data if the timer thread is not found', async () => {
|
||||
it('should publish an event to source timer data if the timer is not found', async () => {
|
||||
await Events.find((e) => e.id === EVENT_IDS.TIMER_EVENT)
|
||||
.communicate(game, {}, { gameManager: gameManager, timerManager: timerManager, eventManager: eventManager });
|
||||
.communicate(game, {}, {
|
||||
gameManager: gameManager,
|
||||
timerManager: timerManager,
|
||||
eventManager: eventManager,
|
||||
timerEventSubtype: GAME_PROCESS_COMMANDS.PAUSE_TIMER,
|
||||
requestingSocketId: '2'
|
||||
});
|
||||
expect(eventManager.publisher.publish).toHaveBeenCalled();
|
||||
});
|
||||
it('should send a message to the thread if it is found', async () => {
|
||||
const mockThread = { exitCode: null, kill: () => {}, send: (...args) => {}, killed: false };
|
||||
timerManager.timerThreads = { ABCD: mockThread };
|
||||
spyOn(timerManager.timerThreads.ABCD, 'send').and.callThrough();
|
||||
it('should call timer methods directly if the timer is found', async () => {
|
||||
const mockTimer = { currentTimeInMillis: 5000 };
|
||||
timerManager.timers = { ABCD: mockTimer };
|
||||
game.timerParams = { paused: true };
|
||||
const mockSocket = { id: '2' };
|
||||
const mockEmitObj = { emit: jasmine.createSpy('emit') };
|
||||
|
||||
namespace.sockets.set('2', mockSocket);
|
||||
namespace.to.and.returnValue(mockEmitObj);
|
||||
|
||||
await Events.find((e) => e.id === EVENT_IDS.TIMER_EVENT)
|
||||
.communicate(game, {}, {
|
||||
gameManager: gameManager,
|
||||
timerManager: timerManager,
|
||||
eventManager: eventManager,
|
||||
timerEventSubtype: EVENT_IDS.GET_TIME_REMAINING,
|
||||
timerEventSubtype: GAME_PROCESS_COMMANDS.GET_TIME_REMAINING,
|
||||
requestingSocketId: '2',
|
||||
logger: { logLevel: 'trace' }
|
||||
});
|
||||
expect(mockThread.send).toHaveBeenCalledWith({
|
||||
command: EVENT_IDS.GET_TIME_REMAINING,
|
||||
accessCode: 'ABCD',
|
||||
socketId: '2',
|
||||
logLevel: 'trace'
|
||||
});
|
||||
expect(mockEmitObj.emit).toHaveBeenCalledWith(
|
||||
GAME_PROCESS_COMMANDS.GET_TIME_REMAINING,
|
||||
5000,
|
||||
true
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -91,16 +91,16 @@ describe('GameManager', () => {
|
||||
it('should reset all relevant game parameters, including when the game has a timer', async () => {
|
||||
game.timerParams = { hours: 2, minutes: 2, paused: false };
|
||||
game.hasTimer = true;
|
||||
timerManager.timerThreads = { ABCD: { kill: () => {} } };
|
||||
timerManager.timers = { ABCD: { stopTimer: () => {} } };
|
||||
game.status = STATUS.ENDED;
|
||||
|
||||
const threadKillSpy = spyOn(timerManager.timerThreads.ABCD, 'kill');
|
||||
const timerStopSpy = spyOn(timerManager.timers.ABCD, 'stopTimer');
|
||||
const emitSpy = spyOn(namespace.in(), 'emit');
|
||||
|
||||
await gameManager.restartGame(game, namespace);
|
||||
|
||||
expect(game.status).toEqual(STATUS.LOBBY);
|
||||
expect(threadKillSpy).toHaveBeenCalled();
|
||||
expect(timerStopSpy).toHaveBeenCalled();
|
||||
expect(emitSpy).toHaveBeenCalledWith(globals.EVENT_IDS.RESTART_GAME);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user