X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FTournamentController.php;h=e2fbe941a31b6b2b705b1e35c69724e495351191;hb=f446d5bcf3b87bd9443a060e27e9c0601c96fbb9;hp=edf6d120f7403ad198c3332bf11db712d12be89c;hpb=55f2d7cd6c290a0d26db177d54d20c393f890bbb;p=alttp.git diff --git a/app/Http/Controllers/TournamentController.php b/app/Http/Controllers/TournamentController.php index edf6d12..e2fbe94 100644 --- a/app/Http/Controllers/TournamentController.php +++ b/app/Http/Controllers/TournamentController.php @@ -2,9 +2,114 @@ 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( + 'applications', + 'applications.user', + 'rounds', + 'rounds.results', + 'participants', + 'participants.user', + )->findOrFail($id); + $this->authorize('view', $tournament); + foreach ($tournament->rounds as $round) { + try { + $this->authorize('seeResults', $round); + } catch (AuthorizationException) { + $round->hideResults(); + } + } + return $tournament->toJson(); + } + + 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_template' => 'string|nullable', + ]); + 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 open(Request $request, Tournament $tournament) { + $this->authorize('update', $tournament); + $tournament->accept_applications = true; + $tournament->save(); + TournamentChanged::dispatch($tournament); + Protocol::tournamentOpenen($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(); + } + }