stattleship-ruby 0.1.25 → 0.1.26
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -6
- data/examples/README.md +3 -1
- data/examples/at_bats.rb +25 -0
- data/examples/baseball_games.rb +2 -1
- data/examples/basketball_team_game_logs.rb +13 -6
- data/examples/line_ups.rb +21 -0
- data/examples/pitches.rb +26 -0
- data/lib/stattleship.rb +4 -0
- data/lib/stattleship/at_bats.rb +81 -0
- data/lib/stattleship/endpoint.rb +37 -0
- data/lib/stattleship/line_ups.rb +61 -0
- data/lib/stattleship/models.rb +4 -0
- data/lib/stattleship/models/at_bat.rb +51 -0
- data/lib/stattleship/models/line_up.rb +102 -0
- data/lib/stattleship/models/pitch.rb +369 -0
- data/lib/stattleship/models/player.rb +9 -1
- data/lib/stattleship/params.rb +4 -0
- data/lib/stattleship/params/at_bats_params.rb +23 -0
- data/lib/stattleship/params/line_ups_params.rb +18 -0
- data/lib/stattleship/params/pitches_params.rb +40 -0
- data/lib/stattleship/pitches.rb +70 -0
- data/lib/stattleship/validators.rb +14 -1
- data/lib/stattleship/validators/hit_location_validator.rb +9 -0
- data/lib/stattleship/validators/hit_type_validator.rb +9 -0
- data/lib/stattleship/validators/hitter_id_validator.rb +6 -0
- data/lib/stattleship/validators/inning_validator.rb +9 -0
- data/lib/stattleship/validators/lower_speed_validator.rb +9 -0
- data/lib/stattleship/validators/pitch_outcome_type_validator.rb +9 -0
- data/lib/stattleship/validators/pitch_type_validator.rb +9 -0
- data/lib/stattleship/validators/pitcher_id_validator.rb +6 -0
- data/lib/stattleship/validators/speed_and_over_validator.rb +9 -0
- data/lib/stattleship/validators/speed_and_under_validator.rb +9 -0
- data/lib/stattleship/validators/speed_validator.rb +10 -0
- data/lib/stattleship/validators/upper_speed_validator.rb +9 -0
- data/lib/stattleship/version.rb +1 -1
- metadata +26 -2
@@ -0,0 +1,102 @@
|
|
1
|
+
module Stattleship
|
2
|
+
module Models
|
3
|
+
class LineUp < OpenStruct
|
4
|
+
def to_sentence
|
5
|
+
"#{player_label} : #{inning_half} #{inning} inning #{batting_order} batting order #{lineup_position} lineup position"
|
6
|
+
end
|
7
|
+
|
8
|
+
def player_name
|
9
|
+
player.full_name
|
10
|
+
end
|
11
|
+
|
12
|
+
def player_label
|
13
|
+
player.label
|
14
|
+
end
|
15
|
+
|
16
|
+
def team_name
|
17
|
+
if team
|
18
|
+
team.name
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def team_nickname
|
23
|
+
if team
|
24
|
+
team.nickname
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def team_hash
|
29
|
+
if team
|
30
|
+
team.dump
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def pitcher?
|
35
|
+
position_abbreviation == 'SP' ||
|
36
|
+
position_abbreviation == 'RP'
|
37
|
+
end
|
38
|
+
|
39
|
+
def inflielder?
|
40
|
+
position_abbreviation == '1B' ||
|
41
|
+
position_abbreviation == '2B' ||
|
42
|
+
position_abbreviation == '3B' ||
|
43
|
+
position_abbreviation == 'SS' ||
|
44
|
+
position_abbreviation == 'C'
|
45
|
+
end
|
46
|
+
|
47
|
+
def outflielder?
|
48
|
+
position_abbreviation == 'LF' ||
|
49
|
+
position_abbreviation == 'CF' ||
|
50
|
+
position_abbreviation == 'RF' ||
|
51
|
+
position_abbreviation == 'OF'
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def dump
|
56
|
+
{
|
57
|
+
id: id,
|
58
|
+
player_name: player_name,
|
59
|
+
inning_half: inning_half,
|
60
|
+
position_name: position_name,
|
61
|
+
position_abbreviation: position_abbreviation,
|
62
|
+
inning: inning,
|
63
|
+
batting_order: batting_order,
|
64
|
+
lineup_position: lineup_position,
|
65
|
+
sequence: sequence,
|
66
|
+
}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
module LineUpRepresenter
|
71
|
+
include Roar::JSON
|
72
|
+
include Roar::Coercion
|
73
|
+
include Virtus.model
|
74
|
+
|
75
|
+
[
|
76
|
+
:id,
|
77
|
+
:inning_half,
|
78
|
+
:position_name,
|
79
|
+
:position_abbreviation,
|
80
|
+
].each do |attribute|
|
81
|
+
property attribute
|
82
|
+
end
|
83
|
+
|
84
|
+
[
|
85
|
+
:inning,
|
86
|
+
:batting_order,
|
87
|
+
:lineup_position,
|
88
|
+
:sequence,
|
89
|
+
].each do |attribute|
|
90
|
+
property attribute, type: Integer
|
91
|
+
end
|
92
|
+
|
93
|
+
[
|
94
|
+
:game_id,
|
95
|
+
:player_id,
|
96
|
+
:team_id,
|
97
|
+
].each do |relationship|
|
98
|
+
property relationship
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,369 @@
|
|
1
|
+
module Stattleship
|
2
|
+
module Models
|
3
|
+
class Pitch < OpenStruct
|
4
|
+
def to_sentence
|
5
|
+
description
|
6
|
+
end
|
7
|
+
|
8
|
+
def pitcher_name
|
9
|
+
if pitcher
|
10
|
+
pitcher.full_name
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def hitter_name
|
15
|
+
if hitter
|
16
|
+
hitter.full_name
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def hitter_team_name
|
21
|
+
if hitter_team
|
22
|
+
hitter_team.name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def team_name
|
27
|
+
if team
|
28
|
+
team.name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def at_bat?
|
33
|
+
is_at_bat
|
34
|
+
end
|
35
|
+
|
36
|
+
def at_bat_over?
|
37
|
+
is_at_bat_over
|
38
|
+
end
|
39
|
+
|
40
|
+
def bunt?
|
41
|
+
is_bunt
|
42
|
+
end
|
43
|
+
|
44
|
+
def bunt_shown?
|
45
|
+
is_bunt_shown
|
46
|
+
end
|
47
|
+
|
48
|
+
def double_play?
|
49
|
+
is_double_play
|
50
|
+
end
|
51
|
+
|
52
|
+
def hit?
|
53
|
+
is_hit
|
54
|
+
end
|
55
|
+
|
56
|
+
def on_base?
|
57
|
+
is_on_base
|
58
|
+
end
|
59
|
+
|
60
|
+
def passed_ball?
|
61
|
+
is_passed_ball
|
62
|
+
end
|
63
|
+
|
64
|
+
def triple_play?
|
65
|
+
is_triple_play
|
66
|
+
end
|
67
|
+
|
68
|
+
def wild_pitch?
|
69
|
+
is_wild_pitch
|
70
|
+
end
|
71
|
+
|
72
|
+
def even_count?
|
73
|
+
if at_bat_pitch_count > 0
|
74
|
+
at_bat_balls == at_bat_strikes
|
75
|
+
else
|
76
|
+
false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def full_count?
|
81
|
+
(at_bat_balls == 3) &&
|
82
|
+
(at_bat_strikes == 2)
|
83
|
+
end
|
84
|
+
|
85
|
+
def hitter_pitch_count
|
86
|
+
"#{at_bat_balls}-#{at_bat_strikes}"
|
87
|
+
end
|
88
|
+
|
89
|
+
def inning_half_label
|
90
|
+
case half
|
91
|
+
when 'B'
|
92
|
+
'Bottom of'
|
93
|
+
when 'T'
|
94
|
+
'Top of'
|
95
|
+
else
|
96
|
+
''
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def ordinal_inning
|
101
|
+
inning.ordinalize
|
102
|
+
end
|
103
|
+
|
104
|
+
def inning_label
|
105
|
+
if inning
|
106
|
+
"#{inning_half_label} #{ordinal_inning}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def description
|
111
|
+
[pitch_description, hit_description].compact.join("\n\n")
|
112
|
+
end
|
113
|
+
|
114
|
+
def pitch_description
|
115
|
+
if (pitch_type != 'IB' && pitch_speed.to_i > 0)
|
116
|
+
[
|
117
|
+
"#{hitter_name} at bat. #{pitcher_name} throws a #{pitch_speed.to_i} mph #{pitch_name}. #{pitch_outcome}.",
|
118
|
+
"#{hitter_name} batting. #{pitcher_name} with a #{pitch_speed.to_i} mph #{pitch_name}. #{pitch_outcome}.",
|
119
|
+
"#{pitcher_name} pitching to #{hitter_name}. #{pitch_speed.to_i} mph #{pitch_name} for a #{pitch_outcome}.",
|
120
|
+
"#{pitcher_name} throws a #{pitch_speed.to_i} mph #{pitch_name} to #{hitter_name}. #{pitch_outcome}.",
|
121
|
+
].sample
|
122
|
+
elsif pitch_type == 'IB'
|
123
|
+
"#{pitcher_name} pitching to #{hitter_name}. #{pitch_name}."
|
124
|
+
elsif !pitch_name == ''
|
125
|
+
"#{hitter_name} up to bat. #{pitcher_name} throws a #{pitch_name} to #{hitter_name}."
|
126
|
+
elsif !pitch_outcome == ''
|
127
|
+
"#{pitcher_name} pitching to #{hitter_name}. #{pitch_outcome}."
|
128
|
+
else
|
129
|
+
"#{pitcher_name} pitching to #{hitter_name}."
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def hit_name
|
134
|
+
case hit_type
|
135
|
+
when 'GB'
|
136
|
+
'Ground Ball'
|
137
|
+
when 'FB'
|
138
|
+
'Fly Ball'
|
139
|
+
when 'PU'
|
140
|
+
'Pop Fly'
|
141
|
+
when 'LD'
|
142
|
+
'Line Drive'
|
143
|
+
else
|
144
|
+
'Hit'
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def hit_location_name
|
149
|
+
case hit_location
|
150
|
+
when '1'
|
151
|
+
' to left'
|
152
|
+
when '2'
|
153
|
+
' to left center'
|
154
|
+
when '3'
|
155
|
+
' to center'
|
156
|
+
when '4'
|
157
|
+
' to right center'
|
158
|
+
when '5'
|
159
|
+
' to right'
|
160
|
+
when '6'
|
161
|
+
' to deep left field'
|
162
|
+
when '7'
|
163
|
+
' to deep left center'
|
164
|
+
when '8'
|
165
|
+
' to deep center field'
|
166
|
+
when '9'
|
167
|
+
' to deep right center'
|
168
|
+
when '10'
|
169
|
+
' to deep right field'
|
170
|
+
when '11'
|
171
|
+
' to left field'
|
172
|
+
when '12'
|
173
|
+
' to left center alley'
|
174
|
+
when '13'
|
175
|
+
' to center field'
|
176
|
+
when '14'
|
177
|
+
' to right center alley'
|
178
|
+
when '15'
|
179
|
+
' to right field'
|
180
|
+
when '16'
|
181
|
+
' to shallow left field'
|
182
|
+
when '17'
|
183
|
+
' to shallow left center'
|
184
|
+
when '18'
|
185
|
+
' to shallow center'
|
186
|
+
when '19'
|
187
|
+
' to shallow right center'
|
188
|
+
when '20'
|
189
|
+
' to shallow right'
|
190
|
+
when '21'
|
191
|
+
' to third'
|
192
|
+
when '22'
|
193
|
+
' to short'
|
194
|
+
when '23'
|
195
|
+
' to second'
|
196
|
+
when '24'
|
197
|
+
' to first'
|
198
|
+
when '25'
|
199
|
+
' to left infield grass'
|
200
|
+
when '26'
|
201
|
+
' to right infield grass'
|
202
|
+
when '27'
|
203
|
+
' to pitcher'
|
204
|
+
when '28'
|
205
|
+
" to catcher's left"
|
206
|
+
when '29'
|
207
|
+
' infront of the catcher'
|
208
|
+
when '30'
|
209
|
+
" to catcher's right"
|
210
|
+
when '31'
|
211
|
+
' to left field foul'
|
212
|
+
when '32'
|
213
|
+
' to third base foul'
|
214
|
+
when '33'
|
215
|
+
'behind the catcher'
|
216
|
+
when '34'
|
217
|
+
' to first base foul'
|
218
|
+
when '35'
|
219
|
+
' to right field foul'
|
220
|
+
else
|
221
|
+
nil
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
def hit_description
|
226
|
+
if is_hit?
|
227
|
+
"#{hit_name}#{hit_location_name}."
|
228
|
+
else
|
229
|
+
"#{hit_name}#{hit_location_name}." if hit_location_name
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
def title
|
234
|
+
@title = []
|
235
|
+
|
236
|
+
@title << game.scoreline
|
237
|
+
@title << inning_label
|
238
|
+
|
239
|
+
unless hitter_pitch_count == '0-0'
|
240
|
+
@title << "#{hitter_pitch_count} count"
|
241
|
+
end
|
242
|
+
|
243
|
+
if at_bat_outs && at_bat_outs > 0
|
244
|
+
@title << "#{h.pluralize(at_bat_outs, 'out')}"
|
245
|
+
end
|
246
|
+
|
247
|
+
if pitch_count && pitch_count > 0
|
248
|
+
@title << "#{pitch_count.ordinalize} pitch"
|
249
|
+
end
|
250
|
+
|
251
|
+
@title.join(' | ')
|
252
|
+
end
|
253
|
+
|
254
|
+
|
255
|
+
def dump
|
256
|
+
{
|
257
|
+
id: id,
|
258
|
+
half: half,
|
259
|
+
hit_location: hit_location,
|
260
|
+
hit_type: hit_type,
|
261
|
+
inning_label: inning_label,
|
262
|
+
pitch_name: pitch_name,
|
263
|
+
pitch_outcome: pitch_outcome,
|
264
|
+
pitch_outcome_type: pitch_outcome_type,
|
265
|
+
pitch_type: pitch_type,
|
266
|
+
at_bat_balls: at_bat_balls,
|
267
|
+
at_bat_outs: at_bat_outs,
|
268
|
+
at_bat_pitch_count: at_bat_pitch_count,
|
269
|
+
at_bat_strikes: at_bat_strikes,
|
270
|
+
even_count: even_count,
|
271
|
+
full_count: full_count,
|
272
|
+
hitter_pitch_count: hitter_pitch_count,
|
273
|
+
inning: inning,
|
274
|
+
sequence: sequence,
|
275
|
+
pitch_speed: pitch_speed,
|
276
|
+
pitch_count: pitch_count,
|
277
|
+
pitch_zone: pitch_zone,
|
278
|
+
is_at_bat: is_at_bat,
|
279
|
+
is_at_bat_over: is_at_bat_over,
|
280
|
+
is_bunt: is_bunt,
|
281
|
+
is_bunt_shown: is_bunt_shown,
|
282
|
+
is_double_play: is_double_play,
|
283
|
+
is_hit: is_hit,
|
284
|
+
is_on_base: is_on_base,
|
285
|
+
is_passed_ball: is_passed_ball,
|
286
|
+
is_triple_play: is_triple_play,
|
287
|
+
is_wild_pitch: is_wild_pitch,
|
288
|
+
pitched_at: pitched_at,
|
289
|
+
event_id: event_id,
|
290
|
+
game_id: game_id,
|
291
|
+
team_id: team_id,
|
292
|
+
pitcher_id: pitcher_id,
|
293
|
+
hitter_id: hitter_id,
|
294
|
+
hitter_team_id: hitter_team_id,
|
295
|
+
pitcher_name: pitcher_name,
|
296
|
+
}
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
module PitchRepresenter
|
301
|
+
include Roar::JSON
|
302
|
+
include Roar::Coercion
|
303
|
+
include Virtus.model
|
304
|
+
|
305
|
+
[
|
306
|
+
:id,
|
307
|
+
:half,
|
308
|
+
:hit_location,
|
309
|
+
:hit_type,
|
310
|
+
:inning_label,
|
311
|
+
:pitch_name,
|
312
|
+
:pitch_outcome,
|
313
|
+
:pitch_outcome_type,
|
314
|
+
:pitch_type,
|
315
|
+
].each do |attribute|
|
316
|
+
property attribute
|
317
|
+
end
|
318
|
+
|
319
|
+
[
|
320
|
+
:at_bat_balls,
|
321
|
+
:at_bat_outs,
|
322
|
+
:at_bat_pitch_count,
|
323
|
+
:at_bat_strikes,
|
324
|
+
:even_count,
|
325
|
+
:full_count,
|
326
|
+
:hitter_pitch_count,
|
327
|
+
:inning,
|
328
|
+
:sequence,
|
329
|
+
:pitch_speed,
|
330
|
+
:pitch_count,
|
331
|
+
:pitch_zone,
|
332
|
+
].each do |attribute|
|
333
|
+
property attribute, type: Integer
|
334
|
+
end
|
335
|
+
|
336
|
+
[
|
337
|
+
:is_at_bat,
|
338
|
+
:is_at_bat_over,
|
339
|
+
:is_bunt,
|
340
|
+
:is_bunt_shown,
|
341
|
+
:is_double_play,
|
342
|
+
:is_hit,
|
343
|
+
:is_on_base,
|
344
|
+
:is_passed_ball,
|
345
|
+
:is_triple_play,
|
346
|
+
:is_wild_pitch,
|
347
|
+
].each do |attribute|
|
348
|
+
property attribute, type: Boolean, default: false
|
349
|
+
end
|
350
|
+
|
351
|
+
[
|
352
|
+
:pitched_at,
|
353
|
+
].each do |attribute|
|
354
|
+
property attribute, type: DateTime
|
355
|
+
end
|
356
|
+
|
357
|
+
[
|
358
|
+
:event_id,
|
359
|
+
:game_id,
|
360
|
+
:team_id,
|
361
|
+
:pitcher_id,
|
362
|
+
:hitter_id,
|
363
|
+
:hitter_team_id,
|
364
|
+
].each do |relationship|
|
365
|
+
property relationship
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
369
|
+
end
|