Highscore table – NodeJS

After starting the task in class, I have gone on to complete a highscore table that can be used to store anyone’s highscore and retrieve a list of them back. I wrote the server in NodeJS and used a MongoDB to store the data.

Creating the server with Node wasn’t too difficult as we used a thing called Express. That gives you a set up for a server and then you extend on it by having different routes for different pages. The functionality of the highscore table is a little limited due to time restraints, but it works well for a top 10. It allows a creator to store a user’s score, username and date into the database to be retrieved at anytime. When the data is to be retrieved, two parameters are required to be passed. First, the starting point of the rankings. This is so if you want to see maybe rank 10-20, you can ask for them. Secondly, you need to pass the amount of results you want back. That way, if for example you want the top 100, you ask for results starting at 0, and you want 100 results back.

I struggled with the MongoDB stuff as a few things from their website didn’t appear to work. I am certain I must have been using them wrong, because things such as .forEach was not working. However, I found other ways of doing it and after many trial and errors scenarios, I finally managed to get it all working together. The next step was to actually use this data in a different application that wasn’t the server. I decided I would do it in Unity.

Unity has a nice class called WWW built in that allows you to send and receive data from a server. This made it super simple to send data to be stored and ask for it back. I set up a few input boxes and buttons and then hooked them up to be able to send and receive data. I then display it on the screen to demonstrate it works. There were not too many issues with getting it to work in Unity. The largest issue come in how to format the data to send. Before using Unity with the server, I was using Postman to send Post requests. There, the body looked like this:

{“username”:”corey”, “score”:100}

However, I didn’t realise I had to send them separately in unity. So I was sending it as a single object and it was inserting it as one object, which was not desirable. Once I got past that, the other issue I had was sending an integer. As I am using the Greater than and Less than operations to send data back, they need to be ints. But when I sent the data (even as an int), it kept getting converted to a string. To fix this I had to parse the text on the server side. It looks for the score object and parses it to an int.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s