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