Class: Basketball::Season::Coordinator
- Inherits:
-
Entity
- Object
- Entity
- Basketball::Season::Coordinator
show all
- Extended by:
- Forwardable
- Defined in:
- lib/basketball/season/coordinator.rb
Overview
Main iterator-based object that knows how to manage a calendar and simulate games per day.
Defined Under Namespace
Classes: AlreadyPlayedGameError, GameNotCurrentError, OutOfBoundsError, PlayedGamesError, UnknownGameError, UnplayedGamesError
Instance Attribute Summary collapse
Attributes inherited from Entity
#id
Instance Method Summary
collapse
Methods inherited from Entity
#<=>, #==, #comparable_id, #hash, #to_s
Constructor Details
#initialize(calendar:, league:, current_date:, results: []) ⇒ Coordinator
Returns a new instance of Coordinator.
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/basketball/season/coordinator.rb', line 32
def initialize(calendar:, league:, current_date:, results: [])
super()
raise ArgumentError, 'calendar is required' unless calendar
raise ArgumentError, 'current_date is required' if current_date.to_s.empty?
raise ArgumentError, 'league is required' unless league
@calendar = calendar
@current_date = current_date
@arena = Arena.new
@results = []
@league = league
results.each { |result| replay!(result) }
assert_current_date
assert_all_past_dates_are_played
assert_all_future_dates_arent_played
assert_all_known_teams
end
|
Instance Attribute Details
#arena ⇒ Object
Returns the value of attribute arena.
16
17
18
|
# File 'lib/basketball/season/coordinator.rb', line 16
def arena
@arena
end
|
#calendar ⇒ Object
Returns the value of attribute calendar.
16
17
18
|
# File 'lib/basketball/season/coordinator.rb', line 16
def calendar
@calendar
end
|
#current_date ⇒ Object
Returns the value of attribute current_date.
16
17
18
|
# File 'lib/basketball/season/coordinator.rb', line 16
def current_date
@current_date
end
|
#league ⇒ Object
Returns the value of attribute league.
16
17
18
|
# File 'lib/basketball/season/coordinator.rb', line 16
def league
@league
end
|
#results ⇒ Object
Returns the value of attribute results.
16
17
18
|
# File 'lib/basketball/season/coordinator.rb', line 16
def results
@results
end
|
Instance Method Details
#add!(game) ⇒ Object
135
136
137
138
139
140
141
|
# File 'lib/basketball/season/coordinator.rb', line 135
def add!(game)
assert_today_or_in_future(game)
calendar.add!(game)
self
end
|
#current_games ⇒ Object
123
124
125
|
# File 'lib/basketball/season/coordinator.rb', line 123
def current_games
games_for(date: current_date) - results.map(&:game)
end
|
#days_left ⇒ Object
103
104
105
|
# File 'lib/basketball/season/coordinator.rb', line 103
def days_left
(regular_end_date - current_date).to_i
end
|
#done? ⇒ Boolean
127
128
129
|
# File 'lib/basketball/season/coordinator.rb', line 127
def done?
current_date == regular_end_date && games.length == results.length
end
|
#exhibition_results ⇒ Object
153
154
155
|
# File 'lib/basketball/season/coordinator.rb', line 153
def exhibition_results
results.select { |result| result.game.is_a?(Exhibition) }
end
|
#exhibitions_left ⇒ Object
111
112
113
|
# File 'lib/basketball/season/coordinator.rb', line 111
def exhibitions_left
total_exhibitions - exhibition_result_events.length
end
|
#not_done? ⇒ Boolean
131
132
133
|
# File 'lib/basketball/season/coordinator.rb', line 131
def not_done?
!done?
end
|
#regular_results ⇒ Object
149
150
151
|
# File 'lib/basketball/season/coordinator.rb', line 149
def regular_results
results.select { |result| result.game.is_a?(Regular) }
end
|
#regulars_left ⇒ Object
119
120
121
|
# File 'lib/basketball/season/coordinator.rb', line 119
def regulars_left
total_regulars - regular_result_events.length
end
|
#release!(player) ⇒ Object
53
54
55
|
# File 'lib/basketball/season/coordinator.rb', line 53
def release!(player)
tap { league.release!(player) }
end
|
#result_for(game) ⇒ Object
143
144
145
146
147
|
# File 'lib/basketball/season/coordinator.rb', line 143
def result_for(game)
results.find do |result|
result.game == game
end
end
|
#sign!(player:, team:) ⇒ Object
57
58
59
|
# File 'lib/basketball/season/coordinator.rb', line 57
def sign!(player:, team:)
tap { league.sign!(player:, team:) }
end
|
#sim! ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/basketball/season/coordinator.rb', line 73
def sim!
raise ArgumentError, 'league is required' unless league
return [] if done?
events = []
games = games_for(date: current_date)
games.each do |game|
home_players = opponent_team(game.home_opponent).players
away_players = opponent_team(game.away_opponent).players
matchup = Matchup.new(game:, home_players:, away_players:)
event = arena.play(matchup)
play!(event)
yield(event) if block_given?
events << event
end
increment_current_date!
events
end
|
#sim_rest! ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/basketball/season/coordinator.rb', line 61
def sim_rest!(&)
events = []
while not_done?
new_events = sim!(&)
events += new_events
end
events
end
|
#total_days ⇒ Object
99
100
101
|
# File 'lib/basketball/season/coordinator.rb', line 99
def total_days
(regular_end_date - exhibition_start_date).to_i
end
|
#total_exhibitions ⇒ Object
107
108
109
|
# File 'lib/basketball/season/coordinator.rb', line 107
def total_exhibitions
exhibitions_for.length
end
|
#total_regulars ⇒ Object
115
116
117
|
# File 'lib/basketball/season/coordinator.rb', line 115
def total_regulars
regulars_for.length
end
|