mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
various refactors
This commit is contained in:
@@ -3,13 +3,13 @@ const router = express.Router();
|
||||
const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('debug');
|
||||
const logger = require('../modules/Logger')(debugMode);
|
||||
const eventManager = (require('../modules/singletons/EventManager.js')).instance;
|
||||
const globals = require('../config/globals.js');
|
||||
const cors = require('cors');
|
||||
const { CORS_OPTIONS, CONTENT_TYPE_VALIDATOR } = require('../config/globals');
|
||||
|
||||
router.use(cors(globals.CORS));
|
||||
router.use(cors(CORS_OPTIONS));
|
||||
|
||||
router.post('/sockets/broadcast', (req, res, next) => {
|
||||
globals.CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||
CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||
});
|
||||
|
||||
// TODO: implement client-side display of this message.
|
||||
|
||||
@@ -4,8 +4,8 @@ const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())
|
||||
const logger = require('../modules/Logger')(debugMode);
|
||||
const GameManager = require('../modules/singletons/GameManager.js');
|
||||
const rateLimit = require('express-rate-limit').default;
|
||||
const globals = require('../config/globals.js');
|
||||
const cors = require('cors');
|
||||
const { CORS_OPTIONS, CONTENT_TYPE_VALIDATOR, PRIMITIVES, ERROR_MESSAGES, ENVIRONMENTS } = require('../config/globals');
|
||||
|
||||
const gameManager = GameManager.instance;
|
||||
|
||||
@@ -20,19 +20,19 @@ const gameEndpointLimiter = rateLimit({
|
||||
legacyHeaders: false
|
||||
});
|
||||
|
||||
router.use(cors(globals.CORS));
|
||||
router.options('/:code/players', cors(globals.CORS));
|
||||
router.options('/create', cors(globals.CORS));
|
||||
router.options('/restart', cors(globals.CORS));
|
||||
router.use(cors(CORS_OPTIONS));
|
||||
router.options('/:code/players', cors(CORS_OPTIONS));
|
||||
router.options('/create', cors(CORS_OPTIONS));
|
||||
router.options('/restart', cors(CORS_OPTIONS));
|
||||
|
||||
router.post('/create', (req, res, next) => {
|
||||
globals.CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||
CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||
});
|
||||
router.patch('/players', (req, res, next) => {
|
||||
globals.CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||
CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||
});
|
||||
router.patch('/restart', (req, res, next) => {
|
||||
globals.CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||
CONTENT_TYPE_VALIDATOR(req, res, next);
|
||||
});
|
||||
|
||||
router.post('/create', gameEndpointLimiter, function (req, res) {
|
||||
@@ -43,8 +43,8 @@ router.post('/create', gameEndpointLimiter, function (req, res) {
|
||||
res.status(201).send(result); // game was created successfully, and access code was returned
|
||||
}
|
||||
}).catch((e) => {
|
||||
if (e === globals.ERROR_MESSAGE.BAD_CREATE_REQUEST) {
|
||||
res.status(400).send(globals.ERROR_MESSAGE.BAD_CREATE_REQUEST);
|
||||
if (e === ERROR_MESSAGES.BAD_CREATE_REQUEST) {
|
||||
res.status(400).send(ERROR_MESSAGES.BAD_CREATE_REQUEST);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -78,7 +78,7 @@ router.patch('/:code/players', async function (req, res) {
|
||||
} else {
|
||||
const game = await gameManager.getActiveGame(req.body.accessCode);
|
||||
if (game) {
|
||||
const inUseCookie = gameManager.environment === globals.ENVIRONMENT.PRODUCTION ? req.body.localCookie : req.body.sessionCookie;
|
||||
const inUseCookie = gameManager.environment === ENVIRONMENTS.PRODUCTION ? req.body.localCookie : req.body.sessionCookie;
|
||||
gameManager.joinGame(game, req.body.playerName, inUseCookie, req.body.joinAsSpectator).then((data) => {
|
||||
res.status(200).send({ cookie: data, environment: gameManager.environment });
|
||||
}).catch((data) => {
|
||||
@@ -103,7 +103,7 @@ router.patch('/:code/restart', async function (req, res) {
|
||||
} else {
|
||||
const game = await gameManager.getActiveGame(req.body.accessCode);
|
||||
if (game) {
|
||||
gameManager.restartGame(game, gameManager.namespace, req.query.status).then((data) => {
|
||||
gameManager.restartGame(game, gameManager.namespace).then((data) => {
|
||||
res.status(200).send();
|
||||
}).catch((code) => {
|
||||
res.status(code).send();
|
||||
@@ -123,11 +123,11 @@ function validateName (name) {
|
||||
}
|
||||
|
||||
function validateCookie (cookie) {
|
||||
return cookie === null || cookie === false || (typeof cookie === 'string' && cookie.length === globals.INSTANCE_ID_LENGTH);
|
||||
return cookie === null || cookie === false || (typeof cookie === 'string' && cookie.length === PRIMITIVES.INSTANCE_ID_LENGTH);
|
||||
}
|
||||
|
||||
function validateAccessCode (accessCode) {
|
||||
return /^[a-zA-Z0-9]+$/.test(accessCode) && accessCode?.length === globals.ACCESS_CODE_LENGTH;
|
||||
return /^[a-zA-Z0-9]+$/.test(accessCode) && accessCode?.length === PRIMITIVES.ACCESS_CODE_LENGTH;
|
||||
}
|
||||
|
||||
function validateSpectatorFlag (spectatorFlag) {
|
||||
Reference in New Issue
Block a user