I have a simple website with a little game in it. It has a basic scoreboard using express - every time you die the website send a post request to the server with your score and the server adds it to the leaderboard. My problem is that its very simple for the leaderboard to be exploited. through devtools people can easily just:
I thought about creating a server side score counter and collision checker but my game is fast paced so I think this would be problematic, not talking about the computing power that it would put on the server if multiple people play at the same time.
Is this really the only solution or are there simpler solutions I'm not thinking of?
server code:
const express = require('express');
const app = express();
const fs = require('fs');
const { json } = require('express');
app.use(express.json());
app.use(express.urlencoded({extended:true}));
app.use(express.static('Build'))
app.get('/leaderBoard', (req,res)=>{
var rawData = fs.readFileSync('./database.json');
var data = JSON.parse(rawData);
res.json(data);
})
app.post('/leaderBoard', (req, res)=>{
var rawData = fs.readFileSync('./database.json');
var leaderBoard = JSON.parse(rawData).leaderBoard;
for(var i = 0; i < leaderBoard.length; i++){
if(req.body.score > leaderBoard[i].score){
for(let i2 = leaderBoard.length -1; i2 > i; i2--)
leaderBoard[i2] = leaderBoard[i2-1];
leaderBoard[i] = req.body;
break;
}
}
fs.writeFileSync("./database.json",JSON.stringify({leaderBoard},null,2));
})
app.listen(process.env.PORT || 3000);
A server side check would be imperative if you want to truly avoid any cheating... BUT, you could always make it harder to cheat on the client side with simple checks, like defining a maximum score change (for example if someone gains more than 100 points in 1 second, then he's cheating).
Using a minified code can't hurt, and you could also replace your variables name by abstract ones (a, b, c, instead of "score"). For that same reason, a collision check shouldn't just be defined by a true/false flag.