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:

17
	<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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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:

18
19
20
21
22
23
24
25
26
27
28
29
30
31
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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');
 
?>

Wish there was more?

I'm considering writing an ebook - click here.

.

Luke is the primary editor of Building Browsergames, and has written a large portion of the articles that you read here. He generally has no idea what to say when asked to write about himself in the third person.

Tuesday, June 17th, 2008 buildingbrowsergames, code, php
  • Hi! Firstly, I'd just like to say thank you for putting this site together. Its truly awesome and has given me a bit of a kick to actually stop thinking about doing something, and actually doing something!

    Secondly, on line 28, you poll the database to get the current gold count, yet on line 20 you've already done this and not changed it. Wouldn't it be more efficient to insert a line above 28 such as:
    $gold = $gold - $amount;
    and then change the setStat to:
    setStat( 'gc', $userID, $gold);

    You could do the same with the actual healing value as well. Avoid two calls to getStat('curhp',$userID) as you've already got that value.

    From a performance point of view wouldn't this be better?

  • That'd definitely be better for the sake of minimizing database hits - while
    I was building these scripts, I'm afraid they sort of grew organically, so
    optimizations that should have been made sometimes didn't.

  • thanks for the info, I will try it now ...:D

blog comments powered by Disqus

About

Building Browsergames is a blog about browsergames(also known as PBBG's). It's geared towards the beginner to intermediate developer who has an interest in building their own browsergame.

Sponsors

Got Something to Say?

Send an e-mail to luke@buildingbrowsergames.com, or get in touch through Twitter at http://twitter.com/bbrowsergames