1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use App\Models\ProblemModel as OutdatedProblemModel; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use App\Models\ContestModel as OutdatedContestModel; |
9
|
|
|
use Cache; |
10
|
|
|
use DateTimeInterface; |
11
|
|
|
|
12
|
|
|
class Contest extends Model |
13
|
|
|
{ |
14
|
|
|
protected $table='contest'; |
15
|
|
|
protected $primaryKey='cid'; |
16
|
|
|
|
17
|
|
|
protected function serializeDate(DateTimeInterface $date) |
18
|
|
|
{ |
19
|
|
|
return $date->format('Y-m-d H:i:s'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getParsedRuleAttribute() |
23
|
|
|
{ |
24
|
|
|
$rule=["Unknown", "ICPC", "IOI", "Custom ICPC", "Custom IOI", "HASAAOSE Compulter Exam"]; |
25
|
|
|
return $rule[$this->rule]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function boot() |
29
|
|
|
{ |
30
|
|
|
parent::boot(); |
31
|
|
|
static::saving(function($model) { |
32
|
|
|
if ($model->custom_icon!="" && $model->custom_icon!=null && $model->custom_icon[0]!="/") { |
33
|
|
|
$model->custom_icon="/$model->custom_icon"; |
34
|
|
|
} |
35
|
|
|
if ($model->img!="" && $model->img!=null && $model->img[0]!="/") { |
36
|
|
|
$model->img="/$model->img"; |
37
|
|
|
} |
38
|
|
|
}); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
//Repository function |
42
|
|
|
public function participants($ignore_frozen=true) |
43
|
|
|
{ |
44
|
|
|
if ($this->registration) { |
45
|
|
|
$participants=ContestParticipant::where('cid', $this->cid)->get(); |
46
|
|
|
$participants->load('user'); |
47
|
|
|
$users=collect(); |
48
|
|
|
foreach ($participants as $participant) { |
49
|
|
|
$user=$participant->user; |
50
|
|
|
$users->add($user); |
51
|
|
|
} |
52
|
|
|
return $users->unique(); |
53
|
|
|
} else { |
54
|
|
|
$this->load('submissions.user'); |
55
|
|
|
if ($ignore_frozen) { |
56
|
|
|
$frozen_time=$this->frozen_time; |
57
|
|
|
$submissions=$this->submissions()->where('submission_date', '<', $frozen_time)->get(); |
58
|
|
|
} else { |
59
|
|
|
$submissions=$this->submissions; |
60
|
|
|
} |
61
|
|
|
$users=collect(); |
62
|
|
|
foreach ($submissions as $submission) { |
63
|
|
|
$user=$submission->user; |
64
|
|
|
$users->add($user); |
65
|
|
|
} |
66
|
|
|
return $users->unique(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Repository/Service? function |
71
|
|
|
public function rankRefresh() |
72
|
|
|
{ |
73
|
|
|
$ret=[]; |
74
|
|
|
$participants=$this->participants(); |
75
|
|
|
$contest_problems=$this->problems; |
76
|
|
|
$contest_problems->load('problem'); |
77
|
|
|
if ($this->rule==1) { |
78
|
|
|
// ACM/ICPC Mode |
79
|
|
|
foreach ($participants as $participant) { |
80
|
|
|
$prob_detail=[]; |
81
|
|
|
$totPen=0; |
82
|
|
|
$totScore=0; |
83
|
|
|
foreach ($contest_problems as $contest_problem) { |
84
|
|
|
$prob_stat=$contest_problem->userStatus($participant); |
85
|
|
|
$prob_detail[]=[ |
86
|
|
|
'ncode'=>$contest_problem->ncode, |
87
|
|
|
'pid'=>$contest_problem->pid, |
88
|
|
|
'color'=>$prob_stat['color'], |
89
|
|
|
'wrong_doings'=>$prob_stat['wrong_doings'], |
90
|
|
|
'solved_time_parsed'=>$prob_stat['solved_time_parsed'] |
91
|
|
|
]; |
92
|
|
|
if ($prob_stat['solved']) { |
93
|
|
|
$totPen+=$prob_stat['wrong_doings'] * 20; |
94
|
|
|
$totPen+=$prob_stat['solved_time'] / 60; |
95
|
|
|
$totScore+=$prob_stat['solved']; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$ret[]=[ |
99
|
|
|
"uid" => $participant->id, |
100
|
|
|
"name" => $participant->name, |
101
|
|
|
"nick_name" => DB::table("group_member")->where([ |
102
|
|
|
"uid" => $participant->id, |
103
|
|
|
"gid" => $this->group->gid |
104
|
|
|
])->where("role", ">", 0)->first()["nick_name"] ?? '', |
105
|
|
|
"score" => $totScore, |
106
|
|
|
"penalty" => $totPen, |
107
|
|
|
"problem_detail" => $prob_detail |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
usort($ret, function($a, $b) { |
111
|
|
|
if ($a["score"]==$b["score"]) { |
112
|
|
|
if ($a["penalty"]==$b["penalty"]) { |
113
|
|
|
return 0; |
114
|
|
|
} elseif (($a["penalty"]>$b["penalty"])) { |
115
|
|
|
return 1; |
116
|
|
|
} else { |
117
|
|
|
return -1; |
118
|
|
|
} |
119
|
|
|
} elseif ($a["score"]>$b["score"]) { |
120
|
|
|
return -1; |
121
|
|
|
} else { |
122
|
|
|
return 1; |
123
|
|
|
} |
124
|
|
|
}); |
125
|
|
|
Cache::tags(['contest', 'rank'])->put($this->cid, $ret, 60); |
126
|
|
|
return $ret; |
127
|
|
|
} else { |
128
|
|
|
// IO Mode |
129
|
|
|
$c=new OutdatedContestModel(); |
130
|
|
|
return $c->contestRankCache($this->cid); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function clarifications() |
135
|
|
|
{ |
136
|
|
|
return $this->hasMany('App\Models\Eloquent\ContestClarification', 'cid', 'cid'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function problems() |
140
|
|
|
{ |
141
|
|
|
return $this->hasMany('App\Models\Eloquent\ContestProblem', 'cid', 'cid')->orderBy('number', 'asc'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function submissions() |
145
|
|
|
{ |
146
|
|
|
return $this->hasMany('App\Models\Eloquent\Submission', 'cid', 'cid'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function group() |
150
|
|
|
{ |
151
|
|
|
return $this->hasOne('App\Models\Eloquent\Group', 'gid', 'gid'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function getFrozenTimeAttribute() |
155
|
|
|
{ |
156
|
|
|
$end_time=strtotime($this->end_time); |
157
|
|
|
return $end_time-$this->froze_length; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getIsEndAttribute() |
161
|
|
|
{ |
162
|
|
|
return strtotime($this->end_time)<time(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getProblemSet($renderLatex=false) |
166
|
|
|
{ |
167
|
|
|
$ret=[]; |
168
|
|
|
$problemset=$this->problems; |
169
|
|
|
foreach ($problemset as $problem) { |
170
|
|
|
$problemDetail=Problem::find($problem->pid); |
171
|
|
|
$problemRet=(new OutdatedProblemModel())->detail($problemDetail->pcode); |
172
|
|
|
if ($renderLatex) { |
173
|
|
|
foreach (['description', 'input', 'output', 'note'] as $section) { |
174
|
|
|
$problemRet['parsed'][$section]=latex2Image($problemRet['parsed'][$section]); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
$problemRet['index']=$problem->ncode; |
178
|
|
|
$problemRet['testcases']=$problemRet['samples']; |
179
|
|
|
unset($problemRet['samples']); |
180
|
|
|
$ret[]=$problemRet; |
181
|
|
|
} |
182
|
|
|
return $ret; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function isJudgingComplete() |
186
|
|
|
{ |
187
|
|
|
return $this->submissions->whereIn('verdict', ['Waiting', 'Pending'])->count()==0; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|