Class: Hlockey::League

Inherits:
Object
  • Object
show all
Defined in:
lib/hlockey/league.rb

Overview

The Hlockey League

Constant Summary collapse

GAMES_IN_REGULAR_SEASON =
111

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(infinite:, start_time:, divisions:) ⇒ League

Returns a new instance of League.

Parameters:

  • start_time (Array<Integer>)

    parameters passed to Time.new

  • divisions (Hash<Symbol => Array<Hash>>)

    division data

  • infinite (Boolean)

    if the league is infinite, rather than a single season



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hlockey/league.rb', line 45

def initialize(infinite:, start_time:, divisions:)
  @infinite = infinite
  @start_time = Time.utc(*start_time).localtime
  @divisions = divisions.transform_values { |teams| teams.map { Team.new(**_1) } }

  @day = 0
  @season = infinite ? "?" : VERSION

  @games = []
  @games_in_progress = []
  @alerts = []

  @champion_team = nil

  @last_update_time = @start_time
  @passed_updates = 0

  @prng = Utils::Rng.new(@start_time.to_i)

  @teams = @divisions.values.flatten
  @sorted_teams_by_wins = @teams.shuffle(random: @prng)
  @playoff_teams = []

  @game_in_matchup = @matchup_game_amt = 3

  return if @infinite

  # warm vs warm / cool vs cool
  rotated_divisions = @divisions.values.rotate
  @shuffled_division_pairs = Array.new(2) do |i|
    (rotated_divisions[i] + rotated_divisions[-i - 1]).shuffle(random: @prng)
  end
end

Instance Attribute Details

#alertsArray<String> (readonly)

Returns:

  • (Array<String>)


34
35
36
# File 'lib/hlockey/league.rb', line 34

def alerts
  @alerts
end

#champion_teamTeam? (readonly)

Returns:



37
38
39
# File 'lib/hlockey/league.rb', line 37

def champion_team
  @champion_team
end

#dayInteger (readonly)

Returns:

  • (Integer)


25
26
27
# File 'lib/hlockey/league.rb', line 25

def day
  @day
end

#divisionsHash<Symbol => Array<Team>> (readonly)

Returns:

  • (Hash<Symbol => Array<Team>>)


22
23
24
# File 'lib/hlockey/league.rb', line 22

def divisions
  @divisions
end

#gamesArray<Game> (readonly)

Returns:



31
32
33
# File 'lib/hlockey/league.rb', line 31

def games
  @games
end

#games_in_progressArray<Game> (readonly)

Returns:



31
32
33
# File 'lib/hlockey/league.rb', line 31

def games_in_progress
  @games_in_progress
end

#infiniteBoolean (readonly) Also known as: infinite?

Returns:

  • (Boolean)


15
16
17
# File 'lib/hlockey/league.rb', line 15

def infinite
  @infinite
end

#playoff_teamsArray<Team> (readonly)

Returns:



40
41
42
# File 'lib/hlockey/league.rb', line 40

def playoff_teams
  @playoff_teams
end

#seasonString (readonly)

Returns:

  • (String)


28
29
30
# File 'lib/hlockey/league.rb', line 28

def season
  @season
end

#start_timeTime (readonly)

Returns:

  • (Time)


19
20
21
# File 'lib/hlockey/league.rb', line 19

def start_time
  @start_time
end

#teamsArray<Team> (readonly)

Returns:



40
41
42
# File 'lib/hlockey/league.rb', line 40

def teams
  @teams
end

Instance Method Details

#to_h(simple: false) ⇒ Hash

Returns:

  • (Hash)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/hlockey/league.rb', line 80

def to_h(simple: false)
  res = {
    infinite: @infinite,
    start_time: @start_time.getutc.to_a.first(6).reverse,
    divisions: @divisions.transform_values { |d| d.map { |t| t.to_h(simple:) } }
  }

  unless simple
    # At this point, we don't need to pass simple to any of the to_h calls,
    # because we know it is false
    res[:last_update_time] = @last_update_time.getutc.to_a.first(6).reverse
    res[:day] = @day
    res[:games] = @games.map(&:to_h)
    res[:games_in_progress] = @games_in_progress.map(&:to_h)
    res[:alerts] = @alerts
    res[:champion_team] = @champion_team&.name
    res[:teams] = @teams.map(&:to_h)
    res[:playoff_teams] = @playoff_teams.map(&:to_h)
  end

  res
end

#update_stateObject

Updates the league to the current state This should be called whenever you need the current state of the league



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hlockey/league.rb', line 105

def update_state
  return if @champion_team

  now_i = Time.now.to_i
  now = Time.at(now_i - (now_i % UPDATE_FREQUENCY_SECONDS))
  intervals = (now - @last_update_time).div(UPDATE_FREQUENCY_SECONDS)

  return unless intervals.positive?

  intervals.times do |i|
    if !@infinite && ((i + @passed_updates) % UPDATES_PER_HOUR).zero?
      update_teams
      new_games
      next
    end

    update_games

    new_games if @infinite && @games_in_progress.empty?

    break if @champion_team
  end

  @last_update_time = now
  @passed_updates += intervals
end