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 settings(Request $request, Tournament $tournament) {
106 $this->authorize('update', $tournament);
107 $validatedData = $request->validate([
108 'show_numbers' => 'boolean|nullable',
110 if (array_key_exists('show_numbers', $validatedData)) {
111 $tournament->show_numbers = $validatedData['show_numbers'];
114 if ($tournament->wasChanged()) {
115 TournamentChanged::dispatch($tournament);
116 Protocol::tournamentSettings($tournament, $request->user());
118 return $tournament->toJson();
121 public function open(Request $request, Tournament $tournament) {
122 $this->authorize('update', $tournament);
123 $tournament->accept_applications = true;
125 TournamentChanged::dispatch($tournament);
126 Protocol::tournamentOpened($tournament, $request->user());
127 return $tournament->toJson();
130 public function close(Request $request, Tournament $tournament) {
131 $this->authorize('update', $tournament);
132 $tournament->accept_applications = false;
134 TournamentChanged::dispatch($tournament);
135 Protocol::tournamentClosed($tournament, $request->user());
136 return $tournament->toJson();
139 public function lock(Request $request, Tournament $tournament) {
140 $this->authorize('update', $tournament);
141 $tournament->locked = true;
143 TournamentChanged::dispatch($tournament);
144 Protocol::tournamentLocked($tournament, $request->user());
145 return $tournament->toJson();
148 public function unlock(Request $request, Tournament $tournament) {
149 $this->authorize('update', $tournament);
150 $tournament->locked = false;
152 TournamentChanged::dispatch($tournament);
153 Protocol::tournamentUnlocked($tournament, $request->user());
154 return $tournament->toJson();