add 400 for create game

This commit is contained in:
AlecM33
2021-12-28 00:22:30 -05:00
parent d5a606b66d
commit 9920ce4055
5 changed files with 24 additions and 3 deletions

View File

@@ -26,6 +26,7 @@
"@babel/preset-env": "^7.16.5",
"acorn": "^8.6.0",
"babel-loader": "^8.2.3",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-force-https": "^1.0.0",
"express-rate-limit": "^6.0.1",

View File

@@ -4,6 +4,7 @@ const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())
const logger = require('../modules/Logger')(debugMode);
const GameManager = require('../modules/GameManager.js');
const rateLimit = require('express-rate-limit').default
const globals = require('../config/globals');
const gameManager = new GameManager().getInstance();
@@ -19,7 +20,7 @@ if (process.env.NODE_ENV.trim() === 'production') { // in prod, limit clients to
}
router.post('/create', function (req, res) {
logger.debug('Received request to create new game: ' + JSON.stringify(req.body, null, 4));
logger.trace('Received request to create new game: ' + JSON.stringify(req.body, null, 4));
const gameCreationPromise = gameManager.createGame(req.body, false);
gameCreationPromise.then((result) => {
if (result instanceof Error) {
@@ -27,6 +28,10 @@ router.post('/create', function (req, res) {
} else {
res.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);
}
});
});

View File

@@ -33,7 +33,8 @@ const globals = {
SPECTATOR: "spectator"
},
ERROR_MESSAGE: {
GAME_IS_FULL: "This game is full"
GAME_IS_FULL: "This game is full",
BAD_CREATE_REQUEST: "Game has invalid options."
},
EVENTS: {
PLAYER_JOINED: "playerJoined",

View File

@@ -4,6 +4,7 @@ const https = require('https');
const path = require('path');
const fs = require('fs');
const app = express();
const cors = require('cors')
const bodyParser = require('body-parser');
const GameManager = require('./modules/GameManager.js');
const globals = require('./config/globals');
@@ -66,6 +67,12 @@ app.set('port', port);
let io;
if (process.env.NODE_ENV.trim() === 'development') {
const corsOptions = {
origin: "http://localhost:" + port,
optionsSuccessStatus: 200,
methods: ["GET", "POST"]
}
app.use(cors(corsOptions));
io = require("socket.io")(main, {
cors: {
origin: "http://localhost:" + port,
@@ -75,6 +82,13 @@ if (process.env.NODE_ENV.trim() === 'development') {
}
});
} else {
const corsOptions = {
origin: ["https://playwerewolf.uk.r.appspot.com"],
methods: ["GET", "POST"],
allowedHeaders: ["Content-Type", "X-Requested-With", "Accept"],
optionsSuccessStatus: 200,
}
app.use(cors(corsOptions));
io = require("socket.io")(main, {
cors: {
origin: ["https://playwerewolf.uk.r.appspot.com", "wss://playwerewolf.uk.r.appspot.com"],

View File

@@ -176,7 +176,7 @@ class GameManager {
const expectedKeys = ['deck', 'hasTimer', 'timerParams'];
if (typeof gameParams !== 'object' || expectedKeys.some((key) => !Object.keys(gameParams).includes(key))) {
this.logger.error('Tried to create game with invalid options: ' + JSON.stringify(gameParams));
return Promise.reject('Tried to create game with invalid options: ' + gameParams);
return Promise.reject(globals.ERROR_MESSAGE.BAD_CREATE_REQUEST);
} else {
// to avoid excessive memory build-up, every time a game is created, check for and purge any stale games.
pruneStaleGames(this.activeGameRunner.activeGames, this.activeGameRunner.timerThreads, this.logger);