This entry is based on the Building Browsergames blog entry: Displaying A User’s Stats (PHP)
It’s a pretty good idea to at least skim quickly through the original article before you go through this one, just to familiarize yourself with what we’re going to do.
Second Verse Same As The First
> ruby script/generate migration AddGoldToUser gold:integer
To add a new stat in the database for the amount of gold held we’ll create and edit a new migration. You saw something just like this a few entries back in this series:
class AddGoldToUser < ActiveRecord::Migration def self.up add_column :users, :gold, :integer, :default => 0 # Any existing users will get the new stat just like new users. If we had # to do any special setup for the value though, we could do it here. end def self.down remove_column :users, :gold end end
Note that a default value for the gold stat has been added above. The code as generated won’t have that. Be sure to run the new migration so we get our database schema updated:
> rake db:migrate
Now we’ll change the index page to display the various stats that we’ve assigned to our user whenever he/she logs in.
<h1>Welcome To The Game</h1> <% if logged_in? %> <h2><%= @current_user.login %></h2> <ul> <li>Attack: <strong><%= @current_user.attack %></strong></li> <li>Defense: <strong><%= @current_user.defense %></strong></li> <li>Magic: <strong><%= @current_user.magic %></strong></li> <li>Gold in hand: <strong><%= @current_user.gold %></strong></li> </ul> <% end %>
Note that neither the controller nor the view have ever accessed the database or set any values. Instead they get the user model which represents a particular user and call functions on it to get its values. This is exactly the kind of thing we want to see, controllers and views which are ignorant on details (like SQL) and depend upon a well written model to handle them. This will give us compartmentalized code that is easier to understand, to test, and to modify.
By this point, you should be starting to get a little more comfortable with how Rails lays out things. You’ll see HTML in the views with the occasional reference to variables we need to display (e.g. @current_user). Those variables are setup in a controller method which is called before we get the view to render (e.g. index). The controller will rely upon the model (e.g. User) to handle all the nitty gritty details of loading and storing data and also rules for that data (e.g. no more than two weapons per user). You’ll see this again in the next installment as we turn to adding combat to our game.
- I’m told that I’m a pretty good teacher, in person, but this is not in person and there are probably a lot of times I pick the wrong things to touch lightly on and to belabor. So, a few good books, a very few, is a good idea both for their reference value and because they tend to cover material I don’t cover or give you another chance to see the same things in a different way. Here are two I recommend: The Rails Way (Addison-Wesley Professional Ruby Series) (the best reference text by far) and RailsSpace: Building a Social Networking Website with Ruby on Rails (Addison-Wesley Professional Ruby Series) (a complete social network site built in Rails 1.2, a little bit of the code is older but still one of the best all around books out there).
Wish there was more?
I'm considering writing an ebook - click here.
John Munsch is a professional software developer with over 20 years experience. He created a series of game development sites (XPlus and DevGames.com) on his own before co-founding GameDev.net in 1999. The blog for his PBBG work is located at MadGamesLab.com.