Class: Adventure::Purse

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Purse

Create a new instance of Purse.

Parameters:

  • opts (Hash)

    A list of parameters.

Options Hash (**opts):



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

#cpObject (readonly)

Copper pieces (cp).



16
17
18
# File 'lib/adventure/purse.rb', line 16

def cp
  @cp
end

#gpObject (readonly)

Gold pieces (gp).



20
21
22
# File 'lib/adventure/purse.rb', line 20

def gp
  @gp
end

#ppObject (readonly)

Platinum pieces (pp).



22
23
24
# File 'lib/adventure/purse.rb', line 22

def pp
  @pp
end

#spObject (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.

Parameters:

  • args (Hash)

    A Hash with each coin quantity.

Options Hash (**args):

  • :cp (Integer)

    The quantity of Copper Pieces.

  • :sp (Integer)

    The quantity of Silver Pieces.

  • :gp (Integer)

    The quantity of Gold Pieces.

  • :pp (Integer)

    The quantity of Platinum Pieces.



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.

Parameters:

  • total (Float)

    A floating-point gp equivalent.

Returns:

  • (Hash)

    A Hash of quantities of cps, sps, gps, and 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.

Parameters:

  • debt (Float, Hash)

    Either a Float of the gp equivalent of the debt, or a Hash of each coin to be charged.

Raises:

  • (StandardError)

    Throws an error if either the Adventure::Purse doesn't have enough to pay the given debt, or if the given debt is neither a Float or a Hash.



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_equivalentFloat

Gets a Float equivalent of gps based on the current contents of this Adventure::Purse.

Returns:

  • (Float)

    A floating-point equivalent of gps.



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.

Parameters:

  • credit (Float, Hash)

    Either a Float of the gp equivalent of the credit, or a Hash of each coin to be credited.

Raises:

  • (StandardError)

    Throws an error if the given credit is neither a Float or a Hash.



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_aArray

The Adventure::Purse's contents as an array of each coins' quantities.

Returns:

  • (Array)

    An Array containing the quantities, in this order, of cps, sps, gps, and pps.



75
76
77
# File 'lib/adventure/purse.rb', line 75

def to_a
  [@cp, @sp, @gp, @pp]
end

#to_fFloat

Alias for gp_equivalent

Returns:

  • (Float)

    A floating-point equivalent of gps.



59
60
61
# File 'lib/adventure/purse.rb', line 59

def to_f
  gp_equivalent
end

#to_hHash

The Adventure::Purse's contents as a hash of each coins' quantities.

Returns:

  • (Hash)

    A Hash containing the quantities of cps, sps, gps, and pps, each under the equivalent Symbol (e.g. :sp).



83
84
85
# File 'lib/adventure/purse.rb', line 83

def to_h
  { cp: @cp, sp: @sp, gp: @gp, pp: @pp }
end

#to_sString

The Adventure::Purse's contents as a string of each coins' quantities.

Returns:

  • (String)

    A String of each correspondent quantity of cps, sps, gps, and pps.



67
68
69
# File 'lib/adventure/purse.rb', line 67

def to_s
  "#{@cp}cp #{@sp}sp #{@gp}gp #{@pp}pp"
end