Healing Your Players (Ruby on Rails)

If you went through the entry on building the bank then this is essentially the same thing again but the code is actually slightly simpler. As before it starts off with generating a controller and an index page to handle the user interface: Healing Your Players (PHP)

> ruby script/generate controller Healer index

We add a new healing function to the User model (app/models/user.rb):

def heal(amount)
  if (amount < 0 or amount > self.gold)
    amount = self.gold
  end
 
  if (amount > (self.max_hp - self.cur_hp))
    amount = self.max_hp - self.cur_hp
  end
 
  self.gold -= amount
  self.cur_hp += amount
  self.save
 
  amount
end

The contents of app/views/healer/index.html.erb:

<p>Welcome to the healer. You currently have 
<strong><%= current_user.cur_hp %></strong> HP out of a maximum of 
<strong><%= current_user.max_hp %></strong>.</p>
 
<p>You have <strong><%= current_user.gold%></strong> gold to heal yourself with, 
and it will cost you <strong>1 gold per HP healed</strong> to heal yourself.</p>
 
<% form_tag('/healer/do_some_healing', :method => :post) do %>
  <%= text_field_tag 'amount' %>
  <%= submit_tag 'Heal Me'%>
<% end %>
<p><%= link_to "Home", :controller => 'welcome' %></p>
 
<script type="text/javascript">
  window.onload = function() {
    document.getElementById('amount').focus();
  }
</script>

And, as always, we pull together our model changes and the view in the controller (app/controllers/healer_controller.rb):

class HealerController < ApplicationController
  def index
  end
 
  def do_some_healing
    # The amount we actually healed might be different than the amount requested
    # due to a variety of factors (i.e. they didn't need that much healing, they
    # didn't have enough gold, etc.) so we record the amount they actually healed
    # which comes back from the method call.
    amount = current_user.heal(params[:amount].to_i)
 
    flash[:notice] = "You have been healed for #{amount} HP."
    render :action => "index"
  end
end

As with the bank and the forest, our finishing touch is to add a link to the destination on the welcome page (app/views/welcome/index.html.erb):

<%= link_to "The Healer", :controller => "healer" %>

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.

Tags: ,

Friday, October 3rd, 2008 buildingbrowsergames, code, medieval
  • Ruby on rails is excellent for Php use. John have you ever tried simplescripts? It's pretty nice for php scripting. I've bookmarked your site keep up the great work. </out>

  • I took a look at simplescripts, and it doesn't seem to be geared towards programming at all - all you do is have them install certain scripts(like wordpress, punbb, and so on) onto your server for you.

    I fail to see how that's 'nice' for php scripting at all, if you're planning on building anything custom.

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