Skip to content

Commit

Permalink
added ip storing
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur-adriansens authored Nov 24, 2023
1 parent 23dd835 commit e73eadb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
21 changes: 11 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,7 +55,7 @@ module.exports = {
},

// Post (add) a new score
addScore: async (user, newScore) => {
addScore: async (user, newScore, ip) => {
let success = false;

try {
Expand All @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h2>personal highscores</h2>
<div id="game">
<div id="canvas">
<canvas id="gameCanvas" height="585" width="390" class="over"></canvas>
<p class="TRscore">score: <span id="score">0</span></p>
<p class="TRscore">score:<span id="score">0</span></p>
</div>

<canvas id="nextBlock" height="200" width="200"></canvas>
Expand Down
6 changes: 4 additions & 2 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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%); */
}

Expand All @@ -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%;
Expand Down Expand Up @@ -126,7 +128,7 @@ input:invalid {

#nextBlock {
position: absolute;
top: 2.5%;
top: calc(2.5% - 2px);
left: 100%;
}

Expand Down

0 comments on commit e73eadb

Please sign in to comment.