aboutsummaryrefslogtreecommitdiff
path: root/src/server.js
diff options
context:
space:
mode:
authorMark Powers <markppowers0@gmail.com>2020-04-13 15:20:01 -0500
committerMark Powers <markppowers0@gmail.com>2020-04-13 15:20:01 -0500
commitafc1a847f4e93c9fb0ec718286089d409030c95a (patch)
tree55e2825d15667cd57156be41d59ea2aa6c1830c3 /src/server.js
parent7e6648902b262484bc0aac824c3784b1ae4b8591 (diff)
Update documentation and make setScore idempotent
Diffstat (limited to 'src/server.js')
-rw-r--r--src/server.js41
1 files changed, 28 insertions, 13 deletions
diff --git a/src/server.js b/src/server.js
index 0fe5b29..07fa430 100644
--- a/src/server.js
+++ b/src/server.js
@@ -1,7 +1,7 @@
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
-
+const uuidv4 = require('uuid/v4');
const Op = require('sequelize').Op;
const server = express();
@@ -24,28 +24,43 @@ function setUpRoutes(models, jwtFunctions, database) {
})
server.get('/', (req, res) => res.sendFile(__dirname + "/index.html"))
- server.get('/setScore', (req, res, next) => {
- let game = req.query.game
- let username = req.query.username
- let score = req.query.score
- console.log(game, username, score)
- models.scores.create({
- game: req.query.game,
- username: req.query.username,
- score: req.query.score
+ server.get('/setScore', async (req, res, next) => {
+ var results = await models.scores.findAll({
+ where: { uuid: { [Op.eq]: req.query.uuid } }
+ })
+ if(results.length > 0){
+ res.status(400).send("uuid already used")
+ return
+ }
+ models.scores.create({
+ game: req.query.game,
+ username: req.query.username,
+ score: req.query.score,
+ uuid: req.query.uuid
})
- res.status(200)
+ res.status(200).send("score saved")
})
server.get('/scores', async (req, res, next) => {
var games = await database.query(`SELECT DISTINCT game FROM scores`, { type: database.QueryTypes.SELECT });
var scoresByGames = {}
- for(var i = 0; i < games.length; i++){
- scoresByGames[games[i].game] = await models.scores.findAll({ atttributes: ["username", "score", "createdAt"], where : {game: {[Op.eq]: games[i].game}}, order: [['score', 'DESC']], limit: 15}).map(x => x.get({ plain: true }))
+ for (var i = 0; i < games.length; i++) {
+ scoresByGames[games[i].game] = await models.scores.findAll({ attributes: ["username", "score", "createdAt"], where: { game: { [Op.eq]: games[i].game } }, order: [['score', 'DESC']], limit: 15 }).map(x => x.get({ plain: true }))
};
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"))
+ server.get('/uuid', async (req, res) => {
+ var uuid;
+ while (true) {
+ uuid = uuidv4()
+ var result = await database.query(`SELECT uuid FROM scores where uuid = '${uuid}'`, { type: database.QueryTypes.SELECT });
+ if (result.length == 0) {
+ break;
+ }
+ }
+ res.status(200).send(uuid)
+ })
}
module.exports = {