basketball 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29805ef216312c25dee77d7f7c2a82bf220287404e9e241d24e16dddfb895fcc
4
- data.tar.gz: 3d8a0038acf62e4e1aa230299cc1b2d7d777596bc133b159b1e661639c75c76d
3
+ metadata.gz: 71ab0cd4aa4e2bb10d0a9640e18a756938fbe749842abdbbda9be9f2023be9cf
4
+ data.tar.gz: 5d216901cadb3c92d41a1e445956aef4a11fc1d3e418430003d050693c43d3be
5
5
  SHA512:
6
- metadata.gz: ae3baac2215dbfee6b9cf70a44b2bdf9257d7dade811c6a30bf37b22f840bd85621f6441d137e07f38b615210092d0cede011a2e9f4655aa1e05c6281411a856
7
- data.tar.gz: 4bcb67de94178f2945376faa2cb57c5f5884d60b24479d73e12d1145c7c20c1b9f3dd547f321c578f39d43868fb2f8d158859020b14d7e436fbface8a89b2809
6
+ metadata.gz: a3812684e58e97711745e31a8696c12c32c79e542661302db86eb7b0fecc3784ede1650ec2d0079b66a641f5e09da657f22f596583630d932e03e55ba7ed3514
7
+ data.tar.gz: f1edd69f31a2ff8ca4d5e53bab9bd59c74a091ab276db1a53901aa1e7de17202898ba01a917304f1e7018da1b3ad129fb012a3ac12516e832f4b8fa6cb8cd232
data/CHANGELOG.md CHANGED
@@ -1,7 +1,13 @@
1
+ #### 0.0.4 - May 5th, 2023
2
+
3
+ * Add ability to skip draft picks using `Basketball::Drafting::Engine#skip!`
4
+ * Add ability to output event full drafting event log using CLI: `exe/basketball-draft -i tmp/draft-wip.json -l`
5
+ * Add ability to skip draft picks using CLI: `exe/basketball-draft -i tmp/draft-wip.json -x 1`
6
+
1
7
  #### 0.0.3 - May 5th, 2023
2
8
 
3
- * Drafting::Engine#sim! should return events
4
- * Added Drafting::Engine#undrafted_player_search
9
+ * `Drafting::Engine#sim!` should return events
10
+ * Added `Drafting::Engine#undrafted_player_search`
5
11
 
6
12
  #### 0.0.2 - May 4th, 2023
7
13
 
@@ -9,12 +9,16 @@ module Basketball
9
9
  module Drafting
10
10
  # Example:
11
11
  # exe/basketball-draft -o tmp/draft.json
12
- # exe/basketball-draft -i tmp/draft.json -o tmp/draft-wip.json -s 28 -p ONEALSH01,ONEALJE01 -t 10 -q PG
12
+ # exe/basketball-draft -i tmp/draft.json -o tmp/draft-wip.json -s 26 -p P-5,P-10 -t 10 -q PG
13
+ # exe/basketball-draft -i tmp/draft-wip.json -x 2
13
14
  # exe/basketball-draft -i tmp/draft-wip.json -r -t 10
14
15
  # exe/basketball-draft -i tmp/draft-wip.json -t 10 -q SG
15
16
  # exe/basketball-draft -i tmp/draft-wip.json -s 30 -t 10
16
17
  # exe/basketball-draft -i tmp/draft-wip.json -a -r
18
+ # exe/basketball-draft -i tmp/draft-wip.json -l
17
19
  class CLI
20
+ class PlayerNotFound < StandardError; end
21
+
18
22
  attr_reader :opts, :serializer, :io
19
23
 
20
24
  def initialize(args:, io: $stdout)
@@ -43,11 +47,13 @@ module Basketball
43
47
  io.puts('Draft is complete!')
44
48
  else
45
49
  io.puts("#{engine.remaining_picks} Remaining pick(s)")
46
- io.puts("Round #{engine.current_round} pick #{engine.current_round_pick} for #{engine.current_team}")
50
+ io.puts("Up Next: Round #{engine.current_round} pick #{engine.current_round_pick} for #{engine.current_team}")
47
51
  end
48
52
 
49
53
  write(engine)
50
54
 
55
+ log(engine)
56
+
51
57
  rosters(engine)
52
58
 
