From 7016f4b28fa1324269ae9e2a8aad28dd562927d4 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sat, 19 Mar 2022 13:00:32 +0100 Subject: [PATCH] lock tournaments --- app/Console/Commands/LockRound.php | 46 ++++++++--------- app/Console/Commands/LockTournament.php | 51 +++++++++++++++++++ app/Events/TournamentChanged.php | 40 +++++++++++++++ app/Models/Protocol.php | 12 +++++ app/Policies/TournamentPolicy.php | 2 +- .../2022_03_19_114733_lock_tournaments.php | 32 ++++++++++++ resources/js/components/pages/Tournament.js | 5 ++ resources/js/helpers/permissions.js | 2 +- 8 files changed, 165 insertions(+), 25 deletions(-) create mode 100644 app/Console/Commands/LockTournament.php create mode 100644 app/Events/TournamentChanged.php create mode 100644 database/migrations/2022_03_19_114733_lock_tournaments.php diff --git a/app/Console/Commands/LockRound.php b/app/Console/Commands/LockRound.php index f0f81c1..40939fb 100644 --- a/app/Console/Commands/LockRound.php +++ b/app/Console/Commands/LockRound.php @@ -9,27 +9,27 @@ use Illuminate\Console\Command; class LockRound extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'round:lock {round}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = 'Lock the round'; - - /** - * Execute the console command. - * - * @return int - */ - public function handle() - { + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'round:lock {round}'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Lock the round'; + + /** + * Execute the console command. + * + * @return int + */ + public function handle() + { $round = Round::findOrFail($this->argument('round')); if ($round->locked) { @@ -47,6 +47,6 @@ class LockRound extends Command RoundChanged::dispatch($round); - return 0; - } + return 0; + } } diff --git a/app/Console/Commands/LockTournament.php b/app/Console/Commands/LockTournament.php new file mode 100644 index 0000000..5b9dd4d --- /dev/null +++ b/app/Console/Commands/LockTournament.php @@ -0,0 +1,51 @@ +argument('tournament')); + + if ($tournament->locked) { + $this->line('already locked'); + return 0; + } + + $tournament->locked = true; + $tournament->save(); + + Protocol::tournamentLocked( + $tournament, + ); + + TournamentChanged::dispatch($tournament); + + return 0; + } +} diff --git a/app/Events/TournamentChanged.php b/app/Events/TournamentChanged.php new file mode 100644 index 0000000..4a06f96 --- /dev/null +++ b/app/Events/TournamentChanged.php @@ -0,0 +1,40 @@ +tournament = $tournament; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new Channel('Tournament.'.$this->tournament->id); + } + + public $tournament; + +} diff --git a/app/Models/Protocol.php b/app/Models/Protocol.php index fee8b88..5b871f4 100644 --- a/app/Models/Protocol.php +++ b/app/Models/Protocol.php @@ -74,6 +74,18 @@ class Protocol extends Model ProtocolAdded::dispatch($protocol); } + public static function tournamentLocked(Tournament $tournament, User $user = null) { + $protocol = static::create([ + 'tournament_id' => $tournament->id, + 'user_id' => $user ? $user->id : null, + 'type' => 'tournament.lock', + 'details' => [ + 'tournament' => static::tournamentMemo($tournament), + ], + ]); + ProtocolAdded::dispatch($protocol); + } + protected static function resultMemo(Result $result) { return [ diff --git a/app/Policies/TournamentPolicy.php b/app/Policies/TournamentPolicy.php index 534813c..2cc6b6e 100644 --- a/app/Policies/TournamentPolicy.php +++ b/app/Policies/TournamentPolicy.php @@ -101,7 +101,7 @@ class TournamentPolicy */ public function addRound(User $user, Tournament $tournament) { - return $user->role === 'admin' || $user->isParticipant($tournament); + return $user->role === 'admin' || (!$tournament->locked && $user->isParticipant($tournament)); } /** diff --git a/database/migrations/2022_03_19_114733_lock_tournaments.php b/database/migrations/2022_03_19_114733_lock_tournaments.php new file mode 100644 index 0000000..dd41238 --- /dev/null +++ b/database/migrations/2022_03_19_114733_lock_tournaments.php @@ -0,0 +1,32 @@ +boolean('locked')->default(false); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('tournaments', function(Blueprint $table) { + $table->dropColumn('locked'); + }); + } +}; diff --git a/resources/js/components/pages/Tournament.js b/resources/js/components/pages/Tournament.js index d5df3a1..11dc388 100644 --- a/resources/js/components/pages/Tournament.js +++ b/resources/js/components/pages/Tournament.js @@ -52,6 +52,11 @@ const Tournament = () => { if (e.round) { setTournament(tournament => patchRound(tournament, e.round)); } + }) + .listen('TournamentChanged', e => { + if (e.tournament) { + setTournament(tournament => ({ ...tournament, ...e.tournament })); + } }); return () => { window.Echo.leave(`Tournament.${id}`); diff --git a/resources/js/helpers/permissions.js b/resources/js/helpers/permissions.js index b7d7cfd..769edda 100644 --- a/resources/js/helpers/permissions.js +++ b/resources/js/helpers/permissions.js @@ -18,7 +18,7 @@ export const hasFinished = (user, round) => round.results.find(r => r.user_id == user.id && r.has_finished); export const mayAddRounds = (user, tournament) => - isAdmin(user) || isParticipant(user, tournament); + isAdmin(user) || (!tournament.locked && isParticipant(user, tournament)); export const maySetSeed = (user, tournament) => isAdmin(user) || isParticipant(user, tournament); -- 2.39.2