3 namespace App\Http\Controllers;
5 use App\Events\ApplicationAdded;
6 use App\Events\TournamentChanged;
7 use App\Models\Application;
8 use App\Models\Protocol;
9 use App\Models\Tournament;
10 use Illuminate\Auth\Access\AuthorizationException;
11 use Illuminate\Http\Request;
13 class TournamentController extends Controller
16 public function apply(Request $request, Tournament $tournament) {
17 $this->authorize('apply', $tournament);
18 $application = new Application();
19 $application->tournament_id = $tournament->id;
20 $application->user_id = $request->user()->id;
22 ApplicationAdded::dispatch($application);
23 Protocol::applicationReceived($tournament, $application, $request->user());
24 return $tournament->toJson();
27 public function single(Request $request, $id) {
28 $tournament = Tournament::with(
34 $this->authorize('view', $tournament);
35 $rounds = $tournament->rounds()->with(['results', 'results.user'])->limit(25)->get();
36 foreach ($rounds as $round) {
38 $this->authorize('seeResults', $round);
39 } catch (AuthorizationException) {
40 $round->hideResults();
43 $json = $tournament->toArray();
44 $json['rounds'] = $rounds->toArray();
48 public function discord(Request $request, Tournament $tournament) {
49 $this->authorize('update', $tournament);
50 $validatedData = $request->validate([
51 'guild_id' => 'string|nullable',
53 if (array_key_exists('guild_id', $validatedData)) {
54 $tournament->discord = $validatedData['guild_id'];
57 if ($tournament->wasChanged()) {
58 TournamentChanged::dispatch($tournament);
59 Protocol::tournamentDiscord($tournament, $request->user());
61 return $tournament->toJson();
64 public function discordSettings(Request $request, Tournament $tournament) {
65 $this->authorize('update', $tournament);
66 $validatedData = $request->validate([
67 'round_category' => 'string|nullable',
68 'round_template' => 'string|nullable',
70 if (array_key_exists('round_category', $validatedData)) {
71 $tournament->discord_round_category = $validatedData['round_category'];
73 if (array_key_exists('round_template', $validatedData)) {
74 $tournament->discord_round_template = $validatedData['round_template'];
77 if ($tournament->wasChanged()) {
78 TournamentChanged::dispatch($tournament);
79 Protocol::tournamentDiscordSettings($tournament, $request->user());
81 return $tournament->toJson();
84 public function moreRounds(Request $request, Tournament $tournament) {
85 $this->authorize('view', $tournament);
87 $validatedData = $request->validate([
88 'last_known' => 'integer|required',
91 $rounds = $tournament->rounds()
92 ->where('number', '<', $validatedData['last_known'])
93 ->with(['results', 'results.user'])
95 foreach ($rounds as $round) {
97 $this->authorize('seeResults', $round);
98 } catch (AuthorizationException) {
99 $round->hideResults();
102 return $rounds->toArray();
105 public function open(Request $request, Tournament $tournament) {
106 $this->authorize('update', $tournament);
107 $tournament->accept_applications = true;
109 TournamentChanged::dispatch($tournament);
110 Protocol::tournamentOpened($tournament, $request->user());
111 return $tournament->toJson();
114 public function close(Request $request, Tournament $tournament) {
115 $this->authorize('update', $tournament);
116 $tournament->accept_applications = false;
118 TournamentChanged::dispatch($tournament);
119 Protocol::tournamentClosed($tournament, $request->user());
120 return $tournament->toJson();
123 public function lock(Request $request, Tournament $tournament) {
124 $this->authorize('update', $tournament);
125 $tournament->locked = true;
127 TournamentChanged::dispatch($tournament);
128 Protocol::tournamentLocked($tournament, $request->user());
129 return $tournament->toJson();
132 public function unlock(Request $request, Tournament $tournament) {
133 $this->authorize('update', $tournament);
134 $tournament->locked = false;
136 TournamentChanged::dispatch($tournament);
137 Protocol::tournamentUnlocked($tournament, $request->user());
138 return $tournament->toJson();