Sending scores to the web

Hi to all,

I have a question that culb be very stupid but…
Can I make a game in Unity that sends the final scores of the player to a database(On the web)? I mean, in a car race, when the player finish, he sends his results to a database or something like that to know his position (First, second, ,)

Thank you in advance

Well, you have web access with the WWW classes so basically what you would do is have a script on your internet server (PHP, ASP, whatever) to control database access and adding scores, then you call a querystring from Unity like you would load a normal page. Example: yoursite.com/HighScores/Update.php?mode=add&score=12000

The tricky part with that is securing it to prevent unauthorized access. The simplest method (not secure, but helps a bit) would be to include a password that you save in your unity script and send that to the server in the querystring as well. Also to help prevent DOS attacks and other similar annoyances, you may want to limit the number of scores that can be added per IP address a day to 100 or something.

You would need to be pretty good at server-side programming to create anything secure unfortanately. Would need encryption, etc. If you have any specific questions I may be able to help you out (I use PHP).

Hope that helps,
-Jeremy

Thank you Jeremy, you give me the first flash of light!

And, Unity has an action/script to make that? Not the server configuration, I mean the send (action of the player) that info (Score in this case) to the server?

Thank you in advance

Yes, using the WWW class as jeremy said. See the documentation here (It even contains a simple highscore example.)

Hi Freyr,

Thank you for that info, I think I have problems since I am a 3d artist and my programing skills are very limited.
Another shot:
Does Unity has a counter or clock (Pre-made, like the gravity, ect,) to use in the racing game? To register the time that the player uses to finish the track. In minutes, seconds, and so on?

Thank you in advance

There is a game timer example either in the script tutorial folder that comes with the unity samples or it was in the scripting tutorial PDF. I am not sure and I am not at my Mac right now :wink:

As for the score system, it will take a bit of server-side prgramming knowledge (in whatever language) to do a very basic system. It will take a fair bit more to make it even a bit secure. But it is possible!

I could whip up a dirt basic PHP script to add the data to the database for you if you would like.

-Jeremy

Hi Jeremy, and thank you for your offer that will be very helpfull!

for now I have another question:

Can I make that the game must be played on the web? I mean, if the computer is not conected to the internet, the game will not run.

Thank you in advance

You can make it so your game must be connected to the web in order to play. The simplest way would be to use the WWW class. You could get fancier and use the .Net classes if you needed more control. I think you might need to consider some questions in your design like:

  1. Must the player stay continuously connected? If so, how to handle lost connections in the game?
  2. You can just check your server with the WWW class. However, you need to account for times when your server might be down. Perhaps have a fall-back server to check (maybe even some sight like Google).
  3. Is the game interactive with a web server, such as sending and getting information from the server periodically? You can still accomplish this with the WWW class.
  4. Will the game be interactive with other players. If so, are the performance needs high or low. If low, you could probably get away with a simple queing web server (written in PHP, ASP, Python, Ruby, or the like). If the demands are high it looks like it will be .Net.
  5. At what interval do you check?
  6. If the user is not connected, how do you tell the user and how do you have the user tell the game to try again? Or do you yave the game automatically retry?

Not knowing what you are trying to accomplish, these are the ones that spring to mind right away. There are others here that will be able to come up with much better considerations for you :slight_smile:

Thank you Ifrog,

Yuor response is very detailed(Thank you for that). I want that the user prompt an user and password to start the game and that info must be used to know if the player is online or not in order to continue the game and at the end he send his results (Score) to quilify.
Thats the idea.

Another question:

The size of a web game made on Unity is the same for Windows and Mac?

The Windows plug in works ok (Stable)?

Thank you in advance

Ok, I have thrown together a dirt-basic PHP script to take scores and add them to a MySQL database. It is really easy to do and you will want to modify the script to behave how you want and to also add to the proper databases and tables. It all depends how you set up your database on your server. The settings are pretty straight foward so you shouldn’t have any trouble.

WARNING: this is not secure and I haven’t tested it! All this does is use a simple key to try to limit unauthorized access to the script. This is just meant to be a stepping stone as you will have to modify it to get it to behave exactly like you want it to.

There are two files, first db.inc.php:

<?php
/****
db.inc.php
file to hold all db information and connection scripts
****/

//our access information
$db_server = "myserver.com";
$db = "GameScores";
$user = "username";
$pass = "password";

//conect to the db
@mysql_connect($db_server, $user, $pass);

//select the database to connect to
@mysql_select_db($db);


?>

Edit the variables to whatever settings and passwords you have set up and make sure to put this file under a folder called INCLUDES that is only accessable from the server itself. That helps make sure no one can view that file with their browser, it should only be usable by the server itself.

Next the actual script file. Name it whatever you want with .php on the end.

<?php

$AUTHID = $_GET['AUTHID'];
$name = $_GET['name'];
$score = $_get['score'];
$mode = $_GET['mode'];

//set a random code to make it (very slightly) harder to use this system without your game.
//Make sure to send this exact same code in the AUTHID part of the querystring.
$PASSID = "Eesd453FWF3gGar4gEWfg";

if ($AUTHID == $PASSID)
{

    //check that all data has been sent
    if ($AUTHID == "" || $name == "" || $score == "" || $mode == "")
    {
        //do nothing. TODO: you might want to redirect to an error page and read the
        //error page with Unity's WWW classes
    }
    else {   //all the data is here so let's add it

        if ($mode == "add")
        {
            //load db access info and connect to the db
            require_once("INCLUDES/db.inc.php");
    
            $query = "INSERT into scores (name, score) VALUES('$name', '$score')";
            mysql_query($query);
    
            //now redirect to the results page to read into unity.  Comment out this line if you don't want this behaviour
            header ("Location: http://www.yoursite.com/results.php");
        }
    }
}

?>

Again tweak the settings as needed. To use, follow the sample freyr posted a link to in the unity docs. the site request (querystring) should look like this:
mysite.com/scores.php?mode=add&name=johndoe&score=1200&AUTHID=Eesd453FWF3gGar4gEWfg

Should work fine. You would obviously want to change the PASSID in the main script file, and that would be the code you send from Unity.

If you have any trouble with it let me know. If you want to add to it/modify it, php.net is a great resource for the syntax and methods. Or ask here and if I can help I will :wink:

Hope that helps get you started, and it can get very complex from here.
-Jeremy

Many, many thanks Jeremy,

This is a good way to start!!!

This forum is tremendous!!

Thank you to all

No problem. I hope my usage directions weren’t too cryptic…was kinda tired :slight_smile: Hope it helps you get started. PHP isn’t all that bad once you get the basics. Same with Unity.

-Jeremy

Can we modify Kevin’s code on the Wiki to pull this down into a screen? And what about showing, say the top 10 scores?

An easy way to help prevent unauthorized accesses is to MD5 hash the name concatenated to the score concatenated to a secret salt. Then include the hash along in the query string and have PHP compute the hash itself and see if they’re equal.

Then to hide the secret salt in case someone decompiles your Unity code to look at the algorithm. (I did it to OpenFire a while ago to prove a point :slight_smile: )

A way to hide it could be to store this salt in a public variable assigned in the Inspector instead of hardcoded into your script. Then people need to reverse engineer OTEE’s dark magic or possibly step through a debugger to find your secret salt. Or get access to your web server and read the php script. That shouldn’t happen I hope. :slight_smile:

-Jon