예제 #1
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');
 }
예제 #2
0
파일: Users.php 프로젝트: aniston/Platypus
 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;
 }
예제 #3
0
    ?>
        <?php 
    if (is_object($g->game_time) and $g->game_time->sec <= time() and $g->canReport($CURRENT_USER)) {
        $reportScoreLink = $this->html->link('<i title="Report Score" class="hasTooltip icon-edit"></i>', array('Leagues::reportScore', 'id' => $g->_id), array('escape' => false));
    } else {
        $reportScoreLink = '';
    }
    $fs = $g->getFieldsite();
    $teams = $g->teams;
    for ($i = 0; $i < 2; $i++) {
        $t = isset($teams[$i]) ? (string) $teams[$i] : null;
        $thisTeam = null;
        if (isset($t) and isset($teamCache[$t])) {
            $thisTeam = $teamCache[$t];
        } elseif (isset($t)) {
            $thisTeam = Teams::find($t);
            $teamCache[$t] = $thisTeam;
        }
        if (isset($thisTeam)) {
            echo "<td>" . $this->html->link($thisTeam->name, array('Teams::view', 'id' => $thisTeam->_id)) . "</td>";
            $team_id = $thisTeam->_id;
            if (isset($g->scores->{$team_id})) {
                $scoreVar = "score_{$i}";
                ${$scoreVar} = $g->scores->{$team_id};
            }
        } else {
            echo "<td>n/a</td>";
        }
    }
    ?>
예제 #4
0
 public function importRoster()
 {
     if (!isset($this->CURRENT_USER) or !$this->league->isManager($this->CURRENT_USER)) {
         $redirectUrl = $this->request->env('HTTP_REFERER') ?: '/';
         $this->flashMessage('You do not have permission to check this league\'s schedule.', array('alertType' => 'error'));
         return $this->redirect($redirectUrl);
     }
     $lineErrors = array();
     $errors = array();
     $playerList = array();
     $rosters = array();
     if (isset($this->request->data['draft_results'])) {
         $draft_results = $this->request->data['draft_results'];
         $draftLines = explode("\n", trim($draft_results));
         $line = 0;
         foreach ($draftLines as $l) {
             $line++;
             $fields = explode(",", $l);
             // Check field count
             if (count($fields) != 2) {
                 $errors["{$line}"] = 'Wrong number of fields';
                 continue;
             }
             $team_id = trim($fields[0]);
             $user_id = trim($fields[1]);
             if (!isset($rosters[$team_id])) {
                 $team = Teams::find($team_id);
                 if (!isset($team) or !$team->exists()) {
                     $errors["{$line}"] = "Team with the ID '{$team_id}' not found.";
                     continue;
                 }
                 if ($team->league_id != $this->league->_id) {
                     $errors["{$line}"] = "Team with the ID '{$team_id}' is not in this league.";
                     continue;
                 }
                 $rosters[$team_id] = array();
             }
             if (isset($playerList[$user_id])) {
                 $errors["{$line}"] = "User with ID '{$user_id}' has been assigned multiple times.";
                 continue;
             }
             $user = Users::find($user_id);
             if (!isset($user) or !$user->exists()) {
                 $errors["{$line}"] = "User with the ID '{$user_id}' not found.";
                 continue;
             }
             $playerList[$user_id] = new \MongoId($team_id);
             $rosters[$team_id][] = new \MongoId($user_id);
         }
         if (empty($errors)) {
             // Assign Team Rosters
             foreach ($rosters as $team_id => $roster) {
                 $conditions = array('_id' => new \MongoId($team_id));
                 $query = array('$set' => array('players' => $roster));
                 Teams::update($query, $conditions);
             }
             // Assign Player Teams
             foreach ($playerList as $user_id => $team_id) {
                 $conditions = array('_id' => new \MongoId($user_id));
                 $query = array('$addToSet' => array('teams' => $team_id));
                 Users::update($query, $conditions);
             }
             $this->flashMessage('Roster Imported Successfully!', array('alertType' => 'success'));
             return $this->redirect(array('Leagues::view', 'id' => $this->league->_id));
         }
     } else {
         $draft_results = '';
     }
     return compact('draft_results', 'errors', 'rosters');
 }
예제 #5
0
파일: Games.php 프로젝트: aniston/Platypus
 public function getTeams($entity)
 {
     $team_ids = $entity->teams->export();
     $conditions = array('_id' => array('$in' => $team_ids['data']));
     return Teams::all(compact('conditions'));
 }
예제 #6
0
 public function teams(Teams $teams)
 {
     $this->data['teams'] = $teams->getActive();
     return view('pages.teams', $this->data);
 }
예제 #7
0
 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));
         }
     }
 }