playoffs 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1a073c8554e9f8e237a7d55bab24bd078671fd84464bcedca0e1ef557851286
4
- data.tar.gz: 07ecbd52187a90afb8fbc3e27abadac5ecb4c2ffdc75cd5ae686f742eb7921b9
3
+ metadata.gz: f197d46a02950cbcb057d745d07a3926082e09efc8e91c4935e84675c0c52f60
4
+ data.tar.gz: 746469e25a41e507424a225c4fb05acd6fe38e1808478b07154733a3286b4aa5
5
5
  SHA512:
6
- metadata.gz: 867f0f7137c5e84349f7a1ee530eb7662a93f2782a6c0752908a0d26e0bcf2045c1857231f5e4bed0bafeb34e308a9a98751962709083cf7c8b1c746c3e7723a
7
- data.tar.gz: 1a2c1debbbfddbcccabd9ebc38a6f568c0c6a1f7220ebfa678c0992de78701f6f78ce6e91c6328d4b551b4c1b0744c5cf2cf784afd485a9af43c97ab55d05077
6
+ metadata.gz: 0ecc670d3b1736f563772f3eaa21b224cc641295c618cb2b9eeb05cd325341a59784ea3d06c988c3667557a6f3a162a4a238c9f43d4b0ae8219c8b2aef72ab69
7
+ data.tar.gz: 86fd1b47d7ffa6209b190c122d6df0af7b6b63d13fa4f9c435000b6cc29461091f88b64e17d05a39b813947d5cd32fe44648376177c2808d185dba3bf2e0dc5c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### 1.0.1
2
+
3
+ * Extract out portions of Cli into Loader and Simulator classes.
4
+
1
5
  ### 1.0.0
2
6
 
3
7
  * Initial version.
data/lib/playoffs/cli.rb CHANGED
@@ -15,10 +15,25 @@ module Playoffs
15
15
  sig { returns(String) }
16
16
  attr_reader :script
17
17
 
18
- sig { params(io: T.any(IO, StringIO), script: String).void }
19
- def initialize(io, script: 'bin/playoffs')
18
+ sig { returns(Loader) }
19
+ attr_reader :loader
20
+
21
+ sig { returns(Simulator) }
22
+ attr_reader :simulator
23
+
24
+ sig do
25
+ params(
26
+ io: T.any(IO, StringIO),
27
+ script: String,
28
+ loader: Loader,
29
+ simulator: Simulator
30
+ ).void
31
+ end
32
+ def initialize(io, script: 'bin/playoffs', loader: Loader.new, simulator: RandomSimulator.new)
20
33
  @io = io
21
34
  @script = script
35
+ @loader = loader
36
+ @simulator = simulator
22
37
  end
23
38
 
24
39
  sig { params(args: T::Array[String]).returns(Cli) }
@@ -45,39 +60,6 @@ module Playoffs
45
60
  self
46
61
  end
47
62
 
48
- protected
49
-
50
- sig { overridable.params(path: String, tournament: Tournament).returns(Cli) }
51
- def save(path, tournament)
52
- FileUtils.mkdir_p(File.dirname(path))
53
-
54
- File.open(path, 'w') { |out| YAML.dump(tournament, out) }
55
-
56
- self
57
- end
58
-
59
- sig { overridable.params(path: String).returns(Tournament) }
60
- def load(path)
61
- YAML.load_file(
62
- path,
63
- permitted_classes: [
64
- BestOf,
65
- Round,
66
- Series,
67
- Team,
68
- Tournament
69
- ],
70
- aliases: true
71
- )
72
- end
73
-
74
- sig { overridable.params(series: Series).returns(Team) }
75
- def pick(series)
76
- index = rand(0..1)
77
-
78
- T.must(series.teams[index])
79
- end
80
-
81
63
  private
82
64
 
83
65
  # rubocop:disable Metrics/AbcSize
@@ -119,28 +101,28 @@ module Playoffs
119
101
 
120
102
  tournament = Basketball.tournament_for(eastern_teams:, western_teams:)
121
103
 
122
- save(path, tournament)
104
+ loader.save(path, tournament)
123
105
 
124
106
  self
125
107
  end
126
108
 
127
109
  sig { params(path: String, _action: String, _args: T::Array[String]).returns(Cli) }
128
110
  def run_bracket(path, _action, _args)
129
- io.puts(load(path).print_bracket)
111
+ io.puts(loader.load(path).print_bracket)
130
112
 
131
113
  self
132
114
  end
133
115
 
134
116
  sig { params(path: String, _action: String, _args: T::Array[String]).returns(Cli) }
135
117
  def run_rounds(path, _action, _args)
136
- io.puts(load(path).print_rounds)
118
+ io.puts(loader.load(path).print_rounds)
137
119
 
138
120
  self
139
121
  end
140
122
 
141
123
  sig { params(path: String, _action: String, _args: T::Array[String]).returns(Cli) }
142
124
  def run_up(path, _action, _args)
