Building Browsergames: displaying a user’s stats (Perl)

For all that our registration page might be setting the new player’s stats, right now they aren’t getting displayed anywhere – and what good are stats if you aren’t displaying them anywhere? Today you’ll get to build the logic to display a user’s stats on the main page.

First off, we’re going to add another stat to the mix – because almost every browsergame out there(except ours currently) has some sort of currency. I’m going to call the stat ‘Gold’, but you can call it whatever you want – all that matters is that there’s some form of currency for players to spend.

INSERT INTO stats(display_name,short_name) VALUES ('Gold','gc');

Now that we’ve inserted that stat, we should also modify our index template to display the stats – here’s the extra HTML we’ll be adding:

7
8
9
10
11
12
	<ul>
		<li>Attack: <strong><!--tmpl_var name='attack'--></strong></li>
		<li>Defence: <strong><!--tmpl_var name='defence'--></strong></li>
		<li>Magic: <strong><!--tmpl_var name='magic'--></strong></li>
		<li>Gold in hand: <strong><!--tmpl_var name='gold'--></strong></li>
	</ul>

Once we’ve made those modifications to our template file, we’re going to need to change our index page code – so that it retrieves the User’s ID, and then the stat values associated with that User ID. Because we’ve stored the user’s username + password into the cookie ‘username+password’, we can retrieve the user’s username and use it to query our users table to find their User ID. Here’s how we do that:

11
12
13
14
15
16
17
18
use DBI;
use config;
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost",$dbuser,$dbpass,{RaiseError => 1});
my $sth = $dbh->prepare("SELECT id FROM users WHERE UPPER(username) = UPPER(?)");
$sth->execute($username);
my $userID;
$sth->bind_columns(\$userID);
$sth->fetch;

Now that we have the User’s ID, we can use it to retrieve their stat values:

19
20
21
22
23
24
25
26
27
28
29
use stats;
my %stats;
$stats{attack} = stats::getStat('atk',$userID);
$stats{defence} = stats::getStat('def',$userID);
$stats{magic} = stats::getStat('mag',$userID);
$stats{gold} = stats::getStat('gc',$userID);
 
my $template = HTML::Template->new(
		filename	=>	'index.tmpl',
	);
$template->param(username	=>	$username,%stats);

And after this is all done, if you log in and visit the index page, you’ll see the four stat values. Attack, Defence, and Magic will all be 5(or whatever default you decided to set them to in your registration page), and Gold will be 0 – because we never set it up anywhere, so our stats code set it to a default for us. If you’d like, you can customize your registration page so that it gives your players some gold to start off with as well – I’ll leave writing the code itself as an exercise for you(hint: try just after the other stat-setting code).

I reorganized the index page code slightly, because I like to keep all of my use statements at the top of my code – but here’s what the code looks like now:

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
#!/usr/bin/perl -w
 
use strict;
use CGI qw(:cgi);
use HTML::Template;
use DBI;
use config;		# our database settings
use stats;
 
my $query = new CGI;
my $cookie = $query->cookie('username+password');
 
my ($username) = split(/\+/,$cookie);
my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost",$dbuser,$dbpass,{RaiseError => 1});
my $sth = $dbh->prepare("SELECT id FROM users WHERE UPPER(username) = UPPER(?)");
$sth->execute($username);
my $userID;
$sth->bind_columns(\$userID);
$sth->fetch;
my %stats;
$stats{attack} = stats::getStat('atk',$userID);
$stats{defence} = stats::getStat('def',$userID);
$stats{magic} = stats::getStat('mag',$userID);
$stats{gold} = stats::getStat('gc',$userID);
 
my $template = HTML::Template->new(
		filename	=>	'index.tmpl',
	);
$template->param(username	=>	$username,%stats);
print $query->header(), $template->output;

Next time, we’ll start working with some of our stats a little bit more – in addition to writing some of the code for the buildings we mentioned in our brief design document from earlier.

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.

Friday, June 6th, 2008 buildingbrowsergames, code, perl
  • Hey Firzen,

    Thanks for your support - there's a lot of work that goes into the things that I and others write here, and it's support from people like you that make writing for this site worth it.

    Thanks.

  • firzen

    Dude, this has no relation to the particular post, but I just want to let you know that this website rules. I check it on a regular basis and your lessons are amazing, you put so much work in it that it'd be a shame if I didn't thank you at least once.
    Lots of respect and admiration to you! Keep it up! :)

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