Skip to content

Commit

Permalink
添加显示比赛期间榜单的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
shi-yang committed Jun 8, 2019
1 parent 2e04307 commit 28cca00
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 26 deletions.
20 changes: 17 additions & 3 deletions controllers/ContestController.php
Expand Up @@ -361,8 +361,11 @@ public function actionClarify($id, $cid = -1)

/**
* 比赛榜单
* @param integer $id
* @return mixed
* @param $id
* @return string
* @throws ForbiddenHttpException
* @throws NotFoundHttpException
* @throws \yii\db\Exception
*/
public function actionStanding($id)
{
Expand All @@ -371,8 +374,19 @@ public function actionStanding($id)
if (!$model->canView()) {
return $this->render('/contest/forbidden', ['model' => $model]);
}
$showStandingBeforeEnd = 0;
if (Yii::$app->request->get('showStandingBeforeEnd')) {
$showStandingBeforeEnd = Yii::$app->request->get('showStandingBeforeEnd');
}
if ($showStandingBeforeEnd) {
$rankResult = $model->getRankData(true);
} else {
$rankResult = $model->getRankData(true, time());
}
return $this->render('/contest/standing', [
'model' => $model
'model' => $model,
'rankResult' => $rankResult,
'showStandingBeforeEnd' => $showStandingBeforeEnd
]);
}

Expand Down
29 changes: 22 additions & 7 deletions models/Contest.php
Expand Up @@ -316,11 +316,26 @@ public function getSubmissionStatistics()
/**
* 获取比赛排名数据
* @param bool $lock 是否获取封榜的数据
* @param null $endtime 在此时间之前的榜单
* @param null $endtime Unix 时间戳格式,在此时间之前的榜单
* @return array
* @throws \yii\db\Exception
*/
public function getRankData($lock = true, $endtime = null)
{
if ($this->type == Contest::TYPE_OI || $this->type == Contest::TYPE_IOI) {
return $this->getOIRankData($lock, $endtime);
}
return $this->getICPCRankData($lock, $endtime);
}

