Basic online high scores with XNA / C# and PHP / MYSQL

Basic online high scores with XNA / C# and PHP / MYSQL


In this tutorial I want to give you a very straight forward and basic way to implement online high scores (aka. online leaderboards or online highscore tables) for XNA Game Studio PC games. We used a similar aproach to quickly implement basic online highscores for an early version of our game zeit2. I assume that you know how to work with C#,PHP,HTML and MySQL for this little tutorial. Of course in a real setting you would probably want to implement a user management/login to identify every unique user. But to make it simplier for now we only submit a name to be stored with every score.
What you need is a PHP server with MySQL running as a database backend.

STEP 1 – Setting up the database

First you will have to create a MySQL Table:
CREATE TABLE IF NOT EXISTS `mygame_highscores` (
`ID` int(11) NOT NULL auto_increment,
`ModeID` varchar(256) NOT NULL,
`Name` varchar(256) NOT NULL,
`Info` varchar(256) NOT NULL,
`Score` int(11) NOT NULL,
`Timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

ID contains an auto-incremented Number that is unique for every entry.
ModeID contains an identifier for which mode or level a highscore was achieved. This way you can save multiple highscores tables in one MySQL table.
Name is the name of the player that got the new score. (In an advanced version you would use an UserID that refers to another table in your database)
Info contains anything you wanna store with your score, i.e. what time the user needed to finish a level.
Score has the number that represents the score the player got.
Timestamp lets you identify when a score was saved. That way you can have weekly leaderboards for example.

Let’s enter some dummy data to our table. this example only inserts two records but feel free to add more for better testing.

INSERT INTO `databasename`.`mygame_highscores` (
`ID` ,
`ModeID` ,
`Name` ,
`Info` ,
`Score` ,
`Timestamp`
)
VALUES (
NULL , '1', 'Marcus', '10:30 min', '100',
CURRENT_TIMESTAMP
), (
NULL , '1', 'Peter', '9:12 min', '90',
CURRENT_TIMESTAMP
);

Read on the next page how we write our PHP script to request scores.



Article Pages:

  1. Setting up the database
  2. PHP script for requesting high scores
  3. HTML script for sending post requests
  4. PHP script for saving scores
  5. C# code for online scores using WebRequest
  6. C# code for sending scores and generating the hash code
  7. Summary
  8. View All

Pages: 1 2 3 4 5 6 7