53
59
  query(engine)
@@ -70,6 +76,8 @@ module Basketball
70
76
  o.integer '-t', '--top', 'Output the top rated available players (default is 0).', default: 0
71
77
  o.string '-q', '--query', "Filter TOP by position: #{Position::ALL_VALUES.join(', ')}."
72
78
  o.bool '-r', '--rosters', 'Output all team rosters.', default: false
79
+ o.integer '-x', '--skip', 'Number of picks to skip (default is 0).', default: 0
80
+ o.bool '-l', '--log', 'Output event log.', default: false
73
81
 
74
82
  o.on '-h', '--help', 'Print out help, like this is doing right now.' do
75
83
  io.puts(o)
@@ -121,6 +129,15 @@ module Basketball
121
129
  end
122
130
  end
123
131
 
132
+ def log(engine)
133
+ return unless opts[:log]
134
+
135
+ io.puts
136
+ io.puts('Event Log')
137
+
138
+ puts engine.events
139
+ end
140
+
124
141
  # rubocop:disable Metrics/AbcSize
125
142
  def query(engine)
126
143
  top = opts[:top]
@@ -161,6 +178,8 @@ module Basketball
161
178
 
162
179
  player = engine.players.find { |p| p.id == id.to_s.upcase }
163
180
 
181
+ raise PlayerNotFound, "player not found by id: #{id}" unless player
182
+
164
183
  event = engine.pick!(player)
165
184
 
166
185
  io.puts(event)
@@ -168,6 +187,14 @@ module Basketball
168
187
  event_count += 1
169
188
  end
170
189
 
190
+ opts[:skip].times do
191
+ event = engine.skip!
192
+
193
+ io.puts(event)
194
+
195
+ event_count += 1
196
+ end
197
+
171
198
  engine.sim!(opts[:simulate]) do |event|
172
199
  io.puts(event)
173
200
 
@@ -96,6 +96,22 @@ module Basketball
96
96
  !done?
97
97
  end
98
98
 
99
+ def skip!
100
+ return if done?
101
+
102
+ event = SkipEvent.new(
103
+ id: SecureRandom.uuid,
104
+ team: current_team,
105
+ pick: current_pick,
106
+ round: current_round,
107
+ round_pick: current_round_pick
108
+ )
109
+
110
+ play!(event)
111
+
112
+ event
113
+ end
114
+
99
115
  def sim!(times = nil)
100
116
  counter = 0
101
117
  events = []
@@ -156,12 +172,16 @@ module Basketball
156
172
 
157
173
  attr_reader :players_by_id, :teams_by_id
158
174
 
175
+ def player_events
176
+ events.select { |e| e.respond_to?(:player) }
177
+ end
178
+
159
179
  def internal_current_pick
160
180
  events.length + 1
161
181
  end
162
182
 
163
183
  def drafted_players(team = nil)
164
- events.each_with_object([]) do |e, memo|
184
+ player_events.each_with_object([]) do |e, memo|
165
185
  next unless team.nil? || e.team == team
166
186
 
167
187
  memo << e.player
@@ -172,15 +192,21 @@ module Basketball
172
192
  # rubocop:disable Metrics/CyclomaticComplexity
173
193
  # rubocop:disable Metrics/PerceivedComplexity
174
194
  def play!(event)
175
- raise AlreadyPickedError, "#{player} was already picked" if drafted_players.include?(event.player)
176
- raise DupeEventError, "#{event} is a dupe" if events.include?(event)
177
- raise EventOutOfOrder, "#{event} team cant pick right now" if event.team != current_team
178
- raise EventOutOfOrder, "#{event} has wrong pick" if event.pick != current_pick
179
- raise EventOutOfOrder, "#{event} has wrong round" if event.round != current_round
180
- raise EventOutOfOrder, "#{event} has wrong round_pick" if event.round_pick != current_round_pick
181
- raise UnknownTeamError, "#{team} doesnt exist" unless teams.include?(event.team)
182
- raise UnknownPlayerError, "#{player} doesnt exist" unless players.include?(event.player)
183
- raise EndOfDraftError, "#{total_picks} pick limit reached" if events.length > total_picks + 1
195
+ if event.respond_to?(:player) && drafted_players.include?(event.player)
196
+ raise AlreadyPickedError, "#{player} was already picked"
197
+ end
198
+
199
+ if event.respond_to?(:player) && !players.include?(event.player)
200
+ raise UnknownPlayerError, "#{event.player} doesnt exist"
201
+ end
202
+
203
+ raise DupeEventError, "#{event} is a dupe" if events.include?(event)
204
+ raise EventOutOfOrder, "#{event} team cant pick right now" if event.team != current_team
205
+ raise EventOutOfOrder, "#{event} has wrong pick" if event.pick != current_pick
206
+ raise EventOutOfOrder, "#{event} has wrong round" if event.round != current_round
207
+ raise EventOutOfOrder, "#{event} has wrong round_pick" if event.round_pick != current_round_pick
208
+ raise UnknownTeamError, "#{team} doesnt exist" unless teams.include?(event.team)
209
+ raise EndOfDraftError, "#{total_picks} pick limit reached" if events.length > total_picks + 1
184
210
 
