]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/TournamentController.php
9b21736d8ea4a111eece00198e6cd858ca856773
[alttp.git] / app / Http / Controllers / TournamentController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
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;
12
13 class TournamentController extends Controller
14 {
15
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;
21                 $application->save();
22                 ApplicationAdded::dispatch($application);
23                 Protocol::applicationReceived($tournament, $application, $request->user());
24                 return $tournament->toJson();
25         }
26
27         public function single(Request $request, $id) {
28                 $tournament = Tournament::with(
29                         'applications',
30                         'applications.user',
31                         'rounds',
32                         'rounds.results',
33                         'rounds.results.user',
34                         'participants',
35                         'participants.user',
36                 )->findOrFail($id);
37                 $this->authorize('view', $tournament);
38                 foreach ($tournament->rounds as $round) {
39                         try {
40                                 $this->authorize('seeResults', $round);
41                         } catch (AuthorizationException) {
42                                 $round->hideResults();
43                         }
44                 }
45                 return $tournament->toJson();
46         }
47
48         public function discord(Request $request, Tournament $tournament) {
49                 $this->authorize('update', $tournament);
50                 $validatedData = $request->validate([
51                         'guild_id' => 'string|nullable',
52                 ]);
53                 if (array_key_exists('guild_id', $validatedData)) {
54                         $tournament->discord = $validatedData['guild_id'];
55                 }
56                 $tournament->save();
57                 if ($tournament->wasChanged()) {
58                         TournamentChanged::dispatch($tournament);
59                         Protocol::tournamentDiscord($tournament, $request->user());
60                 }
61                 return $tournament->toJson();
62         }
63
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',
69                 ]);
70                 if (array_key_exists('round_category', $validatedData)) {
71                         $tournament->discord_round_category = $validatedData['round_category'];
72                 }
73                 if (array_key_exists('round_template', $validatedData)) {
74                         $tournament->discord_round_template = $validatedData['round_template'];
75                 }
76                 $tournament->save();
77                 if ($tournament->wasChanged()) {
78                         TournamentChanged::dispatch($tournament);
79                         Protocol::tournamentDiscordSettings($tournament, $request->user());
80                 }
81                 return $tournament->toJson();
82         }
83
84         public function open(Request $request, Tournament $tournament) {
85                 $this->authorize('update', $tournament);
86                 $tournament->accept_applications = true;
87                 $tournament->save();
88                 TournamentChanged::dispatch($tournament);
89                 Protocol::tournamentOpened($tournament, $request->user());
90                 return $tournament->toJson();
91         }
92
93         public function close(Request $request, Tournament $tournament) {
94                 $this->authorize('update', $tournament);
95                 $tournament->accept_applications = false;
96                 $tournament->save();
97                 TournamentChanged::dispatch($tournament);
98                 Protocol::tournamentClosed($tournament, $request->user());
99                 return $tournament->toJson();
100         }
101
102         public function lock(Request $request, Tournament $tournament) {
103                 $this->authorize('update', $tournament);
104                 $tournament->locked = true;
105                 $tournament->save();
106                 TournamentChanged::dispatch($tournament);
107                 Protocol::tournamentLocked($tournament, $request->user());
108                 return $tournament->toJson();
109         }
110
111         public function unlock(Request $request, Tournament $tournament) {
112                 $this->authorize('update', $tournament);
113                 $tournament->locked = false;
114                 $tournament->save();
115                 TournamentChanged::dispatch($tournament);
116                 Protocol::tournamentUnlocked($tournament, $request->user());
117                 return $tournament->toJson();
118         }
119
120 }