public function index() { $l_list = Leagues::all(array('order' => array('start_date' => -1, 'end_date' => 1))); $leagues = array('past' => array(), 'present' => array(), 'future' => array()); foreach ($l_list as $l) { $commissioner_names = array(); foreach ($l->getCommissioners() as $c) { $commissioner_names[] = $c->firstname . ' ' . $c->lastname; } $league_data = $l->to('array'); $league_data['commissioners'] = implode(', ', $commissioner_names); $league_data['meta']['registration_open'] = $l->registrationOpen() ?: false; if ($l->start_date->sec > time()) { $leagues['future'][] = $league_data; } else { if ($l->end_date->sec < time()) { $leagues['past'][] = $league_data; } else { $leagues['present'][] = $league_data; } } } return compact('leagues'); }
protected function updateLeagueStandings() { // Compile a list of all teams needing an update across all leagues $conditions = array('needs_standings_update' => true); $fields = array('_id'); $leagues_needing_update = Leagues::all(compact('conditions', 'fields')); foreach ($leagues_needing_update as $l) { $team_list = array(); $conditions = array('league_id' => $l->_id); $leage_teams = Teams::all(compact('conditions')); foreach ($leage_teams as $t) { $team_list[] = $t; } usort($team_list, function ($a, $b) { // One of the team has no reported scores, other team wins by default. if (is_object($a->stats) and !is_object($b->stats)) { return 1; } else { if (is_object($a->stats) and !is_object($b->stats)) { return -1; } else { if (!is_object($a->stats) and !is_object($b->stats)) { return 0; } } } // Criteria #1: winning percentage $a_pct = $a->stats->wins / ($a->stats->wins + $a->stats->losses); $b_pct = $b->stats->wins / ($b->stats->wins + $b->stats->losses); if ($a_pct != $b_pct) { // Needs to return an int, these percentages won't be ints. return intval(($a_pct - $b_pct) * 100); } // Criteria #2: Number of wins if ($a->stats->wins != $b->stats->wins) { return $a->stats->wins - $b->stats->wins; } // Criteria #3: Head-to-head wins / Criteria #4: Head-to-head point-diff $conditions = array('teams' => array($a->_id, $b->_id), 'scores' => array('$exists' => true)); $joint_games = Games::all(compact('conditions')); $head_to_head_wins = 0; $head_to_head_pnts = 0; foreach ($joint_games as $g) { // Note: Lithium does not support $and yet, so we can't do this optimally if ($a->_id != $g->teams[0] and $a->_id != $g->teams[1] or $b->_id != $g->teams[0] and $b->_id != $g->teams[1]) { // This game isn't head-to-head, doesn't count; continue; } if ($g->winner == $a->_id) { $head_to_head_wins++; $head_to_head_pnts += $g->getScoreDiff(); } else { if ($g->winner == $b->_id) { $head_to_head_wins--; $head_to_head_pnts -= $g->getScoreDiff(); } } } if ($head_to_head_wins != 0) { return $head_to_head_wins; } if ($head_to_head_pnts != 0) { return $head_to_head_pnts; } // Criteria #5: Head-to-head point-diff if ($a->stats->point_differential != $b->stats->point_differential) { return $a->stats->point_differential - $b->stats->point_differential; } // Still tied? $a->stats->tied = true; $b->stats->tied = true; return 0; }); // Assign their ranks, handle ties correctly: $rank = 0; $tied_rank = null; foreach (array_reverse($team_list) as $t) { $rank++; if (!isset($tied_rank) and $t->stats->tied == true) { $tied_rank = $rank; } else { if ($t->stats->tied == false) { $tied_rank = null; } } $display_rank = $tied_rank ?: $rank; $t->save(array('stats.rank' => $display_rank)); } } }