mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
refactor singletons
This commit is contained in:
@@ -2,8 +2,8 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('debug');
|
||||
const logger = require('../modules/Logger')(debugMode);
|
||||
const socketManager = new (require('../modules/SocketManager.js'))().getInstance();
|
||||
const gameManager = new (require('../modules/GameManager.js'))().getInstance();
|
||||
const socketManager = (require('../modules/SocketManager.js')).instance;
|
||||
const gameManager = (require('../modules/GameManager.js')).instance;
|
||||
const globals = require('../config/globals.js');
|
||||
const cors = require('cors');
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ const rateLimit = require('express-rate-limit').default;
|
||||
const globals = require('../config/globals.js');
|
||||
const cors = require('cors');
|
||||
|
||||
const gameManager = new GameManager().getInstance();
|
||||
const gameManager = GameManager.instance;
|
||||
|
||||
const gameCreationLimit = process.env.NODE_ENV.trim() === 'production'
|
||||
? 20
|
||||
|
||||
@@ -4,9 +4,14 @@ const globals = require('../config/globals');
|
||||
|
||||
class ActiveGameRunner {
|
||||
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.timerThreads = {};
|
||||
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
|
||||
@@ -61,17 +66,4 @@ class ActiveGameRunner {
|
||||
};
|
||||
}
|
||||
|
||||
class Singleton {
|
||||
constructor (logger) {
|
||||
if (!Singleton.instance) {
|
||||
logger.info('CREATING SINGLETON ACTIVE GAME RUNNER');
|
||||
Singleton.instance = new ActiveGameRunner(logger);
|
||||
}
|
||||
}
|
||||
|
||||
getInstance () {
|
||||
return Singleton.instance;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Singleton;
|
||||
module.exports = ActiveGameRunner;
|
||||
|
||||
@@ -6,11 +6,16 @@ const GameStateCurator = require('./GameStateCurator');
|
||||
const UsernameGenerator = require('./UsernameGenerator');
|
||||
|
||||
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.environment = environment;
|
||||
this.activeGameRunner = new ActiveGameRunner(logger).getInstance();
|
||||
this.activeGameRunner = activeGameRunner;
|
||||
this.namespace = null;
|
||||
GameManager.instance = this;
|
||||
}
|
||||
|
||||
setGameSocketNamespace = (namespace) => {
|
||||
@@ -501,17 +506,4 @@ function getGameSize (cards) {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
class Singleton {
|
||||
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;
|
||||
module.exports = GameManager;
|
||||
|
||||
@@ -6,6 +6,7 @@ const fs = require('fs');
|
||||
const crypto = require('crypto');
|
||||
const SocketManager = require('./SocketManager.js');
|
||||
const GameManager = require('./GameManager.js');
|
||||
const ActiveGameRunner = require("./ActiveGameRunner.js");
|
||||
const { ENVIRONMENT } = require('../config/globals.js');
|
||||
const rateLimit = require('express-rate-limit').default;
|
||||
|
||||
@@ -13,10 +14,11 @@ const ServerBootstrapper = {
|
||||
|
||||
singletons: (logger) => {
|
||||
return {
|
||||
socketManager: new SocketManager(logger).getInstance(),
|
||||
activeGameRunner: new ActiveGameRunner(logger),
|
||||
socketManager: new SocketManager(logger, ActiveGameRunner.instance),
|
||||
gameManager: process.env.NODE_ENV.trim() === 'development'
|
||||
? new GameManager(logger, ENVIRONMENT.LOCAL).getInstance()
|
||||
: new GameManager(logger, ENVIRONMENT.PRODUCTION).getInstance()
|
||||
? new GameManager(logger, ENVIRONMENT.LOCAL, ActiveGameRunner.instance)
|
||||
: new GameManager(logger, ENVIRONMENT.PRODUCTION, ActiveGameRunner.instance)
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
@@ -3,9 +3,15 @@ const EVENT_IDS = globals.EVENT_IDS;
|
||||
const { RateLimiterMemory } = require('rate-limiter-flexible');
|
||||
|
||||
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.io = null;
|
||||
this.activeGameRunner = activeGameRunner;
|
||||
SocketManager.instance = this;
|
||||
}
|
||||
|
||||
broadcast = (message) => {
|
||||
@@ -122,17 +128,4 @@ function registerRateLimiter (server, logger) {
|
||||
});
|
||||
}
|
||||
|
||||
class Singleton {
|
||||
constructor (logger) {
|
||||
if (!Singleton.instance) {
|
||||
logger.info('CREATING SINGLETON SOCKET MANAGER');
|
||||
Singleton.instance = new SocketManager(logger);
|
||||
}
|
||||
}
|
||||
|
||||
getInstance () {
|
||||
return Singleton.instance;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Singleton;
|
||||
module.exports = SocketManager;
|
||||
|
||||
Reference in New Issue
Block a user