Server Side Highscores

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (<php>tag caused php code to be partially evaluated... scary)
m (PHP - display.php: More <php> tag problems)
Line 53: Line 53:
 
==PHP - display.php==
 
==PHP - display.php==
 
This script will take the top 5 scores from the MySQL Database and put them in a GUIText.
 
This script will take the top 5 scores from the MySQL Database and put them in a GUIText.
<php><?php
+
<perl><?php
 
     // Send variables for the MySQL database class.
 
     // Send variables for the MySQL database class.
 
     $database = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
 
     $database = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
Line 68: Line 68:
 
         echo $row['name'] . "\t" . $row['score'] . "\n";
 
         echo $row['name'] . "\t" . $row['score'] . "\n";
 
     }
 
     }
?></php>
+
?></perl>
  
 
Upload them to wherever you want them on your server.
 
Upload them to wherever you want them on your server.

Revision as of 06:49, 12 August 2008


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 PRIMARY KEY,
  `name` varchar(15) NOT NULL DEFAULT 'anonymous',
  `score` int(10) UNSIGNED NOT NULL DEFAULT '0'

) TYPE=MyISAM;</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. <perl><?php

       $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');
       // Strings must be escaped to prevent SQL injection attack. 
       $name = mysql_real_escape_string($_GET['name'], $db); 
       $score = mysql_real_escape_string($_GET['score'], $db); 
       $hash = $_GET['hash']; 
       $secretKey="mySecretKey"; # Change this value to match the value stored in the client javascript below 
       $real_hash = md5($name . $score . $secretKey); 
       if($real_hash == $hash) { 
           // Send variables for the MySQL database class. 
           $query = "insert into scores values (NULL, '$name', '$score');"; 
           $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
       } 

?></perl>

PHP - display.php

This script will take the top 5 scores from the MySQL Database and put them in a GUIText. <perl><?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";
   }

?></perl>

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).

You will also need to include the MD5 script previously posted to this Wiki in your project folder.

JavaScript - HSController.js

<javascript>private 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.EscapeURL(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