Adding a Reward System to your Game

I was recently contacted by Sunchaser from the BBGameZone forums about membership within the PBBG network for his topsite, top-pbbg.com. As you can see, his membership was accepted – and I also managed to get him to write a quick tutorial about how to integrate his site into your PBBG – so that you can drive traffic to your game by providing incentives for your players, using a reward system. Here’s the tutorial in full:

It’s possible to find on the internet a large number of sites that shows a list of browser-based games (these are normally called Topsites, or Toplists). A game owner can register their game into these sites – and by inserting a voting link in their game, their players can cast their vote for the game. With enough votes, your game can be pushed into the top positions on the topsite – increasing visibility, and driving more traffic to your game.

Some of these sites allow you, the game owner, to configure a reward system for your game, so that you can give your players an incentive to vote for your game.

Once you have configured the reward URL, an example of a voting link would be http://exampletoplist.com/in.php?u=789&key=12345667.

The parameter ‘u’ will contain the ID of the player that is voting, and the key is a random number. After the topsite has verified the vote, it will call back your configured link (for example, http://yourgame.com/reward.php?u=789&key=12345667&listname=top_pbbg). The reward script should check for a corresponding couple (userid + key), and then give the player the reward.

This tutorial will walk you through setting up a system that:

  • Sends a player vote to a toplist, recording the vote
  • Processes a callback from the toplist

Sending a player vote to a toplist

In order to trace the votes and prevent cheating, the votes sent to the toplist will need to be recorded within a database table. The first thing we’ll do is create a database table where we store different toplist sites, along with a table for recording user votes:

CREATE TABLE IF NOT EXISTS `cfg_ranking_sites` (
  `id` int(11) NOT NULL AUTO_INCREMENT, --primary key
  `username` varchar(25) NOT NULL,      --the username you registered with in the toplist
  `name` varchar(50) NOT NULL,          --the toplist name
  `url` varchar(255) NOT NULL,          --the toplist url (voting links of the toplist)
  `refererurl` varchar(255) NOT NULL,   --the toplist root url
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM;

 

CREATE TABLE IF NOT EXISTS `voting_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `list_id` int(11) NOT NULL,
  `vkey` varchar(25) NOT NULL,
  `status` varchar(25) NOT NULL,
  `ip` varchar(15) NOT NULL,
  `urlcalled` varchar(255) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM;

Next, we’ll add any topsites we wanted to track into our cf_ranking_sites table (in this case, top-pbbg.com):

INSERT INTO `cfg_ranking_sites` (`id`, `username`, `name`, `voting_link`, `refererurl`) VALUES
(1, 'vlad', 'top_pbbg', 'http://www.top-pbbg.com/index.php?a=in&u=vlad&k=KEY', 'http://www.top-pbbg.com');

Next, we create a file called vote.php, which loads the topsite urls, generates a random key for users, tracks the vote, and sends the vote to the topsite. Here’s the code:

 

/****************************************************************************
File:   vote.php
Desc:   traces a vote creating a random key, and sends the vote.
****************************************************************************/
$listname = mysql_real_escape_string( $_REQUEST['listname'] );
 
// load data of rating site
$data = mysql_query( 
	"select id, voting_link 
  from cfg_ranking_sites
  where name = '{$listname}' " );
 
list( $list_id, $voting_link) = mysql_fetch_row( $data );
 
// generate a rnd string
$k =  substr( md5 ( time() . rand(1, 10000000) ), 1, 25);
 
// get the user id from session
// (substitute your method for finding the user id)
$user = User::get_char_info();
$u = $user['user_id'];
 
// place the generated random key and trace the vote
$urlcalled = str_replace( "KEY", $k, $voting_link );
 
mysql_query( "insert into 
	voting_log
  ( user_id, vkey, list_id, ip, status, urlcalled ) 
  values
  ( '{$u}', '{$k}', $list_id, '".$_SERVER['REMOTE_ADDR']."', 'new', '{$urlcalled}' ) ");
 
// send the vote
header ( "location: $urlcalled" );
exit;

Next, put your voting link inside a page that’s visible to your players, so that they can start clicking on it. Make sure the link points to your vote.php file:

<a href='vote.php?listname=top_pbbg'>Vote for us at Top PBBG!</a>

Now, your players should be able to vote, and you should be able to record their votes in the table voting_log.

Processing a callback from the toplist

Let’s assume you have configured your reward URL on top-pbbg.com to be the following reward URL:

<a href='http://yourgame.com/reward.php?listname=top_pbbg'>Vote for us at Top PBBG!</a>

To process the toplist callback and give rewards to your player(s), first create a file called reward.php and insert the following code:

 

// Set content type
header('Content-type: text/xml');
// Check input, if keys are not present return error.
// This check and the fields needs to be changed for each toplist
if (
	!isset($_GET['key']) or 
	!isset($_GET['success']) or
	!isset($_GET['listname']) or
	) {
	echo '<response rewarded="0" message="Invalid input." />';
	die();
}
 
// Get data
$key = mysql_real_escape_string($_GET['key']);
$success = mysql_real_escape_string($_GET['success']);
$referer = $_SERVER['HTTP_REFERER'];
 
$sql = "select v.id, v.user_id, r.name
from voting_log v, cfg_ranking_sites r
where vkey = '{$key}' and v.list_id = r.id
and   refererurl = '{$referer}'
and   status = 'new'";
$data = mysql_query( $sql );
 
if ( mysql_num_rows( $data ) == 0 )
{
// insert your debug message, return error
// mydebug( true, "match not found. ip: " . $_SERVER['REMOTE_ADDR'] . " key: " . $key . " referer: " . $referer);
echo '<response rewarded="0" message="match not found." />';
exit;
}
 
list( $id, $user_id, $list_name ) = mysql_fetch_row( $data );
 
// give rewards to user, only if the vote was unique ($success==1)
if ( $success == 1 )
{
	$r = rand( 1,2 );
	if ( $r == 1 ) 
	{
		;
		//example: give reward and send information to user
		//mysql_query( "update user set char_adamantium = char_adamantium + 2 where user_id = " . $user_id );
		//User::send_news( 'char', $user_id, "You got 2 adamantium for your vote at " . $list_name );
	}
 
	else
	{
		;
		//example: give reward and send information to user
		//mysql_query( "update user set char_mythril = char_mythril + 2 where user_id = " . $user_id );
		//User::send_news( 'char', $user_id, "You got 2 mythril for your vote at " . $list_name );
	}
	mysql_query( "update voting_log set status = 'rewardgiven' where id = " . $id );
	echo '<response rewarded="1" message="Reward given." />';
}
else
{
	mysql_query( "update voting_log set status = 'rewardnotgiven' where id = " . $id );
	echo '<response rewarded="0" message="Reward not given (not unique vote)." />';
}
exit;

Now, by changing the code within the if statements above, you should be able to give rewards to your players for voting for your game on different topsites.

Note

The script reward.php has been written to process rewards for one toplist – if you want it to process results from multiple toplists, you will want to have it handle the listname parameter, using that to decide which piece of code to run for which topsite.

And that’s all there is to setting up your game with a rewards URL! If you’re interested in doing this for your game, sign up at top-pbbg.com and get started – and then post a link to your game in the comments when you’re finished.