class Monotony::PurchasableProperty

Represents any purchasable property tile.

Attributes

cost[RW]
is_mortgaged[RW]
mortgage_value[RW]
owner[RW]
value[RW]

Public Class Methods

new(opts) click to toggle source

@param opts [Hash] @option opts [Integer] :value the value of the property. @option opts [Integer] :mortgage_value the mortgaged value of the property. @option opts [Symbol] :set a symbol identifying this property as a member of a set of properties. @option opts [String] :name the name of the property.

Calls superclass method
# 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

Public Instance Methods

give_to(player) click to toggle source

Gives a property to another player. Available for use as part of a trading behaviour.

# 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?() click to toggle source

@return [Boolean] whether or not the property is currently mortgaged.

# File lib/monotony/purchasable.rb, line 127
def is_mortgaged?
        @is_mortgaged
end
mortgage() click to toggle source

Mortgage the property to raise cash for its owner. @return [self]

# 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) click to toggle source

Returns the number of properties in the set containing self. @return [Integer]

# File lib/monotony/purchasable.rb, line 45
def number_in_set(game = @owner.game)
        properties_in_set(game).count
end
number_of_set_owned() click to toggle source

@return [Integer] the number of properties in the same set as this one which are currenly owned by players.

# 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) click to toggle source

Offer to purchase this property from another player, thus calling the :trade_proposed behaviour of the receiving player.

# 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) click to toggle source

Returns property objects for all properties in the same set as self. @return [Array<Square>]

# 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) click to toggle source

Transfer a property to another player, in return for currency. @param player [Player] Receiving player @param amount [Integer] Sale value

# 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?() click to toggle source

@return [Boolean] whether or not this property is part of a complete set owned by a single player.

# 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() click to toggle source

Unmortgage the property. @return [self]

# 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