protected function edit() { $league = Leagues::find((string) $this->game->league_id); if (!$this->CURRENT_USER or !$league->isManager($this->CURRENT_USER)) { $this->flashMessage('You don\'t have permission to view that page.', array('alertType' => 'error')); return $this->redirect('/'); } if (!isset($this->game)) { $this->flashMessage('Game not found.', array('alertType' => 'error')); $this->redirect('/'); return; } if ($this->game->game_time->sec < time()) { $this->flashMessage('This game is in the past! Any reported scores will be erased if this game is edited.', array('alertType' => 'warning')); } if ($this->request->data) { $gameStartTime = $this->request->data['game_date'] . ' ' . $this->request->data['game_time']; unset($this->request->data['game_date']); unset($this->request->data['game_time']); $this->request->data['game_time'] = strtotime($gameStartTime); $this->game->set($this->request->data); if ($this->game->save()) { // Handle game with scores already if ($this->game->scores) { $old_score_report = $this->game->scores->export(); $old_score_report = $old_score_report['data']; if (!empty($old_score_report)) { // Log the previously-reported score $conditions = array('_id' => $this->game->_id); $query = array('$push' => array('old_scores' => $old_score_report)); Games::update($query, $conditions); // Remove the scores and winner $query = array('$unset' => array('scores' => 1, 'winner' => 1)); Games::update($query, $conditions); } } $this->flashMessage('The game has been updated successfully.', array('alertType' => 'success')); return $this->redirect(array('Leagues::view', 'id' => $league->_id)); } } return compact('league'); }
public function reportScore() { $game = Games::find($this->request->id); if (!$game) { $redirectUrl = $this->request->env('HTTP_REFERER') ?: '/'; $this->flashMessage('Game not found.', array('alertType' => 'error')); return $this->redirect($redirectUrl); } if (!isset($this->CURRENT_USER) or !$game->canReport($this->CURRENT_USER)) { $redirectUrl = $this->request->env('HTTP_REFERER') ?: '/'; $this->flashMessage('You do not have permission to report scores for that game.', array('alertType' => 'error')); return $this->redirect($redirectUrl); } if ($this->request->data) { $doSave = true; if ($game->scores) { $old_score_report = $game->scores->export(); $old_score_report = $old_score_report['data']; } $team_ids = array(); // NOTE: This code also lives in the Games Controller (for editing games that already have scores) // This should probably be abstracted and put in the games model $new_score_report = array('reporter_id' => $this->CURRENT_USER->_id, 'report_time' => time(), 'reporter_ip' => $this->request->env('REMOTE_ADDR'), 'rainout' => false, 'forfeit' => false); if (isset($this->request->data['rainout'])) { $new_score_report['rainout'] = true; } else { if (isset($this->request->data['forfeit'])) { # make sure winner is in team list # save winner with forfeit score (wherever that is) $doSave = false; } else { $max_score = -1; $winner_id = null; foreach ($this->request->data['scores'] as $id => $score) { $score = abs(intval($score)); if ($score > $max_score) { $max_score = $score; $winner_id = $id; } else { if ($score == $max_score) { // This means we have a tie, somehow $winner_id = null; } } $new_score_report[$id] = $score; $team_ids[] = new \MongoId($id); } } } $game->scores = $new_score_report; $game->winner = $winner_id; if ($doSave) { if ($game->save()) { if (isset($old_score_report)) { // Log the previously-reported score $conditions = array('_id' => $game->_id); $query = array('$push' => array('old_scores' => $old_score_report)); Games::update($query, $conditions); } // Mark teams as needing an update $conditions = array('_id' => array('$in' => $team_ids)); $query = array('$set' => array('stats.needs_update' => true)); Teams::update($query, $conditions, array('multiple' => true)); $this->flashMessage('Your score has been successfully reported.', array('alertType' => 'success')); return $this->redirect($this->request->env('HTTP_REFERER') ?: '/'); } } } $league = $game->getLeague(); $teams = array(); foreach ($game->teams as $t_id) { $teams[(string) $t_id] = Teams::find((string) $t_id); } return compact('game', 'league', 'teams'); }