1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Http\Controllers;
7
8use App\Exceptions\InvariantException;
9use App\Models\Contest;
10use App\Transformers\ContestTransformer;
11use Auth;
12
13class ContestsController extends Controller
14{
15 public function index()
16 {
17 $contests = Contest::orderBy('id', 'desc');
18
19 if (!Auth::check() || !Auth::user()->isAdmin()) {
20 $contests->where('visible', true);
21 }
22
23 return ext_view('contests.index', [
24 'contests' => $contests->get(),
25 ]);
26 }
27
28 public function judge($id)
29 {
30 $contest = Contest::with('entries.judgeVotes')
31 ->with('entries.judgeVotes.scores')
32 ->with('scoringCategories')
33 ->findOrFail($id);
34
35 abort_if(!$contest->isJudged(), 404);
36
37 priv_check('ContestJudgeShow', $contest)->ensureCan();
38
39 $contestJson = json_item($contest, new ContestTransformer(), [
40 'entries.current_user_judge_vote.scores',
41 'scoring_categories',
42 ]);
43
44 return ext_view('contests.judge', [
45 'contestJson' => $contestJson,
46 ]);
47 }
48
49 public function show($id)
50 {
51 $contest = Contest::findOrFail($id);
52
53 $user = Auth::user();
54 if (!$contest->visible && (!$user || !$user->isAdmin())) {
55 abort(404);
56 }
57
58 if ($contest->isVotingStarted() && isset($contest->getExtraOptions()['children'])) {
59 $contestIds = $contest->getExtraOptions()['children'];
60 $contests = Contest::whereIn('id', $contestIds)
61 ->orderByField('id', $contestIds)
62 ->get();
63 } else {
64 $contests = collect([$contest]);
65 }
66
67 set_opengraph($contest);
68
69 if ($contest->isVotingStarted()) {
70 if ($contest->isVotingOpen()) {
71 // TODO: add support for $contests requirement instead of at parent
72 try {
73 $contest->assertVoteRequirement($user);
74 } catch (InvariantException $e) {
75 $noVoteReason = $e->getMessage();
76 }
77 }
78
79 return ext_view('contests.voting', [
80 'contestMeta' => $contest,
81 'contests' => $contests,
82 'noVoteReason' => $noVoteReason ?? null,
83 ]);
84 } else {
85 return ext_view('contests.enter', [
86 'contestMeta' => $contest,
87 'contest' => $contests->first(),
88 ]);
89 }
90 }
91}