Class: Adventure::Purse
- Inherits:
-
Object
- Object
- Adventure::Purse
- Defined in:
- lib/adventure/purse.rb
Overview
Represents a coin storage ingame.
The base Purse is capable of processing only the four 10-based D&D default coins, i.e.:
- Copper Pieces (
100cp=1gp); - Silver Pieces (
10sp=1gp); - Gold Pieces (
gp); and - Platinum Pieces (
1pp=10gp).
Instance Attribute Summary collapse
-
#cp ⇒ Object
readonly
Copper pieces (
cp). -
#gp ⇒ Object
readonly
Gold pieces (
gp). -
#pp ⇒ Object
readonly
Platinum pieces (
pp). -
#sp ⇒ Object
readonly
Silver pieces (
sp).
Class Method Summary collapse
-
.parse_coins(**args) ⇒ Object
Turn the given coins quantities in their corresponding
gpequivalent. -
.parse_total(total) ⇒ Hash
Turn Gold Pieces (
gp) equivalent to Hash of each lesser and greater coins.
Instance Method Summary collapse
-
#charge(debt) ⇒ Object
Charge a given quantity from this Purse.
-
#gp_equivalent ⇒ Float
Gets a Float equivalent of
gps based on the current contents of this Purse. -
#initialize(**opts) ⇒ Purse
constructor
Create a new instance of Purse.
-
#receive(credit) ⇒ Object
Adds a given quantity to this Purse.
-
#to_a ⇒ Array
The Purse's contents as an array of each coins' quantities.
-
#to_f ⇒ Float
Alias for gp_equivalent.
-
#to_h ⇒ Hash
The Purse's contents as a hash of each coins' quantities.
-
#to_s ⇒ String
The Purse's contents as a string of each coins' quantities.
Constructor Details
#initialize(**opts) ⇒ Purse
Create a new instance of Purse.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/adventure/purse.rb', line 28 def initialize(**opts) if opts.key?(:total) @cp, @sp, @gp, @pp = self.class.parse_total(opts[:total].to_f).values elsif opts.key?(:cp) || opts.key?(:sp) || opts.key?(:gp) || opts.key?(:pp) @cp = opts.key?(:cp) ? opts[:cp].to_i : 0 @sp = opts.key?(:sp) ? opts[:sp].to_i : 0 @gp = opts.key?(:gp) ? opts[:gp].to_i : 0 @pp = opts.key?(:pp) ? opts[:pp].to_i : 0 else @cp = 0 @sp = 0 @gp = 0 @pp = 0 end end |
Instance Attribute Details
#cp ⇒ Object (readonly)
Copper pieces (cp).
16 17 18 |
# File 'lib/adventure/purse.rb', line 16 def cp @cp end |
#gp ⇒ Object (readonly)
Gold pieces (gp).
20 21 22 |
# File 'lib/adventure/purse.rb', line 20 def gp @gp end |
#pp ⇒ Object (readonly)
Platinum pieces (pp).
22 23 24 |
# File 'lib/adventure/purse.rb', line 22 def pp @pp end |
#sp ⇒ Object (readonly)
Silver pieces (sp).
18 19 20 |
# File 'lib/adventure/purse.rb', line 18 def sp @sp end |
Class Method Details
.parse_coins(**args) ⇒ Object
Turn the given coins quantities in their corresponding
gp equivalent.
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/adventure/purse.rb', line 164 def self.parse_coins(**args) cp = args.key?(:cp) ? args[:cp].to_f : 0.0 sp = args.key?(:sp) ? args[:sp].to_f : 0.0 gp = args.key?(:gp) ? args[:gp].to_f : 0.0 pp = args.key?(:pp) ? args[:pp].to_f : 0.0 total = gp total += cp / 100.0 total += sp / 10.0 total += pp * 10.0 total end |
.parse_total(total) ⇒ Hash
Turn Gold Pieces (gp) equivalent to Hash of
each lesser and greater coins.
While there are many ways to turn a gp
equivalent to each of the four basic D&D coins,
this method assumes the minimum of each of the lesser
coins (cp, sp, and gp), and all the remainder
of the value being represented in pps.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/adventure/purse.rb', line 145 def self.parse_total(total) cp = (total * 100).to_i sp = cp / 10 cp -= sp * 10 gp = sp / 10 sp -= gp * 10 pp = gp / 10 gp -= pp * 10 { cp: cp, sp: sp, gp: gp, pp: pp } end |
Instance Method Details
#charge(debt) ⇒ Object
Charge a given quantity from this Adventure::Purse.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/adventure/purse.rb', line 91 def charge(debt) # Get total equivalent in `gp`s. money = gp_equivalent # Parse given debt according to its type. if debt.is_a? Hash debt = self.class.parse_coins(**debt) elsif debt.is_a? Numeric debt = debt.to_f else raise StandardError, 'Invalid debt.' end # Raise an error if the money is insuficient to pay # the debt. raise StandardError, 'Insuficient money error.' if money < debt # If the money is suficient, begin charging. new_money = money - debt @cp, @sp, @gp, @pp = self.class.parse_total(new_money).values end |
#gp_equivalent ⇒ Float
Gets a Float equivalent of gps based on the current
contents of this Adventure::Purse.
48 49 50 51 52 53 54 |
# File 'lib/adventure/purse.rb', line 48 def gp_equivalent total = @cp / 100.00 total += @sp / 10.0 total += @gp total += @pp * 10 total end |
#receive(credit) ⇒ Object
Adds a given quantity to this Adventure::Purse.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/adventure/purse.rb', line 116 def receive(credit) # Parse given credit according to its type if credit.is_a? Hash @cp += credit.key?(:cp) ? credit[:cp].to_i.abs : 0 @sp += credit.key?(:sp) ? credit[:sp].to_i.abs : 0 @gp += credit.key?(:gp) ? credit[:gp].to_i.abs : 0 @pp += credit.key?(:pp) ? credit[:pp].to_i.abs : 0 elsif credit.is_a? Numeric new_cp, new_sp, new_gp, new_pp = self.class.parse_total(credit).values.map(&:abs) @cp += new_cp @sp += new_sp @gp += new_gp @pp += new_pp else raise StandardError, 'Invalid credit.' end end |
#to_a ⇒ Array
The Adventure::Purse's contents as an array of each coins' quantities.
75 76 77 |
# File 'lib/adventure/purse.rb', line 75 def to_a [@cp, @sp, @gp, @pp] end |
#to_f ⇒ Float
Alias for gp_equivalent
59 60 61 |
# File 'lib/adventure/purse.rb', line 59 def to_f gp_equivalent end |
#to_h ⇒ Hash
The Adventure::Purse's contents as a hash of each coins' quantities.
83 84 85 |
# File 'lib/adventure/purse.rb', line 83 def to_h { cp: @cp, sp: @sp, gp: @gp, pp: @pp } end |
#to_s ⇒ String
The Adventure::Purse's contents as a string of each coins' quantities.
67 68 69 |
# File 'lib/adventure/purse.rb', line 67 def to_s "#{@cp}cp #{@sp}sp #{@gp}gp #{@pp}pp" end |