| Total Complexity | 72 |
| Total Lines | 463 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ContestController 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 ContestController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class ContestController extends Controller |
||
| 12 | { |
||
| 13 | public function info(Request $request) { |
||
| 14 | $contest=$request->contest; |
||
| 15 | return response()->json([ |
||
| 16 | 'success' => true, |
||
| 17 | 'message' => 'Succeed', |
||
| 18 | 'ret' => [ |
||
| 19 | "cid" => $contest->cid, |
||
| 20 | "name" => $contest->name, |
||
| 21 | "img" => url($contest->img), |
||
| 22 | "begin_time" => $contest->begin_time, |
||
| 23 | "end_time" => $contest->end_time, |
||
| 24 | "problems" => count($contest->problems), |
||
| 25 | "organizer" => $contest->group->name, |
||
| 26 | "description" => $contest->description, |
||
| 27 | "badges" => [ |
||
| 28 | "rule_parsed" => ["Unknown", "ICPC", "IOI", "Custom ICPC", "Custom IOI"][$contest->rule], |
||
| 29 | "audit_status" => $contest->audit_status ? true : false, |
||
| 30 | "public" => $contest->public ? true : false, |
||
| 31 | "verified" => $contest->verified ? true : false, |
||
| 32 | "rated" => $contest->rated ? true : false, |
||
| 33 | "anticheated" => $contest->anticheated ? true : false, |
||
| 34 | "desktop" => $contest->desktop ? true : false, |
||
| 35 | ] |
||
| 36 | ], |
||
| 37 | 'err' => [] |
||
| 38 | ]); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function status(Request $request) { |
||
| 42 | $page=$request->page ?? 1; |
||
|
|
|||
| 43 | $filter=$request->filter; |
||
| 44 | $contest=$request->contest; |
||
| 45 | |||
| 46 | $account=$filter['account'] ?? null; |
||
| 47 | $problem=$filter['problem'] ?? null; |
||
| 48 | $result=$filter['result'] ?? null; |
||
| 49 | |||
| 50 | //filter |
||
| 51 | $builder=$contest->submissions()->orderBy('submission_date', 'desc')->with(['user', 'contest.group', 'problem']); |
||
| 52 | if ($account!==null) { |
||
| 53 | $participants=$contest->participants(); |
||
| 54 | $user=null; |
||
| 55 | foreach ($participants as $participant) { |
||
| 56 | if ($participant->name==$account) { |
||
| 57 | $user=$participant; |
||
| 58 | break; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | $builder=$builder->where('uid', $user==null ? -1 : $user->id); |
||
| 62 | } |
||
| 63 | if ($problem!==null) { |
||
| 64 | $problem=$contest->problems()->where('ncode', $problem)->first(); |
||
| 65 | $builder=$builder->where('pid', $problem->pid ?? null); |
||
| 66 | } |
||
| 67 | if ($result!==null) { |
||
| 68 | $builder=$builder->where('verdict', $result); |
||
| 69 | } |
||
| 70 | |||
| 71 | //status_visibility |
||
| 72 | if ($contest->status_visibility==1) { |
||
| 73 | if (auth()->check()) { |
||
| 74 | $builder=$builder->where('uid', auth()->user()->id); |
||
| 75 | } else { |
||
| 76 | $builder=$builder->where('uid', -1); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | if ($contest->status_visibility==0) { |
||
| 80 | $builder=$builder->where('uid', -1); |
||
| 81 | } |
||
| 82 | |||
| 83 | $submissions=$builder->paginate(50); |
||
| 84 | |||
| 85 | $regex='/\?page=([\d+])$/'; |
||
| 86 | $matches=[]; |
||
| 87 | $pagination=[ |
||
| 88 | 'current_page' => $submissions->currentPage(), |
||
| 89 | 'has_next_page' => $submissions->nextPageUrl()===null ? false : true, |
||
| 90 | 'has_previous_page' => $submissions->previousPageUrl()===null ? false : true, |
||
| 91 | 'next_page' => null, |
||
| 92 | 'previous_page' => null, |
||
| 93 | 'num_pages' => $submissions->lastPage(), |
||
| 94 | 'num_items' => $submissions->count(), |
||
| 95 | ]; |
||
| 96 | if ($pagination['has_next_page']) { |
||
| 97 | $next_page=preg_match($regex, $submissions->nextPageUrl(), $matches); |
||
| 98 | $pagination['next_page']=intval($matches[1]); |
||
| 99 | } |
||
| 100 | if ($pagination['has_previous_page']) { |
||
| 101 | $next_page=preg_match($regex, $submissions->previousPageUrl(), $matches); |
||
| 102 | $pagination['previous_page']=intval($matches[1]); |
||
| 103 | } |
||
| 104 | |||
| 105 | $data=[]; |
||
| 106 | foreach ($submissions->items() as $submission) { |
||
| 107 | $score_parse=0; |
||
| 108 | if ($contest->rule==2) { |
||
| 109 | if ($submission->verdict=='Accepted') { |
||
| 110 | $score_parse=100; |
||
| 111 | } else if ($submission->verdict=='Partially Accepted') { |
||
| 112 | $score_parse=round($submission->score / $submission->problem->tot_score * $contest->problems()->where('pid', $submission->problem->pid)->first()->points, 1); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | $data[]=[ |
||
| 116 | 'sid' => $submission->sid, |
||
| 117 | 'name' => $submission->user->name, |
||
| 118 | 'nickname' => $submission->nick_name, |
||
| 119 | 'ncode' => $submission->ncode, |
||
| 120 | 'color' => $submission->color, |
||
| 121 | 'verdict' => $submission->verdict, |
||
| 122 | 'score_parsed' => $score_parse, |
||
| 123 | 'time' => $submission->time, |
||
| 124 | 'memory' => $submission->memory, |
||
| 125 | 'language' => $submission->language, |
||
| 126 | 'submission_date' => date('Y-m-d H:i:s', $submission->submission_date), |
||
| 127 | 'submission_date_parsed' => $submission->submission_date_parsed |
||
| 128 | ]; |
||
| 129 | } |
||
| 130 | return response()->json([ |
||
| 131 | 'success' => true, |
||
| 132 | 'message' => 'Succeed', |
||
| 133 | 'ret' => [ |
||
| 134 | 'pagination' => $pagination, |
||
| 135 | 'data' => $data |
||
| 136 | ], |
||
| 137 | 'err' => [] |
||
| 138 | ]); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function scoreboard(Request $request) { |
||
| 142 | $contest=$request->contest; |
||
| 143 | $contestModel=new OutdatedContestModel(); |
||
| 144 | $contestRank=$contestModel->contestRank($contest->cid, auth()->check() ? auth()->user()->id : 0); |
||
| 145 | |||
| 146 | //frozen about |
||
| 147 | if ($contest->forze_length!=0) { |
||
| 148 | $frozen=[ |
||
| 149 | 'enable' => true, |
||
| 150 | 'frozen_length' => $contest->forze_length |
||
| 151 | ]; |
||
| 152 | } else { |
||
| 153 | $frozen=[ |
||
| 154 | 'enable' => false, |
||
| 155 | 'frozen_length' => 0 |
||
| 156 | ]; |
||
| 157 | } |
||
| 158 | |||
| 159 | //header |
||
| 160 | if ($contest->rule==1) { |
||
| 161 | $header=[ |
||
| 162 | 'rank' => 'Rank', |
||
| 163 | 'normal' => [ |
||
| 164 | 'Account', 'Score', 'Penalty' |
||
| 165 | ], |
||
| 166 | 'subHeader' => true, |
||
| 167 | 'problems' => [], |
||
| 168 | 'problemsSubHeader' => [] |
||
| 169 | ]; |
||
| 170 | $problems=$contest->problems()->orderBy('ncode', 'asc')->get(); |
||
| 171 | foreach ($problems as $problem) { |
||
| 172 | $header['problems'][]=$problem->ncode; |
||
| 173 | $header['problemsSubHeader'][]=$problem->submissions()->where('submission_date', '<=', $contest->frozen_time)->where('verdict', 'Accepted')->count() |
||
| 174 | . ' / '.$problem->submissions()->where('submission_date', '<=', $contest->frozen_time)->count(); |
||
| 175 | } |
||
| 176 | } else if ($contest->rule==2) { |
||
| 177 | $header=[ |
||
| 178 | 'rank' => 'Rank', |
||
| 179 | 'normal' => [ |
||
| 180 | 'Account', 'Score', 'Solved' |
||
| 181 | ], |
||
| 182 | 'subHeader' => false, |
||
| 183 | 'problems' => [] |
||
| 184 | ]; |
||
| 185 | $problems=$contest->problems()->orderBy('ncode', 'asc')->get(); |
||
| 186 | foreach ($problems as $problem) { |
||
| 187 | $header['problems'][]=$problem->ncode; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | $user=auth()->user(); |
||
| 191 | //body |
||
| 192 | if ($contest->rule==1) { |
||
| 193 | $body=[]; |
||
| 194 | $lastRank=null; |
||
| 195 | $rank=1; |
||
| 196 | foreach ($contestRank as $userRank) { |
||
| 197 | if (!empty($lastRank)) { |
||
| 198 | if ($lastRank['score']!=$userRank['score'] || $lastRank['penalty']!=$userRank['penalty']) { |
||
| 199 | $rank+=1; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | $lastRank=$userRank; |
||
| 203 | $userBody=[ |
||
| 204 | 'rank' => $rank, |
||
| 205 | 'normal' => [ |
||
| 206 | $userRank['name'], $userRank['score'], intval($userRank['penalty']) |
||
| 207 | ], |
||
| 208 | 'problems' => [] |
||
| 209 | ]; |
||
| 210 | foreach ($userRank['problem_detail'] as $problem) { |
||
| 211 | $userBody['problems'][]=[ |
||
| 212 | 'mainColor' => $problem['color']==="" ? null : $problem['color'], |
||
| 213 | 'mainScore' => $problem['solved_time_parsed']==="" ? null : $problem['solved_time_parsed'], |
||
| 214 | 'subColor' => null, |
||
| 215 | 'subScore' => $problem['wrong_doings']==0 ? null : '-'.$problem['wrong_doings'] |
||
| 216 | ]; |
||
| 217 | } |
||
| 218 | $userBody['extra']=[ |
||
| 219 | 'owner' => isset($userBody['remote']) && $userBody['remote'] ? false : $user->id==$userRank['uid'], |
||
| 220 | 'remote' => $userBody['remote'] ?? false |
||
| 221 | ]; |
||
| 222 | $body[]=$userBody; |
||
| 223 | } |
||
| 224 | } else if ($contest->rule==2) { |
||
| 225 | $body=[]; |
||
| 226 | $lastRank=null; |
||
| 227 | $rank=1; |
||
| 228 | foreach ($contestRank as $userRank) { |
||
| 229 | if (!empty($lastRank)) { |
||
| 230 | if ($lastRank['score']!=$userRank['score'] || $lastRank['solved']!=$userRank['solved']) { |
||
| 231 | $rank+=1; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | $lastRank=$userRank; |
||
| 235 | $userBody=[ |
||
| 236 | 'rank' => $rank, |
||
| 237 | 'normal' => [ |
||
| 238 | $userRank['name'], $userRank['score'], intval($userRank['solved']) |
||
| 239 | ], |
||
| 240 | 'problems' => [] |
||
| 241 | ]; |
||
| 242 | foreach ($userRank['problem_detail'] as $problem) { |
||
| 243 | $userBody['problems'][]=[ |
||
| 244 | 'mainColor' => $problem['color']==="" ? null : $problem['color'], |
||
| 245 | 'mainScore' => $problem['score']==="" ? null : $problem['score_parsed'], |
||
| 246 | 'subColor' => null, |
||
| 247 | 'subScore' => null |
||
| 248 | ]; |
||
| 249 | } |
||
| 250 | $userBody['extra']=[ |
||
| 251 | 'owner' => isset($userBody['remote']) && $userBody['remote'] ? false : $user->id==$userRank['uid'], |
||
| 252 | 'remote' => $userBody['remote'] ?? false |
||
| 253 | ]; |
||
| 254 | $body[]=$userBody; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | return response()->json([ |
||
| 259 | 'success' => true, |
||
| 260 | 'message' => 'Succeed', |
||
| 261 | 'ret' => [ |
||
| 262 | 'frozen' => $frozen, |
||
| 263 | 'header' => $header, |
||
| 264 | 'body' => $body, |
||
| 265 | ], |
||
| 266 | 'err' => [] |
||
| 267 | ]); |
||
| 268 | } |
||
| 269 | |||
| 270 | public function clarification(Request $request) { |
||
| 271 | $contest=$request->contest; |
||
| 272 | return response()->json([ |
||
| 273 | 'success' => true, |
||
| 274 | 'message' => 'Succeed', |
||
| 275 | 'ret' => [ |
||
| 276 | 'clarifications' => $contest->clarifications()->orderBy('created_at', 'desc')->get() |
||
| 277 | ], |
||
| 278 | 'err' => [] |
||
| 279 | ]); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function requestClarification(Request $request) { |
||
| 311 | ]); |
||
| 312 | } |
||
| 313 | |||
| 314 | public function problems(Request $request) { |
||
| 315 | $contest=$request->contest; |
||
| 316 | $contestProblems=$contest->problems()->with('problem')->orderBy('ncode', 'asc')->get(); |
||
| 317 | $problems=[]; |
||
| 318 | foreach ($contestProblems as $contestProblem) { |
||
| 319 | //get status |
||
| 320 | $ac_submission=$contestProblem->submissions()->where('uid', auth()->user()->id)->where('verdict', 'Accepted')->orderBy('submission_date', 'desc')->first(); |
||
| 321 | $last_submission=$contestProblem->submissions()->where('uid', auth()->user()->id)->orderBy('submission_date', 'desc')->first(); |
||
| 322 | //get compilers |
||
| 323 | $compilers_info=[]; |
||
| 324 | $compilers=$contestProblem->compilers->get(); |
||
| 325 | foreach ($compilers as $compiler) { |
||
| 326 | $compilers_info[]=[ |
||
| 327 | 'coid' => $compiler->coid, |
||
| 328 | 'oid' => $compiler->oid, |
||
| 329 | 'comp' => $compiler->comp, |
||
| 330 | 'lang' => $compiler->lang, |
||
| 331 | 'lcode' => $compiler->lcode, |
||
| 332 | 'icon' => $compiler->icon, |
||
| 333 | 'display_name' => $compiler->display_name |
||
| 334 | ]; |
||
| 335 | } |
||
| 336 | $highest_submit=$contestProblem->submissions()->where('uid', auth()->user()->id)->orderBy('score', 'desc')->first(); |
||
| 337 | $problems[]=[ |
||
| 338 | 'pid' => $contestProblem->pid, |
||
| 339 | 'pcode' => $contestProblem->problem->pcode, |
||
| 340 | 'ncode' => $contestProblem->ncode, |
||
| 341 | 'title' => $contestProblem->problem->title, |
||
| 342 | 'limitations' => [ |
||
| 343 | 'time_limit' => $contestProblem->problem->time_limit, |
||
| 344 | 'memory_limit' => $contestProblem->problem->memory_limit, |
||
| 345 | ], |
||
| 346 | 'statistics' => $contest->rule==1 ? [ |
||
| 347 | 'accepted' => $contestProblem->submissions()->where('submission_date', '<=', $contest->frozen_time)->where('verdict', 'Accepted')->count(), |
||
| 348 | 'attempted' => $contestProblem->submissions()->where('submission_date', '<=', $contest->frozen_time)->count(), |
||
| 349 | 'score' => null, |
||
| 350 | 'current_score' => null |
||
| 351 | ] : [ |
||
| 352 | 'accepted' => null, |
||
| 353 | 'attempted' => null, |
||
| 354 | 'score' => $contestProblem->points, |
||
| 355 | 'current_score' => empty($highest_submit) ? 0 : $highest_submit->score / $contestProblem->problem->tot_score * $contestProblem->points |
||
| 356 | ], |
||
| 357 | 'status' => [ |
||
| 358 | 'verdict' => !empty($ac_submission) ? $ac_submission->verdict : (!empty($last_submission) ? $last_submission->verdict : 'NOT SUBMIT'), |
||
| 359 | 'color' => !empty($ac_submission) ? $ac_submission->color : (!empty($last_submission) ? $last_submission->color : ''), |
||
| 360 | 'last_submission' => !empty($last_submission) ? [ |
||
| 361 | 'sid' => $last_submission->sid, |
||
| 362 | 'verdict' => $last_submission->verdict, |
||
| 363 | 'compile_info' => $last_submission->compile_info, |
||
| 364 | 'color' => $last_submission->color, |
||
| 365 | 'solution' => $last_submission->solution, |
||
| 366 | 'coid' => $last_submission->coid, |
||
| 367 | 'submission_date' => $last_submission->submission_date |
||
| 368 | ] : false |
||
| 369 | ], |
||
| 370 | 'compilers' => $compilers_info |
||
| 371 | ]; |
||
| 372 | } |
||
| 373 | return response()->json([ |
||
| 374 | 'success' => true, |
||
| 375 | 'message' => 'Succeed', |
||
| 376 | 'ret' => [ |
||
| 377 | "file" => [ |
||
| 378 | "enable" => false, |
||
| 379 | "name" => null, |
||
| 380 | "url" => null, |
||
| 381 | "extension" => null |
||
| 382 | ], |
||
| 383 | 'problems' => $problems |
||
| 384 | ], |
||
| 385 | 'err' => [] |
||
| 386 | ]); |
||
| 387 | } |
||
| 388 | |||
| 389 | public function submitSolution(Request $request) { |
||
| 390 | $contest=$request->contest; |
||
| 391 | $contest_problem=$request->contest_problem; |
||
| 392 | $problem=$request->problem; |
||
| 393 | $compiler=$request->compiler; |
||
| 394 | |||
| 395 | if (empty($request->solution) || strlen($request->solution)>65535) { |
||
| 396 | return response()->json([ |
||
| 397 | 'success' => false, |
||
| 398 | 'message' => 'Parameter \'solution\' Invalid', |
||
| 399 | 'ret' => [], |
||
| 400 | 'err' => [ |
||
| 401 | 'code' => 1100, |
||
| 402 | 'msg' => 'Parameter \'solution\' Invalid', |
||
| 403 | 'data'=>[] |
||
| 404 | ] |
||
| 405 | ]); |
||
| 406 | } |
||
| 407 | $submission=Submission::create([ |
||
| 408 | 'time'=>'0', |
||
| 409 | 'memory'=>'0', |
||
| 410 | 'verdict'=>'Pending', |
||
| 411 | 'solution'=>$request->solution, |
||
| 412 | 'language'=>$compiler->display_name, |
||
| 413 | 'submission_date'=>time(), |
||
| 414 | 'uid'=>auth()->user()->id, |
||
| 415 | 'pid'=>$problem->pid, |
||
| 416 | 'remote_id'=>'', |
||
| 417 | 'coid'=>$compiler->coid, |
||
| 418 | 'cid'=>$contest->cid, |
||
| 419 | 'vcid'=>$request->vcid, |
||
| 420 | 'jid'=>null, |
||
| 421 | 'score'=>0 |
||
| 422 | ]); |
||
| 423 | $all_data=[ |
||
| 424 | 'lang' => $compiler->lcode, |
||
| 425 | 'pid' => $problem->pid, |
||
| 426 | 'pcode' => $problem->pcode, |
||
| 427 | 'cid' => $problem->contest_id, |
||
| 428 | 'vcid' => $request->vcid, |
||
| 429 | 'iid' => $problem->index_id, |
||
| 430 | 'oj' => $problem->oj, |
||
| 431 | 'coid' => $compiler->coid, |
||
| 432 | 'solution' => $request->solution, |
||
| 433 | 'contest' => $contest->cid, |
||
| 434 | 'sid' => $submission->sid |
||
| 435 | ]; |
||
| 436 | try { |
||
| 437 | dispatch(new ProcessSubmission($all_data))->onQueue($problem->oj); |
||
| 438 | } catch (\Throwable $e) { |
||
| 439 | return response()->json([ |
||
| 440 | 'success' => false, |
||
| 441 | 'message' => $e->getMessage(), |
||
| 442 | 'ret' => [], |
||
| 443 | 'err' => [ |
||
| 444 | 'code' => 1100, |
||
| 445 | 'msg' => $e->getMessage(), |
||
| 446 | 'data'=> [] |
||
| 447 | ] |
||
| 448 | ]); |
||
| 449 | } |
||
| 450 | return response()->json([ |
||
| 451 | 'success' => true, |
||
| 452 | 'message' => 'Succeed', |
||
| 453 | 'ret' => [ |
||
| 454 | "sid" => $submission->sid, |
||
| 455 | ], |
||
| 456 | 'err' => [] |
||
| 457 | ]); |
||
| 458 | } |
||
| 459 | |||
| 460 | public function fetchAnnouncement(Request $request) { |
||
| 474 | ]); |
||
| 475 | } |
||
| 476 | } |
||
| 477 |