public function actionQuit() { $id = Yii::app()->request->getParam('id'); if (!$this->_userinfo()) { $this->json_return(false, 'need_login', '/?refer=' . urlencode($this->createUrl('tournament/detail', array('id' => $id)))); } $tournament = Tournament::model()->findByPk($id); if (!$tournament || $tournament->status != 'signing') { $this->json_return(false, '只有尚未开始的比赛才能取消报名。'); } $joined = Tournament_join::model()->find("tid={$id} and uid=" . $this->_userinfo()->id); if (!$joined) { $this->json_return(false, '您还没有报过名。'); } $quit_sn = $joined->t_sn; $joined->delete(); Tournament_join::model()->getDbConnection()->createCommand('update tournament_join set t_sn=t_sn-1 where t_sn>' . $quit_sn . ' and tid=' . $id)->execute(); TournamentTool::setUpTournament($tournament); $this->json_return(true); }
/** * 比赛结束,计算总分和名次,处理比赛状态。 */ public static function end_tournament($id) { $tournament = Tournament::model()->findByPk($id); if (!$tournament) { return false; } $total_game = $tournament->player_joined * ($tournament->player_joined - 1); if ($tournament->t_kind == 'single') { $total_game = $total_game / 2; } $tour_games = Games::model()->findAll("`status` in ('黑胜','白胜','和棋') and tid={$id}"); //如果所有比赛都结束了,则处理比赛结束的逻辑。 if ($total_game == count($tour_games)) { $tj = Tournament_join::model()->findAll("tid={$id}"); $users_score = array(); foreach ($tj as $v) { $users_score[$v->uid] = array('score' => 0, 'win' => 0); } //遍历game,算分和胜。 foreach ($tour_games as $g) { switch ($g->status) { case '黑胜': $users_score[$g->black_id]['score']++; $users_score[$g->black_id]['win']++; break; case '白胜': $users_score[$g->white_id]['score']++; $users_score[$g->white_id]['win']++; break; case '和棋': $users_score[$g->black_id]['score'] += 0.5; $users_score[$g->white_id]['score'] += 0.5; break; } } foreach ($tj as $v) { $v->t_score = $users_score[$v->uid]['score']; $v->win_game = $users_score[$v->uid]['win']; $v->save(); } //这里就直接按照积分和胜局排顺序了,不考虑直胜问题。 $tj = Tournament_join::model()->findAll("tid={$id} order by t_score desc,win_game desc"); foreach ($tj as $k => $v) { $v->rating = $k + 1; $v->save(); } $tournament->status = 'ended'; $tournament->save(); } Yii::app()->cache->delete(self::CACHE_PREFIX . $tournament->id); }