Skip to content

Commit

Permalink
fix 11: all pass in auctions
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Apr 12, 2020
1 parent e59592c commit 1c045ae
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 33 deletions.
4 changes: 2 additions & 2 deletions lib/engine/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module Engine
class Company
include Ownable

attr_accessor :revenue
attr_reader :name, :sym, :value, :desc
attr_accessor :revenue, :value
attr_reader :name, :sym, :desc

def initialize(name:, value:, revenue: 0, desc: '', sym: '', abilities: [])
@name = name
Expand Down
4 changes: 2 additions & 2 deletions lib/engine/game/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def next_round!
case @round
when Round::Auction
rotate_players(@round.last_to_act)
@companies.all?(&:owner) ? new_stock_round : new_operating_round
new_stock_round
when Round::Stock
rotate_players(@round.last_to_act)
new_operating_round
Expand All @@ -317,7 +317,7 @@ def next_round!
else
@turn += 1
@operating_rounds = @phase.operating_rounds
@companies.all?(&:owner) ? new_stock_round : new_auction_round
new_stock_round
end
else
raise "Unexected round type #{@round}"
Expand Down
68 changes: 49 additions & 19 deletions lib/engine/round/auction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Auction < Base

def initialize(entities, game:, min_increment: 5)
super
@companies = game.companies.reject(&:owner).sort_by(&:value)
@companies = game.companies.sort_by(&:value)
@cheapest = @companies.first
@bank = game.bank
@min_increment = min_increment

Expand All @@ -28,19 +29,7 @@ def description
end

def finished?
@companies.empty? || @entities.all?(&:passed?)
end

def change_entity(_action)
if (bids = @bids[@auctioning_company]).any?
@current_entity = bids.min_by(&:price).entity
else
# if someone bought a share outright, then we find the next person who hasn't passed
loop do
@current_entity = next_entity
break if !@current_entity.passed? || finished?
end
end
@companies.empty?
end

def min_bid(company)
Expand All @@ -65,6 +54,10 @@ def may_bid?(company)

private

def all_passed?
@entities.all?(&:passed?)
end

def _process_action(bid)
if @auctioning_company
add_bid(bid)
Expand All @@ -77,6 +70,39 @@ def action_processed(action)
action.entity.unpass!
end

def change_entity(_action)
if (bids = @bids[@auctioning_company]).any?
@current_entity = bids.min_by(&:price).entity
else
# if someone bought a share outright, then we find the next person who hasn't passed
loop do
@current_entity = next_entity
break if !@current_entity.passed? || all_passed?
end
end
end

def action_finalized(_action)
return if !all_passed? || finished?

@entities.each(&:unpass!)

if @companies.include?(@cheapest)
value = @cheapest.value
@cheapest.value -= 5
new_value = @cheapest.value
@log << "#{@cheapest.name} value decreases from $#{value} to $#{new_value}"

if new_value <= 0
buy_company(@current_entity, @cheapest, 0)
resolve_bids
change_entity(nil)
end
else
payout_companies
end
end

def pass_processed(_action)
@bids[@auctioning_company]&.reject! { |bid| bid.entity == @current_entity }
resolve_bids
Expand Down Expand Up @@ -112,13 +138,9 @@ def accept_bid(bid)
price = bid.price
company = bid.company
player = bid.entity
company.owner = player
player.companies << company
player.spend(price, @bank)
@companies.delete(company)
buy_company(player, company, price)
@bids.delete(company)
@auctioning_company = nil
@log << "#{player.name} buys #{company.name} for $#{price}"
end

def add_bid(bid)
Expand All @@ -127,6 +149,14 @@ def add_bid(bid)
bids << bid
@log << "#{bid.entity.name} bids $#{bid.price} for #{bid.company.name}"
end

def buy_company(player, company, price)
company.owner = player
player.companies << company
player.spend(price, @bank)
@companies.delete(company)
@log << "#{player.name} buys #{company.name} for $#{price}"
end
end
end
end
9 changes: 9 additions & 0 deletions lib/engine/round/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ def presidential_share_swap(corporation, new_p, old_p = nil, p_share = nil)
@log << "#{new_p.name} becomes the president of #{corporation.name}"
end

def payout_companies
@game.companies.select(&:owner).each do |company|
owner = company.owner
revenue = company.revenue
@game.bank.spend(revenue, owner)
@log << "#{owner.name} collects $#{revenue} from #{company.name}"
end
end

def log_share_price(entity, from)
to = entity.share_price.price
@log << "#{entity.name}'s share price changes from $#{from} to $#{to}" if from != to
Expand Down
11 changes: 1 addition & 10 deletions lib/engine/round/operating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def initialize(entities, game:, round_num: 1)
@step = self.class::STEPS.first
@current_routes = []

companies_payout
payout_companies
place_home_stations
end

Expand Down Expand Up @@ -140,15 +140,6 @@ def next_step!
end
end

def companies_payout
@companies.select(&:owner).each do |company|
owner = company.owner
revenue = company.revenue
@bank.spend(revenue, owner)
@log << "#{owner.name} collects $#{revenue} from #{company.name}"
end
end

def place_home_stations
@entities.each do |corporation|
hex = @hexes.find { |h| h.coordinates == corporation.coordinates }
Expand Down

0 comments on commit 1c045ae

Please sign in to comment.