]> git.localhorst.tv Git - alttp.git/commitdiff
lock tournaments
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 19 Mar 2022 12:00:32 +0000 (13:00 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 19 Mar 2022 12:00:32 +0000 (13:00 +0100)
app/Console/Commands/LockRound.php
app/Console/Commands/LockTournament.php [new file with mode: 0644]
app/Events/TournamentChanged.php [new file with mode: 0644]
app/Models/Protocol.php
app/Policies/TournamentPolicy.php
database/migrations/2022_03_19_114733_lock_tournaments.php [new file with mode: 0644]
resources/js/components/pages/Tournament.js
resources/js/helpers/permissions.js

index f0f81c18829bc560d2299e21715883c220e57ea4..40939fb774c726bde8d899047814e890022f138b 100644 (file)
@@ -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 (file)
index 0000000..5b9dd4d
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Events\TournamentChanged;
+use App\Models\Protocol;
+use App\Models\Tournament;
+use Illuminate\Console\Command;
+
+class LockTournament extends Command
+{
+       /**
+        * The name and signature of the console command.
+        *
+        * @var string
+        */
+       protected $signature = 'tournament:lock {tournament}';
+
+       /**
+        * The console command description.
+        *
+        * @var string
+        */
+       protected $description = 'Lock the tournament';
+
+       /**
+        * Execute the console command.
+        *
+        * @return int
+        */
+       public function handle()
+       {
+               $tournament = Tournament::findOrFail($this->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 (file)
index 0000000..4a06f96
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Events;
+
+use App\Models\Tournament;
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class TournamentChanged implements ShouldBroadcast
+{
+       use Dispatchable, InteractsWithSockets, SerializesModels;
+
+       /**
+        * Create a new event instance.
+        *
+        * @return void
+        */
+       public function __construct(Tournament $tournament)
+       {
+               $this->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;
+
+}
index fee8b881b492b0e0739df1289d3cde53c7575a19..5b871f43a71d87adbc8170963b89bd46629f39a4 100644 (file)
@@ -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 [
index 534813c5277f28c9c7f5e1d650ef74a8c4eff9c8..2cc6b6ed2932dd3c1eb60d4424cfe4b322a563d0 100644 (file)
@@ -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 (file)
index 0000000..dd41238
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        *
+        * @return void
+        */
+       public function up()
+       {
+               Schema::table('tournaments', function(Blueprint $table) {
+                       $table->boolean('locked')->default(false);
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        *
+        * @return void
+        */
+       public function down()
+       {
+               Schema::table('tournaments', function(Blueprint $table) {
+                       $table->dropColumn('locked');
+               });
+       }
+};
index d5df3a1ca4916e4cae6a17c55df1c76babee86a0..11dc388c67d60c3832a977a7265a93033b7010ee 100644 (file)
@@ -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}`);
index b7d7cfdd616b72ba5a0f271805e87e19d708d61d..769edda98e04d79f0be12c76190998e47e936de3 100644 (file)
@@ -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);