Building Browsergames: Healing your players (PHP)

For all that we’ve built a banking and a combat system, we need a way for users to heal themselves after combat now. So today, we’ll be building the healer page.

For once, we don’t need to add any new stats to our game – we’ll be working off of the Maximum HP and Current HP stats from earlier. What that means is that we can dig right into building our new page – starting with the link that we add to index.tpl:

 

<p><a href='healer.php'>The Healer</a></p>

Once that’s set up, we can move on to creating the template for our healer page. There isn’t really much to it, and this is healer.tpl in its entirety:

 

<html>
<head>
	<title>The Healer</title>
</head>
<body>
	<p>Welcome to the healer. You currently have <strong>{$curhp}</strong> HP out of a maximum of <strong>{$maxhp}</strong>.</p>
	<p>You have <strong>{$gold}</strong> gold to heal yourself with, and it will cost you <strong>1 gold per HP healed</strong> to heal yourself.</p>
	{if $healed ne 0}
		<p>You have been healed for <strong>{$healed}</strong> HP.</p>
	{/if}
	<form action='healer.php' method='post'>
		<input type='text' name='amount' id='amount' /><br />
		<input type='submit' name='action' value='Heal Me' />
	</form>
	<p><a href='index.php'>Back to main</a></p>
	<script type='text/javascript'>
		document.getElementById('amount').focus();
	</script>
</body>
</html>

Just be looking at that template, chances are you can tell what we’re going to be doing with it – but I’ll let you in on the code anyways. Here’s the starter code, to load in our template and populate it with some values:

 

<?php
 
require_once 'smarty.php';
 
session_start();
 
require_once 'config.php';		// our database settings
require_once 'stats.php';
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
	or die('Error connecting to mysql');
mysql_select_db($dbname);
// retrieve user ID
$query = sprintf("SELECT id FROM users WHERE UPPER(username) = UPPER('%s')",
			mysql_real_escape_string($_SESSION['username']));
$result = mysql_query($query);
list($userID) = mysql_fetch_row($result);
 
$smarty->assign('curhp',getStat('curhp',$userID));
$smarty->assign('maxhp',getStat('maxhp',$userID));
$smarty->assign('gold',getStat('gc',$userID));
 
$smarty->display('healer.tpl');
 
?>

With this code written, all we need to do is write a little bit more to handle the value that the user enters as how much they want to get healed. Just like we did for our bank page, we’re going to assume that any ‘weird’ values that the user entered just mean ‘use the maximum available’ – and either heal them to full, or heal them as much as we can with their current gold on hand. Here’s our code to handle the user inputting a value and then clicking on the ‘Heal’ button:

 

if($_POST) {
	$amount = $_POST['amount'];
	$gold = getStat('gc',$userID);
	$needed = getStat('maxhp',$userID) - getStat('curhp',$userID);
	if($amount > $needed || $amount == '') {
		$amount = $needed;	
	}
	if($amount > $gold) {
		$amount = $gold;	
	}
	setStat('gc',$userID,getStat('gc',$userID) - $amount);
	setStat('curhp',$userID,getStat('curhp',$userID) + $amount);
	$smarty->assign('healed',$amount);
}

If you compare this code to the code from our banking page from earlier, you might notice a slight difference when we test the amount that the user wants to heal for. That’s becaused the first check makes sure that they’re healing for, at maximum, the amount of HP that they need to be healed for. The second test makes sure that they can afford to be healed for the amount that they entered. Once the two checks are done, we heal the player for whatever amount we can, in addition to subtracting from their current gold value. We also set the $healed variable in our Smarty template, so that we can display a message to the user showing them that they’ve been healed.

And that’s how to build a healer page! Now your users can fight monsters, deposit their gold in the bank, and heal themselves when they get injured. Here’s all the code for the healer page in one spot:

 

<?php
 
require_once 'smarty.php';
 
session_start();
 
require_once 'config.php';		// our database settings
require_once 'stats.php';
$conn = mysql_connect($dbhost,$dbuser,$dbpass)
	or die('Error connecting to mysql');
mysql_select_db($dbname);
// retrieve user ID
$query = sprintf("SELECT id FROM users WHERE UPPER(username) = UPPER('%s')",
			mysql_real_escape_string($_SESSION['username']));
$result = mysql_query($query);
list($userID) = mysql_fetch_row($result);
 
if($_POST) {
	$amount = $_POST['amount'];
	$gold = getStat('gc',$userID);
	$needed = getStat('maxhp',$userID) - getStat('curhp',$userID);
	if($amount > $needed || $amount == '') {
		$amount = $needed;	
	}
	if($amount > $gold) {
		$amount = $gold;	
	}
	setStat('gc',$userID,getStat('gc',$userID) - $amount);
	setStat('curhp',$userID,getStat('curhp',$userID) + $amount);
	$smarty->assign('healed',$amount);
}
 
$smarty->assign('curhp',getStat('curhp',$userID));
$smarty->assign('maxhp',getStat('maxhp',$userID));
$smarty->assign('gold',getStat('gc',$userID));
 
$smarty->display('healer.tpl');
 
?>