public function run() { //copied from HackathonProjects.pdf $pitch_results = array(array('slug' => 'SS', 'description' => 'Swinging Strike', 'strike' => 1, 'ball' => 0), array('slug' => 'SL', 'description' => 'Strike Looking', 'strike' => 1, 'ball' => 0), array('slug' => 'F', 'description' => 'Foul', 'strike' => 1, 'ball' => 0), array('slug' => 'FT', 'description' => 'Foul Tip', 'strike' => 1, 'ball' => 0), array('slug' => 'FB', 'description' => 'Foul Bunt', 'strike' => 1, 'ball' => 0), array('slug' => 'MB', 'description' => 'Missed Bunt', 'strike' => 1, 'ball' => 0), array('slug' => 'B', 'description' => 'Ball', 'strike' => 0, 'ball' => 1), array('slug' => 'BID', 'description' => 'Ball in Dirt', 'strike' => 0, 'ball' => 1), array('slug' => 'HBP', 'description' => 'Hit By Pitch', 'strike' => 0, 'ball' => 1), array('slug' => 'IB', 'description' => 'Interntional Ball', 'strike' => 0, 'ball' => 1), array('slug' => 'PO', 'description' => 'Pitch Out', 'strike' => 0, 'ball' => 1), array('slug' => 'IP', 'description' => 'Ball in Play', 'strike' => 0, 'ball' => 0), array('slug' => 'AS', 'description' => 'Automatic Strike', 'strike' => 1, 'ball' => 0), array('slug' => 'AB', 'description' => 'Automatic Ball', 'strike' => 0, 'ball' => 1), array('slug' => 'CI', 'description' => 'Catcher Interference', 'strike' => 0, 'ball' => 0), array('slug' => 'UK', 'description' => 'Unknown', 'strike' => 0, 'ball' => 0)); foreach ($pitch_results as $p) { $pitch_result = PitchResult::where('slug', $p['slug'])->first(); if (!$pitch_result) { $pitch_result = new PitchResult(); $pitch_result->slug = $p['slug']; $pitch_result->description = $p['description']; $pitch_result->timestamp_utc = time(); } $pitch_result->ball = $p['ball']; $pitch_result->strike = $p['strike']; $pitch_result->save(); } }
function getPitchResults() { $pitch_results = PitchResult::select('id', 'description', 'slug')->get(); return $pitch_results; }
public function run() { $there_is_more = RawDatum::select('id')->whereNull('processed_utc')->first(); if ($there_is_more) { $raw_data = RawDatum::whereNull('processed_utc')->take(4000)->get(); foreach ($raw_data as $raw_datum) { $batter = Player::where('mlb_id', $raw_datum->batter_id)->first(); if (!$batter) { $batter = new Player(); $batter->mlb_id = $raw_datum->batter_id; $batter->name = $raw_datum->batter; } $batter->batter_hand = $raw_datum->batter_hand; $batter->timestamp_utc = time(); $batter->save(); $pitcher = Player::where('mlb_id', $raw_datum->pitcher_id)->first(); if (!$pitcher) { $pitcher = new Player(); $pitcher->mlb_id = $raw_datum->pitcher_id; $pitcher->name = $raw_datum->pitcher; } $pitcher->pitcher_hand = $raw_datum->pitcher_hand; $pitcher->timestamp_utc = time(); $pitcher->save(); $catcher = Player::where('mlb_id', $raw_datum->catcher_id)->first(); if (!$catcher) { $catcher = new Player(); $catcher->mlb_id = $raw_datum->catcher_id; $catcher->name = $raw_datum->catcher; } $catcher->timestamp_utc = time(); $catcher->save(); $pitch_result = PitchResult::where('slug', $raw_datum->pitch_result)->first(); if (!$pitch_result) { $pitch_result = new PitchResult(); $pitch_result->slug = $raw_datum->pitch_result; $pitch_result->timestamp_utc = time(); $pitch_result->save(); } $pitch_type = PitchType::where('slug', $raw_datum->pitch_type)->first(); if (!$pitch_type) { $pitch_type = new PitchType(); $pitch_type->slug = $raw_datum->pitch_type; $pitch_type->timestamp_utc = time(); $pitch_type->save(); } $plate_appearance_result = PlateAppearanceResult::where('slug', $raw_datum->pa_result)->first(); if (!$plate_appearance_result) { $plate_appearance_result = new PlateAppearanceResult(); $plate_appearance_result->slug = $raw_datum->pa_result; $plate_appearance_result->timestamp_utc = time(); $plate_appearance_result->save(); } $batted_ball_type = BattedBallType::where('slug', $raw_datum->batted_ball_type)->first(); if (!$batted_ball_type) { $batted_ball_type = new BattedBallType(); $batted_ball_type->slug = $raw_datum->batted_ball_type; $batted_ball_type->timestamp_utc = time(); $batted_ball_type->save(); } $pitch = Pitch::where('raw_data_id', $raw_datum->id)->first(); if (!$pitch) { $pitch = new Pitch(); $pitch->raw_data_id = $raw_datum->id; } $pitch->season_year = $raw_datum->season_year; $pitch->game_string = $raw_datum->game_string; $pitch->game_date = $raw_datum->game_date; $pitch->game_type = $raw_datum->game_type; $pitch->visitor = $raw_datum->visitor; $pitch->home = $raw_datum->home; $pitch->visiting_team_final_runs = $raw_datum->visiting_team_final_runs; $pitch->home_team_final_runs = $raw_datum->home_team_final_runs; $pitch->inning = $raw_datum->inning; $pitch->side = $raw_datum->side; if ($batter->id == 0 || $pitcher->id == 0) { dd(array('raw_data' => $raw_datum, 'batter' => $batter, 'pitcher' => $pitcher)); } $pitch->batter_id = $batter->id; $pitch->pitcher_id = $pitcher->id; $pitch->inning = $raw_datum->inning; $pitch->catcher_id = $catcher->id; $pitch->times_faced = $raw_datum->times_faced; $pitch->batter_pos = $raw_datum->batter_pos; $position = Position::where('abbr', $raw_datum->batter_pos)->first(); if ($position) { $player_position = PlayerPosition::where('player_id', $batter->id)->where('position_id', $position->id)->first(); if (!$player_position) { $player_position = new PlayerPosition(); $player_position->player_id = $batter->id; $player_position->position_id = $position->id; $player_position->save(); } } $pitch->balls = $raw_datum->balls; $pitch->strikes = $raw_datum->strikes; $pitch->outs = $raw_datum->outs; $pitch->man_on_first = $raw_datum->man_on_first == "TRUE"; $pitch->man_on_second = $raw_datum->man_on_second == "TRUE"; $pitch->man_on_third = $raw_datum->man_on_third == "TRUE"; $pitch->end_man_on_first = $raw_datum->end_man_on_first == "TRUE"; $pitch->end_man_on_second = $raw_datum->end_man_on_second == "TRUE"; $pitch->end_man_on_third = $raw_datum->end_man_on_third == "TRUE"; $pitch->visiting_team_current_runs = $raw_datum->visiting_team_current_runs; $pitch->home_team_current_runs = $raw_datum->home_team_current_runs; $pitch->pitch_result_id = $pitch_result->id; $pitch->pitch_type_id = $pitch_type->id; $pitch->release_velocity = $raw_datum->release_velocity; $pitch->spin_rate = $raw_datum->spin_rate; $pitch->spin_direction = $raw_datum->spin_direction; $pitch->px = $raw_datum->px; $pitch->pz = $raw_datum->pz; $pitch->szt = $raw_datum->szt; $pitch->szb = $raw_datum->szb; $pitch->x0 = $raw_datum->x0; $pitch->y0 = $raw_datum->y0; $pitch->z0 = $raw_datum->z0; $pitch->vx0 = $raw_datum->vx0; $pitch->vy0 = $raw_datum->vy0; $pitch->vz0 = $raw_datum->vz0; $pitch->ax = $raw_datum->ax; $pitch->ay = $raw_datum->ay; $pitch->az = $raw_datum->az; $pitch->pa_result_id = $plate_appearance_result->id; $pitch->runs_home = $raw_datum->runsHome; $pitch->batted_ball_type_id = $batted_ball_type->id; $pitch->batted_ball_angle = $raw_datum->batted_ball_angle; $pitch->batted_ball_distance = $raw_datum->batted_ball_distance; $pitch->atbat_desc = $raw_datum->atbat_desc; $pitch->timestamp_utc = time(); if (!$pitch->save()) { dd($raw_datum); } if ($raw_datum->id % 1000 == 0) { echo "Processed record number " . $raw_datum->id . "\n\r"; } $raw_datum->processed_utc = time(); $raw_datum->save(); } } }