143
- tournament = load(path)
125
+ tournament = loader.load(path)
144
126
 
145
127
  series = tournament.up_next
146
128
 
@@ -154,26 +136,26 @@ module Playoffs
154
136
  id = args[2].to_s
155
137
 
156
138
  team = Team.new(id)
157
- tournament = load(path)
139
+ tournament = loader.load(path)
158
140
  series = tournament.up_next
159
141
 
160
142
  T.must(series).win(team)
161
143
 
162
144
  io.puts(series)
163
145
 
164
- save(path, tournament)
146
+ loader.save(path, tournament)
165
147
 
166
148
  self
167
149
  end
168
150
 
169
151
  sig { params(path: String, _action: String, _args: T::Array[String]).returns(Cli) }
170
152
  def run_sim(path, _action, _args)
171
- tournament = load(path)
153
+ tournament = loader.load(path)
172
154
 
173
155
  total = 0
174
156
  # while not nil (and assign series)
175
157
  while (series = tournament.up_next)
176
- team = pick(series)
158
+ team = simulator.pick(series)
177
159
 
178
160
  series.win(team)
179
161
 
@@ -184,14 +166,14 @@ module Playoffs
184
166
 
185
167
  io.puts(tournament.winner.to_s)
186
168
 
187
- save(path, tournament)
169
+ loader.save(path, tournament)
188
170
 
189
171
  self
190
172
  end
191
173
 
192
174
  sig { params(path: String, _action: String, _args: T::Array[String]).returns(Cli) }
193
175
  def run_winner(path, _action, _args)
194
- tournament = load(path)
176
+ tournament = loader.load(path)
195
177
 
196
178
  io.puts(tournament.winner) if tournament.over?
197
179
 
@@ -0,0 +1,47 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Playoffs
5
+ # Knows how to (de)serialize a tournament and read/write from/to disk.
6
+ class Loader
7
+ extend T::Sig
8
+
9
+ sig { overridable.params(tournament: Tournament).returns(String) }
10
+ def serialize(tournament)
11
+ io = StringIO.new
12
+
13
+ YAML.dump(tournament, io)
14
+
15
+ io.string
16
+ end
17
+
18
+ sig { overridable.params(string: String).returns(Tournament) }
19
+ def deserialize(string)
20
+ YAML.load(
21
+ string,
22
+ permitted_classes: [
23
+ BestOf,
24
+ Round,
25
+ Series,
26
+ Team,
27
+ Tournament
28
+ ],
29
+ aliases: true
30
+ )
31
+ end
32
+
33
+ sig { overridable.params(path: String, tournament: Tournament).void }
34
+ def save(path, tournament)
35
+ FileUtils.mkdir_p(File.dirname(path))
36
+
37
+ File.write(path, serialize(tournament))
38
+
39
+ self
40
+ end
41
+
42
+ sig { overridable.params(path: String).returns(Tournament) }
43
+ def load(path)
44
+ deserialize(File.read(path))
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,19 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative 'simulator'
5
+
6
+ module Playoffs
7
+ # Very simple example of how to select a winner for a series.
8
+ class RandomSimulator
9
+ extend T::Sig
10
+ include Simulator
11
+
12
+ sig { override.params(series: Series).returns(Team) }
13
+ def pick(series)
14
+ index = rand(0..1)
15
+
16
+ T.must(series.teams[index])
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Playoffs
5
+ # Describes what functionality a Simulator should implement.
6
+ module Simulator
7
+ extend T::Sig
8
+ extend T::Helpers
9
+ interface!
10
+
11
+ sig { abstract.params(series: Series).returns(Team) }
12
+ def pick(series); end
13
+ end
14
+ end
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Playoffs
5
- VERSION = '1.0.0'
5
+ VERSION = '1.0.1'
6
6
  end
data/lib/playoffs.rb CHANGED
@@ -9,6 +9,8 @@ require_relative 'playoffs/basketball'
9
9
  require_relative 'playoffs/best_of'
10
10
  require_relative 'playoffs/cli'
11
11
  require_relative 'playoffs/contestant'
12
+ require_relative 'playoffs/loader'
13
+ require_relative 'playoffs/random_simulator'
12
14
  require_relative 'playoffs/round'
13
15
  require_relative 'playoffs/series'
14
16
  require_relative 'playoffs/team'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: playoffs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Ruggio
@@ -276,8 +276,11 @@ files:
276
276
  - lib/playoffs/best_of.rb
277
277
  - lib/playoffs/cli.rb
278
278
  - lib/playoffs/contestant.rb
279
+ - lib/playoffs/loader.rb
280
+ - lib/playoffs/random_simulator.rb
279
281
  - lib/playoffs/round.rb
280
282
  - lib/playoffs/series.rb
283
+ - lib/playoffs/simulator.rb
281
284
  - lib/playoffs/team.rb
282
285
  - lib/playoffs/tournament.rb
283
286
  - lib/playoffs/tournament/bracketable.rb