mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 07:47:50 +01:00
refactor singletons
This commit is contained in:
@@ -2,8 +2,8 @@ const express = require('express');
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('debug');
|
const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('debug');
|
||||||
const logger = require('../modules/Logger')(debugMode);
|
const logger = require('../modules/Logger')(debugMode);
|
||||||
const socketManager = new (require('../modules/SocketManager.js'))().getInstance();
|
const socketManager = (require('../modules/SocketManager.js')).instance;
|
||||||
const gameManager = new (require('../modules/GameManager.js'))().getInstance();
|
const gameManager = (require('../modules/GameManager.js')).instance;
|
||||||
const globals = require('../config/globals.js');
|
const globals = require('../config/globals.js');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const rateLimit = require('express-rate-limit').default;
|
|||||||
const globals = require('../config/globals.js');
|
const globals = require('../config/globals.js');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
|
|
||||||
const gameManager = new GameManager().getInstance();
|
const gameManager = GameManager.instance;
|
||||||
|
|
||||||
const gameCreationLimit = process.env.NODE_ENV.trim() === 'production'
|
const gameCreationLimit = process.env.NODE_ENV.trim() === 'production'
|
||||||
? 20
|
? 20
|
||||||
|
|||||||
@@ -4,9 +4,14 @@ const globals = require('../config/globals');
|
|||||||
|
|
||||||
class ActiveGameRunner {
|
class ActiveGameRunner {
|
||||||
constructor (logger) {
|
constructor (logger) {
|
||||||
|
if (ActiveGameRunner.instance) {
|
||||||
|
throw new Error('The server tried to instantiate more than one ActiveGameRunner');
|
||||||
|
}
|
||||||
|
logger.info('CREATING SINGLETON ACTIVE GAME RUNNER');
|
||||||
this.activeGames = new Map();
|
this.activeGames = new Map();
|
||||||
this.timerThreads = {};
|
this.timerThreads = {};
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
ActiveGameRunner.instance = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We're only going to fork a child process for games with a timer. They will report back to the parent process whenever
|
/* We're only going to fork a child process for games with a timer. They will report back to the parent process whenever
|
||||||
@@ -61,17 +66,4 @@ class ActiveGameRunner {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
class Singleton {
|
module.exports = ActiveGameRunner;
|
||||||
constructor (logger) {
|
|
||||||
if (!Singleton.instance) {
|
|
||||||
logger.info('CREATING SINGLETON ACTIVE GAME RUNNER');
|
|
||||||
Singleton.instance = new ActiveGameRunner(logger);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getInstance () {
|
|
||||||
return Singleton.instance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = Singleton;
|
|
||||||
|
|||||||
@@ -6,11 +6,16 @@ const GameStateCurator = require('./GameStateCurator');
|
|||||||
const UsernameGenerator = require('./UsernameGenerator');
|
const UsernameGenerator = require('./UsernameGenerator');
|
||||||
|
|
||||||
class GameManager {
|
class GameManager {
|
||||||
constructor (logger, environment) {
|
constructor (logger, environment, activeGameRunner) {
|
||||||
|
if (GameManager.instance) {
|
||||||
|
throw new Error('The server tried to instantiate more than one GameManager');
|
||||||
|
}
|
||||||
|
logger.info('CREATING SINGLETON GAME MANAGER');
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
this.activeGameRunner = new ActiveGameRunner(logger).getInstance();
|
this.activeGameRunner = activeGameRunner;
|
||||||
this.namespace = null;
|
this.namespace = null;
|
||||||
|
GameManager.instance = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
setGameSocketNamespace = (namespace) => {
|
setGameSocketNamespace = (namespace) => {
|
||||||
@@ -501,17 +506,4 @@ function getGameSize (cards) {
|
|||||||
return quantity;
|
return quantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Singleton {
|
module.exports = GameManager;
|
||||||
constructor (logger, environment) {
|
|
||||||
if (!Singleton.instance) {
|
|
||||||
logger.info('CREATING SINGLETON GAME MANAGER');
|
|
||||||
Singleton.instance = new GameManager(logger, environment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getInstance () {
|
|
||||||
return Singleton.instance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = Singleton;
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const fs = require('fs');
|
|||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const SocketManager = require('./SocketManager.js');
|
const SocketManager = require('./SocketManager.js');
|
||||||
const GameManager = require('./GameManager.js');
|
const GameManager = require('./GameManager.js');
|
||||||
|
const ActiveGameRunner = require("./ActiveGameRunner.js");
|
||||||
const { ENVIRONMENT } = require('../config/globals.js');
|
const { ENVIRONMENT } = require('../config/globals.js');
|
||||||
const rateLimit = require('express-rate-limit').default;
|
const rateLimit = require('express-rate-limit').default;
|
||||||
|
|
||||||
@@ -13,10 +14,11 @@ const ServerBootstrapper = {
|
|||||||
|
|
||||||
singletons: (logger) => {
|
singletons: (logger) => {
|
||||||
return {
|
return {
|
||||||
socketManager: new SocketManager(logger).getInstance(),
|
activeGameRunner: new ActiveGameRunner(logger),
|
||||||
|
socketManager: new SocketManager(logger, ActiveGameRunner.instance),
|
||||||
gameManager: process.env.NODE_ENV.trim() === 'development'
|
gameManager: process.env.NODE_ENV.trim() === 'development'
|
||||||
? new GameManager(logger, ENVIRONMENT.LOCAL).getInstance()
|
? new GameManager(logger, ENVIRONMENT.LOCAL, ActiveGameRunner.instance)
|
||||||
: new GameManager(logger, ENVIRONMENT.PRODUCTION).getInstance()
|
: new GameManager(logger, ENVIRONMENT.PRODUCTION, ActiveGameRunner.instance)
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,15 @@ const EVENT_IDS = globals.EVENT_IDS;
|
|||||||
const { RateLimiterMemory } = require('rate-limiter-flexible');
|
const { RateLimiterMemory } = require('rate-limiter-flexible');
|
||||||
|
|
||||||
class SocketManager {
|
class SocketManager {
|
||||||
constructor (logger) {
|
constructor (logger, activeGameRunner) {
|
||||||
|
if (SocketManager.instance) {
|
||||||
|
throw new Error('The server attempted to instantiate more than one SocketManager.');
|
||||||
|
}
|
||||||
|
logger.info('CREATING SINGLETON SOCKET MANAGER');
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.io = null;
|
this.io = null;
|
||||||
|
this.activeGameRunner = activeGameRunner;
|
||||||
|
SocketManager.instance = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcast = (message) => {
|
broadcast = (message) => {
|
||||||
@@ -122,17 +128,4 @@ function registerRateLimiter (server, logger) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
class Singleton {
|
module.exports = SocketManager;
|
||||||
constructor (logger) {
|
|
||||||
if (!Singleton.instance) {
|
|
||||||
logger.info('CREATING SINGLETON SOCKET MANAGER');
|
|
||||||
Singleton.instance = new SocketManager(logger);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getInstance () {
|
|
||||||
return Singleton.instance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = Singleton;
|
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ const USER_TYPES = globals.USER_TYPES;
|
|||||||
const STATUS = globals.STATUS;
|
const STATUS = globals.STATUS;
|
||||||
const Person = require('../../../../server/model/Person');
|
const Person = require('../../../../server/model/Person');
|
||||||
const GameManager = require('../../../../server/modules/GameManager.js');
|
const GameManager = require('../../../../server/modules/GameManager.js');
|
||||||
const GameStateCurator = require('../../../../server/modules/GameStateCurator');
|
const GameStateCurator = require('../../../../server/modules/GameStateCurator.js');
|
||||||
|
const ActiveGameRunner = require("../../../../server/modules/ActiveGameRunner.js");
|
||||||
const logger = require('../../../../server/modules/Logger.js')(false);
|
const logger = require('../../../../server/modules/Logger.js')(false);
|
||||||
|
|
||||||
describe('GameManager', () => {
|
describe('GameManager', () => {
|
||||||
@@ -18,7 +19,7 @@ describe('GameManager', () => {
|
|||||||
const inObj = { emit: () => {} };
|
const inObj = { emit: () => {} };
|
||||||
namespace = { in: () => { return inObj; }, to: () => { return inObj; } };
|
namespace = { in: () => { return inObj; }, to: () => { return inObj; } };
|
||||||
socket = { id: '123', emit: () => {}, to: () => { return { emit: () => {} }; } };
|
socket = { id: '123', emit: () => {}, to: () => { return { emit: () => {} }; } };
|
||||||
gameManager = new GameManager(logger, globals.ENVIRONMENT.PRODUCTION).getInstance();
|
gameManager = new GameManager(logger, globals.ENVIRONMENT.PRODUCTION, new ActiveGameRunner(logger));
|
||||||
gameManager.setGameSocketNamespace(namespace);
|
gameManager.setGameSocketNamespace(namespace);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user