Exemple #1
0
 public function getTeams($entity)
 {
     if (!$entity->exists()) {
         return null;
     }
     $conditions = array('players' => $entity->_id);
     $order = array('league_id' => 1);
     $list = Teams::all(compact('conditions', 'order'));
     return $list;
 }
Exemple #2
0
 public function index()
 {
     // TODO: Security? teams.search
     if (isset($this->request->query['q'])) {
         $query = $this->request->query['q'];
         $conditions = array('name' => array('$regex' => "{$query}", '$options' => 'i'));
         $teamList = Teams::all(compact('conditions'));
     }
     return compact('teamList', 'query');
 }
Exemple #3
0
 public function getTeams($entity)
 {
     $team_ids = $entity->teams->export();
     $conditions = array('_id' => array('$in' => $team_ids['data']));
     return Teams::all(compact('conditions'));
 }
 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));
         }
     }
 }