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
Returns the value of attribute cp.
-
#gp ⇒ Object
readonly
Returns the value of attribute gp.
-
#pp ⇒ Object
readonly
Returns the value of attribute pp.
-
#sp ⇒ Object
readonly
Returns the value of attribute 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/adventure/purse.rb', line 32 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)
Returns the value of attribute cp.
17 18 19 |
# File 'lib/adventure/purse.rb', line 17 def cp @cp end |
#gp ⇒ Object (readonly)
Returns the value of attribute gp.
23 24 25 |
# File 'lib/adventure/purse.rb', line 23 def gp @gp end |
#pp ⇒ Object (readonly)
Returns the value of attribute pp.
26 27 28 |
# File 'lib/adventure/purse.rb', line 26 def pp @pp end |
#sp ⇒ Object (readonly)
Returns the value of attribute sp.
20 21 22 |
# File 'lib/adventure/purse.rb', line 20 def sp @sp end |
Class Method Details
.parse_coins(**args) ⇒ Object
Turn the given coins quantities in their corresponding
gp equivalent.
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/adventure/purse.rb', line 168 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.
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/adventure/purse.rb', line 149 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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/adventure/purse.rb', line 95 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.
52 53 54 55 56 57 58 |
# File 'lib/adventure/purse.rb', line 52 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.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/adventure/purse.rb', line 120 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.
79 80 81 |
# File 'lib/adventure/purse.rb', line 79 def to_a [@cp, @sp, @gp, @pp] end |
#to_f ⇒ Float
Alias for gp_equivalent
63 64 65 |
# File 'lib/adventure/purse.rb', line 63 def to_f gp_equivalent end |
#to_h ⇒ Hash
The Adventure::Purse's contents as a hash of each coins' quantities.
87 88 89 |
# File 'lib/adventure/purse.rb', line 87 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.
71 72 73 |
# File 'lib/adventure/purse.rb', line 71 def to_s "#{@cp}cp #{@sp}sp #{@gp}gp #{@pp}pp" end |