Class: Baron::Turn::OperatingTurn

Inherits:
Baron::Turn show all
Defined in:
lib/baron/turn/operating_turn.rb

Overview

An operating turn is taken by the player on behalf of a company

Each company will generally get one turn during each operating round during which they may:

  • Lay tiles

  • Place Tokens

  • Run Trains

  • Purchase Trains

and more

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Baron::Turn

#perform

Constructor Details

#initialize(game, player, company) ⇒ OperatingTurn

Initialize the operating turn

Examples:

Baron::Turn::OperatingTurn.new(game, director, company)

Parameters:


61
62
63
64
65
# File 'lib/baron/turn/operating_turn.rb', line 61

def initialize(game, player, company)
  @game = game
  @company = company
  @player = player
end

Instance Attribute Details

#companyBaron::Company (readonly)

The company performing this operation

Examples:

turn.company

Returns:


30
31
32
# File 'lib/baron/turn/operating_turn.rb', line 30

def company
  @company
end

#gameBaron::Game (readonly)

The game that this operating turn is a part of

Examples:

turn.game

Returns:


21
22
23
# File 'lib/baron/turn/operating_turn.rb', line 21

def game
  @game
end

#playerBaron::Player (readonly)

The player in charge of this operation

This will be the director of the company

Examples:

turn.player

Returns:


41
42
43
# File 'lib/baron/turn/operating_turn.rb', line 41

def player
  @player
end

#run_amountFixnum (readonly)

The amount that the company ran for this operation

Examples:

turn.run_amount

Returns:

  • (Fixnum)

50
51
52
# File 'lib/baron/turn/operating_turn.rb', line 50

def run_amount
  @run_amount
end

Instance Method Details

#available_actionsArray<Baron::Action>

A list of actions that the player can take on behalf of the company

Examples:

turn.available_actions

Returns:


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/baron/turn/operating_turn.rb', line 74

def available_actions
  [
    can_place_tile,
    can_place_token,
    can_run_trains,
    can_payout,
    can_retain,
    can_buy_trains,
    can_done
  ].compact
end

#buytrain(_) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Take an action to buy a train

Parameters:


167
168
# File 'lib/baron/turn/operating_turn.rb', line 167

def buytrain(_)
end

#can_buy_trainsBaron::Action::BuyTrain

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the buy trains action is the company can buy trains


253
254
255
# File 'lib/baron/turn/operating_turn.rb', line 253

def can_buy_trains
  Action::BuyTrain if declared_earnings? && !done?
end

#can_doneBaron::Action::Done

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the done action is the company can declar it is done

Returns:


261
262
263
# File 'lib/baron/turn/operating_turn.rb', line 261

def can_done
  Action::Done if declared_earnings? && !done?
end

#can_payoutBaron::Action::Payout

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the payout action is the company can pay out


237
238
239
# File 'lib/baron/turn/operating_turn.rb', line 237

def can_payout
  Action::Payout if run_trains? && !declared_earnings?
end

#can_place_tileBaron::Action::PlaceTile

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the place tile action is the company can place tiles


210
211
212
213
214
215
# File 'lib/baron/turn/operating_turn.rb', line 210

def can_place_tile
  Action::PlaceTile unless
    count_actions_taken(Action::PlaceTile).equal?(2) ||
    placed_token? ||
    run_trains?
end

#can_place_tokenBaron::Action::PlaceToken

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the place token action is the company can place a token


221
222
223
# File 'lib/baron/turn/operating_turn.rb', line 221

def can_place_token
  Action::PlaceToken unless placed_token? || run_trains?
end

#can_retainBaron::Action::Retain

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the retain action is the company can retain


245
246
247
# File 'lib/baron/turn/operating_turn.rb', line 245

def can_retain
  Action::Retain if can_payout
end

#can_run_trainsBaron::Action::PlaceToken

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the run trains action is the company can run trains


229
230
231
# File 'lib/baron/turn/operating_turn.rb', line 229

def can_run_trains
  Action::RunTrains unless run_trains?
end

#count_actions_taken(klass) ⇒ Fixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Count the number of times the action taken this turn

has not been taken this turn.

Returns:

  • (Fixnum)

    The number of times the action has been taken. 0 if it


175
176
177
# File 'lib/baron/turn/operating_turn.rb', line 175

def count_actions_taken(klass)
  taken_actions.count { |action| action.instance_of? klass }
end

#declared_earnings?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Has this declared what it is going to do with its earnings

Returns:

  • (Boolean)

199
200
201
202
203
204
# File 'lib/baron/turn/operating_turn.rb', line 199

def declared_earnings?
  run_amount.equal?(0) || (
    count_actions_taken(Action::Payout) +
    count_actions_taken(Action::Retain)
  ).positive?
end

#done(_) ⇒ void

This method returns an undefined value.

End the operation turn

This action is called then the current company finishes their turn. Any end of turn effects will be placed here.

Examples:

turn.done

Parameters:


112
113
# File 'lib/baron/turn/operating_turn.rb', line 112

def done(_)
end

#done?Boolean

Is this operating turn over?

The company operating turn is over when they have no more legal actions to perform, or if they have declared that they are done through a done action.

Examples:

turn.done?

Returns:

  • (Boolean)

    Returns true if this turn is done, false otherwise


97
98
99
# File 'lib/baron/turn/operating_turn.rb', line 97

def done?
  count_actions_taken(Action::Done).positive?
end

#payout(_) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Pay out earnings to shareholders

Parameters:


147
148
149
150
151
# File 'lib/baron/turn/operating_turn.rb', line 147

def payout(_)
  payout_amounts.each do |player, amount|
    game.bank.give(player, Money.new(amount)) if amount.positive?
  end
end

#payout_amountsHash<Baron::Player, Fixnup>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The amounts to be paid out to each payer

are to be paid

Returns:

  • (Hash<Baron::Player, Fixnup>)

    The player and amount that they


270
271
272
273
274
# File 'lib/baron/turn/operating_turn.rb', line 270

def payout_amounts
  game.players.map do |player|
    [player, (run_amount * player.percentage_owned(company)).to_int]
  end
end

#placed_token?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Has this company placed a token this turn

Returns:

  • (Boolean)

183
184
185
# File 'lib/baron/turn/operating_turn.rb', line 183

def placed_token?
  count_actions_taken(Action::PlaceToken).positive?
end

#placetile(_) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Place a tile on the board

Parameters:


120
121
122
# File 'lib/baron/turn/operating_turn.rb', line 120

def placetile(_)
  # TODO: Implement placetile
end

#placetoken(_) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Place a token on the board

Parameters:


129
130
131
# File 'lib/baron/turn/operating_turn.rb', line 129

def placetoken(_)
  # TODO: Implement placetoken
end

#retain(_) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Retain earnings to the company

Parameters:


158
159
160
# File 'lib/baron/turn/operating_turn.rb', line 158

def retain(_)
  game.bank.give(company, Money.new(run_amount))
end

#run_trains?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Has this company run trains this turn

Returns:

  • (Boolean)

191
192
193
# File 'lib/baron/turn/operating_turn.rb', line 191

def run_trains?
  count_actions_taken(Action::RunTrains).positive?
end

#runtrains(action) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Run trains this companies operating turn

Parameters:


138
139
140
# File 'lib/baron/turn/operating_turn.rb', line 138

def runtrains(action)
  @run_amount = action.amount
end