]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/TournamentController.php
tournament/guild connection
[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                         'participants',
34                         'participants.user',
35                 )->findOrFail($id);
36                 $this->authorize('view', $tournament);
37                 foreach ($tournament->rounds as $round) {
38                         try {
39                                 $this->authorize('seeResults', $round);
40                         } catch (AuthorizationException) {
41                                 $round->hideResults();
42                         }
43                 }
44                 return $tournament->toJson();
45         }
46
47         public function discord(Request $request, Tournament $tournament) {
48                 $this->authorize('update', $tournament);
49                 $validatedData = $request->validate([
50                         'guild_id' => 'string|nullable',
51                 ]);
52                 if (array_key_exists('guild_id', $validatedData)) {
53                         $tournament->discord = $validatedData['guild_id'];
54                 }
55                 $tournament->save();
56                 if ($tournament->wasChanged()) {
57                         TournamentChanged::dispatch($tournament);
58                         Protocol::tournamentDiscord($tournament, $request->user());
59                 }
60                 return $tournament->toJson();
61         }
62
63         public function open(Request $request, Tournament $tournament) {
64                 $this->authorize('update', $tournament);
65                 $tournament->accept_applications = true;
66                 $tournament->save();
67                 TournamentChanged::dispatch($tournament);
68                 Protocol::tournamentOpenen($tournament, $request->user());
69                 return $tournament->toJson();
70         }
71
72         public function close(Request $request, Tournament $tournament) {
73                 $this->authorize('update', $tournament);
74                 $tournament->accept_applications = false;
75                 $tournament->save();
76                 TournamentChanged::dispatch($tournament);
77                 Protocol::tournamentClosed($tournament, $request->user());
78                 return $tournament->toJson();
79         }
80
81         public function lock(Request $request, Tournament $tournament) {
82                 $this->authorize('update', $tournament);
83                 $tournament->locked = true;
84                 $tournament->save();
85                 TournamentChanged::dispatch($tournament);
86                 Protocol::tournamentLocked($tournament, $request->user());
87                 return $tournament->toJson();
88         }
89
90         public function unlock(Request $request, Tournament $tournament) {
91                 $this->authorize('update', $tournament);
92                 $tournament->locked = false;
93                 $tournament->save();
94                 TournamentChanged::dispatch($tournament);
95                 Protocol::tournamentUnlocked($tournament, $request->user());
96                 return $tournament->toJson();
97         }
98
99 }