1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Web; |
4
|
|
|
|
5
|
|
|
use App\Entities\Contest; |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use App\Repositories\ContestRepository; |
8
|
|
|
use App\Repositories\Criteria\OrderBy; |
9
|
|
|
use App\Repositories\Criteria\Where; |
10
|
|
|
use App\Repositories\SolutionRepository; |
11
|
|
|
use App\Services\ContestService; |
12
|
|
|
use App\Services\Ranking; |
13
|
|
|
use App\Services\TopicService; |
14
|
|
|
use App\Services\UserService; |
15
|
|
|
use Czim\Repository\Criteria\Common\WithRelations; |
16
|
|
|
|
17
|
|
|
class ContestController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** @var ContestService */ |
20
|
|
|
private $contestService; |
21
|
|
|
|
22
|
|
|
public function __construct(ContestService $service) |
23
|
|
|
{ |
24
|
|
|
$this->contestService = $service; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function index() |
28
|
|
|
{ |
29
|
|
|
/** @var ContestRepository $repository */ |
30
|
|
|
$repository = app(ContestRepository::class); |
31
|
|
|
$repository->clearCriteria(); |
32
|
|
|
$repository->pushCriteria(new Where('status', Contest::ST_NORMAL)); |
33
|
|
|
$repository->pushCriteria(new OrderBy('id', 'desc')); |
34
|
|
|
|
35
|
|
|
$contests = $repository->paginate(request('per_page', 100)); |
|
|
|
|
36
|
|
|
|
37
|
|
|
return view('web.contest.index', ['contests' => $contests]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function show($id) |
41
|
|
|
{ |
42
|
|
|
/** @var Contest $contest */ |
43
|
|
|
$contest = app(ContestRepository::class)->findOrFail($id); |
44
|
|
|
|
45
|
|
|
$problems = $contest->problems; |
46
|
|
|
|
47
|
|
|
return view('web.contest.view', ['contest' => $contest, 'problems' => $problems]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function problem($id, $order) |
51
|
|
|
{ |
52
|
|
|
/** @var Contest $contest */ |
53
|
|
|
$contest = app(ContestRepository::class)->findOrFail($id); |
54
|
|
|
|
55
|
|
|
$problem = $this->contestService->getProblemByOrder($contest, $order); |
56
|
|
|
if ($problem === null) { |
57
|
|
|
return back()->withErrors('Problem not found in contest!'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return view('web.contest.problem', ['contest' => $contest, 'problem' => $problem]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function submit($id) |
64
|
|
|
{ |
65
|
|
|
/** @var Contest $contest */ |
66
|
|
|
$contest = app(ContestRepository::class)->findOrFail($id); |
67
|
|
|
|
68
|
|
|
if ($contest->isEnd()) { |
69
|
|
|
return redirect(route('contest.view', $contest->id)) |
70
|
|
|
->withErrors('Contest is End!'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (! auth()->user()) { |
74
|
|
|
return redirect(route('contest.view', $contest->id)) |
75
|
|
|
->withErrors('Login first'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$problem = $this->contestService->getProblemByOrder($contest, request('order')); |
79
|
|
|
|
80
|
|
|
return view('web.contest.submit', compact('contest', 'problem')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function status($id) |
84
|
|
|
{ |
85
|
|
|
$contest = $this->contestService->getContest($id); |
86
|
|
|
/** @var SolutionRepository $repository */ |
87
|
|
|
$repository = app(SolutionRepository::class); |
88
|
|
|
|
89
|
|
|
if (request('username')) { |
90
|
|
|
$user = app(UserService::class)->findByName(request('username')); |
91
|
|
|
$repository->pushCriteria(new Where('user_id', $user->id)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (request('problem_id')) { |
95
|
|
|
$order = ord(strtolower(request('problem_id'))) - ord('a'); |
|
|
|
|
96
|
|
|
$repository->pushCriteria(new Where('order', $order)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (request()->filled('language')) { |
100
|
|
|
$filter = new Where('language', request('language')); |
101
|
|
|
$repository->pushCriteria($filter); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (request()->filled('status')) { |
105
|
|
|
$filter = new Where('result', request('status')); |
106
|
|
|
$repository->pushCriteria($filter); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$repository->pushCriteria(new Where('contest_id', $contest->id)); |
110
|
|
|
|
111
|
|
|
$repository->pushCriteria(new WithRelations(['user'])); |
112
|
|
|
$repository->pushCriteria(new OrderBy('created_at', 'desc')); |
113
|
|
|
$solutions = $repository->paginate(request('per_page', 50)); |
|
|
|
|
114
|
|
|
|
115
|
|
|
return view('web.contest.status', compact('contest', 'solutions')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function standing($id) |
119
|
|
|
{ |
120
|
|
|
/** @var Contest $contest */ |
121
|
|
|
$contest = app(ContestRepository::class)->findOrFail($id); |
122
|
|
|
|
123
|
|
|
$standing = new Ranking($contest); |
124
|
|
|
|
125
|
|
|
return view('web.contest.standing', [ |
126
|
|
|
'contest' => $contest, |
127
|
|
|
'teams' => $standing->result(), |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function clarify($id) |
132
|
|
|
{ |
133
|
|
|
/** @var Contest $contest */ |
134
|
|
|
$contest = app(ContestRepository::class)->findOrFail($id); |
135
|
|
|
|
136
|
|
|
$topics = (new TopicService())->topicsForContest($contest->id); |
137
|
|
|
|
138
|
|
|
return view('web.contest.clarify', compact('contest', 'topics')); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|