Last active
November 4, 2019 21:20
-
-
Save yllus/d0a88533451884b838dfca697a3f2e16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Rules: | |
// 1) Get the top three teams by points from each division. | |
// 2) For each division, check to see if the 3rd place team in it has less points than the | |
// 4th place team of the other division (must have at least one point less, a tie means no crossover occurs). | |
// 2a) If it does, insert that 4th place team above the other division's 3rd place. | |
// 2b) Else leave the standings for that division as-is. | |
public static function get_crossover( $args ) { | |
// If the season request is < 2016, return an empty object. | |
if ( (int) $args['season'] < 1996 ) { | |
$response = new Response( array(), array('Crossover data is not available prior to the 1996 season.') ); | |
return $response; | |
} | |
$arr_result = Standings::do_query($args); | |
// If we get results back, create and return an array of Standing objects with the provided data. | |
// If no results are returned, get a list of existing teams and output them all zeroed out. | |
if ( sizeof($arr_result) > 0 ) { | |
$arr_standings = Standings::create_array($arr_result); | |
} | |
else { | |
$arr_standings = Standings::create_zeroed_out_array($args); | |
} | |
// Get a list of divisions in the results. | |
$arr_divisions = array(); | |
foreach ( $arr_standings->divisions as $division ) { | |
$division_id = $division->division_id; | |
if ( !isset($arr_divisions[$division_id]) ) { | |
$obj_division = new stdClass(); | |
$obj_division->id = $division_id; | |
$obj_division->slug = Standings::get_division_slug($division->division_name); | |
$obj_division->other_division_slug = Standings::get_opposing_division_slug($division->division_name); | |
$obj_division->contains_crossover_team = false; | |
$arr_divisions[] = $obj_division; | |
} | |
} | |
// Create new "crossover" and "out_of_playoffs" divisions. | |
$arr_standings->divisions['crossover'] = new stdClass(); | |
$arr_standings->divisions['crossover']->division_id = 0; | |
$arr_standings->divisions['crossover']->division_name = 'Crossover'; | |
$arr_standings->divisions['crossover']->division_slug = 'crossover'; | |
$arr_standings->divisions['crossover']->standings = array(); | |
$arr_standings->divisions['out_of_playoffs'] = new stdClass(); | |
$arr_standings->divisions['out_of_playoffs']->division_id = 0; | |
$arr_standings->divisions['out_of_playoffs']->division_name = 'Out'; | |
$arr_standings->divisions['out_of_playoffs']->division_slug = 'out_of_playoffs'; | |
$arr_standings->divisions['out_of_playoffs']->standings = array(); | |
// For each division, check to see if the 3rd place team in it has less points than the 4th place team | |
// of the other division. | |
foreach ( $arr_divisions as $division ) { | |
$team_division_third_place = $arr_standings->divisions[$division->slug]->standings[2]; | |
$team_other_division_fourth_place = $arr_standings->divisions[$division->other_division_slug]->standings[3]; | |
// If the condition matches, place the fourth place team in the other division at the top of our "crossover" division. | |
if ( $team_division_third_place->points < $team_other_division_fourth_place->points ) { | |
$arr_teams_to_crossover = array_splice($arr_standings->divisions[$division->other_division_slug]->standings, 3, 1); | |
$arr_standings->divisions['crossover']->standings[] = $arr_teams_to_crossover[0]; | |
// Flag that this division and season has a crossover team in it. | |
$division->contains_crossover_team = true; | |
} | |
} | |
// If the division we're iterating on contains a crossover team, cut it off at 2 teams. | |
// Else cut the division off at 3 teams. | |
foreach ( $arr_divisions as $division ) { | |
if ( $division->contains_crossover_team === true ) { | |
$arr_standings->divisions['out_of_playoffs']->standings = array_merge($arr_standings->divisions['out_of_playoffs']->standings, array_splice($arr_standings->divisions[$division->slug]->standings, 2)); | |
} | |
else { | |
$arr_standings->divisions['out_of_playoffs']->standings = array_merge($arr_standings->divisions['out_of_playoffs']->standings, array_splice($arr_standings->divisions[$division->slug]->standings, 3)); | |
} | |
} | |
// Sort the "out of playoffs" section on points and then wins. | |
$sort = array(); | |
foreach( $arr_standings->divisions['out_of_playoffs']->standings as $k => $v ) { | |
$sort['points'][$k] = $v->points; | |
$sort['wins'][$k] = $v->wins; | |
} | |
array_multisort($sort['points'], SORT_DESC, $sort['wins'], SORT_ASC, $arr_standings->divisions['out_of_playoffs']->standings); | |
$response = new Response($arr_standings, array(), null, $args); | |
return $response; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment