Building Browsergames: Swapping Weapons (PHP)
It was a close race, but in the end the results of our poll on whether users should dual wield their weapons or have to toggle which one was active have been decided: users will toggle which weapon is currently active. Today, we’re going to be implementing that functionality.
The ’swap weapon’ page is a fairly simple one – all we need is a template and some code to handle it(we even wrote most of it earlier, when we built our weapons shop). We’ll start off with the template, and call it equipment.tpl:
<html> <head> <title>Equipment Management</title> </head> <body> <h3>Current Equipment:</h3> <p><a href='index.php'>Back to main</a></p> <ul> <li> Primary Hand: {if $phand ne ''} {$phand} <form action='weapon-shop.php' method='post'> <input type='hidden' name='sell' value='phand' /> <input type='submit' value='Sell' /> </form> {else} None {/if} </li> <li> Secondary Hand: {if $shand ne ''} {$shand} <form action='weapon-shop.php' method='post'> <input type='hidden' name='sell' value='shand' /> <input type='submit' value='Sell' /> </form> {else} None {/if} </li> </ul> <p> <form action='equipment.php' method='post'> <input type='submit' value='Swap' name='swap' /> </form> </p> </body> </html>
This template is relatively simple, and essentially just the top parts of the weapon shop’s template – we even left in the ‘Sell’ buttons, so that users can sell their equipment straight from their equipment page. We have to make sure that the ’swap’ button has a ‘name’ attribute – that way we can check $_POST later to see if it was clicked. Next we’ll build equipment.php, which is responsible for retrieving the current weapons a user is using, and displaying this template:
<?php require_once 'smarty.php'; session_start(); require_once 'config.php'; // our database settings $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die('Error connecting to mysql'); mysql_select_db($dbname); // retrieve player's 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); require_once 'stats.php'; // player stats $phand = getStat('phand',$userID); $shand = getStat('shand',$userID); $phand_query = sprintf("SELECT name FROM items WHERE id = %s", mysql_real_escape_string($phand)); $result = mysql_query($phand_query); if($result) { list($phand_name) = mysql_fetch_row($result); $smarty->assign('phand',$phand_name); } $shand_query = sprintf("SELECT name FROM items WHERE id = %s", mysql_real_escape_string($shand)); $result = mysql_query($shand_query); if($result) { list($shand_name) = mysql_fetch_row($result); $smarty->assign('shand',$shand_name); } $smarty->display('equipment.tpl'); ?>
Essentially, all that we’re going to be adding on to this code is handling for a POST request; if something is POSTed to this page, we swap the user’s current weapons. The code is fairly simple, as you can see:
if($_POST) { setStat('phand',$userID,$shand); setStat('shand',$userID,$phand); $temp = $shand; $shand = $phand; $phand = $temp; }
And that’s all there is to it! We just add another link to our index page:
<p><a href='equipment.php'>Equipment Management</a></p> And we’re finished! Here’s the code for our template: <html> <head> <title>Equipment Management</title> </head> <body> <h3>Current Equipment:</h3> <p><a href='index.php'>Back to main</a></p> <ul> <li> Primary Hand: {if $phand ne ''} {$phand} <form action='weapon-shop.php' method='post'> <input type='hidden' name='sell' value='phand' /> <input type='submit' value='Sell' /> </form> {else} None {/if} </li> <li> Secondary Hand: {if $shand ne ''} {$shand} <form action='weapon-shop.php' method='post'> <input type='hidden' name='sell' value='shand' /> <input type='submit' value='Sell' /> </form> {else} None {/if} </li> </ul> <p> <form action='equipment.php' method='post'> <input type='submit' value='Swap' name='swap' /> </form> </p> </body> </html>
And here’s the code for the file that handles the functionality behind the template:
<?php require_once 'smarty.php'; session_start(); require_once 'config.php'; // our database settings $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die('Error connecting to mysql'); mysql_select_db($dbname); // retrieve player's 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); require_once 'stats.php'; // player stats $phand = getStat('phand',$userID); $shand = getStat('shand',$userID); if($_POST) { setStat('phand',$userID,$shand); setStat('shand',$userID,$phand); $temp = $shand; $shand = $phand; $phand = $temp; } $phand_query = sprintf("SELECT name FROM items WHERE id = %s", mysql_real_escape_string($phand)); $result = mysql_query($phand_query); if($result) { list($phand_name) = mysql_fetch_row($result); $smarty->assign('phand',$phand_name); } $shand_query = sprintf("SELECT name FROM items WHERE id = %s", mysql_real_escape_string($shand)); $result = mysql_query($shand_query); if($result) { list($shand_name) = mysql_fetch_row($result); $smarty->assign('shand',$shand_name); } $smarty->display('equipment.tpl'); ?>