Class: Baron::Rules

Inherits:
Object
  • Object
show all
Defined in:
lib/baron/rules.rb

Overview

Configuration file parsing. This loads the companies, stock market, map tiles, trains and other basic information from the config file.

Defined Under Namespace

Classes: MajorCompanyConfig, PrivateCompanyConfig, YamlConfig

Instance Method Summary collapse

Constructor Details

#initialize(config_file_name) ⇒ Rules

Returns a new instance of Rules.

Examples:

Baron::Rules.new('1860')
# loads the rules from `games/1860.yml`

Parameters:

  • config_file_name (String)

See Also:

  • description

14
15
16
# File 'lib/baron/rules.rb', line 14

def initialize(config_file_name)
  @config ||= YAML.load(File.open("games/#{config_file_name}.yml"))
end

Instance Method Details

#auctionable_companiesArray<Baron::Company>

The auctionable private companies

Examples:

rules.auctionable_companies

Returns:


73
74
75
76
77
78
79
# File 'lib/baron/rules.rb', line 73

def auctionable_companies
  @config.fetch('auction').map do |abbreviation|
    companies.find do |company|
      company.abbreviation.eql? abbreviation
    end
  end
end

#companiesArray<Baron::Company>

Return all companies

This returns all private and major companies

Examples:

rules.companies

Returns:


62
63
64
# File 'lib/baron/rules.rb', line 62

def companies
  private_companies + major_companies
end

#major_companiesArray<Baron::Company::MajorCompany>

The major companies in the rules

Examples:

rules.major_companies
# returns all major comapnies in these rules

Returns:


38
39
40
# File 'lib/baron/rules.rb', line 38

def major_companies
  @major_companies ||= init_companies('major', MajorCompanyConfig)
end

#market_valuesArray<Fixnum>

The stock market values for the game

Examples:

rules.market_values

Returns:

  • (Array<Fixnum>)

118
119
120
# File 'lib/baron/rules.rb', line 118

def market_values
  @config.fetch('stock_market').fetch('values')
end

#number_of_operating_rounds(phase) ⇒ Fixnum

The number of operating rounds for the game phase

Examples:

rules.number_of_operating_rounds(3) # => 2

Returns:

  • (Fixnum)

129
130
131
132
133
# File 'lib/baron/rules.rb', line 129

def number_of_operating_rounds(phase)
  @config.fetch('operating_rounds').find do |round|
    round.fetch('phase').equal?(phase)
  end.fetch('count')
end

#private_companiesArray<Baron::Company::PrivateCompany>

The private companies in the rules

Examples:

rules.private_companies
# returns all private comapnies in these rules

Returns:


26
27
28
# File 'lib/baron/rules.rb', line 26

def private_companies
  @private_companies ||= init_companies('private', PrivateCompanyConfig)
end

#share_configurationArray<BigDecimal>

The configuration of the shares, as defined in the yaml file

configuration. The big decimals should sum to 1.

Examples:

rules.share_configuration
#=> [0.2, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]

Returns:

  • (Array<BigDecimal>)

    An array of big decimals defining the share


90
91
92
93
94
95
96
97
# File 'lib/baron/rules.rb', line 90

def share_configuration
  @config
    .fetch('share_configuration')
    .fetch('major')
    .flat_map do |portion, number|
    Array.new(number) { BigDecimal.new portion }
  end
end

#starting_cash(num_players) ⇒ Baron::Money

The starting cash for this game for the number of players

Examples:

rules.starting_cash #=> $540

Parameters:

  • num_players (Fixnum)

    The number of players in this game

Returns:

  • (Baron::Money)

    The money that each player starts the game with.


107
108
109
# File 'lib/baron/rules.rb', line 107

def starting_cash(num_players)
  Money.new @config.fetch('starting_money').fetch(num_players)
end

#trainsArray<Baron::Train>

All of the trains used in the game

Examples:

rules.trains

Returns:


49
50
51
# File 'lib/baron/rules.rb', line 49

def trains
  @trains ||= init_trains
end