I am thinking to post my little game on the server. On some point I want to enable networking. Before I do I will have just bots to shoot at. To add any competitiveness to the game I thought about adding high score board. GUI displaying it shouldnt be a problem. I am wondering how to store and write/load it. Would you recommend some technique to safely store/modify some json file with the score table?
Just to clarify. The functionality is that player get points, after finished game he can type in his name/nick and is visible in āhall of fameā kind of list with stats.
I believe you can use some server database for storing/retrieving values, or (if the data volume is low) use just writable json file(s). Anyway, implementation will depend on your specific case.
Hey @kri100s,
You need to have persistent data storage (eg. heroku free dynos are cleared from time to time so you donāt have guarantee to keep the data there). In general it can be JSON file, it can be database table. It depends on your needs and your hosting.
Do you want the players to have accounts? So they can log in and try to have better scores?
Assuming simple version without the accounts (as it involves the authentication, which can be done with eg. Firebase) you can have like:
JSON file where you can write / read with NodeJS
networking can be done with HTTP requests (GET to get the data, and PUT / POST to send the data. But then you donāt have āreal time updateā data. Itās client ā server communication. The client sends request and gets the response. For both sides communication you can use websockets. It allowes you to broadcast the information from the server to all players.
But eg. If you will send POST like { name: 'best scorer ever', score: 123
Nothing prevents the user to bypass it and send score like 999999
If you need more assistance with this let me know. If you have any questions - just ask
Hi @neu5 ,
Thanks, the approach you described should be perfect. I dont need any verification or accounts. Its just for fun. User just writes there whatever he wants with some limit for characters amount.
I was wondering about hacking/bypassing issue. That kind of sucks. My js networking knowledge is non-existing. Is it even safe? Like someone could try to send some massive amount of data to file.
In general all the input coming from the user should be treated as a potential threat. So you should escape it because it can be eg. a JS script that can do some damage.
You should do the validation of the data on the server if you donāt want to have any trouble.
So if you expect to get the data in the shape of the object with two properties like { name: (some string), score: (number) }
On the server you should check if the data that is coming from the client have desired shape (schema) and you can add some limitation to that data. Like you require the name to be longer than 1 character and shorter than 17 characters (2-16) and the score to be higher than 0 and lower than 1000.
If the data coming from the client doesnāt match those requirements you donāt write it to the JSON file and you send back an error to the client.
This is not exactly want you want, but using Web Storage API - Web APIs | MDN, you can save anything in a common area for anything up to 5mb right in the browser. Any page with the same:
protocol HTTP or HTTPS
machine WWW
domain name
can retrieve prior data.
This might store the personās high score. Giving feedback on their best time might be even be better
Taking into account the other replies, I would recommend a few different routes depending on various things.
If you have low data volume, use a NodeJS w/ Express app and read/write from a JSON file. If you have a high data volume, you should use a database (still with NodeJS/Express) - MariaDB works fairly well.
@neu5ās suggestion about using a schema would work really well (maybe with regex)
Thanks. It is an option if I am really lazy. It would be more fun to see other people scores. Still, this api would be super nice to store some game settings so the user doesnt have to go through it all every time.
JSON will do for sure.
But if you will want to expand it in the future it might be better to use the database.
Eg. You can have the table users with the columns ID login password nickname
And second table highscores ID ID_user score datetime
Or something like that ofc itās possible to have the same with the JSON file, but SQL is ok too
If you need some more explanation on the subject let me know.
If you feel like one of the answers here is the solution for your issue please mark it as solution so that question can be marked as resolved
Firebase is also very easy to get up and running and have realtime data sync on the front-end out of the box, with no backend code. Their free tier is pretty generous and lasts forever. Its really good for getting persistent, real-time networked data quickly into side projects but would not use for anything serious unless get a professional as nosql is easy to mess up in a way that can be hard to get out of