basic game creation

This commit is contained in:
Alec
2021-11-09 22:54:44 -05:00
parent 226b0c25e2
commit 3dc2cca465
28 changed files with 4411 additions and 43 deletions

20
server/api/GamesAPI.js Normal file
View File

@@ -0,0 +1,20 @@
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 GameManager = require('../modules/GameManager.js');
const gameManager = new GameManager().getInstance();
router.post('/create', function (req, res) {
const gameCreationPromise = gameManager.createGame(req.body, false);
gameCreationPromise.then((result) => {
if (result instanceof Error) {
res.status(500).send();
} else {
res.send(result); // game was created successfully, and access code was returned
}
});
});
module.exports = router;

6
server/config/globals.js Normal file
View File

@@ -0,0 +1,6 @@
const globals = {
ACCESS_CODE_CHAR_POOL: 'abcdefghijklmnopqrstuvwxyz0123456789',
ACCESS_CODE_LENGTH: 6,
};
module.exports = globals;

View File

@@ -7,7 +7,7 @@ const socketIO = require('socket.io');
const app = express();
let main;
const bodyParser = require('body-parser');
// const GameManager = require('./modules/managers/GameManager.js');
const GameManager = require('./modules/GameManager.js');
// const QueueManager = require('./modules/managers/QueueManager');
app.use(bodyParser.json()); // to support JSON-encoded bodies
@@ -55,17 +55,15 @@ const io = socketIO(main);
app.set('port', port);
/* Instantiate the singleton game manager */
//const gameManager = new GameManager(logger).getInstance();
const gameManager = new GameManager(logger).getInstance();
/* Instantiate the singleton queue manager */
//const queueManager = new QueueManager(matchmaking, logger).getInstance();
/* api endpoints */
// const games = require('./api/GamesAPI');
// const words = require('./api/WordsAPI');
// app.use('/api/games', games);
// app.use('/api/words', words);
const games = require('./api/GamesAPI');
app.use('/api/games', games);
/* serve all the app's pages */
app.use('/manifest.json', (req, res) => {
@@ -88,7 +86,6 @@ app.use(function (req, res) {
res.sendFile(path.join(__dirname, '../client/views/404.html'));
});
// Starts the main.
main.listen(port, function () {
logger.log(`Starting server on port ${port} http://localhost:${port}` );
});

View File

@@ -0,0 +1,52 @@
const globals = require('../config/globals');
class GameManager {
constructor (logger) {
this.logger = logger;
//this.activeGameRunner = new ActiveGameRunner(this.postGame).getInstance();
//this.gameSocketUtility = GameSocketUtility;
}
createGame = (gameParams) => {
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);
} else {
const newAccessCode = this.generateAccessCode();
return Promise.resolve(newAccessCode);
}
}
generateAccessCode = () => {
const numLetters = globals.ACCESS_CODE_CHAR_POOL.length;
const codeDigits = [];
let iterations = globals.ACCESS_CODE_LENGTH;
while (iterations > 0) {
iterations--;
codeDigits.push(globals.ACCESS_CODE_CHAR_POOL[getRandomInt(numLetters)]);
}
return codeDigits.join('');
}
}
function getRandomInt (max) {
return Math.floor(Math.random() * Math.floor(max));
}
class Singleton {
constructor (logger) {
if (!Singleton.instance) {
logger.log('CREATING SINGLETON GAME MANAGER');
Singleton.instance = new GameManager(logger);
}
}
getInstance () {
return Singleton.instance;
}
}
module.exports = Singleton;

View File

@@ -3,11 +3,11 @@ const staticRouter = express.Router();
const path = require('path');
const checkIfFileExists = require("./util");
staticRouter.use('/styles/*', (req, res) => {
staticRouter.use('/styles/**', (req, res) => {
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
let extension = path.extname(filePath);
checkIfFileExists(filePath).then((fileExists) => {
if (fileExists && (extension === '.css')) {
if (fileExists && (extension === '.css' || extension === '.min.css')) {
res.sendFile(filePath);
} else {
res.sendStatus(404);
@@ -27,11 +27,11 @@ staticRouter.use('/client/webfonts/*', (req, res) => {
});
});
staticRouter.use('/client/images/*', (req, res) => {
let filePath = path.join(__dirname, ('../' + req.baseUrl));
staticRouter.use('/images/*', (req, res) => {
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
let extension = path.extname(filePath);
checkIfFileExists(filePath).then((fileExists) => {
if (fileExists && (extension === '.svg' || extension === '.png' || extension === '.jpg')) {
if (fileExists && (extension === '.svg' || extension === '.png' || extension === '.jpg' || extension === '.gif')) {
res.sendFile(filePath);
} else {
res.sendStatus(404);
@@ -88,7 +88,19 @@ staticRouter.use('/config/*', (req, res) => {
});
});
staticRouter.use('/modules/*', (req, res) => {
staticRouter.use('/modules/**', (req, res) => {
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
let extension = path.extname(filePath);
checkIfFileExists(filePath).then((fileExists) => {
if (fileExists && (extension === '.js' || extension === '.min.js')) {
res.sendFile(filePath);
} else {
res.sendFile('../views/404.html');
}
});
});
staticRouter.use('/model/**', (req, res) => {
let filePath = path.join(__dirname, ('../../client/' + req.baseUrl));
let extension = path.extname(filePath);
checkIfFileExists(filePath).then((fileExists) => {