Class: Monotony::PurchasableProperty
- Defined in:
- lib/monotony/purchasable.rb
Overview
Represents any purchasable property tile.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cost ⇒ Integer
The current cost to either purchase this property unowned, or unmortgage this property if mortgaged.
-
#is_mortgaged ⇒ Object
Returns the value of attribute is_mortgaged.
-
#mortgage_value ⇒ Object
Returns the value of attribute mortgage_value.
-
#owner ⇒ Object
Returns the value of attribute owner.
-
#value ⇒ Object
Returns the value of attribute value.
Attributes inherited from Square
Instance Method Summary collapse
-
#give_to(player) ⇒ Object
Gives a property to another player.
-
#initialize(opts) ⇒ PurchasableProperty
constructor
A new instance of PurchasableProperty.
-
#is_mortgaged? ⇒ Boolean
Whether or not the property is currently mortgaged.
-
#mortgage ⇒ self
Mortgage the property to raise cash for its owner.
-
#number_in_set(game = @owner.game) ⇒ Integer
Returns the number of properties in the set containing self.
-
#number_of_set_owned ⇒ Integer
The number of properties in the same set as this one which are currenly owned by players.
-
#place_offer(proposer, amount) ⇒ Object
Offer to purchase this property from another player, thus calling the :trade_proposed behaviour of the receiving player.
-
#properties_in_set(game = @owner.game) ⇒ Array<Square>
Returns property objects for all properties in the same set as self.
-
#sell_to(player, amount = cost) ⇒ Object
Transfer a property to another player, in return for currency.
-
#set_owned? ⇒ Boolean
Whether or not this property is part of a complete set owned by a single player.
-
#unmortgage ⇒ self
Unmortgage the property.
Constructor Details
#initialize(opts) ⇒ PurchasableProperty
Returns a new instance of PurchasableProperty.
12 13 14 15 16 17 18 |
# File 'lib/monotony/purchasable.rb', line 12 def initialize(opts) super @game = nil @is_mortgaged = false @value = opts[:value] @mortgage_value = opts[:mortgage_value] end |
Instance Attribute Details
#cost ⇒ Integer
Returns the current cost to either purchase this property unowned, or unmortgage this property if mortgaged.
56 57 58 |
# File 'lib/monotony/purchasable.rb', line 56 def cost @cost end |
#is_mortgaged ⇒ Object
Returns the value of attribute is_mortgaged.
6 7 8 |
# File 'lib/monotony/purchasable.rb', line 6 def is_mortgaged @is_mortgaged end |
#mortgage_value ⇒ Object
Returns the value of attribute mortgage_value.
6 7 8 |
# File 'lib/monotony/purchasable.rb', line 6 def mortgage_value @mortgage_value end |
#owner ⇒ Object
Returns the value of attribute owner.
6 7 8 |
# File 'lib/monotony/purchasable.rb', line 6 def owner @owner end |
#value ⇒ Object
Returns the value of attribute value.
6 7 8 |
# File 'lib/monotony/purchasable.rb', line 6 def value @value end |
Instance Method Details
#give_to(player) ⇒ Object
Gives a property to another player. Available for use as part of a trading behaviour.
90 91 92 93 94 95 |
# File 'lib/monotony/purchasable.rb', line 90 def give_to(player) puts '[%s] Gave %s to %s' % [ @owner.name, @name, player.name ] @owner.properties.delete self @owner = player player.properties << self end |
#is_mortgaged? ⇒ Boolean
Returns whether or not the property is currently mortgaged.
127 128 129 |
# File 'lib/monotony/purchasable.rb', line 127 def is_mortgaged? @is_mortgaged end |
#mortgage ⇒ self
Mortgage the property to raise cash for its owner.
99 100 101 102 103 104 105 106 107 |
# File 'lib/monotony/purchasable.rb', line 99 def mortgage unless is_mortgaged? puts '[%s] Mortgaged %s for £%d' % [ @owner.name, @name, @mortgage_value ] @is_mortgaged = true @owner.currency = @owner.currency + @mortgage_value @mortgage_value end self end |
#number_in_set(game = @owner.game) ⇒ Integer
Returns the number of properties in the set containing self.
45 46 47 |
# File 'lib/monotony/purchasable.rb', line 45 def number_in_set(game = @owner.game) properties_in_set(game).count end |
#number_of_set_owned ⇒ Integer
Returns the number of properties in the same set as this one which are currenly owned by players.
65 66 67 68 69 |
# File 'lib/monotony/purchasable.rb', line 65 def number_of_set_owned if @owner @owner.properties.select { |p| p.is_a? self.class }.select { |p| p.set == @set }.count end end |
#place_offer(proposer, amount) ⇒ Object
Offer to purchase this property from another player, thus calling the :trade_proposed behaviour of the receiving player.
72 73 74 |
# File 'lib/monotony/purchasable.rb', line 72 def place_offer(proposer, amount) @owner.behaviour[:trade_proposed].call(@owner.game, @owner, proposer, self, amount) if proposer.currency >= amount end |
#properties_in_set(game = @owner.game) ⇒ Array<Square>
Returns property objects for all properties in the same set as self.
51 52 53 |
# File 'lib/monotony/purchasable.rb', line 51 def properties_in_set(game = @owner.game) game.board.select { |p| p.is_a? self.class }.select { |p| p.set == @set } end |
#sell_to(player, amount = cost) ⇒ Object
Transfer a property to another player, in return for currency.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/monotony/purchasable.rb', line 23 def sell_to(player, amount = cost) if player.currency < amount then puts '[%s] Unable to buy %s! (short of cash by £%d)' % [ player.name, @name, ( amount - player.currency ) ] false else player.currency = player.currency - amount.to_i if @owner @owner.currency = @owner.currency + amount puts '[%s] Sold %s%s to %s for £%d (new balance: £%d)' % [ @owner.name, @name, (is_mortgaged? ? ' (mortgaged)' : ''), player.name, amount, @owner.currency ] @owner.properties.delete self else puts '[%s] Purchased %s%s for £%d (new balance: £%d)' % [ player.name, @name, (is_mortgaged? ? ' (mortgaged)' : ''), amount, player.currency ] end player.properties << self @owner = player end end |
#set_owned? ⇒ Boolean
Returns whether or not this property is part of a complete set owned by a single player.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/monotony/purchasable.rb', line 77 def set_owned? if @owner player_basic_properties = @owner.properties.select { |p| p.is_a? self.class } board_basic_properties = @owner.game.board.select { |p| p.is_a? self.class } player_properties_in_set = player_basic_properties.select { |p| p.set == @set and p.is_mortgaged? == false } board_properties_in_set = board_basic_properties.select { |p| p.set == @set } (board_properties_in_set - player_properties_in_set).empty? else false end end |
#unmortgage ⇒ self
Unmortgage the property.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/monotony/purchasable.rb', line 111 def unmortgage if is_mortgaged? if @owner.currency > cost puts '[%s] Unmortgaged %s for £%d' % [ @owner.name, @name, cost ] @owner.currency = @owner.currency - cost @is_mortgaged = false else puts '[%s] Unable to unmortgage %s (not enough funds)' % [ @owner.name, @name ] end else puts '[%] Tried to unmortgage a non-mortgaged property (%s)' % [ @owner.name, @name ] end self end |