Server Side Highscores

From Unify Community Wiki
Revision as of 08:08, 20 August 2007 by KeliHlodversson (Talk | contribs)

Jump to: navigation, search


Author: (Kevin Lindeman)

Contents

Description

This set of scripts will send a player's name and score to a server to be stored in a MySQL Database, and then also loaded by the game to be seen.

Step 1: Create the blank MySQL Database

Go into whatever you use to manually modify a MySQL Database (I usually use phpMyAdmin), and create a new database.

Create the Database

Now go into that database and click the SQL tab to run your own SQL

Run the SQL Query

And paste this SQL Query into the box and hit Go.

SQL - score.sql

<sql>CREATE TABLE `scores` (

 `id` int(10) unsigned NOT NULL auto_increment,
 `name` varchar(15) NOT NULL default 'anonymous',
 `score` int(10) unsigned NOT NULL default '0',
 PRIMARY KEY  (`id`),
 UNIQUE KEY `id_2` (`id`),
 KEY `id` (`id`)

) TYPE=MyISAM AUTO_INCREMENT=0 ;</sql>

Step 2: Edit the server side scripts

Edit these scripts to reflect your MySQL host server, user, password, database. The table name "scores" should not be edited unless you edited the SQL Query in Step 1.

PHP - addscore.php

This script will take the name and score HSController.js passes to it and adds them to the MySQL Database. <php><?php $name = $_GET['name']; $score = $_GET['score'];

// Send variables for the MySQL database class. $db = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); mysql_select_db('my_database') or die('Could not select database');

$query = "insert into scores values (NULL, '$name', '$score');"; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); ?></php>

PHP - display.php

This script will take the top 5 scores from the MySQL Database and put them in a GUIText. <php><?php // Send variables for the MySQL database class.

   $database = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
   mysql_select_db('my_database') or die('Could not select database');

$query = "SELECT * FROM `scores` ORDER by `score` DESC LIMIT 5"; $result = mysql_query($query) or die('Query failed: ' . mysql_error());

$num_results = mysql_num_rows($result);

for($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo $row['name'] . "\t" . $row['score'] . "\n"; } ?></php>

Upload them to wherever you want them on your server.

Step 3: The Unity HSController Script

You will attach this script to a GameObject. You need to be able to supply it with a string containing the players name, and a string containing the players score. You also need to edit the URLs in it that point to the correct addscore.php script and the display.php. You will also need to supply it with a GUIText if you want it to get the scores (otherwise you can ignore the getScores() function).

JavaScript - HSController.js

<javascript>prvate var secretKey="mySecretKey"; // Edit this value and make sure it's the same as the one stored on the server var addScoreUrl="http://localhost/unity_test/addScore.php?" var highscoreUrl="http://localhost/unity_test/display.php";

function Start() { getScores(); }

function postScore(name, score) {

   //This connects to a server side php script that will add the name and score to a MySQL DB.
   // Supply it with a string representing the players name and the players score.
   var hash=Md5.Md5Sum(name + score + secretKey); 
   var highscore_url = addScoreUrl + "name=" + WWW.URLEncode(name) + "&score=" + score + "&hash=" + hash;
       
   // Post the URL to the site and create a download object to get the result.
   hs_post = WWW(highscore_url);
   yield hs_post; // Wait until the download is done
   if(hs_post.error) {
       print("There was an error posting the high score: " + hs_post.error);
   }

}

// Get the scores from the MySQL DB to display in a GUIText. function getScores() {

   gameObject.guiText.text = "Loading Scores";
   hs_get = WWW(highscoreUrl);
   yield hs_get;
   
   if(hs_get.error) {
   	print("There was an error getting the high score: " + hs_get.error);
   } else {
       gameObject.guiText.text = hs_get.data; // this is a GUIText that will display the scores in game.
   }

}</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox