diff --git a/server.js b/server.js index ab2d672..8c4ca7b 100644 --- a/server.js +++ b/server.js @@ -36,8 +36,19 @@ class Server { return reply.status(scores ? 200 : 400).view("public/index.hbs", { scores: scores, views: views }); } + async post_score(request, reply) { + const body = request.body; + if (!body.user || !body.score) return reply.status(400); + + let success = await db.addScore(body.user, body.score, request.headers["x-forwarded-for"]); + + if (success == false) return reply.status(400); + return reply.status(200); + } + async get_adminpage(request, reply) { const key = request.cookies.key; + // console.log(key) // reply.setCookie('key', process.env.ADMIN_KEY, { path: '/' }); if (key == process.env.ADMIN_KEY) { @@ -63,16 +74,6 @@ class Server { } } - async post_score(request, reply) { - const body = request.body; - if (!body.user || !body.score) return reply.status(400); - - let success = await db.addScore(body.user, body.score); - - if (success == false) return reply.status(400); - return reply.status(200); - } - start_server() { fastify.listen({ port: process.env.PORT, host: "0.0.0.0" }, (err, address) => { if (err) { diff --git a/sqlite.js b/sqlite.js index c95b671..3083724 100644 --- a/sqlite.js +++ b/sqlite.js @@ -18,7 +18,7 @@ dbWrapper let exists = await db.all("PRAGMA table_info('Scores')"); if (exists.length == 0) { - await db.run("CREATE TABLE Scores (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, highscore INTEGER);"); + await db.run("CREATE TABLE Scores (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, highscore INTEGER, ip VARCHAR(39));"); } // check if analytics table excists @@ -55,7 +55,7 @@ module.exports = { }, // Post (add) a new score - addScore: async (user, newScore) => { + addScore: async (user, newScore, ip) => { let success = false; try { @@ -64,11 +64,14 @@ module.exports = { // add user if (!userInfo.length) { - success = await db.run("INSERT INTO Scores (username, highscore) VALUES (?, ?);", [user, newScore]); + success = await db.run("INSERT INTO Scores (username, highscore, ip) VALUES (?, ?, ?);", [user, newScore, ip]); return success.changes > 0; } // update the score + if (!userInfo[0].ip) { + await db.run("UPDATE Scores SET ip = ? WHERE id = ?", [ip, userInfo[0].id]); + } if (userInfo[0].highscore >= newScore) return success; success = await db.run("UPDATE Scores SET highscore = ? WHERE id = ?", [newScore, userInfo[0].id]); diff --git a/src/index.html b/src/index.html index a1b5942..f28fca9 100644 --- a/src/index.html +++ b/src/index.html @@ -35,7 +35,7 @@

personal highscores

-

score: 0

+

score:0

diff --git a/src/style.css b/src/style.css index cd9d071..f8b3dde 100644 --- a/src/style.css +++ b/src/style.css @@ -11,6 +11,8 @@ body { display: flex; justify-content: space-evenly; align-items: center; + overflow: hidden; + /* background: radial-gradient(circle at 48.7% 44.3%, rgb(30, 144, 231) 0%, rgb(56, 113, 209) 22.9%, rgb(38, 76, 140) 76.7%, rgb(31, 63, 116) 100.2%); */ } @@ -30,7 +32,7 @@ canvas { .TRscore { color: grey; - font-size: 10px; + font-size: 0.7rem; font-family: "Press Start 2P"; position: absolute; top: 2.5%; @@ -126,7 +128,7 @@ input:invalid { #nextBlock { position: absolute; - top: 2.5%; + top: calc(2.5% - 2px); left: 100%; }