X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FTournamentController.php;h=2dc7e51a84ae104597626dd05950b4383c55e873;hb=e1ecc76c1c6d527502d6576ee19be06df2a15bb7;hp=c5bd40d84febb55ef3cb5fd3c632ee6c9f6e52b9;hpb=edd0e97bfdc544114f30bf4c13a929631c44a555;p=alttp.git diff --git a/app/Http/Controllers/TournamentController.php b/app/Http/Controllers/TournamentController.php index c5bd40d..2dc7e51 100644 --- a/app/Http/Controllers/TournamentController.php +++ b/app/Http/Controllers/TournamentController.php @@ -2,20 +2,155 @@ namespace App\Http\Controllers; +use App\Events\ApplicationAdded; +use App\Events\TournamentChanged; +use App\Models\Application; +use App\Models\Protocol; use App\Models\Tournament; +use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\Request; class TournamentController extends Controller { + public function apply(Request $request, Tournament $tournament) { + $this->authorize('apply', $tournament); + $application = new Application(); + $application->tournament_id = $tournament->id; + $application->user_id = $request->user()->id; + $application->save(); + ApplicationAdded::dispatch($application); + Protocol::applicationReceived($tournament, $application, $request->user()); + return $tournament->toJson(); + } + public function single(Request $request, $id) { $tournament = Tournament::with( - 'rounds', - 'rounds.results', + 'applications', + 'applications.user', 'participants', 'participants.user', )->findOrFail($id); $this->authorize('view', $tournament); + $rounds = $tournament->rounds()->with(['results', 'results.user'])->limit(25)->get(); + foreach ($rounds as $round) { + try { + $this->authorize('seeResults', $round); + } catch (AuthorizationException) { + $round->hideResults(); + } + } + $json = $tournament->toArray(); + $json['rounds'] = $rounds->toArray(); + return $json; + } + + public function discord(Request $request, Tournament $tournament) { + $this->authorize('update', $tournament); + $validatedData = $request->validate([ + 'guild_id' => 'string|nullable', + ]); + if (array_key_exists('guild_id', $validatedData)) { + $tournament->discord = $validatedData['guild_id']; + } + $tournament->save(); + if ($tournament->wasChanged()) { + TournamentChanged::dispatch($tournament); + Protocol::tournamentDiscord($tournament, $request->user()); + } + return $tournament->toJson(); + } + + public function discordSettings(Request $request, Tournament $tournament) { + $this->authorize('update', $tournament); + $validatedData = $request->validate([ + 'round_category' => 'string|nullable', + 'round_template' => 'string|nullable', + ]); + if (array_key_exists('round_category', $validatedData)) { + $tournament->discord_round_category = $validatedData['round_category']; + } + if (array_key_exists('round_template', $validatedData)) { + $tournament->discord_round_template = $validatedData['round_template']; + } + $tournament->save(); + if ($tournament->wasChanged()) { + TournamentChanged::dispatch($tournament); + Protocol::tournamentDiscordSettings($tournament, $request->user()); + } + return $tournament->toJson(); + } + + public function moreRounds(Request $request, Tournament $tournament) { + $this->authorize('view', $tournament); + + $validatedData = $request->validate([ + 'last_known' => 'integer|required', + ]); + + $rounds = $tournament->rounds() + ->where('number', '<', $validatedData['last_known']) + ->with(['results', 'results.user']) + ->limit(25)->get(); + foreach ($rounds as $round) { + try { + $this->authorize('seeResults', $round); + } catch (AuthorizationException) { + $round->hideResults(); + } + } + return $rounds->toArray(); + } + + public function settings(Request $request, Tournament $tournament) { + $this->authorize('update', $tournament); + $validatedData = $request->validate([ + 'show_numbers' => 'boolean|nullable', + ]); + if (array_key_exists('show_numbers', $validatedData)) { + $tournament->show_numbers = $validatedData['show_numbers']; + } + $tournament->save(); + if ($tournament->wasChanged()) { + TournamentChanged::dispatch($tournament); + Protocol::tournamentSettings($tournament, $request->user()); + } + return $tournament->toJson(); + } + + public function open(Request $request, Tournament $tournament) { + $this->authorize('update', $tournament); + $tournament->accept_applications = true; + $tournament->save(); + TournamentChanged::dispatch($tournament); + Protocol::tournamentOpened($tournament, $request->user()); + return $tournament->toJson(); + } + + public function close(Request $request, Tournament $tournament) { + $this->authorize('update', $tournament); + $tournament->accept_applications = false; + $tournament->save(); + TournamentChanged::dispatch($tournament); + Protocol::tournamentClosed($tournament, $request->user()); + return $tournament->toJson(); + } + + public function lock(Request $request, Tournament $tournament) { + $this->authorize('update', $tournament); + $tournament->locked = true; + $tournament->save(); + TournamentChanged::dispatch($tournament); + Protocol::tournamentLocked($tournament, $request->user()); + return $tournament->toJson(); + } + + public function unlock(Request $request, Tournament $tournament) { + $this->authorize('update', $tournament); + $tournament->locked = false; + $tournament->save(); + TournamentChanged::dispatch($tournament); + Protocol::tournamentUnlocked($tournament, $request->user()); return $tournament->toJson(); }