]> git.localhorst.tv Git - alttp.git/commitdiff
lock rounds
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 19 Mar 2022 11:43:31 +0000 (12:43 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 19 Mar 2022 11:43:31 +0000 (12:43 +0100)
app/Console/Commands/LockRound.php [new file with mode: 0644]
app/Http/Controllers/ResultController.php
app/Models/Protocol.php
app/Models/Round.php
app/Policies/RoundPolicy.php
database/migrations/2022_03_19_102304_lock_rounds.php [new file with mode: 0644]
resources/js/components/rounds/Item.js
resources/js/helpers/Participant.js
resources/js/helpers/Tournament.js

diff --git a/app/Console/Commands/LockRound.php b/app/Console/Commands/LockRound.php
new file mode 100644 (file)
index 0000000..f0f81c1
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Events\RoundChanged;
+use App\Models\Protocol;
+use App\Models\Round;
+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()
+    {
+               $round = Round::findOrFail($this->argument('round'));
+
+               if ($round->locked) {
+                       $this->line('already locked');
+                       return 0;
+               }
+
+               $round->locked = true;
+               $round->save();
+
+               Protocol::roundLocked(
+                       $round->tournament,
+                       $round,
+               );
+
+               RoundChanged::dispatch($round);
+
+        return 0;
+    }
+}
index b23c907f680b28edcc5dbb40efc1cbcd44c8933b..d72a10020dbe4b3e4f29cb8a488b808e5b583125 100644 (file)
@@ -23,6 +23,9 @@ class ResultController extends Controller
 
                $participant = Participant::findOrFail($validatedData['participant_id']);
                $round = Round::findOrFail($validatedData['round_id']);
+               if (!$round || $round->locked) {
+                       abort(403);
+               }
 
                $user = $request->user();
                if ($user->id != $participant->user->id) {
index ddbc441d40a12fe185fe82ac7f52ce9358b7a21f..fee8b881b492b0e0739df1289d3cde53c7575a19 100644 (file)
@@ -36,6 +36,19 @@ class Protocol extends Model
                ProtocolAdded::dispatch($protocol);
        }
 
+       public static function roundLocked(Tournament $tournament, Round $round, User $user = null) {
+               $protocol = static::create([
+                       'tournament_id' => $tournament->id,
+                       'user_id' => $user ? $user->id : null,
+                       'type' => 'round.lock',
+                       'details' => [
+                               'tournament' => static::tournamentMemo($tournament),
+                               'round' => static::roundMemo($round),
+                       ],
+               ]);
+               ProtocolAdded::dispatch($protocol);
+       }
+
        public static function roundSeedSet(Tournament $tournament, Round $round, User $user) {
                $protocol = static::create([
                        'tournament_id' => $tournament->id,
index 927307c2a7d5dc151b0485a36343134b457f151a..71a4273dc9435b21c609341082fe3c52d99efd33 100644 (file)
@@ -19,6 +19,7 @@ class Round extends Model
 
        protected $casts = [
                'code' => 'array',
+               'locked' => 'boolean',
        ];
 
        protected $fillable = [
index 1e29803d24764f258b311c806c9456c98a064b57..7ea3cacb8544a31cc65e90e3fbdac9f2e40a70ef 100644 (file)
@@ -101,6 +101,6 @@ class RoundPolicy
         */
        public function setSeed(User $user, Round $round)
        {
-               return $user->role === 'admin' || $user->isParticipant($round->tournament);
+               return $user->role === 'admin' || ($user->isParticipant($round->tournament) && !$round->locked);
        }
 }
diff --git a/database/migrations/2022_03_19_102304_lock_rounds.php b/database/migrations/2022_03_19_102304_lock_rounds.php
new file mode 100644 (file)
index 0000000..30059d4
--- /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('rounds', function(Blueprint $table) {
+                       $table->boolean('locked')->default(false);
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        *
+        * @return void
+        */
+       public function down()
+       {
+               Schema::table('rounds', function(Blueprint $table) {
+                       $table->dropColumn('locked');
+               });
+       }
+};
index 10549dd151a5f52048e0fd56c19c0288e55ac92b..d8fe9d8b1ccc88983b2f843c712481fbffe260b3 100644 (file)
@@ -34,7 +34,7 @@ const Item = ({
                                tournament={tournament}
                        />
                </p>
-               {isParticipant(user, tournament) ?
+               {!round.locked && isParticipant(user, tournament) ?
                        <p className="report">
                                <ReportButton
                                        participant={findParticipant(tournament, user)}
@@ -51,6 +51,7 @@ Item.propTypes = {
        round: PropTypes.shape({
                code: PropTypes.arrayOf(PropTypes.string),
                created_at: PropTypes.string,
+               locked: PropTypes.bool,
                number: PropTypes.number,
                seed: PropTypes.string,
        }),
index 3db4121c5b6d3689aca5911b9388c8af4d0cc7ac..a44d465a4e57609b0e45ff5e756fde2344a1a8e2 100644 (file)
@@ -25,7 +25,7 @@ export const compareResult = round => (a, b) => {
        if (b_forfeit) {
                return 1;
        }
-       return 0;
+       return compareUsername(a, b);
 };
 
 export const compareUsername = (a, b) => {
index 2c145a83cc5aa48bf6e3b0c4688ae9c4e5967aa4..ff1a06349424f7567e9de29df8a07f2b3d2a4e30 100644 (file)
@@ -64,7 +64,7 @@ export const patchRound = (tournament, round) => {
        if (!tournament) return tournament;
        return {
                ...tournament,
-               rounds: tournament.rounds.map(r => r.id === round.id ? round : r),
+               rounds: tournament.rounds.map(r => r.id === round.id ? { ...r, ...round } : r),
        };
 };