iPhone Coding Tutorial – Creating an Online Leaderboard For Your Games1

As you may have seen, there are quite a few services out there offering free leaderboards.  These are great and all, but sometimes you want to have complete control over your data.  I have put together a complete tutorial detailing step by step how you can create your own online leaderboard for use in your iPhone games. For those of you who don’t know, a leaderboard is essentially a high scores list. This tutorial will also give you a very simple introduction to interfacing with web services.

The first part of this tutorial will discuss how to build the server.  We will be doing this using PHP and MYSQL.  I will also assume you have some working knowledge of PHP and MYSQL (if not, use the Google machine to get up to speed).

Since this tutorial is so long, I have decided to split it up into pages. You will notice the page number at the bottom. Please use them to navigate between parts of this post. I feel that I have to make this explicit as I will undoubtably get some dude in the comments saying “Where is the rest of the tutorial”.

Creating The Database

When creating a leaderboard, you will need some way of storing the data.  We will be using MYSQL for our storage.  You can also be lame and simply use flat files.  This would add quite a bit of code in the long run as you would have to write all of the sorting and pagination functionality yourself. Don’t do it.

One thing to note about my php server files. I know they could be cleaned up a little and optimized (you could create a config.php that contains all the db interface code), but the goal of this tutorial is not to show you how to code killer PHP. It’s to show you how to create code that you can connect your iPhone apps to.

I like to create one file that does a complete flash of the database.  That way, when I’m testing or switch from staging to production, it is a very simple process.  So, with that being said, here is my code for create_db.php.

<?php
        // create_db.php
	/** MySQL database name */
	define('DB_NAME', '');
	/** MySQL database username */
	define('DB_USER', '');
	/** MySQL database password */
	define('DB_PASSWORD', '');
	/** MySQL hostname */
	define('DB_HOST', $_ENV{DATABASE_SERVER});
 
	$table = "highscores";
 
	// Initialization
	$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
	mysql_select_db(DB_NAME, $conn);
 
	// Error checking
	if(!$conn) {
		die('Could not connect ' . mysql_error());
	}
 
	// Drop existing table if exists
	mysql_query("DROP TABLE $table", $conn);
 
	$retval = mysql_query("CREATE TABLE $table(
		id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
		udid VARCHAR(45),
		name VARCHAR(25),
		score FLOAT(10,2),
		date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
	)",$conn);
 
	if($retval) {
		echo "Database created...";
	} else {
		echo "Could not create database " . mysql_error();
	}
 
	mysql_close($conn);
?>

 

I’m not going to go into too much detail about this code, however I will give a high level description of what’s going on. One thing to note about this code is it assumes you already have the database created.

First, we are defining variables that contain our database information. Make sure you replace my empty strings with your database info. After we connect to the database, we drop the table if it already exists. This is useful if you want to wipe out your test data. Finally, we create the scores table. Navigate to this file in your browser to run it and create the scores table. Pretty easy right?

You will want to make sure to remove this file from your server after running it to avoid people resetting your database.

Now that our database table has been created, it’s time to implement the web service code to publish new scores to our leaderboard.

from:http://icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值