aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/index.html2
-rw-r--r--src/index.js64
-rw-r--r--src/server.js13
3 files changed, 45 insertions, 34 deletions
diff --git a/src/index.html b/src/index.html
index 17b9655..0d858a4 100644
--- a/src/index.html
+++ b/src/index.html
@@ -12,7 +12,7 @@
<div>
<a href="/highscores">High score list</a>
</div>
-
+
<div>
<h1>Games</h1>
<h2>Arcade</h2>
diff --git a/src/index.js b/src/index.js
index 9e59e62..0e19769 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,15 +1,14 @@
-const server = require('./server');
const Sequelize = require('sequelize');
const fs = require('fs');
const path = require('path');
const jwt = require('jsonwebtoken');
-
+// Load the config and configurable parameteres
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json')));
-
-const dbCreds = config.database;
+const dbCreds = config.database
const secret = config.jwt_secret;
+// An object to help sign and verify jwt cookies
const jwtFunctions = {
sign: function (message) {
return jwt.sign({ value: message }, secret);
@@ -19,6 +18,7 @@ const jwtFunctions = {
}
}
+// Create the database object
const database = new Sequelize(dbCreds.database, undefined, undefined, {
logging(str) {
console.debug(`DB:${str}`);
@@ -35,43 +35,44 @@ const database = new Sequelize(dbCreds.database, undefined, undefined, {
idle: 10000,
},
});
-
+// Connect to database
database.authenticate().then(() => {
- console.debug(`database connection successful: ${dbCreds.database}`);
+ console.debug(`database connection successful`);
}, (e) => console.log(e));
+// Create a sync helper function for the database
async function sync(alter, force, callback) {
await database.sync({ alter, force, logging: console.log });
}
-function setUpModels() {
- const models = {
- "scores": database.define('score', {
- username: {
- type: Sequelize.STRING,
- allowNull: false,
- },
- score: {
- type: Sequelize.INTEGER,
- allowNull: false,
- },
- game: {
- type: Sequelize.STRING,
- allowNull: false,
- },
- uuid: {
- type: Sequelize.STRING,
- allowNull: false
- }
- })
- }
- return models;
+// Create ORM models and sync database
+const models = {
+ "scores": database.define('score', {
+ username: {
+ type: Sequelize.STRING,
+ allowNull: false,
+ },
+ score: {
+ type: Sequelize.INTEGER,
+ allowNull: false,
+ },
+ game: {
+ type: Sequelize.STRING,
+ allowNull: false,
+ },
+ uuid: {
+ type: Sequelize.STRING,
+ allowNull: false
+ }
+ })
}
-
-const models = setUpModels();
sync();
+// Set up main routes
+const server = require('./server');
server.setUpRoutes(models, jwtFunctions, database);
+
+// Load routes for each game
server.load("./ur/server", models, jwtFunctions, database)
server.load("./quadrowple/server", models, jwtFunctions, database)
server.load("./snake/server", models, jwtFunctions, database)
@@ -79,5 +80,6 @@ server.load("./stacker/server", models, jwtFunctions, database)
server.load("./pinball/server", models, jwtFunctions, database)
server.load("./math/server", models, jwtFunctions, database)
server.load("./pp/server", models, jwtFunctions, database)
-server.listen(config.port);
+// Start the server
+server.listen(config.port);
diff --git a/src/server.js b/src/server.js
index 07fa430..c159666 100644
--- a/src/server.js
+++ b/src/server.js
@@ -12,18 +12,25 @@ function listen(port) {
server.listen(port, () => console.info(`Listening on port ${port}!`));
}
+// Load a game and its routes
function load(gamePath, models, jwtFunctions, database) {
const game = require(gamePath);
game.setUpRoutes(server, models, jwtFunctions, database);
}
function setUpRoutes(models, jwtFunctions, database) {
+ // Route logger
server.use(function (req, res, next) {
console.debug(new Date(), req.method, req.originalUrl);
next()
})
+ // Static files
server.get('/', (req, res) => res.sendFile(__dirname + "/index.html"))
+ server.get('/highscore.js', (req, res) => res.sendFile(__dirname + "/highscores.js"))
+ server.get('/highscores', (req, res) => res.sendFile(__dirname + "/scores.html"))
+
+ // Sets a score
server.get('/setScore', async (req, res, next) => {
var results = await models.scores.findAll({
where: { uuid: { [Op.eq]: req.query.uuid } }
@@ -40,6 +47,8 @@ function setUpRoutes(models, jwtFunctions, database) {
})
res.status(200).send("score saved")
})
+
+ // Gets all scores, grouped by game
server.get('/scores', async (req, res, next) => {
var games = await database.query(`SELECT DISTINCT game FROM scores`, { type: database.QueryTypes.SELECT });
var scoresByGames = {}
@@ -48,8 +57,8 @@ function setUpRoutes(models, jwtFunctions, database) {
};
res.status(200).send(scoresByGames);
})
- server.get('/highscore.js', (req, res) => res.sendFile(__dirname + "/highscores.js"))
- server.get('/highscores', (req, res) => res.sendFile(__dirname + "/scores.html"))
+
+ // Gets a unique ID
server.get('/uuid', async (req, res) => {
var uuid;
while (true) {