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(
33 'rounds.results.user',
37 $this->authorize('view', $tournament);
38 foreach ($tournament->rounds as $round) {
40 $this->authorize('seeResults', $round);
41 } catch (AuthorizationException) {
42 $round->hideResults();
45 return $tournament->toJson();
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 open(Request $request, Tournament $tournament) {
85 $this->authorize('update', $tournament);
86 $tournament->accept_applications = true;
88 TournamentChanged::dispatch($tournament);
89 Protocol::tournamentOpened($tournament, $request->user());
90 return $tournament->toJson();
93 public function close(Request $request, Tournament $tournament) {
94 $this->authorize('update', $tournament);
95 $tournament->accept_applications = false;
97 TournamentChanged::dispatch($tournament);
98 Protocol::tournamentClosed($tournament, $request->user());
99 return $tournament->toJson();
102 public function lock(Request $request, Tournament $tournament) {
103 $this->authorize('update', $tournament);
104 $tournament->locked = true;
106 TournamentChanged::dispatch($tournament);
107 Protocol::tournamentLocked($tournament, $request->user());
108 return $tournament->toJson();
111 public function unlock(Request $request, Tournament $tournament) {
112 $this->authorize('update', $tournament);
113 $tournament->locked = false;
115 TournamentChanged::dispatch($tournament);
116 Protocol::tournamentUnlocked($tournament, $request->user());
117 return $tournament->toJson();