diff options
author | Mark Powers <markppowers0@gmail.com> | 2020-04-12 14:42:03 -0500 |
---|---|---|
committer | Mark Powers <markppowers0@gmail.com> | 2020-04-12 14:42:03 -0500 |
commit | 7e6648902b262484bc0aac824c3784b1ae4b8591 (patch) | |
tree | bc56275222a58228fd714af0dad58c660f004ab7 /src/server.js | |
parent | 1bffb064e2414ced5b1924a4f9fbd822a09c718e (diff) |
Update games, add scores
Diffstat (limited to 'src/server.js')
-rw-r--r-- | src/server.js | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/src/server.js b/src/server.js index ba5cff9..0fe5b29 100644 --- a/src/server.js +++ b/src/server.js @@ -1,24 +1,18 @@ const express = require('express'); const bodyParser = require('body-parser'); const cookieParser = require('cookie-parser'); -//const request = require('request'); -const crypto = require('crypto'); -const uuidv4 = require('uuid/v4'); -const path = require('path'); -const fs = require('fs'); -const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'))); +const Op = require('sequelize').Op; const server = express(); server.use(cookieParser()) server.use(bodyParser.json()); -//server.use(bodyParser.urlencoded({ extended: true })); function listen(port) { server.listen(port, () => console.info(`Listening on port ${port}!`)); } -function load(gamePath, models, jwtFunctions, database){ +function load(gamePath, models, jwtFunctions, database) { const game = require(gamePath); game.setUpRoutes(server, models, jwtFunctions, database); } @@ -30,6 +24,28 @@ 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 + }) + res.status(200) + }) + 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 })) + }; + 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")) } module.exports = { |