Class: Adventure::Store
- Inherits:
-
Object
- Object
- Adventure::Store
- 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
-
#quotes ⇒ Object
readonly
Returns the value of attribute quotes.
-
#shop_type ⇒ Object
readonly
Returns the value of attribute shop_type.
-
#subtitle ⇒ Object
readonly
Returns the value of attribute subtitle.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
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(title, shop_type, stock = nil, **opts) ⇒ Store
constructor
Create a new instance of Store.
-
#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(title, shop_type, stock = nil, **opts) ⇒ Store
Create a new instance of Store.
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
#quotes ⇒ Object (readonly)
Returns the value of attribute quotes.
14 15 16 |
# File 'lib/adventure/store.rb', line 14 def quotes @quotes end |
#shop_type ⇒ Object (readonly)
Returns the value of attribute shop_type.
17 18 19 |
# File 'lib/adventure/store.rb', line 17 def shop_type @shop_type end |
#subtitle ⇒ Object (readonly)
Returns the value of attribute subtitle.
11 12 13 |
# File 'lib/adventure/store.rb', line 11 def subtitle @subtitle end |
#title ⇒ Object (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.
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.
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.
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_equivalent ⇒ Float
Gets a Float equivalent of gps based on the current
contents of this Purse.
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.
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_a ⇒ Array
The Purse's contents as an array of each coins' quantities.
106 107 108 |
# File 'lib/adventure/store.rb', line 106 def to_a [@cp, @sp, @gp, @pp] end |
#to_f ⇒ Float
Alias for gp_equivalent
90 91 92 |
# File 'lib/adventure/store.rb', line 90 def to_f gp_equivalent end |
#to_h ⇒ Hash
The Purse's contents as a hash of each coins' quantities.
114 115 116 |
# File 'lib/adventure/store.rb', line 114 def to_h { cp: @cp, sp: @sp, gp: @gp, pp: @pp } end |
#to_s ⇒ String
The Purse's contents as a string of each coins' quantities.
98 99 100 |
# File 'lib/adventure/store.rb', line 98 def to_s "#{@cp}cp #{@sp}sp #{@gp}gp #{@pp}pp" end |