-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
215 lines (176 loc) · 6.4 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
$LOAD_PATH << "lib"
require "play_by_play"
require "play_by_play/persistent/game"
require "play_by_play/repository"
require "play_by_play/sample/game"
require "play_by_play/sample/league"
require "play_by_play/sample/season"
require "play_by_play/simulation/game"
require "play_by_play/simulation/random_play_generator"
require "play_by_play/simulation/random_play_generator"
require "play_by_play/simulation/season"
require "play_by_play/views/game"
require "play_by_play/views/season"
require "play_by_play/views/teams"
task default: [ "play:game" ]
namespace :play do
desc "Play a single sample game"
task :game do
repository = PlayByPlay::Repository.new
repository.create
Rake::Task["import:season"].invoke unless repository.league.exists?
repository = PlayByPlay::Repository.new
visitor = repository.teams.find_by_abbrevation(ENV["VISITOR_TEAM"] || "ORL")
home = repository.teams.find_by_abbrevation(ENV["HOME_TEAM"] || "NOP")
random_play_generator = PlayByPlay::Simulation::RandomPlayGenerator.new(repository)
random_seconds_generator = PlayByPlay::Simulation::RandomSecondsGenerator.new(repository)
home_points = 0
home_wins = 0
visitor_points = 0
visitor_wins = 0
times = ENV["TIMES"]&.to_i || 1
times.times do
game = PlayByPlay::Persistent::Game.new(home: home, visitor: visitor)
game = PlayByPlay::Simulation::Game.play!(game, random_play_generator, random_seconds_generator)
if game.winner == home
home_wins += 1
else
visitor_wins += 1
end
home_points += game.possession.home.points
visitor_points += game.possession.visitor.points
puts PlayByPlay::Views::Game.new(game)
puts
end
puts "#{visitor.name} wins: #{visitor_wins}"
puts "#{home.name} wins: #{home_wins}"
puts "#{home.name} win % #{home_wins / times.to_f}"
puts "#{home.name} margin over #{visitor.name}: #{(home_points - visitor_points) / times.to_f}"
end
desc "Simulate a season of games"
task :season do
repository = PlayByPlay::Repository.new
repository.create
days = ENV["DAYS"]&.to_i
scheduled_games_per_teams_count = ENV["GAMES"]&.to_i || 82
seasons = ENV["SEASONS"]&.to_i || 1
year = ENV["YEAR"]&.to_i
random_seconds_generator = PlayByPlay::Simulation::RandomSecondsGenerator.new(repository)
random_seconds_generator.seconds_probability_distribution.pre_fetch!
random_play_generator = PlayByPlay::Simulation::RandomPlayGenerator.new(repository)
random_play_generator.play_probability_distribution.pre_fetch!
seasons.times do
if repository.league.exists?
league = repository.league.find
else
teams_count = ENV["TEAMS"]&.to_i || 30
league = PlayByPlay::Simulation::League.new_random(teams_count)
end
if year
season = repository.league.schedule(year)
else
season = PlayByPlay::Simulation::Season.new_random(league: league, scheduled_games_per_teams_count: scheduled_games_per_teams_count)
end
PlayByPlay::Simulation::Season.play!(days: days, season: season, repository: repository, random_play_generator: random_play_generator, random_seconds_generator: random_seconds_generator)
view = PlayByPlay::Views::Season.new(season)
puts view
view = PlayByPlay::Views::Teams.new(season)
puts view
repository.seasons.create season
end
end
end
namespace :view do
task :season do
repository = PlayByPlay::Repository.new
repository.seasons.simulations.each do |season|
view = PlayByPlay::Views::Season.new(season)
puts view
end
end
end
namespace :parse do
desc "Parse historical play by play file in memory"
task :game do
game_id = ENV["GAME_ID"] || "0021400001"
game = PlayByPlay::Sample::Game.new_game(game_id, "ORL", "NOP")
dir = ENV["DIR"] || "spec/data"
json = PlayByPlay::Sample::Game.read_json(dir, game_id)
PlayByPlay::Sample::Game.parse game, json, invalid_state_error: ENV["DEBUG"]
end
desc "Parse season and show errors but do not save"
task :season do
dir = ENV["DIR"] || "spec/data"
PlayByPlay::Sample::Season.parse dir, invalid_state_error: ENV["DEBUG"]
end
end
namespace :import do
desc "Import historical play by play file into database"
task :game do
PlayByPlay::Repository.new.create!
game_id = ENV["GAME_ID"] || "0021400001"
game = PlayByPlay::Sample::Game.new_game(game_id, "ORL", "NOP")
dir = ENV["DIR"] || "spec/data"
PlayByPlay::Sample::Game.import(game, dir, invalid_state_error: ENV["DEBUG"])
end
desc "Import season into database"
task :season do
PlayByPlay::Repository.new.create
dir = ENV["DIR"] || "spec/data"
PlayByPlay::Sample::Season.import dir, invalid_state_error: ENV["DEBUG"]
end
desc "Import league"
task :league do
dir = ENV["DIR"] || "spec/data"
PlayByPlay::Sample::League.import dir, 2014
end
end
namespace :repository do
desc "Erase and recreate repository"
task :recreate do
`bin/recreate_dbs`
PlayByPlay::Repository.new.create!
end
end
namespace :sample do
desc "Show counts for POSSESSION, TEAM, TEAM_ID"
task :distribution do
repository = PlayByPlay::Repository.new
possession_key = ENV["POSSESSION"].to_sym
team = ENV["TEAM"].to_sym
team_id = ENV["TEAM_ID"].to_i
total_plays = PlayByPlay::Model::PlayMatrix.accessible_plays(possession_key).inject(0) do |total, play|
total + repository.plays.count(possession_key, team, team_id, play)
end
fg = 0
fg_miss = 0
PlayByPlay::Model::PlayMatrix.accessible_plays(possession_key).each do |play|
count = repository.plays.count(possession_key, team, team_id, play)
if play.first == :fg
fg += count
end
if play.first == :fg_miss || play.first == :block
fg_miss += count
end
puts "#{format '%.1f', count * 100 / total_plays.to_f} #{play}"
end
puts "#{fg}/#{fg_miss + fg} #{fg / (fg + fg_miss).to_f}"
end
end
namespace :spec do
require "rspec/core/rake_task"
desc "Run full-stack web spec"
RSpec::Core::RakeTask.new(:web) do |t|
t.rspec_opts = "--tag web"
end
desc "Run fast (no database, no servers) specs"
RSpec::Core::RakeTask.new(:fast) do |t|
t.rspec_opts = "--tag ~web --tag ~database"
end
desc "Run database specs"
RSpec::Core::RakeTask.new(:database) do |t|
t.rspec_opts = "--tag database --tag ~web"
end
desc "Run all specs"
RSpec::Core::RakeTask.new(:all)
end