/**
* 获取ICPC比赛排名数据
* @param bool $lock 是否获取封榜的数据
* @param null $endtime 在此时间之前的榜单
* @return array
* @throws \yii\db\Exception
*/
public function getICPCRankData($lock = true, $endtime = null)
{
$users_solution_data = $this->getUsersSolution();
$users = $this->getContestUser();
Expand All @@ -330,11 +345,11 @@ public function getRankData($lock = true, $endtime = null)
$submit_count = [];
$problem_ids = [];
$count = count($users_solution_data);
$start_time = $this->start_time;
$start_time = strtotime($this->start_time);
$lock_time = 0x7fffffff;
$contest_end_time = strtotime($this->end_time);
if ($endtime == null) {
$endtime = strtotime($contest_end_time);
$endtime = $contest_end_time;
}

foreach ($problems as $problem) {
Expand All @@ -353,14 +368,14 @@ public function getRankData($lock = true, $endtime = null)
}

if (!empty($this->lock_board_time)) {
$lock_time = $this->lock_board_time;
$lock_time = strtotime($this->lock_board_time);
}

for ($i = 0; $i < $count; $i++) {
$row = $users_solution_data[$i];
$user = $row['user_id'];
$pid = $row['problem_id'];
$created_at = $row['created_at'];
$created_at = strtotime($row['created_at']);
if ($created_at > $endtime) {
break;
}
Expand Down Expand Up @@ -399,7 +414,7 @@ public function getRankData($lock = true, $endtime = null)
$submit_count[$pid]['submit']++;

// 封榜,比赛结束后的一定时间解榜,解榜时间 scoreboardFrozenTime 变量的设置详见后台设置页面
if ($lock && strtotime($lock_time) <= strtotime($created_at) &&
if ($lock && $lock_time <= $created_at &&
time() <= $contest_end_time + Yii::$app->setting->get('scoreboardFrozenTime')) {
++$result[$user]['pending'][$pid];
continue;
Expand All @@ -416,7 +431,7 @@ public function getRankData($lock = true, $endtime = null)
}
$first_blood[$pid] = $user;
}
$sec = strtotime($created_at) - strtotime($start_time);
$sec = $created_at - $start_time;
++$result[$user]['solved'];
// 单人赛计分,详见 view/wiki/contest.php。
if ($this->type == self::TYPE_RANK_SINGLE) {
Expand Down
10 changes: 10 additions & 0 deletions modules/admin/views/contest/oi_rank.php
Expand Up @@ -15,6 +15,16 @@
?>
<div class="wrap">
<div class="container">
<div class="alert alert-warning alert-dismissible fade in hidden-print" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<p>提示:</p>
<ul>
<li>可以使用浏览器自带的打印功能(Chrome 浏览器可在页面上鼠标“右键”-“打印”,其它浏览器请自行利用搜索引擎获取使用方法),
可以选择将此页面导出为 PDF 格式。此提示信息不会出现在浏览器的打印窗口中。</li>
<li>比赛期间,若设了封榜,即使到了封榜期间,此榜单依然为实时榜单。</li>
<li>比赛结束后,由于前台依然能够开放提交,此榜单为比赛期间的榜单。</li>
</ul>
</div>
<div class="row">
<div class="col-md-3 text-left">
<strong>Start </strong>
Expand Down
10 changes: 10 additions & 0 deletions modules/admin/views/contest/rank.php
Expand Up @@ -16,6 +16,16 @@

<div class="wrap">
<div class="container">
<div class="alert alert-warning alert-dismissible fade in hidden-print" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<p>提示:</p>
<ul>
<li>可以使用浏览器自带的打印功能(Chrome 浏览器可在页面上鼠标“右键”-“打印”,其它浏览器请自行利用搜索引擎获取使用方法),
可以选择将此页面导出为 PDF 格式。此提示信息不会出现在浏览器的打印窗口中。</li>
<li>比赛期间,若设了封榜,即使到了封榜期间,此榜单依然为实时榜单。</li>
<li>比赛结束后,由于前台依然能够开放提交,此榜单为比赛期间的榜单。</li>
</ul>
</div>
<div class="row">
<div class="col-md-3 text-left">
<strong>Start </strong>
Expand Down
8 changes: 4 additions & 4 deletions views/contest/_standing_group.php
Expand Up @@ -5,12 +5,12 @@
use yii\bootstrap\Modal;

/* @var $model app\models\Contest */
/* @var $rankResult array */

$problems = $model->problems;
$rank_result = $model->getRankData(true, time());
$first_blood = $rank_result['first_blood'];
$result = $rank_result['rank_result'];
$submit_count = $rank_result['submit_count'];
$first_blood = $rankResult['first_blood'];
$result = $rankResult['rank_result'];
$submit_count = $rankResult['submit_count'];
?>
<?php if ($model->isScoreboardFrozen()): ?>
<p>现已是封榜状态,榜单将不再实时更新,待赛后再揭晓</p>
Expand Down
8 changes: 4 additions & 4 deletions views/contest/_standing_oi.php
Expand Up @@ -6,12 +6,12 @@
use app\models\Contest;

/* @var $model app\models\Contest */
/* @var $rankResult array */

$problems = $model->problems;
$rank_result = $model->getOIRankData(true, time());
$first_blood = $rank_result['first_blood'];
$result = $rank_result['rank_result'];
$submit_count = $rank_result['submit_count'];
$first_blood = $rankResult['first_blood'];
$result = $rankResult['rank_result'];
$submit_count = $rankResult['submit_count'];
?>
<?php if ($model->isScoreboardFrozen()) {
echo '<p>待赛后再揭晓</p>';
Expand Down
8 changes: 4 additions & 4 deletions views/contest/_standing_single.php
Expand Up @@ -6,12 +6,12 @@
use app\models\User;

/* @var $model app\models\Contest */
/* @var $rankResult array */

$problems = $model->problems;
$rank_result = $model->getRankData(true, time());
$first_blood = $rank_result['first_blood'];
$result = $rank_result['rank_result'];
$submit_count = $rank_result['submit_count'];
$first_blood = $rankResult['first_blood'];
$result = $rankResult['rank_result'];
$submit_count = $rankResult['submit_count'];
?>
<?php if ($model->isScoreboardFrozen()): ?>
<p>现已是封榜状态,榜单将不再实时更新,待赛后再揭晓</p>
Expand Down
38 changes: 34 additions & 4 deletions views/contest/standing.php
@@ -1,17 +1,41 @@
<?php

use app\models\Contest;
use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model app\models\Contest */
/* @var $form yii\widgets\ActiveForm */
/* @var $data array */
/* @var $showStandingBeforeEnd bool */
/* @var $rankResult array */

$this->title = $model->title;
$this->params['model'] = $model;

$js =<<<EOT
$(".toggle-show-contest-standing input[name='showStandingBeforeEnd']").change(function () {
$(".toggle-show-contest-standing").submit();
});
EOT;
$this->registerJs($js);
?>
<div class="contest-overview text-center center-block">
<?php if ($model->type != Contest::TYPE_OI || $model->getRunStatus() == Contest::STATUS_ENDED): ?>
<div class="legend-strip">
<?php if ($model->getRunStatus() == Contest::STATUS_ENDED): ?>
<?= Html::beginForm(
['/contest/standing', 'id' => $model->id],
'get',
['class' => 'toggle-show-contest-standing pull-left', 'style' => 'margin-top: 6px;']
); ?>
<div class="checkbox">
<label>
<?= Html::checkbox('showStandingBeforeEnd', $showStandingBeforeEnd) ?>
显示比赛期间榜单
</label>
</div>
<?= Html::endForm(); ?>
<?php endif; ?>
<div class="pull-right table-legend">
<?php if ($model->type != Contest::TYPE_OI && $model->type != Contest::TYPE_IOI): ?>
<div>
Expand Down Expand Up @@ -48,15 +72,21 @@
<?php
if ($model->type == $model::TYPE_RANK_SINGLE) {
echo $this->render('_standing_single', [
'model' => $model
'model' => $model,
'showStandingBeforeEnd' => $showStandingBeforeEnd,
'rankResult' => $rankResult
]);
} else if ($model->type == $model::TYPE_OI || $model->type == $model::TYPE_IOI) {
echo $this->render('_standing_oi', [
'model' => $model
'model' => $model,
'showStandingBeforeEnd' => $showStandingBeforeEnd,
'rankResult' => $rankResult
]);
} else {
echo $this->render('_standing_group', [
'model' => $model
'model' => $model,
'showStandingBeforeEnd' => $showStandingBeforeEnd,
'rankResult' => $rankResult
]);
}
?>
Expand Down

0 comments on commit 28cca00

Please sign in to comment.