| Total Complexity | 275 |
| Total Lines | 1896 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 6 | Features | 1 |
Complex classes like ContestModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContestModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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) |
||
| 559 | } |
||
| 560 | |||
| 561 | public function isFrozen($cid) |
||
| 568 | } |
||
| 569 | } |
||
| 570 | |||
| 571 | public function contestProblemInfoACM($cid, $pid, $uid) |
||
| 572 | { |
||
| 573 | $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=[]; |
||
| 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->getContestRankFromMySQL($cid); |
||
| 811 | if(!isset($contestRankRaw)){ |
||
| 812 | $contestRankRaw=$this->contestRankCache($cid); |
||
| 813 | $this->storeContestRankInMySQL($cid, $contestRankRaw); |
||
| 814 | } |
||
| 815 | } |
||
| 816 | }else{ |
||
| 817 | $contestRankRaw=$this->getContestRankFromMySQL($cid); |
||
| 818 | if(!isset($contestRankRaw)){ |
||
| 819 | $contestRankRaw=Cache::tags(['contest', 'rank'])->get($cid); |
||
| 820 | if(!isset($contestRankRaw)){ |
||
| 821 | $contestRankRaw=$this->contestRankCache($cid); |
||
| 822 | } |
||
| 823 | $this->storeContestRankInMySQL($cid, $contestRankRaw); |
||
| 824 | } |
||
| 825 | } |
||
| 826 | } |
||
| 827 | |||
| 828 | /** Old version */ |
||
| 829 | // if ($contestRankRaw==null) { |
||
| 830 | // $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
| 831 | // if(time() > $end_time && !Cache::has($cid)){ |
||
| 832 | // $contestRankRaw=$this->contestRankCache($cid); |
||
| 833 | // // Cache::forever($cid, $contestRankRaw); |
||
| 834 | // }else{ |
||
| 835 | // $contestRankRaw=$this->contestRankCache($cid); |
||
| 836 | // } |
||
| 837 | // } |
||
| 838 | if($contest_info["rule"]==1){ |
||
| 839 | foreach ($contestRankRaw as &$cr) { |
||
| 840 | $solved = 0; |
||
| 841 | foreach($cr['problem_detail'] as $pd){ |
||
| 842 | if(!empty($pd['solved_time_parsed'])){ |
||
| 843 | $solved ++; |
||
| 844 | } |
||
| 845 | } |
||
| 846 | $cr['solved'] = $solved; |
||
| 847 | } |
||
| 848 | } |
||
| 849 | |||
| 850 | $ret=$contestRankRaw; |
||
| 851 | |||
| 852 | foreach ($ret as $r) { |
||
| 853 | if (!$user_in_group) { |
||
| 854 | $r["nick_name"]=''; |
||
| 855 | } |
||
| 856 | } |
||
| 857 | |||
| 858 | return $ret; |
||
| 859 | } |
||
| 860 | |||
| 861 | public function getRejudgeQueue($cid) |
||
| 891 | } |
||
| 892 | |||
| 893 | public function getClarificationList($cid) |
||
| 894 | { |
||
| 895 | $uid = Auth::user()->id; |
||
| 896 | $clearance = $this -> judgeClearance($cid, $uid); |
||
| 897 | if($clearance == 3){ |
||
| 898 | return DB::table("contest_clarification")->where([ |
||
| 899 | "cid"=>$cid |
||
| 900 | ])->orderBy('create_time', 'desc')->get()->all(); |
||
| 901 | }else{ |
||
| 902 | return DB::table("contest_clarification")->where([ |
||
| 903 | "cid"=>$cid |
||
| 904 | ])->where(function ($query) { |
||
| 905 | $query->where([ |
||
| 906 | "public"=>1 |
||
| 907 | ])->orWhere([ |
||
| 908 | "uid" => Auth::user()->id |
||
| 909 | ]); |
||
| 910 | })->orderBy('create_time', 'desc')->get()->all(); |
||
| 911 | } |
||
| 912 | } |
||
| 913 | |||
| 914 | public function fetchClarification($cid) |
||
| 915 | { |
||
| 916 | return DB::table("contest_clarification")->where([ |
||
| 917 | "cid"=>$cid, |
||
| 918 | "type"=>0, |
||
| 919 | "public"=>1 |
||
| 920 | ])->whereBetween( |
||
| 921 | 'create_time', |
||
| 922 | [ |
||
| 923 | date("Y-m-d H:i:s", time()-59), |
||
| 924 | date("Y-m-d H:i:s") |
||
| 925 | ] |
||
| 926 | )->first(); |
||
| 927 | } |
||
| 928 | |||
| 929 | public function getlatestClarification($cid) |
||
| 936 | } |
||
| 937 | |||
| 938 | public function getClarificationDetail($ccid) |
||
| 939 | { |
||
| 940 | return DB::table("contest_clarification")->where([ |
||
| 941 | "ccid"=>$ccid, |
||
| 942 | "public"=>1 |
||
| 943 | ])->first(); |
||
| 944 | } |
||
| 945 | |||
| 946 | public function requestClarification($cid, $title, $content, $uid) |
||
| 947 | { |
||
| 948 | return DB::table("contest_clarification")->insertGetId([ |
||
| 949 | "cid"=>$cid, |
||
| 950 | "type"=>1, |
||
| 951 | "title"=>$title, |
||
| 952 | "content"=>$content, |
||
| 953 | "public"=>"0", |
||
| 954 | "uid"=>$uid, |
||
| 955 | "create_time"=>date("Y-m-d H:i:s") |
||
| 956 | ]); |
||
| 957 | } |
||
| 958 | |||
| 959 | public function issueAnnouncement($cid, $title, $content, $uid, $remote_code=null) |
||
| 960 | { |
||
| 961 | return DB::table("contest_clarification")->insertGetId([ |
||
| 962 | "cid"=>$cid, |
||
| 963 | "type"=>0, |
||
| 964 | "title"=>$title, |
||
| 965 | "content"=>$content, |
||
| 966 | "public"=>"1", |
||
| 967 | "uid"=>$uid, |
||
| 968 | "create_time"=>date("Y-m-d H:i:s"), |
||
| 969 | "remote_code"=>$remote_code |
||
| 970 | ]); |
||
| 971 | } |
||
| 972 | |||
| 973 | public function remoteAnnouncement($remote_code) { |
||
| 974 | return DB::table("contest_clarification")->where("remote_code", $remote_code)->get()->first(); |
||
| 975 | } |
||
| 976 | public function isContestEnded($cid) |
||
| 979 | } |
||
| 980 | |||
| 981 | public function isContestRunning($cid) |
||
| 982 | { |
||
| 983 | 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(); |
||
| 984 | } |
||
| 985 | |||
| 986 | public function formatSubmitTime($date) |
||
| 987 | { |
||
| 988 | $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"]; |
||
| 989 | $lengths=["60", "60", "24", "7", "4.35", "12", "10"]; |
||
| 990 | |||
| 991 | $now=time(); |
||
| 992 | $unix_date=strtotime($date); |
||
| 993 | |||
| 994 | if (empty($unix_date)) { |
||
| 995 | return "Bad date"; |
||
| 996 | } |
||
| 997 | |||
| 998 | if ($now>$unix_date) { |
||
| 999 | $difference=$now-$unix_date; |
||
| 1000 | $tense="ago"; |
||
| 1001 | } else { |
||
| 1002 | $difference=$unix_date-$now; |
||
| 1003 | $tense="from now"; |
||
| 1004 | } |
||
| 1005 | |||
| 1006 | for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) { |
||
| 1007 | $difference/=$lengths[$j]; |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | $difference=round($difference); |
||
| 1011 | |||
| 1012 | if ($difference!=1) { |
||
| 1013 | $periods[$j].="s"; |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | return "$difference $periods[$j] {$tense}"; |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | public function formatAbsTime($sec) |
||
| 1038 | } |
||
| 1039 | |||
| 1040 | public function frozenTime($cid) |
||
| 1044 | } |
||
| 1045 | |||
| 1046 | public function getContestRecord($filter, $cid) |
||
| 1047 | { |
||
| 1048 | $basicInfo=$this->basic($cid); |
||
| 1049 | $userInfo=DB::table('group_member')->where('gid',$basicInfo["gid"])->where('uid',Auth::user()->id)->get()->first(); |
||
| 1050 | $problemSet_temp=DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([ |
||
| 1051 | "cid"=>$cid |
||
| 1052 | ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title", "points", "tot_score")->get()->all(); |
||
| 1053 | $problemSet=[]; |
||
| 1054 | foreach ($problemSet_temp as $p) { |
||
| 1055 | $problemSet[(string) $p["pid"]]=["ncode"=>$p["ncode"], "points"=>$p["points"], "tot_score"=>$p["tot_score"]]; |
||
| 1056 | } |
||
| 1057 | |||
| 1058 | $frozen_time=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
| 1059 | $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
| 1060 | $contestEnd=time()>$end_time; |
||
| 1061 | |||
| 1062 | $filter['pid'] = array_search($filter['ncode'], array_column($problemSet_temp, 'ncode')); |
||
| 1063 | if($filter['pid']==false){ |
||
| 1064 | $filter['pid'] = null; |
||
| 1065 | }else{ |
||
| 1066 | $filter['pid'] = $problemSet_temp[$filter['pid']]['pid']; |
||
| 1067 | } |
||
| 1068 | |||
| 1069 | if($userInfo==null || $userInfo["role"]!=3){ |
||
| 1070 | if ($basicInfo["status_visibility"]==2) { |
||
| 1071 | // View all |
||
| 1072 | $paginator=DB::table("submission")->where([ |
||
| 1073 | 'cid'=>$cid |
||
| 1074 | ])->where( |
||
| 1075 | "submission_date", |
||
| 1076 | "<", |
||
| 1077 | $end_time |
||
| 1078 | )->join( |
||
| 1079 | "users", |
||
| 1080 | "users.id", |
||
| 1081 | "=", |
||
| 1082 | "submission.uid" |
||
| 1083 | )->where(function ($query) use ($frozen_time) { |
||
| 1084 | $query->where( |
||
| 1085 | "submission_date", |
||
| 1086 | "<", |
||
| 1087 | $frozen_time |
||
| 1088 | )->orWhere( |
||
| 1089 | 'uid', |
||
| 1090 | Auth::user()->id |
||
| 1091 | ); |
||
| 1092 | })->select( |
||
| 1093 | "sid", |
||
| 1094 | "uid", |
||
| 1095 | "pid", |
||
| 1096 | "name", |
||
| 1097 | "color", |
||
| 1098 | "verdict", |
||
| 1099 | "time", |
||
| 1100 | "memory", |
||
| 1101 | "language", |
||
| 1102 | "score", |
||
| 1103 | "submission_date", |
||
| 1104 | "share" |
||
| 1105 | )->orderBy( |
||
| 1106 | 'submission_date', |
||
| 1107 | 'desc' |
||
| 1108 | ); |
||
| 1109 | |||
| 1110 | if($filter["pid"]){ |
||
| 1111 | $paginator=$paginator->where(["pid"=>$filter["pid"]]); |
||
| 1112 | } |
||
| 1113 | |||
| 1114 | if($filter["result"]){ |
||
| 1115 | $paginator=$paginator->where(["verdict"=>$filter["result"]]); |
||
| 1116 | } |
||
| 1117 | |||
| 1118 | if($filter["account"]){ |
||
| 1119 | $paginator=$paginator->where(["name"=>$filter["account"]]); |
||
| 1120 | } |
||
| 1121 | |||
| 1122 | $paginator=$paginator->paginate(50); |
||
| 1123 | } elseif ($basicInfo["status_visibility"]==1) { |
||
| 1124 | $paginator=DB::table("submission")->where([ |
||
| 1125 | 'cid'=>$cid, |
||
| 1126 | 'uid'=>Auth::user()->id |
||
| 1127 | ])->where( |
||
| 1128 | "submission_date", |
||
| 1129 | "<", |
||
| 1130 | $end_time |
||
| 1131 | )->join( |
||
| 1132 | "users", |
||
| 1133 | "users.id", |
||
| 1134 | "=", |
||
| 1135 | "submission.uid" |
||
| 1136 | )->select( |
||
| 1137 | "sid", |
||
| 1138 | "uid", |
||
| 1139 | "pid", |
||
| 1140 | "name", |
||
| 1141 | "color", |
||
| 1142 | "verdict", |
||
| 1143 | "time", |
||
| 1144 | "memory", |
||
| 1145 | "language", |
||
| 1146 | "score", |
||
| 1147 | "submission_date", |
||
| 1148 | "share" |
||
| 1149 | )->orderBy( |
||
| 1150 | 'submission_date', |
||
| 1151 | 'desc' |
||
| 1152 | ); |
||
| 1153 | |||
| 1154 | if($filter["pid"]){ |
||
| 1155 | $paginator=$paginator->where(["pid"=>$filter["pid"]]); |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | if($filter["result"]){ |
||
| 1159 | $paginator=$paginator->where(["verdict"=>$filter["result"]]); |
||
| 1160 | } |
||
| 1161 | |||
| 1162 | if($filter["account"]){ |
||
| 1163 | $paginator=$paginator->where(["name"=>$filter["account"]]); |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | $paginator=$paginator->paginate(50); |
||
| 1167 | } else { |
||
| 1168 | return [ |
||
| 1169 | "paginator"=>null, |
||
| 1170 | "records"=>[] |
||
| 1171 | ]; |
||
| 1172 | } |
||
| 1173 | }else{ |
||
| 1174 | if ($basicInfo["status_visibility"]==2) { |
||
| 1175 | // View all |
||
| 1176 | $paginator=DB::table("submission")->where([ |
||
| 1177 | 'cid'=>$cid |
||
| 1178 | ])->where( |
||
| 1179 | "submission_date", |
||
| 1180 | "<", |
||
| 1181 | $end_time |
||
| 1182 | )->join( |
||
| 1183 | "users", |
||
| 1184 | "users.id", |
||
| 1185 | "=", |
||
| 1186 | "submission.uid" |
||
| 1187 | )->select( |
||
| 1188 | "sid", |
||
| 1189 | "uid", |
||
| 1190 | "pid", |
||
| 1191 | "name", |
||
| 1192 | "color", |
||
| 1193 | "verdict", |
||
| 1194 | "time", |
||
| 1195 | "memory", |
||
| 1196 | "language", |
||
| 1197 | "score", |
||
| 1198 | "submission_date", |
||
| 1199 | "share" |
||
| 1200 | )->orderBy( |
||
| 1201 | 'submission_date', |
||
| 1202 | 'desc' |
||
| 1203 | ); |
||
| 1204 | |||
| 1205 | if($filter["pid"]){ |
||
| 1206 | $paginator=$paginator->where(["pid"=>$filter["pid"]]); |
||
| 1207 | } |
||
| 1208 | |||
| 1209 | if($filter["result"]){ |
||
| 1210 | $paginator=$paginator->where(["verdict"=>$filter["result"]]); |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | if($filter["account"]){ |
||
| 1214 | $paginator=$paginator->where(["name"=>$filter["account"]]); |
||
| 1215 | } |
||
| 1216 | |||
| 1217 | $paginator=$paginator->paginate(50); |
||
| 1218 | } elseif ($basicInfo["status_visibility"]==1) { |
||
| 1219 | $paginator=DB::table("submission")->where([ |
||
| 1220 | 'cid'=>$cid, |
||
| 1221 | 'uid'=>Auth::user()->id |
||
| 1222 | ])->where( |
||
| 1223 | "submission_date", |
||
| 1224 | "<", |
||
| 1225 | $end_time |
||
| 1226 | )->join( |
||
| 1227 | "users", |
||
| 1228 | "users.id", |
||
| 1229 | "=", |
||
| 1230 | "submission.uid" |
||
| 1231 | )->select( |
||
| 1232 | "sid", |
||
| 1233 | "uid", |
||
| 1234 | "pid", |
||
| 1235 | "name", |
||
| 1236 | "color", |
||
| 1237 | "verdict", |
||
| 1238 | "time", |
||
| 1239 | "memory", |
||
| 1240 | "language", |
||
| 1241 | "score", |
||
| 1242 | "submission_date", |
||
| 1243 | "share" |
||
| 1244 | )->orderBy( |
||
| 1245 | 'submission_date', |
||
| 1246 | 'desc' |
||
| 1247 | ); |
||
| 1248 | |||
| 1249 | if($filter["pid"]){ |
||
| 1250 | $paginator=$paginator->where(["pid"=>$filter["pid"]]); |
||
| 1251 | } |
||
| 1252 | |||
| 1253 | if($filter["result"]){ |
||
| 1254 | $paginator=$paginator->where(["verdict"=>$filter["result"]]); |
||
| 1255 | } |
||
| 1256 | |||
| 1257 | if($filter["account"]){ |
||
| 1258 | $paginator=$paginator->where(["name"=>$filter["account"]]); |
||
| 1259 | } |
||
| 1260 | |||
| 1261 | $paginator=$paginator->paginate(50); |
||
| 1262 | } else { |
||
| 1263 | return [ |
||
| 1264 | "paginator"=>null, |
||
| 1265 | "records"=>[] |
||
| 1266 | ]; |
||
| 1267 | } |
||
| 1268 | } |
||
| 1269 | |||
| 1270 | $records=$paginator->all(); |
||
| 1271 | foreach ($records as &$r) { |
||
| 1272 | $r["submission_date_parsed"]=$this->formatSubmitTime(date('Y-m-d H:i:s', $r["submission_date"])); |
||
| 1273 | $r["submission_date"]=date('Y-m-d H:i:s', $r["submission_date"]); |
||
| 1274 | $r["nick_name"]=""; |
||
| 1275 | $r["ncode"]=$problemSet[(string) $r["pid"]]["ncode"]; |
||
| 1276 | if ($r["verdict"]=="Partially Accepted") { |
||
| 1277 | $score_parsed=round($r["score"] / $problemSet[(string) $r["pid"]]["tot_score"] * $problemSet[(string) $r["pid"]]["points"], 1); |
||
| 1278 | $r["verdict"].=" ($score_parsed)"; |
||
| 1279 | } |
||
| 1280 | if (!$contestEnd) { |
||
| 1281 | $r["share"]=0; |
||
| 1282 | } |
||
| 1283 | } |
||
| 1284 | return [ |
||
| 1285 | "paginator"=>$paginator, |
||
| 1286 | "records"=>$records |
||
| 1287 | ]; |
||
| 1288 | } |
||
| 1289 | |||
| 1290 | public function registration($cid, $uid=0) |
||
| 1291 | { |
||
| 1292 | if ($uid==0) { |
||
| 1293 | return []; |
||
| 1294 | } |
||
| 1295 | |||
| 1296 | |||
| 1297 | return DB::table("contest_participant")->where([ |
||
| 1298 | "cid" => $cid, |
||
| 1299 | "uid" => $uid, |
||
| 1300 | "audit" => 1 |
||
| 1301 | ])->first(); |
||
| 1302 | } |
||
| 1303 | |||
| 1304 | public function judgeClearance($cid, $uid=0) |
||
| 1305 | { |
||
| 1306 | /*************************** |
||
| 1307 | * 2 stands for participant* |
||
| 1308 | * 3 stands for admin * |
||
| 1309 | ***************************/ |
||
| 1310 | if ($uid==0) { |
||
| 1311 | return 0; |
||
| 1312 | } |
||
| 1313 | $groupModel = new GroupModel(); |
||
| 1314 | $contest_info=DB::table("contest")->where("cid", $cid)->first(); |
||
| 1315 | $userInfo=DB::table('group_member')->where('gid',$contest_info["gid"])->where('uid',$uid)->get()->first(); |
||
| 1316 | |||
| 1317 | if(empty($contest_info)){ |
||
| 1318 | // contest not exist |
||
| 1319 | return 0; |
||
| 1320 | } |
||
| 1321 | |||
| 1322 | if($uid == $contest_info['assign_uid'] || $groupModel->judgeClearance($contest_info['gid'],$uid) == 3){ |
||
| 1323 | return 3; |
||
| 1324 | } |
||
| 1325 | |||
| 1326 | $contest_started = strtotime($contest_info['begin_time']) < time(); |
||
| 1327 | $contest_ended = strtotime($contest_info['end_time']) < time(); |
||
| 1328 | if (!$contest_started) { |
||
| 1329 | // not started or do not exist |
||
| 1330 | return 0; |
||
| 1331 | } |
||
| 1332 | |||
| 1333 | if ($userInfo["role"]==3) { |
||
| 1334 | return 3; |
||
| 1335 | } |
||
| 1336 | |||
| 1337 | if ($contest_info["public"]) { |
||
| 1338 | //public |
||
| 1339 | if ($contest_ended) { |
||
| 1340 | return 1; |
||
| 1341 | } else { |
||
| 1342 | if ($contest_info["registration"]) { |
||
| 1343 | // check if uid in registration, temp return 3 |
||
| 1344 | $isParticipant=DB::table("contest_participant")->where([ |
||
| 1345 | "cid" => $cid, |
||
| 1346 | "uid" => $uid, |
||
| 1347 | "audit" => 1 |
||
| 1348 | ])->count(); |
||
| 1349 | if ($isParticipant) { |
||
| 1350 | return 2; |
||
| 1351 | } else { |
||
| 1352 | return 0; |
||
| 1353 | } |
||
| 1354 | } else { |
||
| 1355 | return 2; |
||
| 1356 | } |
||
| 1357 | } |
||
| 1358 | } else { |
||
| 1359 | //private |
||
| 1360 | $isMember=DB::table("group_member")->where([ |
||
| 1361 | "gid"=> $contest_info["gid"], |
||
| 1362 | "uid"=> $uid |
||
| 1363 | ])->where("role", ">", 0)->count(); |
||
| 1364 | if (!$isMember) { |
||
| 1365 | return 0; |
||
| 1366 | } else { |
||
| 1367 | if ($contest_info["registration"]) { |
||
| 1368 | // check if uid in registration, temp return 3 |
||
| 1369 | $isParticipant=DB::table("contest_participant")->where([ |
||
| 1370 | "cid" => $cid, |
||
| 1371 | "uid" => $uid, |
||
| 1372 | "audit" => 1 |
||
| 1373 | ])->count(); |
||
| 1374 | if ($isParticipant) { |
||
| 1375 | return 2; |
||
| 1376 | } else { |
||
| 1377 | return 0; |
||
| 1378 | } |
||
| 1379 | } else { |
||
| 1380 | return 2; |
||
| 1381 | } |
||
| 1382 | } |
||
| 1383 | } |
||
| 1384 | } |
||
| 1385 | |||
| 1386 | public function judgeOutsideClearance($cid, $uid=0) |
||
| 1387 | { |
||
| 1388 | $contest_info=DB::table("contest")->where("cid", $cid)->first(); |
||
| 1389 | if (empty($contest_info)) { |
||
| 1390 | return 0; |
||
| 1391 | } |
||
| 1392 | if ($contest_info["public"]) { |
||
| 1393 | return 1; |
||
| 1394 | } else { |
||
| 1395 | if ($uid==0) { |
||
| 1396 | return 0; |
||
| 1397 | } |
||
| 1398 | return DB::table("group_member")->where([ |
||
| 1399 | "gid"=> $contest_info["gid"], |
||
| 1400 | "uid"=> $uid |
||
| 1401 | ])->where("role", ">", 0)->count() ? 1 : 0; |
||
| 1402 | } |
||
| 1403 | } |
||
| 1404 | |||
| 1405 | public function contestName($cid) |
||
| 1406 | { |
||
| 1407 | return DB::table("contest")->where("cid", $cid)->select("name")->first()["name"]; |
||
| 1408 | } |
||
| 1409 | |||
| 1410 | public function contestRule($cid) |
||
| 1411 | { |
||
| 1412 | return DB::table("contest")->where("cid", $cid)->select("rule")->first()["rule"]; |
||
| 1413 | } |
||
| 1414 | |||
| 1415 | public function updateProfessionalRate($cid) |
||
| 1428 | } |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | public function contestUpdate($cid,$data,$problems) |
||
| 1432 | { |
||
| 1476 | } |
||
| 1477 | } |
||
| 1478 | |||
| 1479 | public function contestUpdateProblem($cid,$problems) |
||
| 1492 | ]); |
||
| 1493 | } |
||
| 1494 | } |
||
| 1495 | |||
| 1496 | public function arrangeContest($gid, $config, $problems) |
||
| 1497 | { |
||
| 1498 | $cid = -1; |
||
| 1499 | DB::transaction(function () use ($gid, $config, $problems,&$cid) { |
||
| 1500 | $cid=DB::table($this->tableName)->insertGetId([ |
||
| 1501 | "gid"=>$gid, |
||
| 1502 | "name"=>$config["name"], |
||
| 1503 | "assign_uid"=>$config["assign_uid"], |
||
| 1504 | "verified"=>0, //todo |
||
| 1505 | "rated"=>0, |
||
| 1506 | "anticheated"=>0, |
||
| 1507 | "practice"=>$config["practice"], |
||
| 1508 | "featured"=>0, |
||
| 1509 | "description"=>$config["description"], |
||
| 1510 | "rule"=>1, //todo |
||
| 1511 | "begin_time"=>$config["begin_time"], |
||
| 1512 | "end_time"=>$config["end_time"], |
||
| 1513 | "vcid"=>isset($config["vcid"])?$config["vcid"]:null, |
||
| 1514 | "public"=>$config["public"], |
||
| 1515 | "registration"=>0, //todo |
||
| 1516 | "registration_due"=>null, //todo |
||
| 1517 | "registant_type"=>0, //todo |
||
| 1518 | "froze_length"=>0, //todo |
||
| 1519 | "status_visibility"=>2, //todo |
||
| 1520 | "create_time"=>date("Y-m-d H:i:s"), |
||
| 1521 | "crawled" => isset($config['vcid'])?$config['crawled'] : null, |
||
| 1522 | "audit_status"=>$config["public"] ? 0 : 1 |
||
| 1523 | ]); |
||
| 1524 | |||
| 1525 | foreach ($problems as $p) { |
||
| 1526 | $pid=DB::table("problem")->where(["pcode"=>$p["pcode"]])->select("pid")->first()["pid"]; |
||
| 1527 | DB::table("contest_problem")->insert([ |
||
| 1528 | "cid"=>$cid, |
||
| 1529 | "number"=>$p["number"], |
||
| 1530 | "ncode"=>$this->intToChr($p["number"]-1), |
||
| 1531 | "pid"=>$pid, |
||
| 1532 | "alias"=>"", |
||
| 1533 | "points"=>$p["points"] |
||
| 1534 | ]); |
||
| 1535 | } |
||
| 1536 | }, 5); |
||
| 1537 | return $cid; |
||
| 1538 | } |
||
| 1539 | |||
| 1540 | public function updateContestRankTable($cid,$sub) |
||
| 1541 | { |
||
| 1542 | $lock = Cache::lock("contestrank$cid",10); |
||
| 1543 | try{ |
||
| 1544 | if($lock->get()){ |
||
| 1545 | if(Cache::tags(['contest','rank'])->get($cid) != null){ |
||
| 1546 | $ret = Cache::tags(['contest','rank'])->get($cid); |
||
| 1547 | $chache=[]; |
||
| 1548 | $chache['contest_info']=DB::table("contest")->where("cid", $cid)->first(); |
||
| 1549 | $chache['problemSet']=DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([ |
||
| 1550 | "cid"=>$cid |
||
| 1551 | ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title")->get()->all(); |
||
| 1552 | $chache['frozen_time']=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
| 1553 | $chache['end_time']=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
| 1554 | |||
| 1555 | $id = 0; |
||
| 1556 | |||
| 1557 | foreach($chache['problemSet'] as $key => $p){ |
||
| 1558 | if ($p['pid'] == $sub['pid']){ |
||
| 1559 | $chache['problemSet'][$key]['cpid'] = $key; |
||
| 1560 | $id = $key; |
||
| 1561 | } |
||
| 1562 | } |
||
| 1563 | |||
| 1564 | $ret = $this->updateContestRankDetail($chache['contest_info'],$chache['problemSet'][$id],$cid,$sub['uid'],$ret); |
||
| 1565 | $ret = $this->sortContestRankTable($chache['contest_info'],$cid,$ret); |
||
| 1566 | |||
| 1567 | if (time() < $chache['frozen_time']){ |
||
| 1568 | Cache::tags(['contest', 'rank'])->put($cid, $ret); |
||
| 1569 | } |
||
| 1570 | Cache::tags(['contest', 'rank'])->put("contestAdmin$cid", $ret); |
||
| 1571 | if(time() > $chache['end_time']){ |
||
| 1572 | $this->storeContestRankInMySQL($cid, $ret); |
||
| 1573 | } |
||
| 1574 | } |
||
| 1575 | else{ |
||
| 1576 | $ret=[]; |
||
| 1577 | $chache=[]; |
||
| 1578 | $chache['contest_info']=DB::table("contest")->where("cid", $cid)->first(); |
||
| 1579 | $chache['problemSet']=DB::table("contest_problem")->join("problem", "contest_problem.pid", "=", "problem.pid")->where([ |
||
| 1580 | "cid"=>$cid |
||
| 1581 | ])->orderBy('ncode', 'asc')->select("ncode", "alias", "contest_problem.pid as pid", "title")->get()->all(); |
||
| 1582 | $chache['frozen_time']=DB::table("contest")->where(["cid"=>$cid])->select(DB::raw("UNIX_TIMESTAMP(end_time)-froze_length as frozen_time"))->first()["frozen_time"]; |
||
| 1583 | $chache['end_time']=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
||
| 1584 | |||
| 1585 | if ($chache['contest_info']["registration"]) { |
||
| 1586 | $submissionUsers=DB::table("contest_participant")->where([ |
||
| 1587 | "cid"=>$cid, |
||
| 1588 | "audit"=>1 |
||
| 1589 | ])->select('uid')->get()->all(); |
||
| 1590 | }else{ |
||
| 1591 | $submissionUsers=DB::table("submission")->where([ |
||
| 1592 | "cid"=>$cid |
||
| 1593 | ])->where( |
||
| 1594 | "submission_date", |
||
| 1595 | "<", |
||
| 1596 | $chache['frozen_time'] |
||
| 1597 | )->select('uid')->groupBy('uid')->get()->all(); |
||
| 1598 | $submissionUsersAdmin=DB::table("submission")->where([ |
||
| 1599 | "cid"=>$cid |
||
| 1600 | ])->select('uid')->groupBy('uid')->get()->all(); |
||
| 1601 | } |
||
| 1602 | |||
| 1603 | $chacheAdmin = $chache; |
||
| 1604 | |||
| 1605 | foreach ($submissionUsers as $s) { |
||
| 1606 | foreach ($chache['problemSet'] as $key => $p) { |
||
| 1607 | $p['cpid'] = $key; |
||
| 1608 | $ret = $this->updateContestRankDetail($chache['contest_info'],$p,$cid,$s['uid'],$ret); |
||
| 1609 | } |
||
| 1610 | } |
||
| 1611 | $ret = $this->sortContestRankTable($chache['contest_info'],$cid,$ret); |
||
| 1612 | Cache::tags(['contest', 'rank'])->put($cid, $ret); |
||
| 1613 | |||
| 1614 | $retAdmin=[]; |
||
| 1615 | foreach ($submissionUsersAdmin as $s) { |
||
| 1616 | foreach ($chacheAdmin['problemSet'] as $key => $p) { |
||
| 1617 | $p['cpid'] = $key; |
||
| 1618 | $retAdmin = $this->updateContestRankDetail($chacheAdmin['contest_info'],$p,$cid,$s['uid'],$retAdmin); |
||
| 1619 | } |
||
| 1620 | } |
||
| 1621 | $retAdmin = $this->sortContestRankTable($chacheAdmin['contest_info'],$cid,$retAdmin); |
||
| 1622 | Cache::tags(['contest', 'rank'])->put("contestAdmin$cid", $retAdmin); |
||
| 1623 | } |
||
| 1624 | } |
||
| 1625 | }catch(LockTimeoutException $e){ |
||
| 1626 | Log::warning("Contest Rank Lock Timed Out"); |
||
| 1627 | }finally{ |
||
| 1628 | optional($lock)->release(); |
||
| 1629 | } |
||
| 1630 | } |
||
| 1631 | |||
| 1632 | public function sortContestRankTable($contest_info,$cid,$ret) |
||
| 1633 | { |
||
| 1634 | if ($contest_info["rule"]==1){ |
||
| 1635 | usort($ret, function ($a, $b) { |
||
| 1636 | if ($a["score"]==$b["score"]) { |
||
| 1637 | if ($a["penalty"]==$b["penalty"]) { |
||
| 1638 | return 0; |
||
| 1639 | } elseif (($a["penalty"]>$b["penalty"])) { |
||
| 1640 | return 1; |
||
| 1641 | } else { |
||
| 1642 | return -1; |
||
| 1643 | } |
||
| 1644 | } elseif ($a["score"]>$b["score"]) { |
||
| 1645 | return -1; |
||
| 1646 | } else { |
||
| 1647 | return 1; |
||
| 1648 | } |
||
| 1649 | }); |
||
| 1650 | }else if ($contest_info["rule"]==2){ |
||
| 1651 | usort($ret, function ($a, $b) { |
||
| 1652 | if ($a["score"]==$b["score"]) { |
||
| 1653 | if ($a["solved"]==$b["solved"]) { |
||
| 1654 | return 0; |
||
| 1655 | } elseif (($a["solved"]<$b["solved"])) { |
||
| 1656 | return 1; |
||
| 1657 | } else { |
||
| 1658 | return -1; |
||
| 1659 | } |
||
| 1660 | } elseif ($a["score"]>$b["score"]) { |
||
| 1661 | return -1; |
||
| 1662 | } else { |
||
| 1663 | return 1; |
||
| 1664 | } |
||
| 1665 | }); |
||
| 1666 | } |
||
| 1667 | return $ret; |
||
| 1668 | } |
||
| 1669 | |||
| 1670 | public function updateContestRankDetail($contest_info,$problem,$cid,$uid,$ret) |
||
| 1770 | } |
||
| 1771 | |||
| 1772 | public function assignMember($cid,$uid){ |
||
| 1775 | ]); |
||
| 1776 | } |
||
| 1777 | |||
| 1778 | public function canUpdateContestTime($cid,$time = []) |
||
| 1779 | { |
||
| 1780 | $begin_time_new = $time['begin'] ?? null; |
||
| 1781 | $end_time_new = $time['end'] ?? null; |
||
| 1782 | |||
| 1783 | $hold_time = DB::table('contest') |
||
| 1784 | ->where('cid',$cid) |
||
| 1785 | ->select('begin_time','end_time') |
||
| 1786 | ->first(); |
||
| 1787 | $begin_stamps = strtotime($hold_time['begin_time']); |
||
| 1788 | $end_stamps = strtotime($hold_time['end_time']); |
||
| 1789 | /* |
||
| 1790 | -1 : have not begun |
||
| 1791 | 0 : ing |
||
| 1792 | 1 : end |
||
| 1793 | */ |
||
| 1794 | $status = time() >= $end_stamps ? 1 |
||
| 1795 | : (time() <= $begin_stamps ? -1 : 0); |
||
| 1796 | if($status === -1){ |
||
| 1797 | if(time() > $begin_time_new){ |
||
| 1798 | return false; |
||
| 1799 | } |
||
| 1800 | return true; |
||
| 1801 | }else if($status === 0){ |
||
| 1802 | if($begin_time_new !== null){ |
||
| 1803 | return false; |
||
| 1804 | } |
||
| 1805 | if($end_time_new !== null){ |
||
| 1806 | if(strtotime($end_time_new) <= time()){ |
||
| 1807 | return false; |
||
| 1808 | }else{ |
||
| 1809 | return true; |
||
| 1810 | } |
||
| 1811 | } |
||
| 1812 | }else{ |
||
| 1813 | return false; |
||
| 1814 | } |
||
| 1815 | |||
| 1816 | return true; |
||
| 1817 | } |
||
| 1818 | |||
| 1819 | public function replyClarification($ccid, $content) |
||
| 1820 | { |
||
| 1821 | return DB::table("contest_clarification")->where('ccid','=',$ccid)->update([ |
||
| 1822 | "reply"=>$content |
||
| 1823 | ]); |
||
| 1824 | } |
||
| 1825 | |||
| 1826 | public function setClarificationPublic($ccid, $public) |
||
| 1827 | { |
||
| 1828 | if($public) |
||
| 1829 | { |
||
| 1830 | return DB::table("contest_clarification")->where('ccid','=',$ccid)->update([ |
||
| 1831 | "public"=>1 |
||
| 1832 | ]); |
||
| 1833 | } |
||
| 1834 | else |
||
| 1835 | { |
||
| 1836 | return DB::table("contest_clarification")->where('ccid','=',$ccid)->update([ |
||
| 1837 | "public"=>0 |
||
| 1838 | ]); |
||
| 1839 | } |
||
| 1840 | } |
||
| 1841 | |||
| 1842 | public function getContestAccount($cid) |
||
| 1843 | { |
||
| 1844 | return Cache::tags(['contest', 'account'])->get($cid); |
||
| 1845 | } |
||
| 1846 | |||
| 1847 | public function praticeAnalysis($cid){ |
||
| 1889 | } |
||
| 1890 | |||
| 1891 | public function storeContestRankInMySQL($cid, $data) |
||
| 1892 | { |
||
| 1893 | $contestRankJson = json_encode($data); |
||
| 1894 | return DB::table('contest')->where('cid','=',$cid)->update([ |
||
| 1895 | 'rank' => $contestRankJson |
||
| 1896 | ]); |
||
| 1897 | } |
||
| 1898 | |||
| 1899 | public function getContestRankFromMySQL($cid) |
||
| 1900 | { |
||
| 1901 | $contestRankJson = DB::table('contest')->where('cid','=',$cid)->pluck('rank')->first(); |
||
| 1902 | $data = json_decode($contestRankJson, true); |
||
| 1903 | return $data; |
||
| 1904 | } |
||
| 1905 | |||
| 1906 | public function isVerified($cid) |
||
| 1909 | } |
||
| 1910 | } |
||
| 1911 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths