Building a Space Strategy Game with Ruby on Rails – Part II

In the second part of this series (see: Part1) I will show you how to get started with Ruby on Rails for our space strategy game and easily integrate an authentication system.

Setting up a rails application

If you’re familiar with RoR you will already know how easy this can be. I’m developing on a Mac OSX platform which comes with Ruby installed and I will be hosting on standard Linux/Debian box with Ruby already installed. I will also be using mysql database both locally and in production.

First thing I will ensure I have the necessary gems to start developing. Starting with an empty gem repository I would issue the following commands into my terminal:

gem install rails -v 2.0.2
sudo gem install mysql

This will fetch the necessary gems for running rails (I’ve specified the version here to avoid getting the bleeding edge version) and connecting to mysql. For a good article on setting up mysql for use with rails on Mac OSX see here.

With our rails environment set up, I can jump in and create our project:

rails -d mysql space_pbbg

cd space_pbbg

The above will create the framework for our application and automatically configure it to connect to a local mysql database.

Now we use rake, ruby’s make tool, to create our database for us:

rake db:create

This will create our development database (space_pbbg_development) ready for us to add tables via our model definitions later.

Finally, to test our application we issue the following command:

ruby script/server

This starts our RoR app on http://localhost:3000 and we should see a nice generic welcome screen there that tells us everything has worked out alright so far.

Hooking up player authentication
As players need to register and login to play our game, and the player model object is going to be root of much of our application anyway, I like to do this part up-front. I also don’t like reinventing the wheel and prefer to use existing code (or code generators) as much as possible (it’s the RoR way!). However, the state of authentication with rails is a bit of a minefield at present: there are many possible solutions out there with some of them being clunky or difficult to work with.

That said I’ve had success in the past with a somewhat old plugin called Acts_as_authenticated which I’m going to use here. I’m going to install the plugin, run some of its generators to generate the necessary model and supporting code and fine tune it a bit so it fits my needs.

ruby script/plugin source HTTP://svn.techno-weenie.net/projects/plugins

ruby script/plugin install acts_as_authenticated

ruby script/generate authenticated player account

ruby script/generate authenticated_mailer player

And there you have the basics needed all in 4 commands! The first lets our plugin manager know where to fetch the plugin. The second downloads the plugin. The third creates the necessary model, controller and helpers that facilitates authentication. And finally, we create a notifier model that uses ActionMailer and makes it easy to send emails out to players.

To get the new player model into our database we run the following command:

rake db:migrate
We’re almost ready to test our player sign up and authentication system but first we have to do our first bit of “coding”… well, a bit of cut and paste and writing a simple welcome screen.

First, lets open up the generated app/controllers/account_controller.rb file where we will find these two lines:

# Be sure to include AuthenticationSystem in Application Controller instead
 
include AuthenticatedSystem
 
# If you want "remember me" functionality, add this before_filter to Application Controller
 
before_filter :login_from_cookie

Following the instructions we cut these lines and paste them inside the class definition for our controllers’ base class in app/controllers/application.rb.

Now we need to configure routing of the application so when players access http;//localhost:3000/signup they get the registration page, http;//localhost:3000/login they get the login page and finally http;//localhost:3000/logout will log them out.

We do this by opening up /config/routes.rb and adding the following lines after the first line which should be “ActionController::Routing::Routes.draw do |map|“:

map.home '', :controller => 'account'
 
map.login '/login', :controller => 'account', :action => 'login'
 
map.logout '/logout', :controller => 'account', :action => 'logout'
 
map.signup '/signup', :controller => 'account', :action => 'signup'

A couple of things to note about the above:

I’m using named routes so rails will automatically provide helper methods to reference the urls for these actions (e.g. login_path returns ‘/login’). This is handy and keeps hard-coded URLS out of the code.
I’ve mapped the empty relative path to home which means when someone accesses http://localhost:3000 they will be sent to the index action method of the account controller. However, this only works if you delete public/index.html so be sure to do that!.
One last thing before we can start our app again and see all this in action: I’m going to replace the “funny” blurb provided in app/views/account/index.rhtml with a welcome screen that looks like this:

<% if current_player %>
 
<p>Welcome <%=current_player.login%>!</p>
 
<p><%=link_to 'Logout', logout_path%></p>
 
<% else %>
 
<p>Hello stranger!</p>
 
<p><%=link_to 'Sign Up', signup_path %><br/>
 
<%=link_to 'Login', login_path %>
 
</p>
 
<% end %>

Right, with those changes done, lets restart the app (ruby script/server in case you forgot) and check it out. As there are no players currently, we should be directed automatically to the signup screen where we can register. And if thats working we have our basic PBBG with player sign up and authentication.

Feel free to leave comments and let me know if the above didn’t work for you.