Skip to content

A Rails plugin that allows you to extend a model with Gold, Silver and Copper money as featured in World of Warcraft

License

Notifications You must be signed in to change notification settings

mattistrading/acts_as_gold

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

acts_as_gold

Acts_as_gold extends ActiveRecord models to use a single integer column to store money in the form of Gold, Silver and Copper. This money system can be found in many games, including World of Warcraft.

Written by Ariejan de Vroom

Copyright 2008 Ariejan de Vroom

Download and installation

You can install acts_as_gold either as a Ruby on Rails plugin or a Ruby Gem. It’s recommended you use the Ruby Gem to get easier updates later on.

Plugin installation

Simply install the plugin:

./script/plugin install git://github.com/ariejan/acts_as_gold.git

Gem installation

Just install the gem:

gem install ariejan-acts_as_gold --source http://gems.github.com

Add the following to your environment.rb if you want to use Rails 2.1’s dependency manager (which is highly recommended):

config.gem "ariejan-acts_as_gold",
           :lib    => "acts_as_gold",
           :source => "http://gems.github.com"

Enabling acts_as_gold

This will use player.money to store the current amount of money.

class Player < ActiveRecord::Base
  acts_as_gold	# Uses the +money+ attribute from Player
end

You may also specify a different column for storing money, for example pennies

class Player < ActiveRecord::Base
  acts_as_gold :column => :pennies # Uses the +pennies+ attribute from Player
end

Using acts_as_gold

Acts_as_gold adds two things. If you make a model act as gold, you’ll get three bonus methods: gold, silver and copper.

You can have a maximum of 99 copper and 99 silver. 99 copper becomes 1 silver and 99 silver becomes 1 gold. The amount of gold is limited by the integer type you use.

214,748 Gold, 36 Silver, 47 Copper for a default :integer column (int(11))
922,337,203,685,477 Gold, 58 Silver, 07 Copper for a bigint (int(20)).

player.money = 3005075
player.gold => 300
player.silver => 50
player.copper => 75

A sample migration to add the money column to your model:

add_column :players, :money, :integer, :limit => 20, :default => 15000

This migration allows up to approximately 922,377 billion gold and gives the player 1 gold and 50 silver by default.

Earning and spending

It’s really easy to earn and spend money.

Earning money is no problem. You may want want to set a limit to the maximum amount of money you can have to avoid interger overflows.

player.earn(2.gold + 25.silver)

Spending is also easy. This method return true if the money was spend successfully. It will raise a NotEnoughMoneyError if there’s not enough money to be spend.

player.spend(10.silver + 3.copper)

Fixnum and Bignum extensions

As een in the previous examples, you can use several helper methods on Fixnum and Bignum to convert them to correct money values. You can use these throughout your application:

25.gold
33.silver
78.copper

More Information

Ariejan.net

About

A Rails plugin that allows you to extend a model with Gold, Silver and Copper money as featured in World of Warcraft

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published