Class: Basketball::Draft::Room
- Inherits:
-
Entity
- Object
- Entity
- Basketball::Draft::Room
show all
- Defined in:
- lib/basketball/draft/room.rb
Overview
Main pick-by-pick iterator object which will round-robin rotate team selections.
Defined Under Namespace
Classes: AlreadyPickedError, DraftUnderwayError, EndOfDraftError, EventOutOfOrderError, FrontOfficeAlreadyRegisteredError, PlayerAlreadyAddedError, UnknownFrontOfficeError, UnknownPlayerError
Constant Summary
collapse
- DEFAULT_ROUNDS =
12
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(events: [], front_offices: [], players: [], rounds: DEFAULT_ROUNDS) ⇒ Room
Returns a new instance of Room.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/basketball/draft/room.rb', line 20
def initialize(events: [], front_offices: [], players: [], rounds: DEFAULT_ROUNDS)
super()
raise InvalidRoundsError, "#{rounds} should be a positive number" unless rounds.positive?
@rounds = rounds
@players = []
@front_offices = []
@events = []
front_offices.each { |front_office| register!(front_office) }
players.each { |player| add_player!(player) }
events.each { |event| add_event!(event) }
end
|
Instance Attribute Details
#events ⇒ Object
Returns the value of attribute events.
18
19
20
|
# File 'lib/basketball/draft/room.rb', line 18
def events
@events
end
|
#front_offices ⇒ Object
Returns the value of attribute front_offices.
18
19
20
|
# File 'lib/basketball/draft/room.rb', line 18
def front_offices
@front_offices
end
|
#players ⇒ Object
Returns the value of attribute players.
18
19
20
|
# File 'lib/basketball/draft/room.rb', line 18
def players
@players
end
|
#rounds ⇒ Object
Returns the value of attribute rounds.
18
19
20
|
# File 'lib/basketball/draft/room.rb', line 18
def rounds
@rounds
end
|
Instance Method Details
#add_player!(player) ⇒ Object
105
106
107
108
109
110
111
112
113
|
# File 'lib/basketball/draft/room.rb', line 105
def add_player!(player)
raise DraftUnderwayError, 'draft already started!' if events.any?
raise ArgumentError, 'player required' unless player
raise PlayerAlreadyAddedError, "#{player} already added" if player?(player)
players << player
self
end
|
#assessment ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/basketball/draft/room.rb', line 37
def assessment
Assessment.new(
drafted_players: drafted_players(front_office),
undrafted_players:,
pick:,
round:,
round_pick:
)
end
|
#done? ⇒ Boolean
81
82
83
|
# File 'lib/basketball/draft/room.rb', line 81
def done?
internal_pick > total_picks
end
|
#drafted_players(front_office = nil) ⇒ Object
89
90
91
92
93
94
95
96
97
|
# File 'lib/basketball/draft/room.rb', line 89
def drafted_players(front_office = nil)
raise UnknownFrontOfficeError, "#{front_office} doesnt exist" if front_office && !registered?(front_office)
player_events.each_with_object([]) do |e, memo|
next unless front_office.nil? || e.front_office == front_office
memo << e.player
end
end
|
#front_office ⇒ Object
65
66
67
68
69
|
# File 'lib/basketball/draft/room.rb', line 65
def front_office
return if done?
front_offices[round_pick - 1]
end
|
#not_done? ⇒ Boolean
85
86
87
|
# File 'lib/basketball/draft/room.rb', line 85
def not_done?
!done?
end
|
#pick ⇒ Object
71
72
73
74
75
|
# File 'lib/basketball/draft/room.rb', line 71
def pick
return if done?
internal_pick
end
|
#pick!(player) ⇒ Object
170
171
172
173
174
175
176
|
# File 'lib/basketball/draft/room.rb', line 170
def pick!(player)
return nil if done?
event = Pick.new(id: pick, front_office:, round:, round_pick:, player:)
add_event!(event)
end
|
#player?(player) ⇒ Boolean
129
130
131
|
# File 'lib/basketball/draft/room.rb', line 129
def player?(player)
players.include?(player)
end
|
#player_events ⇒ Object
178
179
180
|
# File 'lib/basketball/draft/room.rb', line 178
def player_events
events.select { |e| e.respond_to?(:player) }
end
|
#register!(front_office) ⇒ Object
115
116
117
118
119
120
121
122
123
|
# File 'lib/basketball/draft/room.rb', line 115
def register!(front_office)
raise DraftUnderwayError, 'draft already started!' if events.any?
raise ArgumentError, 'front_office required' unless front_office
raise FrontOfficeAlreadyRegisteredError, "#{front_office} already registered" if registered?(front_office)
front_offices << front_office
self
end
|
#registered?(front_office) ⇒ Boolean
125
126
127
|
# File 'lib/basketball/draft/room.rb', line 125
def registered?(front_office)
front_offices.include?(front_office)
end
|
#remaining_picks ⇒ Object
77
78
79
|
# File 'lib/basketball/draft/room.rb', line 77
def remaining_picks
total_picks - internal_pick + 1
end
|
#round ⇒ Object
51
52
53
54
55
|
# File 'lib/basketball/draft/room.rb', line 51
def round
return if done?
(pick / front_offices.length.to_f).ceil
end
|
#round_pick ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/basketball/draft/room.rb', line 57
def round_pick
return if done?
mod = pick % front_offices.length
mod.positive? ? mod : front_offices.length
end
|
#sim! ⇒ Object
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/basketball/draft/room.rb', line 145
def sim!
return if done?
player = front_office.pick(assessment)
event = Pick.new(id: pick, front_office:, round:, round_pick:, player:, auto: true)
add_event!(event)
event
end
|
#sim_rest! ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/basketball/draft/room.rb', line 156
def sim_rest!
events = []
while not_done?
event = sim!
yield event if block_given?
events << event
end
events
end
|
#skip! ⇒ Object
135
136
137
138
139
140
141
142
143
|
# File 'lib/basketball/draft/room.rb', line 135
def skip!
return if done?
event = Skip.new(id: pick, front_office:, round:, round_pick:)
add_event!(event)
event
end
|
#total_picks ⇒ Object
47
48
49
|
# File 'lib/basketball/draft/room.rb', line 47
def total_picks
rounds * front_offices.length
end
|
#undrafted_players ⇒ Object
99
100
101
|
# File 'lib/basketball/draft/room.rb', line 99
def undrafted_players
players - drafted_players
end
|