Class: Adventure::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/adventure/store.rb

Overview

Represents an ingame store.

Defined Under Namespace

Modules: ShopTypes

Constant Summary collapse

DEFAULT_QUOTES =

A list of generic quotes to fill if none are given.

[
  'Take your time, traveler. Good coin is always welcome here, no matter where it was minted.',
  'Ah, back from the wilds? You look like you\'ve seen some things—and like you need what I\'m selling.',
  'Quality is never cheap, friend, but it\'s always less expensive than replacing it out on the road.',
  'Look all you want, but remember: if you break it, you bought it. And my guards aren\'t known for their patience.',
  'The finest craftsmanship in the realm, right here under one roof! You won\'t find better prices from here to the capital.',
  'Step right up! Don\'t let the dust fool you; everything here is in perfect, working order.',
  'A wise coin spent now saves a drop of blood later. What catches your eye?',
  'I don\'t ask where you got that gold, and you don\'t ask where I got these goods. Do we have a deal?',
  'Mend it, replace it, or upgrade it—whatever your journey requires, I can provide.',
  'You get what you pay for around here. No haggling, no exceptions.',
  'The roads ahead are dangerous, stranger. It\'s best not to go empty-handed.',
  'Just arrived this morning from the eastern trade routes! Get it before the local garrison buys me out.',
  'Blessings of the hearth upon you. Have a look around, and let me know when you\'re ready to trade.'
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, shop_type, stock = nil, **opts) ⇒ Store

Create a new instance of Store.

Parameters:

  • opts (Hash)

    A list of parameters.

Options Hash (**opts):

  • :total (Float)

    The gp equivalent of the Purse contents.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/adventure/store.rb', line 60

def initialize(title, shop_type, stock = nil, **opts)
  # Mandatory parameters:
  @title     = title.strip
  @shop_type = shop_type.strip
  @stock     = stock
  # Parse stock given:
  @stock.filter! { |i| i.is_a? Adventure::Item } # Remove non-Item elements.
  @stock     = [] if @stock.nil? || @stock.empty? # If no Items or nil as stock, fill with EMPTY stock.
  # Optional and not-quite-optional parameters:
  @subtitle  = opts.key?(:subtitle) ? opts[:subtitle].strip : ''
  @buys      = opts.key?(:buys) ? opts[:buys] : false
  @sells     = opts.key?(:sells) ? opts[:sells] : true
  @quotes    = opts.key?(:quotes) ? opts[:quotes].to_a : DEFAULT_QUOTES
end

Instance Attribute Details

#quotesObject (readonly)

Returns the value of attribute quotes.



14
15
16
# File 'lib/adventure/store.rb', line 14

def quotes
  @quotes
end

#shop_typeObject (readonly)

Returns the value of attribute shop_type.



17
18
19
# File 'lib/adventure/store.rb', line 17

def shop_type
  @shop_type
end

#subtitleObject (readonly)

Returns the value of attribute subtitle.



11
12
13
# File 'lib/adventure/store.rb', line 11

def subtitle
  @subtitle
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/adventure/store.rb', line 8

def title
  @title
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.



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/adventure/store.rb', line 195

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.



176
177
178
179
180
181
182
183
184
185
# File 'lib/adventure/store.rb', line 176

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 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 Purse doesn't have enough to pay the given debt, or if the given debt is neither a Float or a Hash.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/adventure/store.rb', line 122

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 Purse.

Returns:

  • (Float)

    A floating-point equivalent of gps.



79
80
81
82
83
84
85
# File 'lib/adventure/store.rb', line 79

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 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.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/adventure/store.rb', line 147

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 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.



106
107
108
# File 'lib/adventure/store.rb', line 106

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

#to_fFloat

Alias for gp_equivalent

Returns:

  • (Float)

    A floating-point equivalent of gps.



90
91
92
# File 'lib/adventure/store.rb', line 90

def to_f
  gp_equivalent
end

#to_hHash

The 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).



114
115
116
# File 'lib/adventure/store.rb', line 114

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

#to_sString

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

Returns:

  • (String)

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



98
99
100
# File 'lib/adventure/store.rb', line 98

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