gameday_api 0.5.0 → 0.5.1

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.
data/lib/media.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'gameday_fetcher'
2
+ require 'media_highlight'
3
+ require 'media_mobile'
4
+
5
+
6
+ class Media
7
+
8
+ attr_accessor :gid, :highlights, :mobile
9
+
10
+
11
+ def load_from_id(gid)
12
+ @gid = gid
13
+ @highlights = []
14
+ @mobile = []
15
+ @xml_highlights = GamedayFetcher.fetch_media_highlights(gid)
16
+ @xml_doc = REXML::Document.new(@xml_highlights)
17
+ if @xml_doc.root
18
+ @xml_doc.elements.each("highlights/media") do |element|
19
+ highlight = MediaHighlight.new(element)
20
+ @highlights << highlight
21
+ end
22
+ end
23
+
24
+ @xml_mobile = GamedayFetcher.fetch_media_mobile(gid)
25
+ @xml_doc = REXML::Document.new(@xml_mobile)
26
+ if @xml_doc.root
27
+ @xml_doc.elements.each("mobile/media") do |element|
28
+ mobile = MediaMobile.new(element)
29
+ @mobile << mobile
30
+ end
31
+ end
32
+ end
33
+
34
+
35
+ end
@@ -0,0 +1,33 @@
1
+ class MediaHighlight
2
+
3
+ attr_accessor :id, :date, :type, :v
4
+ attr_accessor :headline, :duration, :thumb_url
5
+ attr_accessor :res_400_url, :res_500_url, :res_800_url
6
+
7
+ def initialize(element)
8
+ @id = element.attributes['id']
9
+ @date = element.attributes['date']
10
+ @type = element.attributes['type']
11
+ @v = element.attributes['v']
12
+ @headline = element.elements["headline"].text
13
+ @duration = element.elements["duration"].text
14
+ @thumb_url = element.elements["thumb"].text
15
+ if element.elements["url[@playback_scenario='FLASH_400K_600X338']"]
16
+ @res_400_url = element.elements["url[@playback_scenario='FLASH_400K_600X338']"].text
17
+ else
18
+ @res_400_url = nil
19
+ end
20
+ if element.elements["url[@playback_scenario='FLASH_500K_512X288']"]
21
+ @res_500_url = element.elements["url[@playback_scenario='FLASH_500K_512X288']"].text
22
+ else
23
+ @res_500_url = nil
24
+ end
25
+ if element.elements["url[@playback_scenario='FLASH_800K_640X360']"]
26
+ @res_800_url = element.elements["url[@playback_scenario='FLASH_800K_640X360']"].text
27
+ else
28
+ @res_800_url = nil
29
+ end
30
+ end
31
+
32
+
33
+ end
@@ -0,0 +1,13 @@
1
+ class MediaMobile
2
+
3
+ attr_accessor :id, :date, :type, :top_play, :player_id
4
+ attr_accessor :team_id, :headline, :duration, :thumb_url
5
+ attr_accessor :iphone_low_url, :iphone_hi_url
6
+ attr_accessor :h264_low_url, :h264_hi_url
7
+
8
+ def initialize(element)
9
+
10
+ end
11
+
12
+
13
+ end
data/lib/pitch.rb ADDED
@@ -0,0 +1,92 @@
1
+
2
+ # This class represents a single pitch in an MLB baseball game.
3
+ # Most of the attributes represent the physics of the pitch thrown.
4
+ #
5
+ # Pitch Type Codes
6
+ # FS = Splitter
7
+ # SL = Slider
8
+ # FF = Fastball
9
+ # SI = Sinker
10
+ # CH = Change
11
+ # FA = Fastball
12
+ # CU = Curve
13
+ # FC = Cutter
14
+ # KN = Knuckle
15
+ # KC = Knuckle Curve
16
+ #
17
+ class Pitch
18
+
19
+ attr_accessor :gid, :ab_num, :pitcher_id, :batter_id
20
+ attr_accessor :des, :pitch_id, :type, :x, :y, :sv_id, :start_speed, :end_speed
21
+ attr_accessor :sz_top, :sz_bot, :pfx_x, :pfx_z, :px, :pz, :x0, :y0, :z0, :vx0, :vy0, :vz0
22
+ attr_accessor :ax, :ay, :az, :break_y, :break_angle, :break_length, :pitch_type, :type_confidence
23
+ attr_accessor :spin_dir, :spin_rate, :on_1b, :on_2b, :on_3b
24
+
25
+ def init(element)
26
+ @des = element.attributes["des"]
27
+ @pitch_id = element.attributes["id"]
28
+ @type = element.attributes["type"]
29
+ @x = element.attributes["x"]
30
+ @y = element.attributes["y"]
31
+ @sv_id = element.attributes["sv_id"]
32
+ @start_speed = element.attributes["start_speed"]
33
+ @end_speed = element.attributes["end_speed"]
34
+ @sz_top = element.attributes["sz_top"]
35
+ @sz_bot = element.attributes["sz_bot"]
36
+ @pfx_x = element.attributes["pfx_x"]
37
+ @pfx_z = element.attributes["pfx_z"]
38
+ @px = element.attributes["px"]
39
+ @pz = element.attributes["pz"]
40
+ @x0 = element.attributes["x0"]
41
+ @y0 = element.attributes["y0"]
42
+ @z0 = element.attributes["z0"]
43
+ @vx0 = element.attributes["vx0"]
44
+ @vy0 = element.attributes["vy0"]
45
+ @vz0 = element.attributes["vz0"]
46
+ @ax = element.attributes["ax"]
47
+ @ay = element.attributes["ay"]
48
+ @az = element.attributes["az"]
49
+ @break_y = element.attributes["break_y"]
50
+ @break_angle = element.attributes["break_angle"]
51
+ @break_length = element.attributes["break_length"]
52
+ @pitch_type = element.attributes["pitch_type"]
53
+ @type_confidence = element.attributes["type_confidence"]
54
+ @spin_dir = element.attributes["spin_dir"]
55
+ @spin_rate = element.attributes["spin_rate"]
56
+ @on_1b = element.attributes["on_1b"]
57
+ @on_2b = element.attributes["on_2b"]
58
+ @on_3b = element.attributes["on_3b"]
59
+ end
60
+
61
+
62
+ def self.get_pitch_name(code)
63
+ case code
64
+ when 'FS'
65
+ 'Splitter'
66
+ when 'SL'
67
+ 'Slider'
68
+ when 'FF'
69
+ 'Fastball' # 4 seam
70
+ when 'FT'
71
+ 'Fastball' # 2 seam
72
+ when 'SI'
73
+ 'Sinker'
74
+ when 'CH'
75
+ 'Change'
76
+ when 'FA'
77
+ 'Fastball'
78
+ when 'CU'
79
+ 'Curve'
80
+ when 'FC'
81
+ 'Cutter'
82
+ when 'KN'
83
+ 'Knuckle'
84
+ when 'KC'
85
+ 'Knuckle Curve'
86
+ else
87
+ code
88
+ end
89
+ end
90
+
91
+ end
92
+
data/lib/pitcher.rb ADDED
@@ -0,0 +1,131 @@
1
+ require 'player'
2
+
3
+ # This class represents a single pitcher whom appeared in an MLB game
4
+ class Pitcher < Player
5
+
6
+ # attributes read from the pitchers/(pid).xml file
7
+ attr_accessor :team_abbrev, :gid, :pid, :first_name, :last_name, :jersey_number
8
+ attr_accessor :height, :weight, :bats, :throws, :dob, :position
9
+ attr_accessor :opponent_season, :opponent_career, :opponent_empty, :opponent_men_on, :opponent_risp
10
+ attr_accessor :opponent_loaded, :opponent_vs_l, :opponent_vs_r
11
+
12
+ attr_accessor :game
13
+
14
+
15
+ # Loads a Pitcher object given a game id and a player id
16
+ def load_from_id(gid, pid)
17
+ @gid = gid
18
+ @pid = pid
19
+ @position = 'P'
20
+ @xml_data = GamedayFetcher.fetch_pitcher(gid, pid)
21
+ @xml_doc = REXML::Document.new(@xml_data)
22
+ @team_abbrev = @xml_doc.root.attributes["team"]
23
+ @first_name = @xml_doc.root.attributes["first_name"]
24
+ @last_name = @xml_doc.root.attributes["last_name"]
25
+ @jersey_number = @xml_doc.root.attributes["jersey_number"]
26
+ @height = @xml_doc.root.attributes["height"]
27
+ @weight = @xml_doc.root.attributes["weight"]
28
+ @bats = @xml_doc.root.attributes["bats"]
29
+ @throws = @xml_doc.root.attributes["throws"]
30
+ @dob = @xml_doc.root.attributes['dob']
31
+ set_opponent_stats
32
+ end
33
+
34
+
35
+ # Returns an array of PitchingAppearance objects for all of the pitchers starts
36
+ def get_all_starts(year)
37
+ results = []
38
+ app = get_all_appearances(year)
39
+ if app.start == true
40
+ results << app
41
+ end
42
+ end
43
+
44
+
45
+ # Returns an array of the atbats against this pitcher during this game
46
+ def get_vs_ab
47
+ results = []
48
+ abs = get_game.get_atbats
49
+ abs.each do |ab|
50
+ if ab.pitcher_id == @pid
51
+ results << ab
52
+ end
53
+ end
54
+ results
55
+ end
56
+
57
+
58
+ # Returns an array of pitches thrown by this pitcher during this game
59
+ def get_pitches
60
+ results = []
61
+ ab = get_vs_ab
62
+ ab.each do |ab|
63
+ results << ab.pitches
64
+ end
65
+ results.flatten
66
+ end
67
+
68
+
69
+ def get_game
70
+ if !@game
71
+ @game = Game.new(@gid)
72
+ end
73
+ @game
74
+ end
75
+
76
+
77
+ # Returns an array of pitcher ids for the game specified
78
+ # pitchers are found by looking in the gid/pitchers directory on gameday
79
+ def self.get_all_ids_for_game(gid)
80
+ pitchers_page = GamedayFetcher.fetch_pitchers_page(gid)
81
+ results = []
82
+ if pitchers_page
83
+ doc = Hpricot(pitchers_page)
84
+ a = doc.at('ul')
85
+ if a
86
+ (a/"a").each do |link|
87
+ # look at each link inside of a ul tag
88
+ if link.inner_html.include?(".xml") == true
89
+ # if the link contains the text '.xml' then it is a pitcher
90
+ str = link.inner_html
91
+ str.strip!
92
+ pid = str[0..str.length-5]
93
+ results << pid
94
+ end
95
+ end
96
+ end
97
+ end
98
+ results
99
+ end
100
+
101
+
102
+ private
103
+
104
+ def set_opponent_stats
105
+ @opponent_season = OpponentStats.new(@xml_doc.root.elements["season"])
106
+ @opponent_career = OpponentStats.new(@xml_doc.root.elements["career"])
107
+ @opponent_empty = OpponentStats.new(@xml_doc.root.elements["Empty"])
108
+ @opponent_men_on = OpponentStats.new(@xml_doc.root.elements["Men_On"])
109
+ @opponent_risp = OpponentStats.new(@xml_doc.root.elements["RISP"])
110
+ @opponent_loaded = OpponentStats.new(@xml_doc.root.elements["Loaded"])
111
+ @opponent_vs_l = OpponentStats.new(@xml_doc.root.elements["vs_LHB"])
112
+ @opponent_vs_r = OpponentStats.new(@xml_doc.root.elements["vs_RHB"])
113
+ end
114
+
115
+ end
116
+
117
+
118
+ class OpponentStats
119
+ attr_accessor :des, :avg, :ab, :hr, :bb, :so
120
+
121
+ def initialize(element)
122
+ if element.attributes['des']
123
+ @des = element.attributes['des']
124
+ end
125
+ @avg = element.attributes['avg']
126
+ @ab = element.attributes['ab']
127
+ @hr = element.attributes['hr']
128
+ @bb = element.attributes['bb']
129
+ @so = element.attributes['so']
130
+ end
131
+ end
@@ -0,0 +1,393 @@
1
+ require 'rubygems'
2
+ require 'mysql'
3
+
4
+
5
+ # This class is used to insert data into the pitchfx database
6
+ class PitchfxDbManager
7
+
8
+
9
+ def initialize(host, user, password, db_name)
10
+ @db = Mysql.real_connect(host, user, password, db_name)
11
+ end
12
+
13
+
14
+ def find_or_create_player(player, team_id)
15
+ res = @db.query("select id from players where gameday_id = #{player.pid}")
16
+ id=0
17
+ if res.num_rows > 0
18
+ res.each do |row|
19
+ id = row[0]
20
+ end
21
+ else
22
+ id = insert_player(player, team_id)
23
+ end
24
+ id
25
+ end
26
+
27
+
28
+ def find_or_create_team(team)
29
+ res = @db.query("select id from teams where abbreviation = '#{team.abrev}'")
30
+ id=0
31
+ if res.num_rows > 0
32
+ res.each do |row|
33
+ id = row[0]
34
+ end
35
+ else
36
+ id = insert_team(team)
37
+ end
38
+ id
39
+ end
40
+
41
+
42
+ def find_or_create_game(game, visitor_id, home_id)
43
+ res = @db.query("select id from games where gid = '#{game.gid}'")
44
+ id=0
45
+ if res.num_rows > 0
46
+ res.each do |row|
47
+ id = row[0]
48
+ end
49
+ else
50
+ id = insert_game(game, visitor_id, home_id)
51
+ end
52
+ id
53
+ end
54
+
55
+
56
+ def find_or_create_atbat(atbat, game_id)
57
+ res = @db.query("select id from atbats where game_id = #{game_id} and num = #{atbat.num}")
58
+ id=0
59
+ if res.num_rows > 0
60
+ res.each do |row|
61
+ id = row[0]
62
+ end
63
+ else
64
+ id = insert_atbat(atbat, game_id)
65
+ end
66
+ id
67
+ end
68
+
69
+
70
+ def find_or_create_pitch(pitch, atbat_id)
71
+ res = @db.query("select id from pitches where pitch_id = '#{pitch.pitch_id}' and atbat_id = '#{atbat_id}'")
72
+ id=0
73
+ if res.num_rows > 0
74
+ res.each do |row|
75
+ id = row[0]
76
+ end
77
+ else
78
+ id = insert_pitch(pitch, atbat_id)
79
+ end
80
+ id
81
+ end
82
+
83
+
84
+ def find_or_create_umpire(name)
85
+ ump_name = @db.escape_string(name)
86
+ res = @db.query("select id from umpires where name = '#{ump_name}'")
87
+ id=0
88
+ if res.num_rows > 0
89
+ res.each do |row|
90
+ id = row[0]
91
+ end
92
+ else
93
+ id = insert_umpire(name)
94
+ end
95
+ id
96
+ end
97
+
98
+
99
+ def find_or_create_pitch_type(type)
100
+ res = @db.query("select id from pitch_types where id = #{type.id}")
101
+ id=0
102
+ if res.num_rows > 0
103
+ res.each do |row|
104
+ id = row[0]
105
+ end
106
+ else
107
+ id = insert_pitch_type(type)
108
+ end
109
+ id
110
+ end
111
+
112
+
113
+ def find_or_create_game_type(type)
114
+ res = @db.query("select id from game_types where id = #{type.id}")
115
+ id=0
116
+ if res.num_rows > 0
117
+ res.each do |row|
118
+ id = row[0]
119
+ end
120
+ else
121
+ id = insert_game_type(type)
122
+ end
123
+ id
124
+ end
125
+
126
+
127
+ def find_or_create_rosters(gid, away_id, home_id)
128
+ gameday_info = GamedayUtil.parse_gameday_id('gid_' + gid)
129
+ active_date = "#{gameday_info['year']}/#{gameday_info['month']}/#{gameday_info['day']}"
130
+ away_res = @db.query("select id from rosters where team_id = #{away_id} and active_date = #{active_date}")
131
+ away_roster_id=0
132
+ if away_res.num_rows > 0
133
+ away_res.each do |row|
134
+ away_roster_id = row[0]
135
+ end
136
+ else
137
+ away_roster_id = insert_roster(away_id, active_date)
138
+ end
139
+ home_res = @db.query("select id from rosters where team_id = #{home_id} and active_date = #{active_date}")
140
+ home_roster_id=0
141
+ if home_res.num_rows > 0
142
+ home_res.each do |row|
143
+ home_roster_id = row[0]
144
+ end
145
+ else
146
+ home_roster_id = insert_roster(home_id, active_date)
147
+ end
148
+ [away_roster_id, home_roster_id]
149
+ end
150
+
151
+
152
+ def find_or_create_roster_player(player, roster_id)
153
+ res = @db.query("select id from players where gameday_id = #{player.pid}")
154
+ player_id=0
155
+ res.each do |row|
156
+ player_id = row[0]
157
+ end
158
+ res = @db.query("select id from roster_players where roster_id = #{roster_id} and player_id = #{player_id}")
159
+ id=0
160
+ if res.num_rows > 0
161
+ res.each do |row|
162
+ id = row[0]
163
+ end
164
+ else
165
+ id = insert_roster_player(roster_id, player, player_id)
166
+ end
167
+ id
168
+ end
169
+
170
+
171
+ def find_or_create_pitcher_line(pitcher, game)
172
+ game_id = find_or_create_game(game, nil, nil)
173
+ res = @db.query("select id from players where gameday_id = #{pitcher.pid}")
174
+ pitcher_id=0
175
+ res.each do |row|
176
+ pitcher_id = row[0]
177
+ end
178
+ res = @db.query("select id from pitcher_lines where game_id = #{game_id} and pitcher_id = #{pitcher_id}")
179
+ id=0
180
+ if res.num_rows > 0
181
+ res.each do |row|
182
+ id = row[0]
183
+ end
184
+ else
185
+ id = insert_pitcher_line(pitcher, game_id, pitcher_id)
186
+ end
187
+ id
188
+ end
189
+
190
+
191
+ def insert_roster(team_id, active_date)
192
+ @db.query("INSERT INTO rosters (team_id, active_date, created_at) VALUES ('#{team_id}','#{active_date}', '#{Time.now.strftime("%Y/%m/%d")}')")
193
+ res = @db.query("select last_insert_id()")
194
+ id = 0
195
+ res.each do |row|
196
+ id = row[0]
197
+ end
198
+ id
199
+ end
200
+
201
+
202
+ def insert_roster_player(roster_id, player, player_id)
203
+ @db.query("INSERT INTO roster_players (roster_id, player_id, number, position, status, avg, hr, rbi,
204
+ wins, losses, era, bat_order, game_position, created_at)
205
+ VALUES ('#{roster_id}',
206
+ '#{player_id}',
207
+ '#{player.num}',
208
+ '#{player.position}',
209
+ '#{player.status}',
210
+ '#{player.avg}',
211
+ '#{player.hr}',
212
+ '#{player.rbi}',
213
+ '#{player.wins}',
214
+ '#{player.losses}',
215
+ '#{player.era}',
216
+ '#{player.bat_order}',
217
+ '#{player.game_position}',
218
+ '#{Time.now.strftime("%Y/%m/%d")}')")
219
+ res = @db.query("select last_insert_id()")
220
+ id = 0
221
+ res.each do |row|
222
+ id = row[0]
223
+ end
224
+ id
225
+ end
226
+
227
+
228
+ def insert_pitcher_line(pitcher, game_id, pitcher_id)
229
+ #puts "INSERT INTO pitcher_lines (game_id, pitcher_id, name, pos, outs, bf, er, r, h, so, hr, bb, w, l, era, note)
230
+ # VALUES ('#{game_id}', '#{pitcher_id}','#{pitcher.pitcher_name}','P', '#{pitcher.out}',
231
+ # '#{pitcher.bf}','#{pitcher.er}','#{pitcher.r}','#{pitcher.h}','#{pitcher.so}','#{pitcher.hr}',
232
+ # '#{pitcher.bb}','#{pitcher.w}','#{pitcher.l}','#{pitcher.era}','#{pitcher.note}')"
233
+ name = @db.escape_string("#{pitcher.pitcher_name}")
234
+ @db.query("INSERT INTO pitcher_lines (game_id, pitcher_id, name, pos, outs, bf, er, r, h, so, hr, bb, w, l, era, note)
235
+ VALUES ('#{game_id}', '#{pitcher_id}','#{name}','P', '#{pitcher.out}','#{pitcher.bf}',
236
+ '#{pitcher.er}','#{pitcher.r}','#{pitcher.h}','#{pitcher.so}','#{pitcher.hr}','#{pitcher.bb}',
237
+ '#{pitcher.w}','#{pitcher.l}','#{pitcher.era}','#{pitcher.note}')")
238
+ res = @db.query("select last_insert_id()")
239
+ id = 0
240
+ res.each do |row|
241
+ id = row[0]
242
+ end
243
+ id
244
+ end
245
+
246
+
247
+ def insert_player(player, team_id)
248
+ first = @db.escape_string("#{player.first}")
249
+ last = @db.escape_string("#{player.last}")
250
+ boxname = @db.escape_string("#{player.boxname}")
251
+ @db.query("INSERT INTO players (team_id, gameday_id, first, last, number, boxname, position, throws)
252
+ VALUES ('#{team_id}', '#{player.pid}','#{first}','#{last}',
253
+ '#{player.num}','#{boxname}','#{player.position}',
254
+ '#{player.rl}')")
255
+ res = @db.query("select last_insert_id()")
256
+ id = 0
257
+ res.each do |row|
258
+ id = row[0]
259
+ end
260
+ id
261
+ end
262
+
263
+
264
+ def insert_team(team)
265
+ @db.query("INSERT INTO teams (abbreviation, city, name, stadium, created_at)
266
+ VALUES ('#{team.abrev}', '#{team.city}','#{team.name}','', '#{Time.now.strftime("%Y/%m/%d")}')")
267
+ res = @db.query("select id from teams where name='#{team.name}'")
268
+ id = 0
269
+ res.each do |row|
270
+ id = row[0]
271
+ end
272
+ id
273
+ end
274
+
275
+
276
+ def insert_atbat(atbat, game_id)
277
+ desc = @db.escape_string("#{atbat.des}")
278
+ @db.query("INSERT INTO atbats (game_id, inning, num, ball, strike, outs, batter_id,
279
+ pitcher_id, stand, des, event, created_at)
280
+ VALUES ('#{game_id}','#{atbat.inning}','#{atbat.num}','#{atbat.b}','#{atbat.s}',
281
+ '#{atbat.o}','#{atbat.batter_id}','#{atbat.pitcher_id}','#{atbat.stand}',
282
+ '#{desc}','#{atbat.event}', '#{Time.now.strftime("%Y/%m/%d")}')")
283
+ res = @db.query("select last_insert_id()")
284
+ id = 0
285
+ res.each do |row|
286
+ id = row[0]
287
+ end
288
+ id
289
+ end
290
+
291
+
292
+ def insert_pitch(pitch, atbat_id)
293
+ @db.query("INSERT INTO pitches (atbat_id, pitch_id, description, outcome, x, y, start_speed, end_speed,
294
+ sz_top, sz_bot, pfx_x, pfx_z, px, pz, x0, y0, z0, vx0, vy0, vz0, ax, ay, az,
295
+ break_y, break_angle, break_length, on_1b, on_2b, on_3b,
296
+ sv_id, pitch_type, type_confidence, spin_dir, spin_rate, created_at)
297
+ VALUES ('#{atbat_id}','#{pitch.pitch_id}', '#{pitch.des}','#{pitch.type}',
298
+ '#{pitch.x}',
299
+ '#{pitch.y}','#{pitch.start_speed}','#{pitch.end_speed}',
300
+ '#{pitch.sz_top}','#{pitch.sz_bot}','#{pitch.pfx_x}',
301
+ '#{pitch.pfx_z}','#{pitch.px}','#{pitch.pz}','#{pitch.x0}',
302
+ '#{pitch.y0}','#{pitch.z0}','#{pitch.vx0}','#{pitch.vy0}',
303
+ '#{pitch.vz0}','#{pitch.ax}','#{pitch.ay}','#{pitch.az}',
304
+ '#{pitch.break_y}','#{pitch.break_angle}','#{pitch.break_length}',
305
+ '#{pitch.on_1b}','#{pitch.on_2b}',
306
+ '#{pitch.on_3b}','#{pitch.sv_id}','#{pitch.pitch_type}',
307
+ '#{pitch.type_confidence}', '#{pitch.spin_dir}', '#{pitch.spin_rate}',
308
+ '#{Time.now.strftime("%Y/%m/%d")}')")
309
+ res = @db.query("select last_insert_id()")
310
+ id = 0
311
+ res.each do |row|
312
+ id = row[0]
313
+ end
314
+ id
315
+ end
316
+
317
+
318
+ def insert_game(game, visitor_id, home_id)
319
+ ump_hid = find_or_create_umpire(game.get_umpires['home'])
320
+ ump_1id = find_or_create_umpire(game.get_umpires['first'])
321
+ ump_2id = find_or_create_umpire(game.get_umpires['second'])
322
+ ump_3id = find_or_create_umpire(game.get_umpires['third'])
323
+
324
+ if game.get_home_runs.to_i > game.get_away_runs.to_i
325
+ winning_team_id = home_id
326
+ losing_team_id = visitor_id
327
+ elsif game.get_home_runs.to_i < game.get_away_runs.to_i
328
+ winning_team_id = visitor_id
329
+ losing_team_id = home_id
330
+ end
331
+
332
+ @db.query("INSERT INTO games (gid, date, home_id, away_id, game_num, umpire_hp_id,
333
+ umpire_1b_id, umpire_2b_id, umpire_3b_id, wind,
334
+ wind_dir, temp, runs_home, runs_away, game_type, status,
335
+ winning_team_id, losing_team_id, created_at)
336
+ VALUES ('#{game.gid}', '#{Date.parse(game.get_date).to_s}', '#{home_id}', '#{visitor_id}',
337
+ '#{game.game_number}', '#{ump_hid}', '#{ump_1id}', '#{ump_2id}',
338
+ '#{ump_3id}', '#{game.get_wind_speed}', '#{game.get_wind_dir}',
339
+ '#{game.get_temp}', '#{game.get_home_runs}', '#{game.get_away_runs}',
340
+ '#{game.game_type}', '#{game.get_boxscore.status_ind}',
341
+ '#{winning_team_id}', '#{losing_team_id}', '#{Time.now.strftime("%Y/%m/%d")}') ")
342
+
343
+ res = @db.query("select id from games where gid='#{game.gid}'")
344
+ id = 0
345
+ res.each do |row|
346
+ id = row[0]
347
+ end
348
+ id
349
+ end
350
+
351
+
352
+ def update_status_for_game(game)
353
+ @db.query("UPDATE games SET status='#{game.get_boxscore.status_ind}' WHERE gid='#{game.gid}'")
354
+ end
355
+
356
+
357
+ def insert_umpire(umpire)
358
+ name = @db.escape_string(umpire)
359
+ @db.query("INSERT INTO umpires (name, created_at)
360
+ VALUES ('#{name}', '#{Time.now.strftime("%Y/%m/%d")}') ")
361
+ res = @db.query("select last_insert_id()")
362
+ id = 0
363
+ res.each do |row|
364
+ id = row[0]
365
+ end
366
+ id
367
+ end
368
+
369
+
370
+ def insert_pitch_type(pitch)
371
+ @db.query("INSERT INTO pitch_types (abbreviation, description, created_at)
372
+ VALUES ('#{pitch.type}', '#{pitch.des}', '#{Time.now.strftime("%Y/%m/%d")}') ")
373
+ res = @db.query("select id from pitch_types where abbreviation='#{pitch.type}'")
374
+ id = 0
375
+ res.each do |row|
376
+ id = row[0]
377
+ end
378
+ id
379
+ end
380
+
381
+
382
+ def insert_game_type(type)
383
+ @db.query("INSERT INTO game_types (name, created_at)
384
+ VALUES ('#{type}', '#{Time.now.strftime("%Y/%m/%d")}') ")
385
+ res = @db.query("select id from game_types where name='#{type}'")
386
+ id = 0
387
+ res.each do |row|
388
+ id = row[0]
389
+ end
390
+ id
391
+ end
392
+
393
+ end