185
211
  events << event
186
212
 
@@ -5,13 +5,15 @@ require_relative 'player'
5
5
  require_relative 'team'
6
6
  require_relative 'pick_event'
7
7
  require_relative 'sim_event'
8
+ require_relative 'skip_event'
8
9
 
9
10
  module Basketball
10
11
  module Drafting
11
12
  class EngineSerializer
12
13
  EVENT_CLASSES = {
13
14
  'PickEvent' => PickEvent,
14
- 'SimEvent' => SimEvent
15
+ 'SimEvent' => SimEvent,
16
+ 'SkipEvent' => SkipEvent
15
17
  }.freeze
16
18
 
17
19
  private_constant :EVENT_CLASSES
@@ -71,7 +73,7 @@ module Basketball
71
73
  roster.id,
72
74
  {
73
75
  events: roster.events.map(&:id),
74
- players: roster.events.map { |event| event.player.id }
76
+ players: roster.players.map(&:id)
75
77
  }
76
78
  ]
77
79
  end
@@ -112,12 +114,13 @@ module Basketball
112
114
  {
113
115
  type: event.class.name.split('::').last,
114
116
  id: event.id,
115
- player: event.player.id,
116
117
  team: event.team.id,
117
118
  pick: event.pick,
118
119
  round: event.round,
119
120
  round_pick: event.round_pick
120
- }
121
+ }.tap do |hash|
122
+ hash[:player] = event.player.id if event.respond_to?(:player)
123
+ end
121
124
  end
122
125
  end
123
126
 
@@ -162,12 +165,15 @@ module Basketball
162
165
  def deserialize_events(json, players, teams)
163
166
  (json.dig(:engine, :events) || []).map do |event_hash|
164
167
  event_opts = event_hash.slice(:id, :pick, :round, :round_pick).merge(
165
- player: players.find { |p| p.id == event_hash[:player] },
166
168
  team: teams.find { |t| t.id == event_hash[:team] }
167
169
  )
168
170
 
169
171
  class_constant = EVENT_CLASSES.fetch(event_hash[:type])
170
172
 
173
+ if [PickEvent, SimEvent].include?(class_constant)
174
+ event_opts[:player] = players.find { |p| p.id == event_hash[:player] }
175
+ end
176
+
171
177
  class_constant.new(**event_opts)
172
178
  end
173
179
  end
@@ -27,8 +27,12 @@ module Basketball
27
27
  @events = events
28
28
  end
29
29
 
30
+ def player_events
31
+ events.select { |e| e.respond_to?(:player) }
32
+ end
33
+
30
34
  def players
31
- events.map(&:player)
35
+ player_events.map(&:player)
32
36
  end
33
37
 
34
38
  def to_s
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'event'
4
+
5
+ module Basketball
6
+ module Drafting
7
+ class SkipEvent < Event
8
+ def to_s
9
+ "skipped #{super}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Basketball
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basketball
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Ruggio
@@ -215,6 +215,7 @@ files:
215
215
  - lib/basketball/drafting/position.rb
216
216
  - lib/basketball/drafting/roster.rb
217
217
  - lib/basketball/drafting/sim_event.rb
218
+ - lib/basketball/drafting/skip_event.rb
218
219
  - lib/basketball/drafting/team.rb
219
220
  - lib/basketball/entity.rb
220
221
  - lib/basketball/value_object.rb