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