PDA

View Full Version : How to implement an online high score table?



Cybermonkey
14-08-2012, 08:05 AM
I wonder how to implement an online high score table. I think I could use a PHP script on the server side and communicate with my Pascal game. I have no clue about PHP, though. Has anyone a hint? Maybe even a ready-to-use script? ;D Or am I totally wrong?

Super Vegeta
14-08-2012, 11:01 AM
I once did this for Alexland. Since I didn't really want to dwell into how to send proper HTTP requests and send data via POST, I used some unit for simple web fetching and passed all the data through GET. So it was kind like

Unit.ConnectToSite('http://mysite.com');
Unit.GetDocument('http://mysite.com/scores/mygame/script.php?data1=value1&data2=value2');

I had three scripts on the server side:
info.php - this one returned the modification date of the high-scores - no need to download them if nothing changed, eh?
get.php - as can be guessed, this one returned the high-score table
submit.php - used to send the scores

Whereas what info.php and get.php should return is obvious, my submit.php returned one of the following:
<a number> - means the score you submitted made it into the table and holds the <number> place
sorry - the score did not make it to the table
error - the score is lacking data or somehow else damaged
gtfo - the score has already been submitted or it's checksum is wrong - you're probably cheating
version - wrong game version

'error' and 'version' could also be returned by info.php and get.php, to signalize errors.

If you want, I could send You the script I used, of course after modyfing it a bit (so I won't give out much of the game ;) ) and adding some comments.

Cybermonkey
14-08-2012, 11:59 AM
I think that's exactly what I need. If you would be so kind to send me the script(s), I'll be more than thankful.
Oh, another question: on the Freepascal side, do you use Synaptic or anything else? (SDL_net?)
EDIT: Sorry, I meant Synapse ... ::)

Super Vegeta
15-08-2012, 11:41 AM
Sorry for the delay, I was a bit busy yesterday evening.

http://svgames.pl/trash/netscores.zip
I hope everything's commented clearly enough. As for the FP side, the game was made using SDL, so I went with SDL_net.

Cybermonkey
15-08-2012, 01:05 PM
Sorry for the delay, I was a bit busy yesterday evening.

http://svgames.pl/trash/netscores.zip
I hope everything's commented clearly enough. As for the FP side, the game was made using SDL, so I went with SDL_net.
Okay, thanks a lot. I will have a look into it at this evening.

User137
15-08-2012, 04:28 PM
Did you take security into account, so that not anyone can open the link and add nonsense-scores?

Super Vegeta
15-08-2012, 05:14 PM
In the downloadable script there's no such thing. In my game I used a simple hash that was calculated by the game and attached to the values sent, the script then calculated the hash again and checked if they matched.

WILL
15-08-2012, 09:01 PM
Very cool topic! :)

If you had purchased a web hosting package you could use the domain and access the database server that the hosting uses. There are a lot of libraries that will allow you to access various database servers. PHP is not really necessary unless you want to add some interaction with the game it's self and a website. ie. displaying highscores or some kind of weekly or monthly stats on a site you designed for the game.

You can however do a lot of neat things if you did setup a databse to receive data from your game such as hosting exciting screenshots from players once they hit a new top highscore and create user accounts for your game kind of like Blizzard does.

Free hosting can be a problem if you want stability so you should consider the low cost of paying a more solid hosting provider if you try this option.

Besides the web hosting options they do have other highscore/award system services for different platforms such as iOS, Mac OS X (with the new Mountain Lion 10.8 version) and Android. Using these would depend on the API or libraries they provide to developers to use. Only C-based headers/libraries would of course mean translation too.

BTW Would anybody be interested in such a library/service if it were provided to Pascal game developers as a simple library file they could add to their games?

Cybermonkey
16-08-2012, 06:45 AM
Yes, I thought about using it on my web host (not free but paid).
And I would be very interested in such a simple library!

WILL
16-08-2012, 08:04 AM
To make your own custom you'd want to find a decent and easy to use library (no dynamic libraries if possible) for accessing a MySQL database from your webserver.

What comes with the standard install of FPC?

WILL
16-08-2012, 08:10 AM
Or if you would rather have your web host do all the database work, you could do some PHP script that would take either POST form data or URL Query and process it to go into your database. The problem then is getting the data back as a file your game can process.

I think if your PHP is bad or non-existent, you're better off with learning the MySQL-based Pascal library.

Plus having a script like that could open it's self up to security leaks or falsely given data. Cheating and hacking.

Cybermonkey
16-08-2012, 09:16 AM
You mean the game itself communicates with a MySQL database on the server? But if I want to have an online high score list I'll need another PHP script which displays the table on the web page, right? Which leads to the same problem, that I have no clue of PHP, yet. (Maybe I should read some tutorials about it, looks a bit like BASH).

WILL
16-08-2012, 09:30 AM
Well yes, if you wanted to have a web page or whole site that shows off your highscores, then yes.

BUT, I'd recommend doing one thing at a time. First get your high scores working between database server and wherever your game runs. THEN worry about web pages that show the high scores. Your development will be all the happier for it. ;)

The PHP docs are actually pretty great to be honest though. When you get to that part it shouldn't be too hard to pick-up and learn PHP. I could even post a small script that you can take and copy paste and edit a few parameters and reuse to get whatever data you want to be displayed whereever you want on any PHP generated webpage.

For now though, figure out your MySQL library access from your game side of things.

Super Vegeta
16-08-2012, 01:22 PM
Implementing a script that would print a nicely-formatted high-sore table basing on a file or data from MySQL takes only basic PHP knowledge, and that's really easy to learn. There are a lot of people who complain about PHP, but IMO this thing is great for making small, simple things - just like shell scripts.

Btw, why do most the packages on FPC documentation produce E404 when clicking "view interface"? :(

sixten
01-12-2012, 01:39 AM
Or you could use a preformated html document that is downloaded and edited and then uploaded again. You know where the different entries are, so editing the file will be easy. The Synapse units does the networking in just a few lines.

WILL
06-12-2012, 12:03 AM
Generating HTML is an option, however it wouldn't act as a solution for being able to extract those highscores for later use in any of the in-game stats.

SilverWarior
06-12-2012, 05:31 AM
Wouldn't be the easiest solution for implementing Online High Score just by using some database. There are several database components which alow easy acces to SQL based databases.
And since almost all WebServer providers offer you an easy way of establishing a database on their servers I don't see why not use it.
As for the web page which will alow viewing highscore list outside your game you can simply use PHP or some Java scripts to read those records from the database and show them in a webpage form.