Total Complexity | 277 |
Total Lines | 1913 |
Duplicated Lines | 0 % |
Changes | 14 | ||
Bugs | 6 | Features | 1 |
Complex classes like ContestModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContestModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ContestModel extends Model |
||
15 | { |
||
16 | protected $tableName='contest'; |
||
17 | protected $table='contest'; |
||
18 | protected $primaryKey='cid'; |
||
19 | const DELETED_AT=null; |
||
20 | const UPDATED_AT=null; |
||
21 | const CREATED_AT=null; |
||
22 | |||
23 | public $rule=["Unknown", "ICPC", "OI", "Custom ICPC", "Custom OI"]; |
||
24 | |||
25 | public function calcLength($a, $b) |
||
26 | { |
||
27 | $s=strtotime($b)-strtotime($a); |
||
28 | $h=intval($s / 3600); |
||
29 | $m=round(($s-$h * 3600) / 60); |
||
30 | if ($m==60) { |
||
31 | $h++; |
||
32 | $m=0; |
||
33 | } |
||
34 | if ($m==0 && $h==0) { |
||
35 | $text="$s Seconds"; |
||
36 | } elseif ($m==0) { |
||
37 | $text="$h Hours"; |
||
38 | } elseif ($h==0) { |
||
39 | $text="$m Minutes"; |
||
40 | } else { |
||
41 | $text="$h Hours $m Minutes"; |
||
42 | } |
||
43 | return $text; |
||
44 | } |
||
45 | |||
46 | public function canViewContest($cid, $uid) |
||
47 | { |
||
48 | $contest_detail=DB::table($this->tableName)->where([ |
||
49 | "cid"=>$cid |
||
50 | ])->first(); |
||
51 | |||
52 | if ($contest_detail["public"]==1) { |
||
53 | return $contest_detail; |
||
54 | } else { |
||
55 | // group contest |
||
56 | if ($uid==0) { |
||
57 | return []; |
||
58 | } |
||
59 | $group_info=DB::table("group_member")->where([ |
||
60 | "uid"=>$uid, |
||
61 | "gid"=>$contest_detail['gid'], |
||
62 | ["role", ">", 0] |
||
63 | ])->first(); |
||
64 | return empty($group_info) ? [] : $contest_detail; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | public function basic($cid) |
||
69 | { |
||
70 | return DB::table($this->tableName)->where([ |
||
71 | "cid"=>$cid |
||
72 | ])->first(); |
||
73 | } |
||
74 | |||
75 | public function detail($cid, $uid=0) |
||
76 | { |
||
77 | $contest_clearance=$this->judgeOutSideClearance($cid, $uid); |
||
78 | $contest_detail=$this->basic($cid); |
||
79 | |||
80 | if ($contest_clearance==0) { |
||
81 | return [ |
||
82 | "ret"=>1000, |
||
83 | "desc"=>"You have no right to view this contest.", |
||
84 | "data"=>null |
||
85 | ]; |
||
86 | } else { |
||
87 | $contest_detail["rule_parsed"]=$this->rule[$contest_detail["rule"]]; |
||
88 | $contest_detail["date_parsed"]=[ |
||
89 | "date"=>date_format(date_create($contest_detail["begin_time"]), 'j'), |
||
90 | "month_year"=>date_format(date_create($contest_detail["begin_time"]), 'M, Y'), |
||
91 | ]; |
||
92 | $contest_detail["length"]=$this->calcLength($contest_detail["begin_time"], $contest_detail["end_time"]); |
||
93 | $contest_detail["description_parsed"]=clean(convertMarkdownToHtml($contest_detail["description"])); |
||
94 | $contest_detail["group_info"]=DB::table("group")->where(["gid"=>$contest_detail["gid"]])->first(); |
||
95 | $contest_detail["problem_count"]=DB::table("contest_problem")->where(["cid"=>$cid])->count(); |
||
96 | return [ |
||
97 | "ret"=>200, |
||
98 | "desc"=>"succeed", |
||
99 | "data"=>[ |
||
100 | "contest_detail"=>$contest_detail |
||
101 | ] |
||
102 | ]; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | public function gid($cid) |
||
107 | { |
||
108 | return DB::table($this->tableName)->where([ |
||
109 | "cid"=>$cid |
||
110 | ])->first()["gid"]; |
||
111 | } |
||
112 | |||
113 | public function gcode($cid) |
||
114 | { |
||
115 | $gid = $this->gid($cid); |
||
116 | return DB::table('group')->where('gid','=',$gid)->first()["gcode"]; |
||
117 | } |
||
118 | |||
119 | public function runningContest() |
||
120 | { |
||
121 | return DB::select("select * from contest where begin_time < SYSDATE() and end_time > SYSDATE()"); |
||
122 | } |
||
123 | |||
124 | public function updateCrawlStatus($cid) { |
||
125 | return DB::table("contest")->where("cid", $cid)->update([ |
||
126 | "crawled"=>1, |
||
127 | ]); |
||
128 | } |
||
129 | |||
130 | public function grantAccess($uid, $cid, $audit=0) |
||
131 | { |
||
132 | return DB::table('contest_participant')->insert([ |
||
133 | "cid"=>$cid, |
||
134 | "uid"=>$uid, |
||
135 | "audit"=>$audit |
||
136 | ]); |
||
137 | } |
||
138 | |||
139 | public function listForSetting($gid) |
||
140 | { |
||
141 | $uid = Auth::user()->id; |
||
142 | $group_contests = DB::table('contest') |
||
143 | ->where('gid',$gid) |
||
144 | ->orderBy('begin_time','desc') |
||
145 | ->get()->all(); |
||
146 | $groupModel = new GroupModel(); |
||
147 | $group_clearance = $groupModel->judgeClearance($gid,$uid); |
||
148 | foreach ($group_contests as &$contest) { |
||
149 | $contest['is_admin'] = ($contest['assign_uid'] == $uid || $group_clearance == 3); |
||
150 | $contest['begin_stamps'] = strtotime($contest['begin_time']); |
||
151 | $contest['end_stamps'] = strtotime($contest['end_time']); |
||
152 | $contest['status'] = time() >= $contest['end_stamps'] ? 1 |
||
153 | : (time() <= $contest['begin_stamps'] ? -1 : 0); |
||
154 | $contest["rule_parsed"]=$this->rule[$contest["rule"]]; |
||
155 | $contest["date_parsed"]=[ |
||
156 | "date"=>date_format(date_create($contest["begin_time"]), 'j'), |
||
157 | "month_year"=>date_format(date_create($contest["begin_time"]), 'M, Y'), |
||
158 | ]; |
||
159 | $contest["length"]=$this->calcLength($contest["begin_time"], $contest["end_time"]); |
||
160 | } |
||
161 | usort($group_contests,function($a,$b){ |
||
162 | if($a['is_admin'] == $b['is_admin']){ |
||
163 | return $b['begin_stamps'] - $a['begin_stamps']; |
||
164 | } |
||
165 | return $b['is_admin'] - $a['is_admin']; |
||
166 | }); |
||
167 | return $group_contests; |
||
168 | } |
||
169 | |||
170 | public function listByGroup($gid) |
||
171 | { |
||
172 | // $contest_list=DB::table($this->tableName)->where([ |
||
173 | // "gid"=>$gid |
||
174 | // ])->orderBy('begin_time', 'desc')->get()->all(); |
||
175 | $preQuery=DB::table($this->tableName); |
||
176 | $paginator=$preQuery->where('gid','=',$gid)->orderBy('begin_time', 'desc')->paginate(10); |
||
177 | $contest_list=$paginator->all(); |
||
178 | if(empty($contest_list)){ |
||
179 | return null; |
||
180 | } |
||
181 | |||
182 | foreach ($contest_list as &$c) { |
||
183 | $c["rule_parsed"]=$this->rule[$c["rule"]]; |
||
184 | $c["date_parsed"]=[ |
||
185 | "date"=>date_format(date_create($c["begin_time"]), 'j'), |
||
186 | "month_year"=>date_format(date_create($c["begin_time"]), 'M, Y'), |
||
187 | ]; |
||
188 | $c["length"]=$this->calcLength($c["begin_time"], $c["end_time"]); |
||
189 | } |
||
190 | return [ |
||
191 | 'paginator' => $paginator, |
||
192 | 'contest_list' => $contest_list, |
||
193 | ]; |
||
194 | } |
||
195 | |||
196 | public function rule($cid) |
||
197 | { |
||
198 | return DB::table($this->tableName)->where([ |
||
199 | "cid"=>$cid |
||
200 | ])->first()["rule"]; |
||
201 | } |
||
202 | |||
203 | public function list($filter,$uid) |
||
204 | { |
||
205 | if ($uid) { |
||
206 | //$paginator=DB::select('SELECT DISTINCT contest.* FROM group_member inner join contest on group_member.gid=contest.gid left join contest_participant on contest.cid=contest_participant.cid where (public=1 and audit=1) or (group_member.uid=:uid and group_member.role>0 and (contest_participant.uid=:uidd or ISNULL(contest_participant.uid)) and (registration=0 or (registration=1 and not ISNULL(contest_participant.uid))))',["uid"=>$uid,"uidd"=>$uid])->paginate(10); |
||
207 | if ($filter['public']=='1') { |
||
208 | $paginator=DB::table($this->tableName)->where([ |
||
209 | "public"=>1, |
||
210 | "audit_status"=>1 |
||
211 | ])->orderBy('begin_time', 'desc'); |
||
212 | if ($filter['rule']) { |
||
213 | $paginator=$paginator->where(["rule"=>$filter['rule']]); |
||
214 | } |
||
215 | if ($filter['verified']) { |
||
216 | $paginator=$paginator->where(["verified"=>$filter['verified']]); |
||
217 | } |
||
218 | if ($filter['rated']) { |
||
219 | $paginator=$paginator->where(["rated"=>$filter['rated']]); |
||
220 | } |
||
221 | if ($filter['anticheated']) { |
||
222 | $paginator=$paginator->where(["anticheated"=>$filter['anticheated']]); |
||
223 | } |
||
224 | if ($filter['practice']) { |
||
225 | $paginator=$paginator->where(["practice"=>$filter['practice']]); |
||
226 | } |
||
227 | $paginator = $paginator ->paginate(10); |
||
228 | }elseif($filter['public']=='0'){ |
||
229 | $paginator=DB::table('group_member') |
||
230 | ->groupBy('contest.cid') |
||
231 | ->select('contest.*') |
||
232 | ->join('contest', 'group_member.gid', '=', 'contest.gid') |
||
233 | ->leftJoin('contest_participant', 'contest.cid', '=', 'contest_participant.cid') |
||
234 | ->where( |
||
235 | function ($query) use ($filter,$uid) { |
||
236 | if ($filter['rule']) { |
||
237 | $query=$query->where(["rule"=>$filter['rule']]); |
||
238 | } |
||
239 | if ($filter['verified']) { |
||
240 | $query=$query->where(["verified"=>$filter['verified']]); |
||
241 | } |
||
242 | if ($filter['rated']) { |
||
243 | $query=$query->where(["rated"=>$filter['rated']]); |
||
244 | } |
||
245 | if ($filter['anticheated']) { |
||
246 | $query=$query->where(["anticheated"=>$filter['anticheated']]); |
||
247 | } |
||
248 | if ($filter['practice']) { |
||
249 | $query=$query->where(["practice"=>$filter['practice']]); |
||
250 | } |
||
251 | $query->where('group_member.uid', $uid) |
||
252 | ->where('group_member.role', '>', 0) |
||
253 | ->where(["public"=>0]); |
||
254 | } |
||
255 | ) |
||
256 | ->orderBy('contest.begin_time', 'desc') |
||
257 | ->paginate(10); |
||
258 | }else{ |
||
259 | $paginator=DB::table('group_member') |
||
260 | ->groupBy('contest.cid') |
||
261 | ->select('contest.*') |
||
262 | ->join('contest', 'group_member.gid', '=', 'contest.gid') |
||
263 | ->leftJoin('contest_participant', 'contest.cid', '=', 'contest_participant.cid') |
||
264 | ->where( |
||
265 | function ($query) use ($filter) { |
||
266 | if ($filter['rule']) { |
||
267 | $query=$query->where(["rule"=>$filter['rule']]); |
||
268 | } |
||
269 | if ($filter['verified']) { |
||
270 | $query=$query->where(["verified"=>$filter['verified']]); |
||
271 | } |
||
272 | if ($filter['rated']) { |
||
273 | $query=$query->where(["rated"=>$filter['rated']]); |
||
274 | } |
||
275 | if ($filter['anticheated']) { |
||
276 | $query=$query->where(["anticheated"=>$filter['anticheated']]); |
||
277 | } |
||
278 | if ($filter['practice']) { |
||
279 | $query=$query->where(["practice"=>$filter['practice']]); |
||
280 | } |
||
281 | $query->where('public', 1) |
||
282 | ->where('audit_status', 1); |
||
283 | } |
||
284 | ) |
||
285 | ->orWhere( |
||
286 | function ($query) use ($filter,$uid) { |
||
287 | if ($filter['rule']) { |
||
288 | $query=$query->where(["rule"=>$filter['rule']]); |
||
289 | } |
||
290 | if ($filter['public']) { |
||
291 | $query=$query->where(["public"=>$filter['public']]); |
||
292 | } |
||
293 | if ($filter['verified']) { |
||
294 | $query=$query->where(["verified"=>$filter['verified']]); |
||
295 | } |
||
296 | if ($filter['rated']) { |
||
297 | $query=$query->where(["rated"=>$filter['rated']]); |
||
298 | } |
||
299 | if ($filter['anticheated']) { |
||
300 | $query=$query->where(["anticheated"=>$filter['anticheated']]); |
||
301 | } |
||
302 | if ($filter['practice']) { |
||
303 | $query=$query->where(["practice"=>$filter['practice']]); |
||
304 | } |
||
305 | $query->where('group_member.uid', $uid) |
||
306 | ->where('group_member.role', '>', 0); |
||
307 | } |
||
308 | ) |
||
309 | ->orderBy('contest.begin_time', 'desc') |
||
310 | ->paginate(10); |
||
311 | } |
||
312 | } else { |
||
313 | $paginator=DB::table($this->tableName)->where([ |
||
314 | "public"=>1, |
||
315 | "audit_status"=>1 |
||
316 | ])->orderBy('begin_time', 'desc'); |
||
317 | if ($filter['rule']) { |
||
318 | $paginator=$paginator->where(["rule"=>$filter['rule']]); |
||
319 | } |
||
320 | if ($filter['verified']) { |
||
321 | $paginator=$paginator->where(["verified"=>$filter['verified']]); |
||
322 | } |
||
323 | if ($filter['rated']) { |
||
324 | $paginator=$paginator->where(["rated"=>$filter['rated']]); |
||
325 | } |
||
326 | if ($filter['anticheated']) { |
||
327 | $paginator=$paginator->where(["anticheated"=>$filter['anticheated']]); |
||
328 | } |
||
329 | if ($filter['practice']) { |
||
330 | $paginator=$paginator->where(["practice"=>$filter['practice']]); |
||
331 | } |
||
332 | $paginator = $paginator ->paginate(10); |
||
333 | } |
||
334 | $contest_list=$paginator->all(); |
||
335 | foreach ($contest_list as &$c) { |
||
336 | $c["rule_parsed"]=$this->rule[$c["rule"]]; |
||
337 | $c["date_parsed"]=[ |
||
338 | "date"=>date_format(date_create($c["begin_time"]), 'j'), |
||
339 | "month_year"=>date_format(date_create($c["begin_time"]), 'M, Y'), |
||
340 | ]; |
||
341 | $c["length"]=$this->calcLength($c["begin_time"], $c["end_time"]); |
||
342 | } |
||
343 | return [ |
||
344 | 'contents' => $contest_list, |
||
345 | 'paginator' => $paginator |
||
346 | ]; |
||
347 | } |
||
348 | |||
349 | public function featured() |
||
350 | { |
||
351 | $featured=DB::table($this->tableName)->where([ |
||
352 | "public"=>1, |
||
353 | "audit_status"=>1, |
||
354 | "featured"=>1 |
||
355 | ])->orderBy('begin_time', 'desc')->first(); |
||
356 | |||
357 | if (!empty($featured)) { |
||
358 | $featured["rule_parsed"]=$this->rule[$featured["rule"]]; |
||
359 | $featured["date_parsed"]=[ |
||
360 | "date"=>date_format(date_create($featured["begin_time"]), 'j'), |
||
361 | "month_year"=>date_format(date_create($featured["begin_time"]), 'M, Y'), |
||
362 | ]; |
||
363 | $featured["length"]=$this->calcLength($featured["begin_time"], $featured["end_time"]); |
||
364 | return $featured; |
||
365 | } else { |
||
366 | return null; |
||
367 | } |
||
368 | } |
||
369 | |||
370 | public function registContest($cid,$uid) |
||
371 | { |
||
372 | $registered=DB::table("contest_participant")->where([ |
||
373 | "cid"=>$cid, |
||
374 | "uid"=>$uid |
||
375 | ])->first(); |
||
376 | |||
377 | if(empty($registered)){ |
||
378 | DB::table("contest_participant")->insert([ |
||
379 | "cid"=>$cid, |
||
380 | "uid"=>$uid, |
||
381 | "audit"=>1 |
||
382 | ]); |
||
383 | return true; |
||
384 | } |
||
385 | return false; |
||
386 | } |
||
387 | |||
388 | public function remainingTime($cid) |
||
389 | { |
||
390 | $end_time=DB::table($this->tableName)->where([ |
||
391 | "cid"=>$cid |
||
392 | ])->select("end_time")->first()["end_time"]; |
||
393 | $end_time=strtotime($end_time); |
||
394 | $cur_time=time(); |
||
395 | return $end_time-$cur_time; |
||
396 | } |
||
397 | |||
398 | public function intToChr($index, $start=65) |
||
399 | { |
||
400 | $str=''; |
||
401 | if (floor($index / 26)>0) { |
||
402 | $str.=$this->intToChr(floor($index / 26)-1); |
||
403 | } |
||
404 | return $str.chr($index % 26+$start); |
||
405 | } |
||
406 | |||
407 | public function problems($cid) |
||
408 | { |
||
409 | return DB::table('contest_problem') |
||
410 | ->join('problem','contest_problem.pid','=','problem.pid') |
||
411 | ->where('cid',$cid) |
||
412 | ->select('problem.pid as pid','pcode','number') |
||
413 | ->orderBy('number') |
||
414 | ->get()->all(); |
||
415 | } |
||
416 | |||
417 | public function contestProblems($cid, $uid) |
||
418 | { |
||
419 | $submissionModel=new SubmissionModel(); |
||
420 | |||
421 | $contest_rule=$this->contestRule($cid); |
||
422 | |||
423 | $problemSet=DB::table("contest_problem") |
||
424 | ->join("problem", "contest_problem.pid", "=", "problem.pid") |
||
425 | ->join("contest", "contest_problem.cid", "=", "contest.cid") |
||
426 | ->where([ |
||
427 | "contest_problem.cid"=>$cid |
||
428 | ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title", "contest.gid as gid", "contest.practice as practice")->get()->all(); |
||
429 | |||
430 | $frozen_time=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
431 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
|
|||
432 | |||
433 | foreach ($problemSet as &$p) { |
||
434 | if($p['practice']){ |
||
435 | $tags = DB::table("group_problem_tag") |
||
436 | ->where('gid',$p['gid']) |
||
437 | ->where('pid',$p['pid']) |
||
438 | ->get()->all(); |
||
439 | $tags_arr = []; |
||
440 | if(!empty($tags)){ |
||
441 | foreach ($tags as $value) { |
||
442 | array_push($tags_arr,$value['tag']); |
||
443 | } |
||
444 | } |
||
445 | $p['tags'] = $tags_arr; |
||
446 | } |
||
447 | if ($contest_rule==1) { |
||
448 | $prob_stat=DB::table("submission")->select( |
||
449 | DB::raw("count(sid) as submission_count"), |
||
450 | DB::raw("sum(verdict='accepted') as passed_count"), |
||
451 | DB::raw("sum(verdict='accepted')/count(sid)*100 as ac_rate") |
||
452 | )->where([ |
||
453 | "pid"=>$p["pid"], |
||
454 | "cid"=>$cid |
||
455 | ])->where("submission_date", "<", $frozen_time)->first(); |
||
456 | |||
457 | if ($prob_stat["submission_count"]==0) { |
||
458 | $p["submission_count"]=0; |
||
459 | $p["passed_count"]=0; |
||
460 | $p["ac_rate"]=0; |
||
461 | } else { |
||
462 | $p["submission_count"]=$prob_stat["submission_count"]; |
||
463 | $p["passed_count"]=$prob_stat["passed_count"]; |
||
464 | $p["ac_rate"]=round($prob_stat["ac_rate"], 2); |
||
465 | } |
||
466 | } else { |
||
467 | $prob_stat=$this->contestProblemInfoOI($cid, $p["pid"], $uid); |
||
468 | $p["points"]=$prob_stat["points"]; |
||
469 | $p["score"]=empty($prob_stat["score_parsed"]) ? 0 : $prob_stat["score_parsed"]; |
||
470 | } |
||
471 | $prob_status=$submissionModel->getProblemStatus($p["pid"], $uid, $cid); |
||
472 | if (empty($prob_status)) { |
||
473 | $p["prob_status"]=[ |
||
474 | "icon"=>"checkbox-blank-circle-outline", |
||
475 | "color"=>"wemd-grey-text" |
||
476 | ]; |
||
477 | } else { |
||
478 | $p["prob_status"]=[ |
||
479 | "icon"=>$prob_status["verdict"]=="Accepted" ? "checkbox-blank-circle" : "cisco-webex", |
||
480 | "color"=>$prob_status["color"] |
||
481 | ]; |
||
482 | } |
||
483 | |||
484 | |||
485 | } |
||
486 | |||
487 | return $problemSet; |
||
488 | } |
||
489 | |||
490 | public function getPid($cid, $ncode) |
||
491 | { |
||
492 | return DB::table("contest_problem")->where([ |
||
493 | "cid"=>$cid, |
||
494 | "ncode"=>$ncode |
||
495 | ])->select("contest_problem.pid")->first()["pid"]; |
||
496 | } |
||
497 | |||
498 | public function getPcode($cid, $ncode) |
||
499 | { |
||
500 | return DB::table("problem")->where([ |
||
501 | "cid"=>$cid |
||
502 | ])->select("contest_problem.pid")->first()["pcode"]; |
||
503 | } |
||
504 | |||
505 | public function getCustomInfo($cid) |
||
506 | { |
||
507 | $basic_info=DB::table($this->tableName)->where([ |
||
508 | "cid"=>$cid |
||
509 | ])->select("verified", "custom_icon", "custom_title")->first(); |
||
510 | return $basic_info["verified"] ? ((is_null($basic_info["custom_icon"]) && is_null($basic_info["custom_title"])) ?null:$basic_info) : null; |
||
511 | } |
||
512 | |||
513 | |||
514 | public function formatTime($seconds) |
||
515 | { |
||
516 | if ($seconds>3600) { |
||
517 | $hours=intval($seconds / 3600); |
||
518 | $minutes=$seconds % 3600; |
||
519 | $time=$hours.":".gmstrftime('%M:%S', $minutes); |
||
520 | } else { |
||
521 | $time=gmstrftime('%H:%M:%S', $seconds); |
||
522 | } |
||
523 | return $time; |
||
524 | } |
||
525 | |||
526 | public function contestProblemInfoOI($cid, $pid, $uid) |
||
527 | { |
||
528 | $ret=[ |
||
529 | "color"=>"", |
||
530 | "score"=>null, |
||
531 | "score_parsed"=>"", |
||
532 | "solved"=>0, |
||
533 | "points"=>DB::table("contest_problem")->where([ |
||
534 | "pid"=>$pid, |
||
535 | "cid"=>$cid |
||
536 | ])->first()["points"] |
||
537 | ]; |
||
538 | |||
539 | $frozen_time=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
540 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
541 | |||
542 | $highest_record=DB::table("submission")->where([ |
||
543 | "cid"=>$cid, |
||
544 | "pid"=>$pid, |
||
545 | "uid"=>$uid |
||
546 | ])->where("submission_date", "<", $frozen_time)->orderBy('score', 'desc')->first(); |
||
547 | |||
548 | if (!empty($highest_record)) { |
||
549 | $ret["score"]=$highest_record["score"]; |
||
550 | |||
551 | $tot_score=DB::table("problem")->where([ |
||
552 | "pid"=>$pid |
||
553 | ])->first()["tot_score"]; |
||
554 | |||
555 | $ret["color"]=($ret["score"]==$tot_score) ? "wemd-teal-text" : "wemd-green-text"; |
||
556 | $ret["solved"]=($ret["score"]==$tot_score) ? 1 : 0; |
||
557 | $ret["score_parsed"]=$ret["score"] / $tot_score * ($ret["points"]); |
||
558 | } |
||
559 | return $ret; |
||
560 | } |
||
561 | |||
562 | public function isFrozen($cid) |
||
563 | { |
||
564 | $frozen=DB::table("contest")->where(["cid"=>$cid])->select("froze_length", DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first(); |
||
565 | if (empty($frozen["froze_length"])) { |
||
566 | return false; |
||
567 | } else { |
||
568 | return time()>$frozen["frozen_time"]; |
||
569 | } |
||
570 | } |
||
571 | |||
572 | public function contestProblemInfoACM($cid, $pid, $uid) |
||
573 | { |
||
574 | $ret=[ |
||
575 | "color"=>"", |
||
576 | "solved"=>0, |
||
577 | "solved_time"=>"", |
||
578 | "solved_time_parsed"=>"", |
||
579 | "wrong_doings"=>0, |
||
580 | "color"=>"", |
||
581 | ]; |
||
582 | |||
583 | $frozen_time=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
584 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
585 | |||
586 | $ac_record=DB::table("submission")->where([ |
||
587 | "cid"=>$cid, |
||
588 | "pid"=>$pid, |
||
589 | "uid"=>$uid, |
||
590 | "verdict"=>"Accepted" |
||
591 | ])->where("submission_date", "<", $frozen_time)->orderBy('submission_date', 'asc')->first(); |
||
592 | |||
593 | if (!empty($ac_record)) { |
||
594 | $ret["solved"]=1; |
||
595 | |||
596 | $ret["solved_time"]=$ac_record["submission_date"]-strtotime(DB::table($this->tableName)->where([ |
||
597 | "cid"=>$cid |
||
598 | ])->first()["begin_time"]); |
||
599 | |||
600 | $ret["solved_time_parsed"]=$this->formatTime($ret["solved_time"]); |
||
601 | |||
602 | $ret["wrong_doings"]=DB::table("submission")->where([ |
||
603 | "cid"=>$cid, |
||
604 | "pid"=>$pid, |
||
605 | "uid"=>$uid |
||
606 | ])->whereIn('verdict', [ |
||
607 | 'Runtime Error', |
||
608 | 'Wrong Answer', |
||
609 | 'Time Limit Exceed', |
||
610 | 'Real Time Limit Exceed', |
||
611 | 'Memory Limit Exceed', |
||
612 | 'Presentation Error', |
||
613 | 'Output Limit Exceeded' |
||
614 | ])->where("submission_date", "<", $ac_record["submission_date"])->count(); |
||
615 | |||
616 | $others_first=DB::table("submission")->where([ |
||
617 | "cid"=>$cid, |
||
618 | "pid"=>$pid, |
||
619 | "verdict"=>"Accepted" |
||
620 | ])->where("submission_date", "<", $ac_record["submission_date"])->count(); |
||
621 | |||
622 | $ret["color"]=$others_first ? "wemd-green-text" : "wemd-teal-text"; |
||
623 | } else { |
||
624 | $ret["wrong_doings"]=DB::table("submission")->where([ |
||
625 | "cid"=>$cid, |
||
626 | "pid"=>$pid, |
||
627 | "uid"=>$uid |
||
628 | ])->whereIn('verdict', [ |
||
629 | 'Runtime Error', |
||
630 | 'Wrong Answer', |
||
631 | 'Time Limit Exceed', |
||
632 | 'Real Time Limit Exceed', |
||
633 | 'Memory Limit Exceed', |
||
634 | 'Presentation Error', |
||
635 | 'Output Limit Exceeded' |
||
636 | ])->where("submission_date", "<", $frozen_time)->count(); |
||
637 | } |
||
638 | |||
639 | return $ret; |
||
640 | } |
||
641 | |||
642 | public function contestRankCache($cid) |
||
643 | { |
||
644 | // if(Cache::tags(['contest','rank'])->get($cid)!=null) return Cache::tags(['contest','rank'])->get($cid); |
||
645 | $ret=[]; |
||
646 | |||
647 | $contest_info=DB::table("contest")->where("cid", $cid)->first(); |
||
648 | $frozen_time=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
649 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
650 | |||
651 | if ($contest_info["registration"]) { |
||
652 | $submissionUsers=DB::table("contest_participant")->where([ |
||
653 | "cid"=>$cid, |
||
654 | "audit"=>1 |
||
655 | ])->select('uid')->get()->all(); |
||
656 | } else { |
||
657 | // Those who submitted are participants |
||
658 | $submissionUsers=DB::table("submission")->where([ |
||
659 | "cid"=>$cid |
||
660 | ])->where( |
||
661 | "submission_date", |
||
662 | "<", |
||
663 | $frozen_time |
||
664 | )->select('uid')->groupBy('uid')->get()->all(); |
||
665 | } |
||
666 | |||
667 | $problemSet=DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([ |
||
668 | "cid"=>$cid |
||
669 | ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title")->get()->all(); |
||
670 | |||
671 | if ($contest_info["rule"]==1) { |
||
672 | // ACM/ICPC Mode |
||
673 | foreach ($submissionUsers as $s) { |
||
674 | $prob_detail=[]; |
||
675 | $totPen=0; |
||
676 | $totScore=0; |
||
677 | foreach ($problemSet as $p) { |
||
678 | $prob_stat=$this->contestProblemInfoACM($cid, $p["pid"], $s["uid"]); |
||
679 | $prob_detail[]=[ |
||
680 | "ncode"=>$p["ncode"], |
||
681 | "pid"=>$p["pid"], |
||
682 | "color"=>$prob_stat["color"], |
||
683 | "wrong_doings"=>$prob_stat["wrong_doings"], |
||
684 | "solved_time_parsed"=>$prob_stat["solved_time_parsed"] |
||
685 | ]; |
||
686 | if ($prob_stat["solved"]) { |
||
687 | $totPen+=$prob_stat["wrong_doings"] * 20; |
||
688 | $totPen+=$prob_stat["solved_time"] / 60; |
||
689 | $totScore+=$prob_stat["solved"]; |
||
690 | } |
||
691 | } |
||
692 | $ret[]=[ |
||
693 | "uid" => $s["uid"], |
||
694 | "name" => DB::table("users")->where([ |
||
695 | "id"=>$s["uid"] |
||
696 | ])->first()["name"], |
||
697 | "nick_name" => DB::table("group_member")->where([ |
||
698 | "uid" => $s["uid"], |
||
699 | "gid" => $contest_info["gid"] |
||
700 | ])->where("role", ">", 0)->first()["nick_name"], |
||
701 | "score" => $totScore, |
||
702 | "penalty" => $totPen, |
||
703 | "problem_detail" => $prob_detail |
||
704 | ]; |
||
705 | } |
||
706 | usort($ret, function ($a, $b) { |
||
707 | if ($a["score"]==$b["score"]) { |
||
708 | if ($a["penalty"]==$b["penalty"]) { |
||
709 | return 0; |
||
710 | } elseif (($a["penalty"]>$b["penalty"])) { |
||
711 | return 1; |
||
712 | } else { |
||
713 | return -1; |
||
714 | } |
||
715 | } elseif ($a["score"]>$b["score"]) { |
||
716 | return -1; |
||
717 | } else { |
||
718 | return 1; |
||
719 | } |
||
720 | }); |
||
721 | } elseif ($contest_info["rule"]==2) { |
||
722 | // OI Mode |
||
723 | foreach ($submissionUsers as $s) { |
||
724 | $prob_detail=[]; |
||
725 | $totScore=0; |
||
726 | $totSolved=0; |
||
727 | foreach ($problemSet as $p) { |
||
728 | $prob_stat=$this->contestProblemInfoOI($cid, $p["pid"], $s["uid"]); |
||
729 | $prob_detail[]=[ |
||
730 | "ncode"=>$p["ncode"], |
||
731 | "pid"=>$p["pid"], |
||
732 | "color"=>$prob_stat["color"], |
||
733 | "score"=>$prob_stat["score"], |
||
734 | "score_parsed"=>$prob_stat["score_parsed"] |
||
735 | ]; |
||
736 | $totSolved+=$prob_stat["solved"]; |
||
737 | $totScore+=intval($prob_stat["score_parsed"]); |
||
738 | } |
||
739 | $ret[]=[ |
||
740 | "uid" => $s["uid"], |
||
741 | "name" => DB::table("users")->where([ |
||
742 | "id"=>$s["uid"] |
||
743 | ])->first()["name"], |
||
744 | "nick_name" => DB::table("group_member")->where([ |
||
745 | "uid" => $s["uid"], |
||
746 | "gid" => $contest_info["gid"] |
||
747 | ])->where("role", ">", 0)->first()["nick_name"], |
||
748 | "score" => $totScore, |
||
749 | "solved" => $totSolved, |
||
750 | "problem_detail" => $prob_detail |
||
751 | ]; |
||
752 | } |
||
753 | usort($ret, function ($a, $b) { |
||
754 | if ($a["score"]==$b["score"]) { |
||
755 | if ($a["solved"]==$b["solved"]) { |
||
756 | return 0; |
||
757 | } elseif (($a["solved"]<$b["solved"])) { |
||
758 | return 1; |
||
759 | } else { |
||
760 | return -1; |
||
761 | } |
||
762 | } elseif ($a["score"]>$b["score"]) { |
||
763 | return -1; |
||
764 | } else { |
||
765 | return 1; |
||
766 | } |
||
767 | }); |
||
768 | } |
||
769 | |||
770 | Cache::tags(['contest', 'rank'])->put($cid, $ret, 60); |
||
771 | |||
772 | return $ret; |
||
773 | } |
||
774 | |||
775 | public function contestRank($cid, $uid = 0) |
||
776 | { |
||
777 | // [ToDo] If the current user's in the organizer group show nick name |
||
778 | // [ToDo] The participants determination |
||
779 | // [ToDo] Frozen Time |
||
780 | // [ToDo] Performance Opt |
||
781 | // [Todo] Ajaxization - Should have done in controller |
||
782 | // [Todo] Authorization ( Public / Private ) - Should have done in controller |
||
783 | |||
784 | $ret=[]; |
||
785 | |||
786 | $contest_info=DB::table("contest")->where("cid", $cid)->first(); |
||
787 | |||
788 | $user_in_group=!empty(DB::table("group_member")->where([ |
||
789 | "uid" => $uid, |
||
790 | "gid" => $contest_info["gid"] |
||
791 | ])->where("role", ">", 0)->first()); |
||
792 | |||
793 | $clearance = $this -> judgeClearance($cid, $uid); |
||
794 | |||
795 | /** New Version With MySQL */ |
||
796 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
797 | |||
798 | if(time() < $end_time){ |
||
799 | if($clearance == 3){ |
||
800 | $contestRankRaw=Cache::tags(['contest', 'rank'])->get("contestAdmin$cid"); |
||
801 | }else{ |
||
802 | $contestRankRaw=Cache::tags(['contest', 'rank'])->get($cid); |
||
803 | } |
||
804 | if(!isset($contestRankRaw)){ |
||
805 | $contestRankRaw=$this->contestRankCache($cid); |
||
806 | } |
||
807 | }else{ |
||
808 | if($clearance == 3){ |
||
809 | $contestRankRaw=Cache::tags(['contest', 'rank'])->get("contestAdmin$cid"); |
||
810 | if (!isset($contestRankRaw)) { |
||
811 | $contestRankRaw=$this->getContestRankFromMySQL($cid); |
||
812 | if(!isset($contestRankRaw)){ |
||
813 | $contestRankRaw=$this->contestRankCache($cid); |
||
814 | $this->storeContestRankInMySQL($cid, $contestRankRaw); |
||
815 | } |
||
816 | } |
||
817 | }else{ |
||
818 | $contestRankRaw=$this->getContestRankFromMySQL($cid); |
||
819 | if(!isset($contestRankRaw)){ |
||
820 | $contestRankRaw=Cache::tags(['contest', 'rank'])->get($cid); |
||
821 | if(!isset($contestRankRaw)){ |
||
822 | $contestRankRaw=$this->contestRankCache($cid); |
||
823 | } |
||
824 | $this->storeContestRankInMySQL($cid, $contestRankRaw); |
||
825 | } |
||
826 | } |
||
827 | } |
||
828 | |||
829 | /** Old version */ |
||
830 | // if ($contestRankRaw==null) { |
||
831 | // $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
832 | // if(time() > $end_time && !Cache::has($cid)){ |
||
833 | // $contestRankRaw=$this->contestRankCache($cid); |
||
834 | // // Cache::forever($cid, $contestRankRaw); |
||
835 | // }else{ |
||
836 | // $contestRankRaw=$this->contestRankCache($cid); |
||
837 | // } |
||
838 | // } |
||
839 | if($contest_info["rule"]==1){ |
||
840 | foreach ($contestRankRaw as &$cr) { |
||
841 | $solved = 0; |
||
842 | foreach($cr['problem_detail'] as $pd){ |
||
843 | if(!empty($pd['solved_time_parsed'])){ |
||
844 | $solved ++; |
||
845 | } |
||
846 | } |
||
847 | $cr['solved'] = $solved; |
||
848 | } |
||
849 | } |
||
850 | |||
851 | $ret=$contestRankRaw; |
||
852 | |||
853 | foreach ($ret as $r) { |
||
854 | if (!$user_in_group) { |
||
855 | $r["nick_name"]=''; |
||
856 | } |
||
857 | } |
||
858 | |||
859 | return $ret; |
||
860 | } |
||
861 | |||
862 | public function getRejudgeQueue($cid) |
||
863 | { |
||
864 | $problemModel=new ProblemModel(); |
||
865 | $submissionModel=new SubmissionModel(); |
||
866 | $compilerModel=new CompilerModel(); |
||
867 | |||
868 | $tempQueue=DB::table("submission")->where([ |
||
869 | "cid"=>$cid |
||
870 | ])->whereIn('verdict', [ |
||
871 | 'Runtime Error', |
||
872 | 'Wrong Answer', |
||
873 | 'Time Limit Exceed', |
||
874 | 'Real Time Limit Exceed', |
||
875 | 'Memory Limit Exceed', |
||
876 | 'Presentation Error', |
||
877 | 'Output Limit Exceeded' |
||
878 | ])->get()->all(); |
||
879 | |||
880 | foreach ($tempQueue as &$t) { |
||
881 | $lang=$compilerModel->detail($t["coid"]); |
||
882 | $probBasic=$problemModel->basic($t["pid"]); |
||
883 | $t["oj"]=$problemModel->ocode($t["pid"]); |
||
884 | $t["lang"]=$lang['lcode']; |
||
885 | $t["cid"]=$probBasic["contest_id"]; |
||
886 | $t["iid"]=$probBasic["index_id"]; |
||
887 | $t["pcode"]=$probBasic["pcode"]; |
||
888 | $t["contest"]=$cid; |
||
889 | } |
||
890 | |||
891 | return $tempQueue; |
||
892 | } |
||
893 | |||
894 | public function getClarificationList($cid) |
||
895 | { |
||
896 | $uid = Auth::user()->id; |
||
897 | $clearance = $this -> judgeClearance($cid, $uid); |
||
898 | if($clearance == 3){ |
||
899 | return DB::table("contest_clarification")->where([ |
||
900 | "cid"=>$cid |
||
901 | ])->orderBy('create_time', 'desc')->get()->all(); |
||
902 | }else{ |
||
903 | return DB::table("contest_clarification")->where([ |
||
904 | "cid"=>$cid |
||
905 | ])->where(function ($query) { |
||
906 | $query->where([ |
||
907 | "public"=>1 |
||
908 | ])->orWhere([ |
||
909 | "uid" => Auth::user()->id |
||
910 | ]); |
||
911 | })->orderBy('create_time', 'desc')->get()->all(); |
||
912 | } |
||
913 | } |
||
914 | |||
915 | public function fetchClarification($cid) |
||
916 | { |
||
917 | return DB::table("contest_clarification")->where([ |
||
918 | "cid"=>$cid, |
||
919 | "type"=>0, |
||
920 | "public"=>1 |
||
921 | ])->whereBetween( |
||
922 | 'create_time', |
||
923 | [ |
||
924 | date("Y-m-d H:i:s", time()-59), |
||
925 | date("Y-m-d H:i:s") |
||
926 | ] |
||
927 | )->first(); |
||
928 | } |
||
929 | |||
930 | public function getlatestClarification($cid) |
||
931 | { |
||
932 | return DB::table("contest_clarification")->where([ |
||
933 | "cid"=>$cid, |
||
934 | "type"=>0, |
||
935 | "public"=>1 |
||
936 | ])->orderBy('create_time', 'desc')->first(); |
||
937 | } |
||
938 | |||
939 | public function getClarificationDetail($ccid) |
||
940 | { |
||
941 | return DB::table("contest_clarification")->where([ |
||
942 | "ccid"=>$ccid, |
||
943 | "public"=>1 |
||
944 | ])->first(); |
||
945 | } |
||
946 | |||
947 | public function requestClarification($cid, $title, $content, $uid) |
||
948 | { |
||
949 | return DB::table("contest_clarification")->insertGetId([ |
||
950 | "cid"=>$cid, |
||
951 | "type"=>1, |
||
952 | "title"=>$title, |
||
953 | "content"=>$content, |
||
954 | "public"=>"0", |
||
955 | "uid"=>$uid, |
||
956 | "create_time"=>date("Y-m-d H:i:s") |
||
957 | ]); |
||
958 | } |
||
959 | |||
960 | public function issueAnnouncement($cid, $title, $content, $uid, $remote_code=null) |
||
961 | { |
||
962 | return DB::table("contest_clarification")->insertGetId([ |
||
963 | "cid"=>$cid, |
||
964 | "type"=>0, |
||
965 | "title"=>$title, |
||
966 | "content"=>$content, |
||
967 | "public"=>"1", |
||
968 | "uid"=>$uid, |
||
969 | "create_time"=>date("Y-m-d H:i:s"), |
||
970 | "remote_code"=>$remote_code |
||
971 | ]); |
||
972 | } |
||
973 | |||
974 | public function remoteAnnouncement($remote_code) { |
||
975 | return DB::table("contest_clarification")->where("remote_code", $remote_code)->get()->first(); |
||
976 | } |
||
977 | public function isContestEnded($cid) |
||
978 | { |
||
979 | return DB::table("contest")->where("cid", $cid)->where("end_time", "<", date("Y-m-d H:i:s"))->count(); |
||
980 | } |
||
981 | |||
982 | public function isContestRunning($cid) |
||
983 | { |
||
984 | return DB::table("contest")->where("cid", $cid)->where("begin_time", "<", date("Y-m-d H:i:s"))->where("end_time", ">", date("Y-m-d H:i:s"))->count(); |
||
985 | } |
||
986 | |||
987 | public function formatSubmitTime($date) |
||
988 | { |
||
989 | $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"]; |
||
990 | $lengths=["60", "60", "24", "7", "4.35", "12", "10"]; |
||
991 | |||
992 | $now=time(); |
||
993 | $unix_date=strtotime($date); |
||
994 | |||
995 | if (empty($unix_date)) { |
||
996 | return "Bad date"; |
||
997 | } |
||
998 | |||
999 | if ($now>$unix_date) { |
||
1000 | $difference=$now-$unix_date; |
||
1001 | $tense="ago"; |
||
1002 | } else { |
||
1003 | $difference=$unix_date-$now; |
||
1004 | $tense="from now"; |
||
1005 | } |
||
1006 | |||
1007 | for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) { |
||
1008 | $difference/=$lengths[$j]; |
||
1009 | } |
||
1010 | |||
1011 | $difference=round($difference); |
||
1012 | |||
1013 | if ($difference!=1) { |
||
1014 | $periods[$j].="s"; |
||
1015 | } |
||
1016 | |||
1017 | return "$difference $periods[$j] {$tense}"; |
||
1018 | } |
||
1019 | |||
1020 | public function formatAbsTime($sec) |
||
1021 | { |
||
1022 | $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"]; |
||
1023 | $lengths=["60", "60", "24", "7", "4.35", "12", "10"]; |
||
1024 | |||
1025 | |||
1026 | $difference=$sec; |
||
1027 | |||
1028 | for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) { |
||
1029 | $difference/=$lengths[$j]; |
||
1030 | } |
||
1031 | |||
1032 | $difference=round($difference); |
||
1033 | |||
1034 | if ($difference!=1) { |
||
1035 | $periods[$j].="s"; |
||
1036 | } |
||
1037 | |||
1038 | return "$difference $periods[$j]"; |
||
1039 | } |
||
1040 | |||
1041 | public function frozenTime($cid) |
||
1042 | { |
||
1043 | $basicInfo=$this->basic($cid); |
||
1044 | return $this->formatAbsTime($basicInfo["froze_length"]); |
||
1045 | } |
||
1046 | |||
1047 | public function getContestRecord($filter, $cid) |
||
1048 | { |
||
1049 | $basicInfo=$this->basic($cid); |
||
1050 | $userInfo=DB::table('group_member')->where('gid',$basicInfo["gid"])->where('uid',Auth::user()->id)->get()->first(); |
||
1051 | $problemSet_temp=DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([ |
||
1052 | "cid"=>$cid |
||
1053 | ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title", "points", "tot_score")->get()->all(); |
||
1054 | $problemSet=[]; |
||
1055 | foreach ($problemSet_temp as $p) { |
||
1056 | $problemSet[(string) $p["pid"]]=["ncode"=>$p["ncode"], "points"=>$p["points"], "tot_score"=>$p["tot_score"]]; |
||
1057 | } |
||
1058 | |||
1059 | $frozen_time=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
1060 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
1061 | $contestEnd=time()>$end_time; |
||
1062 | |||
1063 | $filter['pid'] = array_search($filter['ncode'], array_column($problemSet_temp, 'ncode')); |
||
1064 | if($filter['pid']==false){ |
||
1065 | $filter['pid'] = null; |
||
1066 | }else{ |
||
1067 | $filter['pid'] = $problemSet_temp[$filter['pid']]['pid']; |
||
1068 | } |
||
1069 | |||
1070 | if($userInfo==null || $userInfo["role"]!=3){ |
||
1071 | if ($basicInfo["status_visibility"]==2) { |
||
1072 | // View all |
||
1073 | $paginator=DB::table("submission")->where([ |
||
1074 | 'cid'=>$cid |
||
1075 | ])->where( |
||
1076 | "submission_date", |
||
1077 | "<", |
||
1078 | $end_time |
||
1079 | )->join( |
||
1080 | "users", |
||
1081 | "users.id", |
||
1082 | "=", |
||
1083 | "submission.uid" |
||
1084 | )->where(function ($query) use ($frozen_time) { |
||
1085 | $query->where( |
||
1086 | "submission_date", |
||
1087 | "<", |
||
1088 | $frozen_time |
||
1089 | )->orWhere( |
||
1090 | 'uid', |
||
1091 | Auth::user()->id |
||
1092 | ); |
||
1093 | })->select( |
||
1094 | "sid", |
||
1095 | "uid", |
||
1096 | "pid", |
||
1097 | "name", |
||
1098 | "color", |
||
1099 | "verdict", |
||
1100 | "time", |
||
1101 | "memory", |
||
1102 | "language", |
||
1103 | "score", |
||
1104 | "submission_date", |
||
1105 | "share" |
||
1106 | )->orderBy( |
||
1107 | 'submission_date', |
||
1108 | 'desc' |
||
1109 | ); |
||
1110 | |||
1111 | if($filter["pid"]){ |
||
1112 | $paginator=$paginator->where(["pid"=>$filter["pid"]]); |
||
1113 | } |
||
1114 | |||
1115 | if($filter["result"]){ |
||
1116 | $paginator=$paginator->where(["verdict"=>$filter["result"]]); |
||
1117 | } |
||
1118 | |||
1119 | if($filter["account"]){ |
||
1120 | $paginator=$paginator->where(["name"=>$filter["account"]]); |
||
1121 | } |
||
1122 | |||
1123 | $paginator=$paginator->paginate(50); |
||
1124 | } elseif ($basicInfo["status_visibility"]==1) { |
||
1125 | $paginator=DB::table("submission")->where([ |
||
1126 | 'cid'=>$cid, |
||
1127 | 'uid'=>Auth::user()->id |
||
1128 | ])->where( |
||
1129 | "submission_date", |
||
1130 | "<", |
||
1131 | $end_time |
||
1132 | )->join( |
||
1133 | "users", |
||
1134 | "users.id", |
||
1135 | "=", |
||
1136 | "submission.uid" |
||
1137 | )->select( |
||
1138 | "sid", |
||
1139 | "uid", |
||
1140 | "pid", |
||
1141 | "name", |
||
1142 | "color", |
||
1143 | "verdict", |
||
1144 | "time", |
||
1145 | "memory", |
||
1146 | "language", |
||
1147 | "score", |
||
1148 | "submission_date", |
||
1149 | "share" |
||
1150 | )->orderBy( |
||
1151 | 'submission_date', |
||
1152 | 'desc' |
||
1153 | ); |
||
1154 | |||
1155 | if($filter["pid"]){ |
||
1156 | $paginator=$paginator->where(["pid"=>$filter["pid"]]); |
||
1157 | } |
||
1158 | |||
1159 | if($filter["result"]){ |
||
1160 | $paginator=$paginator->where(["verdict"=>$filter["result"]]); |
||
1161 | } |
||
1162 | |||
1163 | if($filter["account"]){ |
||
1164 | $paginator=$paginator->where(["name"=>$filter["account"]]); |
||
1165 | } |
||
1166 | |||
1167 | $paginator=$paginator->paginate(50); |
||
1168 | } else { |
||
1169 | return [ |
||
1170 | "paginator"=>null, |
||
1171 | "records"=>[] |
||
1172 | ]; |
||
1173 | } |
||
1174 | }else{ |
||
1175 | if ($basicInfo["status_visibility"]==2) { |
||
1176 | // View all |
||
1177 | $paginator=DB::table("submission")->where([ |
||
1178 | 'cid'=>$cid |
||
1179 | ])->where( |
||
1180 | "submission_date", |
||
1181 | "<", |
||
1182 | $end_time |
||
1183 | )->join( |
||
1184 | "users", |
||
1185 | "users.id", |
||
1186 | "=", |
||
1187 | "submission.uid" |
||
1188 | )->select( |
||
1189 | "sid", |
||
1190 | "uid", |
||
1191 | "pid", |
||
1192 | "name", |
||
1193 | "color", |
||
1194 | "verdict", |
||
1195 | "time", |
||
1196 | "memory", |
||
1197 | "language", |
||
1198 | "score", |
||
1199 | "submission_date", |
||
1200 | "share" |
||
1201 | )->orderBy( |
||
1202 | 'submission_date', |
||
1203 | 'desc' |
||
1204 | ); |
||
1205 | |||
1206 | if($filter["pid"]){ |
||
1207 | $paginator=$paginator->where(["pid"=>$filter["pid"]]); |
||
1208 | } |
||
1209 | |||
1210 | if($filter["result"]){ |
||
1211 | $paginator=$paginator->where(["verdict"=>$filter["result"]]); |
||
1212 | } |
||
1213 | |||
1214 | if($filter["account"]){ |
||
1215 | $paginator=$paginator->where(["name"=>$filter["account"]]); |
||
1216 | } |
||
1217 | |||
1218 | $paginator=$paginator->paginate(50); |
||
1219 | } elseif ($basicInfo["status_visibility"]==1) { |
||
1220 | $paginator=DB::table("submission")->where([ |
||
1221 | 'cid'=>$cid, |
||
1222 | 'uid'=>Auth::user()->id |
||
1223 | ])->where( |
||
1224 | "submission_date", |
||
1225 | "<", |
||
1226 | $end_time |
||
1227 | )->join( |
||
1228 | "users", |
||
1229 | "users.id", |
||
1230 | "=", |
||
1231 | "submission.uid" |
||
1232 | )->select( |
||
1233 | "sid", |
||
1234 | "uid", |
||
1235 | "pid", |
||
1236 | "name", |
||
1237 | "color", |
||
1238 | "verdict", |
||
1239 | "time", |
||
1240 | "memory", |
||
1241 | "language", |
||
1242 | "score", |
||
1243 | "submission_date", |
||
1244 | "share" |
||
1245 | )->orderBy( |
||
1246 | 'submission_date', |
||
1247 | 'desc' |
||
1248 | ); |
||
1249 | |||
1250 | if($filter["pid"]){ |
||
1251 | $paginator=$paginator->where(["pid"=>$filter["pid"]]); |
||
1252 | } |
||
1253 | |||
1254 | if($filter["result"]){ |
||
1255 | $paginator=$paginator->where(["verdict"=>$filter["result"]]); |
||
1256 | } |
||
1257 | |||
1258 | if($filter["account"]){ |
||
1259 | $paginator=$paginator->where(["name"=>$filter["account"]]); |
||
1260 | } |
||
1261 | |||
1262 | $paginator=$paginator->paginate(50); |
||
1263 | } else { |
||
1264 | return [ |
||
1265 | "paginator"=>null, |
||
1266 | "records"=>[] |
||
1267 | ]; |
||
1268 | } |
||
1269 | } |
||
1270 | |||
1271 | $records=$paginator->all(); |
||
1272 | foreach ($records as &$r) { |
||
1273 | $r["submission_date_parsed"]=$this->formatSubmitTime(date('Y-m-d H:i:s', $r["submission_date"])); |
||
1274 | $r["submission_date"]=date('Y-m-d H:i:s', $r["submission_date"]); |
||
1275 | $r["nick_name"]=""; |
||
1276 | $r["ncode"]=$problemSet[(string) $r["pid"]]["ncode"]; |
||
1277 | if ($r["verdict"]=="Partially Accepted") { |
||
1278 | $score_parsed=round($r["score"] / $problemSet[(string) $r["pid"]]["tot_score"] * $problemSet[(string) $r["pid"]]["points"], 1); |
||
1279 | $r["verdict"].=" ($score_parsed)"; |
||
1280 | } |
||
1281 | if (!$contestEnd) { |
||
1282 | $r["share"]=0; |
||
1283 | } |
||
1284 | } |
||
1285 | return [ |
||
1286 | "paginator"=>$paginator, |
||
1287 | "records"=>$records |
||
1288 | ]; |
||
1289 | } |
||
1290 | |||
1291 | public function registration($cid, $uid=0) |
||
1292 | { |
||
1293 | if ($uid==0) { |
||
1294 | return []; |
||
1295 | } |
||
1296 | |||
1297 | |||
1298 | return DB::table("contest_participant")->where([ |
||
1299 | "cid" => $cid, |
||
1300 | "uid" => $uid, |
||
1301 | "audit" => 1 |
||
1302 | ])->first(); |
||
1303 | } |
||
1304 | |||
1305 | public function judgeClearance($cid, $uid=0) |
||
1306 | { |
||
1307 | /*************************** |
||
1308 | * 2 stands for participant* |
||
1309 | * 3 stands for admin * |
||
1310 | ***************************/ |
||
1311 | if ($uid==0) { |
||
1312 | return 0; |
||
1313 | } |
||
1314 | $groupModel = new GroupModel(); |
||
1315 | $contest_info=DB::table("contest")->where("cid", $cid)->first(); |
||
1316 | $userInfo=DB::table('group_member')->where('gid',$contest_info["gid"])->where('uid',$uid)->get()->first(); |
||
1317 | |||
1318 | if(empty($contest_info)){ |
||
1319 | // contest not exist |
||
1320 | return 0; |
||
1321 | } |
||
1322 | |||
1323 | if($uid == $contest_info['assign_uid'] || $groupModel->judgeClearance($contest_info['gid'],$uid) == 3){ |
||
1324 | return 3; |
||
1325 | } |
||
1326 | |||
1327 | $contest_started = strtotime($contest_info['begin_time']) < time(); |
||
1328 | $contest_ended = strtotime($contest_info['end_time']) < time(); |
||
1329 | if (!$contest_started) { |
||
1330 | // not started or do not exist |
||
1331 | return 0; |
||
1332 | } |
||
1333 | |||
1334 | if ($userInfo["role"]==3) { |
||
1335 | return 3; |
||
1336 | } |
||
1337 | |||
1338 | if ($contest_info["public"]) { |
||
1339 | //public |
||
1340 | if ($contest_ended) { |
||
1341 | return 1; |
||
1342 | } else { |
||
1343 | if ($contest_info["registration"]) { |
||
1344 | // check if uid in registration, temp return 3 |
||
1345 | $isParticipant=DB::table("contest_participant")->where([ |
||
1346 | "cid" => $cid, |
||
1347 | "uid" => $uid, |
||
1348 | "audit" => 1 |
||
1349 | ])->count(); |
||
1350 | if ($isParticipant) { |
||
1351 | return 2; |
||
1352 | } else { |
||
1353 | return 0; |
||
1354 | } |
||
1355 | } else { |
||
1356 | return 2; |
||
1357 | } |
||
1358 | } |
||
1359 | } else { |
||
1360 | //private |
||
1361 | $isMember=DB::table("group_member")->where([ |
||
1362 | "gid"=> $contest_info["gid"], |
||
1363 | "uid"=> $uid |
||
1364 | ])->where("role", ">", 0)->count(); |
||
1365 | if (!$isMember) { |
||
1366 | return 0; |
||
1367 | } else { |
||
1368 | if ($contest_info["registration"]) { |
||
1369 | // check if uid in registration, temp return 3 |
||
1370 | $isParticipant=DB::table("contest_participant")->where([ |
||
1371 | "cid" => $cid, |
||
1372 | "uid" => $uid, |
||
1373 | "audit" => 1 |
||
1374 | ])->count(); |
||
1375 | if ($isParticipant) { |
||
1376 | return 2; |
||
1377 | } else { |
||
1378 | return 0; |
||
1379 | } |
||
1380 | } else { |
||
1381 | return 2; |
||
1382 | } |
||
1383 | } |
||
1384 | } |
||
1385 | } |
||
1386 | |||
1387 | public function judgeOutsideClearance($cid, $uid=0) |
||
1388 | { |
||
1389 | $contest_info=DB::table("contest")->where("cid", $cid)->first(); |
||
1390 | if (empty($contest_info)) { |
||
1391 | return 0; |
||
1392 | } |
||
1393 | if ($contest_info["public"]) { |
||
1394 | return 1; |
||
1395 | } else { |
||
1396 | if ($uid==0) { |
||
1397 | return 0; |
||
1398 | } |
||
1399 | return DB::table("group_member")->where([ |
||
1400 | "gid"=> $contest_info["gid"], |
||
1401 | "uid"=> $uid |
||
1402 | ])->where("role", ">", 0)->count() ? 1 : 0; |
||
1403 | } |
||
1404 | } |
||
1405 | |||
1406 | public function contestName($cid) |
||
1407 | { |
||
1408 | return DB::table("contest")->where("cid", $cid)->select("name")->first()["name"]; |
||
1409 | } |
||
1410 | |||
1411 | public function contestRule($cid) |
||
1412 | { |
||
1413 | return DB::table("contest")->where("cid", $cid)->select("rule")->first()["rule"]; |
||
1414 | } |
||
1415 | |||
1416 | public function updateProfessionalRate($cid) |
||
1417 | { |
||
1418 | $basic=$this->basic($cid); |
||
1419 | if($basic["rated"]&&!$basic["is_rated"]){ |
||
1420 | $ratingCalculator=new RatingCalculator($cid); |
||
1421 | if($ratingCalculator->calculate()){ |
||
1422 | $ratingCalculator->storage(); |
||
1423 | return true; |
||
1424 | }else{ |
||
1425 | return false; |
||
1426 | } |
||
1427 | } else { |
||
1428 | return false; |
||
1429 | } |
||
1430 | } |
||
1431 | |||
1432 | public function contestUpdate($cid,$data,$problems) |
||
1433 | { |
||
1434 | if($problems !== false){ |
||
1435 | $old_problmes = array_column( |
||
1436 | DB::table('contest_problem') |
||
1437 | ->where('cid',$cid) |
||
1438 | ->get()->all(), |
||
1439 | 'pid' |
||
1440 | ); |
||
1441 | DB::transaction(function () use ($cid, $data, $problems,$old_problmes) { |
||
1442 | DB::table($this->tableName) |
||
1443 | ->where('cid',$cid) |
||
1444 | ->update($data); |
||
1445 | DB::table('contest_problem') |
||
1446 | ->where('cid',$cid) |
||
1447 | ->delete(); |
||
1448 | $new_problems = []; |
||
1449 | foreach ($problems as $p) { |
||
1450 | $pid=DB::table("problem")->where(["pcode"=>$p["pcode"]])->select("pid")->first()["pid"]; |
||
1451 | array_push($new_problems,$pid); |
||
1452 | DB::table("contest_problem")->insert([ |
||
1453 | "cid"=>$cid, |
||
1454 | "number"=>$p["number"], |
||
1455 | "ncode"=>$this->intToChr($p["number"]-1), |
||
1456 | "pid"=>$pid, |
||
1457 | "alias"=>"", |
||
1458 | "points"=>$p["points"] |
||
1459 | ]); |
||
1460 | } |
||
1461 | foreach($old_problmes as $op) { |
||
1462 | if(!in_array($op,$new_problems)){ |
||
1463 | DB::table('submission') |
||
1464 | ->where('cid',$cid) |
||
1465 | ->where('pid',$op) |
||
1466 | ->delete(); |
||
1467 | } |
||
1468 | } |
||
1469 | }, 5); |
||
1470 | $contestRankRaw = $this->contestRankCache($cid); |
||
1471 | Cache::tags(['contest', 'rank'])->put($cid, $contestRankRaw); |
||
1472 | Cache::tags(['contest', 'rank'])->put("contestAdmin$cid", $contestRankRaw); |
||
1473 | }else{ |
||
1474 | DB::table($this->tableName) |
||
1475 | ->where('cid',$cid) |
||
1476 | ->update($data); |
||
1477 | } |
||
1478 | } |
||
1479 | |||
1480 | public function contestUpdateProblem($cid,$problems) |
||
1481 | { |
||
1482 | DB::table('contest_problem') |
||
1483 | ->where('cid',$cid) |
||
1484 | ->delete(); |
||
1485 | foreach ($problems as $p) { |
||
1486 | DB::table("contest_problem")->insertGetId([ |
||
1487 | "cid"=>$cid, |
||
1488 | "number"=>$p["number"], |
||
1489 | "ncode"=>$this->intToChr($p["number"]-1), |
||
1490 | "pid"=>$p['pid'], |
||
1491 | "alias"=>"", |
||
1492 | "points"=>$p["points"] |
||
1493 | ]); |
||
1494 | } |
||
1495 | } |
||
1496 | |||
1497 | public function arrangeContest($gid, $config, $problems) |
||
1498 | { |
||
1499 | $cid = -1; |
||
1500 | DB::transaction(function () use ($gid, $config, $problems,&$cid) { |
||
1501 | $cid=DB::table($this->tableName)->insertGetId([ |
||
1502 | "gid"=>$gid, |
||
1503 | "name"=>$config["name"], |
||
1504 | "assign_uid"=>$config["assign_uid"], |
||
1505 | "verified"=>0, //todo |
||
1506 | "rated"=>0, |
||
1507 | "anticheated"=>0, |
||
1508 | "practice"=>$config["practice"], |
||
1509 | "featured"=>0, |
||
1510 | "description"=>$config["description"], |
||
1511 | "rule"=>1, //todo |
||
1512 | "begin_time"=>$config["begin_time"], |
||
1513 | "end_time"=>$config["end_time"], |
||
1514 | "vcid"=>isset($config["vcid"])?$config["vcid"]:null, |
||
1515 | "public"=>$config["public"], |
||
1516 | "registration"=>0, //todo |
||
1517 | "registration_due"=>null, //todo |
||
1518 | "registant_type"=>0, //todo |
||
1519 | "froze_length"=>0, //todo |
||
1520 | "status_visibility"=>2, //todo |
||
1521 | "create_time"=>date("Y-m-d H:i:s"), |
||
1522 | "crawled" => isset($config['vcid'])?$config['crawled'] : null, |
||
1523 | "audit_status"=>$config["public"] ? 0 : 1 |
||
1524 | ]); |
||
1525 | |||
1526 | foreach ($problems as $p) { |
||
1527 | $pid=DB::table("problem")->where(["pcode"=>$p["pcode"]])->select("pid")->first()["pid"]; |
||
1528 | DB::table("contest_problem")->insert([ |
||
1529 | "cid"=>$cid, |
||
1530 | "number"=>$p["number"], |
||
1531 | "ncode"=>$this->intToChr($p["number"]-1), |
||
1532 | "pid"=>$pid, |
||
1533 | "alias"=>"", |
||
1534 | "points"=>$p["points"] |
||
1535 | ]); |
||
1536 | } |
||
1537 | }, 5); |
||
1538 | return $cid; |
||
1539 | } |
||
1540 | |||
1541 | public function updateContestRankTable($cid,$sub) |
||
1542 | { |
||
1543 | $lock = Cache::lock("contestrank$cid",10); |
||
1544 | try{ |
||
1545 | if($lock->get()){ |
||
1546 | if(Cache::tags(['contest','rank'])->get($cid) != null){ |
||
1547 | $ret = Cache::tags(['contest','rank'])->get($cid); |
||
1548 | $chache=[]; |
||
1549 | $chache['contest_info']=DB::table("contest")->where("cid", $cid)->first(); |
||
1550 | $chache['problemSet']=DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([ |
||
1551 | "cid"=>$cid |
||
1552 | ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title")->get()->all(); |
||
1553 | $chache['frozen_time']=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
1554 | $chache['end_time']=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
1555 | |||
1556 | $id = 0; |
||
1557 | |||
1558 | foreach($chache['problemSet'] as $key => $p){ |
||
1559 | if ($p['pid'] == $sub['pid']){ |
||
1560 | $chache['problemSet'][$key]['cpid'] = $key; |
||
1561 | $id = $key; |
||
1562 | } |
||
1563 | } |
||
1564 | |||
1565 | $ret = $this->updateContestRankDetail($chache['contest_info'],$chache['problemSet'][$id],$cid,$sub['uid'],$ret); |
||
1566 | $ret = $this->sortContestRankTable($chache['contest_info'],$cid,$ret); |
||
1567 | |||
1568 | if (time() < $chache['frozen_time']){ |
||
1569 | Cache::tags(['contest', 'rank'])->put($cid, $ret); |
||
1570 | } |
||
1571 | Cache::tags(['contest', 'rank'])->put("contestAdmin$cid", $ret); |
||
1572 | if(time() > $chache['end_time']){ |
||
1573 | $this->storeContestRankInMySQL($cid, $ret); |
||
1574 | } |
||
1575 | } |
||
1576 | else{ |
||
1577 | $ret=[]; |
||
1578 | $chache=[]; |
||
1579 | $chache['contest_info']=DB::table("contest")->where("cid", $cid)->first(); |
||
1580 | $chache['problemSet']=DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([ |
||
1581 | "cid"=>$cid |
||
1582 | ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title")->get()->all(); |
||
1583 | $chache['frozen_time']=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
1584 | $chache['end_time']=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
1585 | |||
1586 | if ($chache['contest_info']["registration"]) { |
||
1587 | $submissionUsers=DB::table("contest_participant")->where([ |
||
1588 | "cid"=>$cid, |
||
1589 | "audit"=>1 |
||
1590 | ])->select('uid')->get()->all(); |
||
1591 | }else{ |
||
1592 | $submissionUsers=DB::table("submission")->where([ |
||
1593 | "cid"=>$cid |
||
1594 | ])->where( |
||
1595 | "submission_date", |
||
1596 | "<", |
||
1597 | $chache['frozen_time'] |
||
1598 | )->select('uid')->groupBy('uid')->get()->all(); |
||
1599 | $submissionUsersAdmin=DB::table("submission")->where([ |
||
1600 | "cid"=>$cid |
||
1601 | ])->select('uid')->groupBy('uid')->get()->all(); |
||
1602 | } |
||
1603 | |||
1604 | $chacheAdmin = $chache; |
||
1605 | |||
1606 | foreach ($submissionUsers as $s) { |
||
1607 | foreach ($chache['problemSet'] as $key => $p) { |
||
1608 | $p['cpid'] = $key; |
||
1609 | $ret = $this->updateContestRankDetail($chache['contest_info'],$p,$cid,$s['uid'],$ret); |
||
1610 | } |
||
1611 | } |
||
1612 | $ret = $this->sortContestRankTable($chache['contest_info'],$cid,$ret); |
||
1613 | Cache::tags(['contest', 'rank'])->put($cid, $ret); |
||
1614 | |||
1615 | $retAdmin=[]; |
||
1616 | foreach ($submissionUsersAdmin as $s) { |
||
1617 | foreach ($chacheAdmin['problemSet'] as $key => $p) { |
||
1618 | $p['cpid'] = $key; |
||
1619 | $retAdmin = $this->updateContestRankDetail($chacheAdmin['contest_info'],$p,$cid,$s['uid'],$retAdmin); |
||
1620 | } |
||
1621 | } |
||
1622 | $retAdmin = $this->sortContestRankTable($chacheAdmin['contest_info'],$cid,$retAdmin); |
||
1623 | Cache::tags(['contest', 'rank'])->put("contestAdmin$cid", $retAdmin); |
||
1624 | } |
||
1625 | } |
||
1626 | }catch(LockTimeoutException $e){ |
||
1627 | Log::warning("Contest Rank Lock Timed Out"); |
||
1628 | }finally{ |
||
1629 | optional($lock)->release(); |
||
1630 | } |
||
1631 | } |
||
1632 | |||
1633 | public function sortContestRankTable($contest_info,$cid,$ret) |
||
1634 | { |
||
1635 | if ($contest_info["rule"]==1){ |
||
1636 | usort($ret, function ($a, $b) { |
||
1637 | if ($a["score"]==$b["score"]) { |
||
1638 | if ($a["penalty"]==$b["penalty"]) { |
||
1639 | return 0; |
||
1640 | } elseif (($a["penalty"]>$b["penalty"])) { |
||
1641 | return 1; |
||
1642 | } else { |
||
1643 | return -1; |
||
1644 | } |
||
1645 | } elseif ($a["score"]>$b["score"]) { |
||
1646 | return -1; |
||
1647 | } else { |
||
1648 | return 1; |
||
1649 | } |
||
1650 | }); |
||
1651 | }else if ($contest_info["rule"]==2){ |
||
1652 | usort($ret, function ($a, $b) { |
||
1653 | if ($a["score"]==$b["score"]) { |
||
1654 | if ($a["solved"]==$b["solved"]) { |
||
1655 | return 0; |
||
1656 | } elseif (($a["solved"]<$b["solved"])) { |
||
1657 | return 1; |
||
1658 | } else { |
||
1659 | return -1; |
||
1660 | } |
||
1661 | } elseif ($a["score"]>$b["score"]) { |
||
1662 | return -1; |
||
1663 | } else { |
||
1664 | return 1; |
||
1665 | } |
||
1666 | }); |
||
1667 | } |
||
1668 | return $ret; |
||
1669 | } |
||
1670 | |||
1671 | public function updateContestRankDetail($contest_info,$problem,$cid,$uid,$ret) |
||
1771 | } |
||
1772 | |||
1773 | public function assignMember($cid,$uid){ |
||
1776 | ]); |
||
1777 | } |
||
1778 | |||
1779 | public function canUpdateContestTime($cid,$time = []) |
||
1780 | { |
||
1781 | $begin_time_new = $time['begin'] ?? null; |
||
1782 | $end_time_new = $time['end'] ?? null; |
||
1783 | |||
1784 | $hold_time = DB::table('contest') |
||
1785 | ->where('cid',$cid) |
||
1786 | ->select('begin_time','end_time') |
||
1787 | ->first(); |
||
1788 | $begin_stamps = strtotime($hold_time['begin_time']); |
||
1789 | $end_stamps = strtotime($hold_time['end_time']); |
||
1790 | /* |
||
1791 | -1 : have not begun |
||
1792 | 0 : ing |
||
1793 | 1 : end |
||
1794 | */ |
||
1795 | $status = time() >= $end_stamps ? 1 |
||
1796 | : (time() <= $begin_stamps ? -1 : 0); |
||
1797 | if($status === -1){ |
||
1798 | if(time() > $begin_time_new){ |
||
1799 | return false; |
||
1800 | } |
||
1801 | return true; |
||
1802 | }else if($status === 0){ |
||
1803 | if($begin_time_new !== null){ |
||
1804 | return false; |
||
1805 | } |
||
1806 | if($end_time_new !== null){ |
||
1807 | if(strtotime($end_time_new) <= time()){ |
||
1808 | return false; |
||
1809 | }else{ |
||
1810 | return true; |
||
1811 | } |
||
1812 | } |
||
1813 | }else{ |
||
1814 | return false; |
||
1815 | } |
||
1816 | |||
1817 | return true; |
||
1818 | } |
||
1819 | |||
1820 | public function replyClarification($ccid, $content) |
||
1821 | { |
||
1822 | return DB::table("contest_clarification")->where('ccid','=',$ccid)->update([ |
||
1823 | "reply"=>$content |
||
1824 | ]); |
||
1825 | } |
||
1826 | |||
1827 | public function setClarificationPublic($ccid, $public) |
||
1828 | { |
||
1829 | if($public) |
||
1830 | { |
||
1831 | return DB::table("contest_clarification")->where('ccid','=',$ccid)->update([ |
||
1832 | "public"=>1 |
||
1833 | ]); |
||
1834 | } |
||
1835 | else |
||
1836 | { |
||
1837 | return DB::table("contest_clarification")->where('ccid','=',$ccid)->update([ |
||
1838 | "public"=>0 |
||
1839 | ]); |
||
1840 | } |
||
1841 | } |
||
1842 | |||
1843 | public function getContestAccount($cid) |
||
1844 | { |
||
1845 | return Cache::tags(['contest', 'account'])->get($cid); |
||
1846 | } |
||
1847 | |||
1848 | public function praticeAnalysis($cid){ |
||
1890 | } |
||
1891 | |||
1892 | public function storeContestRankInMySQL($cid, $data) |
||
1893 | { |
||
1894 | $contestRankJson = json_encode($data); |
||
1895 | return DB::table('contest')->where('cid','=',$cid)->update([ |
||
1896 | 'rank' => $contestRankJson |
||
1897 | ]); |
||
1898 | } |
||
1899 | |||
1900 | public function getContestRankFromMySQL($cid) |
||
1901 | { |
||
1902 | $contestRankJson = DB::table('contest')->where('cid','=',$cid)->pluck('rank')->first(); |
||
1903 | $data = json_decode($contestRankJson, true); |
||
1904 | return $data; |
||
1905 | } |
||
1906 | |||
1907 | public function isVerified($cid) |
||
1910 | } |
||
1911 | |||
1912 | public function judgeOver($cid) |
||
1913 | { |
||
1914 | $submissions = DB::table('submission') |
||
1915 | ->where(['cid' => $cid]) |
||
1916 | ->whereIn('verdict',['Waiting','Pending']) |
||
1917 | ->select('sid') |
||
1918 | ->get()->all(); |
||
1919 | if(empty($submissions)){ |
||
1920 | return [ |
||
1921 | 'result' => true |
||
1922 | ]; |
||
1923 | }else{ |
||
1924 | return [ |
||
1925 | 'result' => false, |
||
1926 | 'sid' => $submissions |
||
1927 | ]; |
||
1928 | } |
||
1929 | } |
||
1930 | } |
||
1931 |