protected function updateTeamStats()
 {
     // Compile a list of all teams needing an update across all leagues
     $conditions = array('stats.needs_update' => true);
     $fields = array('league_id');
     $teams_needing_update = Teams::all(compact('conditions', 'fields'));
     $team_id_list = array();
     $team_stats = array();
     foreach ($teams_needing_update as $team) {
         $team_id_list[] = $team->_id;
         $team_stats[(string) $team->_id] = array('wins' => 0, 'losses' => 0, 'point_differential' => 0);
     }
     // Grab all of the games that apply to these teams, standings should be re-calced from the ground up.
     $conditions = array('teams' => array('$in' => $team_id_list), 'scores' => array('$exists' => true), 'winner' => array('$ne' => null));
     $fields = array('scores', 'winner', 'teams', 'league_id');
     $relevant_games_list = Games::all(compact('conditions', 'fields'));
     $league_list = array();
     foreach ($relevant_games_list as $game) {
         // Calculate stats for each team
         foreach ($game->getTeams() as $t) {
             $team_id = (string) $t->_id;
             if (!isset($team_stats[$team_id])) {
                 // If we didn't pull all of this team's games, do not update their stats.
                 continue;
             }
             if ($t->_id == $game->winner) {
                 $team_stats[$team_id]['wins']++;
                 $team_stats[$team_id]['point_differential'] += $game->getScoreDiff();
             } else {
                 $team_stats[$team_id]['losses']++;
                 $team_stats[$team_id]['point_differential'] -= $game->getScoreDiff();
             }
         }
         // Make sure we have a list of all of the leagues being updated
         $league_list[] = $game->league_id;
     }
     // Do the team stats updates:
     foreach ($team_stats as $team_id => $stats) {
         $conditions = array('_id' => new \MongoId($team_id));
         $query = array('$set' => array('stats' => $stats));
         Teams::update($query, $conditions);
     }
     // Mark leagues as needing an update
     $league_list = array_unique($league_list);
     $conditions = array('_id' => array('$in' => $league_list));
     $query = array('$set' => array('needs_standings_update' => true));
     Leagues::update($query, $conditions);
 }
Beispiel #2
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');
 }