Creating an online hi-score board
Author: | Flava
|
Submitted: | 2nd April, 2006
|
Views: | 12275
| Rated: |
|
|
Introduction
This has been something requested by people many times, and so I thought I'd make a new guide in simple and easy steps to show you exactly how to create an online score board!
What do you need?
You're going to need a few fiddly things.. and you will need some knowledge in some parts to get this thing going. But I'll try and help you as best as I can. Firstly, you'll need either MMF or TGF, with the MooSock extension (from 3ee.com). Next, you'll need a webserver that is PHP compatible and you'll also need a MySQL database for that server (so no, you can't use crappy sitesleds or freewebs!). Oh, and you'll need String Parser 2 (I'll explain why later).
Setting up your database
Firstly, you need a database in order to store the scores of your players. MySQL is a database system for websites, which basically consists of databases, tables and then fields. Firstly, you're going to need to create a database called scores - and then a table. Call this table the same name as your game - and in this table, you will need the fields: id,name, score, ip. Set id to your primary key, and make it an autonumber. WHAT THE HELL ARE YOU ON ABOUT?? Well basically, you will need to know how to do this - setting this up can be different depending on what kind of webserver/host you're with. I use phpMyadmin to set this up - but if you have problems, contact your host.
Setting up the PHP page
Now here is what we are going to do.. When a player has finished playing your game, we're going to submit their score through a PHP page. You can do this by using variables. First, we need the PHP to add a score to the database:
<?
$name = $_REQUEST['name'];
$score = $_REQUEST['score'];
$ip = $_REQUEST['ip'];
$enc = $_REQUEST['enc'];
$ip = $_REMOTE_ADDR;
$key = "f3h8f8dfnX40";
$check = md5("$name$score$ip$key");
if($enc == $check) {
$conn = @mysql_connect(localhost, username, password)
or die(Err:Conn);
$rs = @mysql_select_db(username_scores, $conn)
or die(Errb);
$sql=insert into gamename (name, score, ip)
values ( \"$name\", \"$score\", \"$ip\");
$rs=mysql_query($sql,$conn);
}
?>
Ok.. so it may look confusing to you, so I'll explain how this system works. Lets call this page score.php. When a play clicks a submit button in the game, we'll use MooSock to connect to this page by connecting to:
http://www.server.com/score.php?name=Flava&score=50&enc=749825f1cffc3e34682913c00aa3aae9
Now, the name is obviously the player name. The score is the player's score. The enc variable however, is a code which tries to prevent any cheating. This is basically an MD5 signature of the string Flava50f3h8f8dfnX40 - where f3h8f8dfnX40 is the secret key/password which we set in the PHP code. The PHP page above will receive the player name, score and encryption - and then encrypt the name and score again to compare to the encryption variable sent by MooSock. Sounds confusing, but it works.
If the variables $enc (encryption sent by moosock) and $check (encryption created by PHP) are the same, then we will add a score to the database. It's likely you'll need some PHP knowledge, but if you need help just ask me!
Sending a score in MMF
I am using MMF to do this, however you can use TGF if you like. As long as you have MooSock and string parser 2 anyway. Right, so the player has finished your game and got a score of 50. Their name is Flava - (woo) - and they've just clicked a Submit Score button. Now, use the following event:
+ Button Clicked
- MooSock: Connect to www.server.com on port 80
+ MooSock: On Connect
- String Parser: Set source string to Edittext$(Name) + score( Player 1 ) + "f3h8f8dfnX40"
- MooSock: Send text line "POST http://www.server.com/score.php?name=" + Edittext$(Name) + "&score=" + score( Player 1 ) + "&enc=" + mdFive$( String Parser ) + "HTTP/1.0"
- MooSock: Send text line "From: name@server.com"
- MooSock: Send text line "User-Agent: HTTPTool/1.0"
- MooSock: Send text line ""
- Disconnect
Right.. basically when the button is clicked, we connect to your server on port 80 (standard for HTTP). Then when connected, we set the string parser to "Flava50f3h8f8dfnX40" - (playername score key). Then we send "http://www.server.com/score.php?name=Flava&score=50&enc=749825f1cffc3e34682913c00aa3aae9".
Then the following text lines are just basics if you need to send stuff in MooSock. Now check your database, and hopefully the score should be added. If not, try changing HTTP/1.0 to HTTP/1.1 and User-Agent: HTTPTool/1.0 to User-Agent: HTTPTool/1.1.
Retrieving the scores
This time we'll receive the scores via PHP in a page called board.php.
<?
$conn = @mysql_connect(localhost, username, password)
or die(Err:Conn);
$rs = @mysql_select_db(username_scores, $conn)
or die(Errb);
$sql=select * from gamename ORDER BY 'score' DESC;
$rs=mysql_query($sql,$conn);
while( $row = mysql_fetch_array($rs) )
{
$name = $row[name];
$score = $row[score];
echo("$name - $score ");
}
?>
This basically gets all the data from the database with a ORDER BY 'score' DESC. This will order the data by the scores descending, so will put the high scores at the top. You can also put a LIMIT 10 to show the top 10 scores.
END
I've written this really quickly as I've got coursework to get finished for college. But if you have any questions, just ask and I will try and update this article as needed
|
|
|