]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/TournamentController.php
option to hide round numbers
[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                         'participants',
32                         'participants.user',
33                 )->findOrFail($id);
34                 $this->authorize('view', $tournament);
35                 $rounds = $tournament->rounds()->with(['results', 'results.user'])->limit(25)->get();
36                 foreach ($rounds as $round) {
37                         try {
38                                 $this->authorize('seeResults', $round);
39                         } catch (AuthorizationException) {
40                                 $round->hideResults();
41                         }
42                 }
43                 $json = $tournament->toArray();
44                 $json['rounds'] = $rounds->toArray();
45                 return $json;
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 moreRounds(Request $request, Tournament $tournament) {
85                 $this->authorize('view', $tournament);
86
87                 $validatedData = $request->validate([
88                         'last_known' => 'integer|required',
89                 ]);
90
91                 $rounds = $tournament->rounds()
92                         ->where('number', '<', $validatedData['last_known'])
93                         ->with(['results', 'results.user'])
94                         ->limit(25)->get();
95                 foreach ($rounds as $round) {
96                         try {
97                                 $this->authorize('seeResults', $round);
98                         } catch (AuthorizationException) {
99                                 $round->hideResults();
100                         }
101                 }
102                 return $rounds->toArray();
103         }
104
105         public function settings(Request $request, Tournament $tournament) {
106                 $this->authorize('update', $tournament);
107                 $validatedData = $request->validate([
108                         'show_numbers' => 'boolean|nullable',
109                 ]);
110                 if (array_key_exists('show_numbers', $validatedData)) {
111                         $tournament->show_numbers = $validatedData['show_numbers'];
112                 }
113                 $tournament->save();
114                 if ($tournament->wasChanged()) {
115                         TournamentChanged::dispatch($tournament);
116                         Protocol::tournamentSettings($tournament, $request->user());
117                 }
118                 return $tournament->toJson();
119         }
120
121         public function open(Request $request, Tournament $tournament) {
122                 $this->authorize('update', $tournament);
123                 $tournament->accept_applications = true;
124                 $tournament->save();
125                 TournamentChanged::dispatch($tournament);
126                 Protocol::tournamentOpened($tournament, $request->user());
127                 return $tournament->toJson();
128         }
129
130         public function close(Request $request, Tournament $tournament) {
131                 $this->authorize('update', $tournament);
132                 $tournament->accept_applications = false;
133                 $tournament->save();
134                 TournamentChanged::dispatch($tournament);
135                 Protocol::tournamentClosed($tournament, $request->user());
136                 return $tournament->toJson();
137         }
138
139         public function lock(Request $request, Tournament $tournament) {
140                 $this->authorize('update', $tournament);
141                 $tournament->locked = true;
142                 $tournament->save();
143                 TournamentChanged::dispatch($tournament);
144                 Protocol::tournamentLocked($tournament, $request->user());
145                 return $tournament->toJson();
146         }
147
148         public function unlock(Request $request, Tournament $tournament) {
149                 $this->authorize('update', $tournament);
150                 $tournament->locked = false;
151                 $tournament->save();
152                 TournamentChanged::dispatch($tournament);
153                 Protocol::tournamentUnlocked($tournament, $request->user());
154                 return $tournament->toJson();
155         }
156
157 }