]> git.localhorst.tv Git - alttp.git/commitdiff
simple result hiding on initial tournament request
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 1 Apr 2022 09:57:50 +0000 (11:57 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 1 Apr 2022 09:57:50 +0000 (11:57 +0200)
app/Http/Controllers/TournamentController.php
app/Models/Round.php
app/Models/User.php
app/Policies/RoundPolicy.php
resources/js/helpers/permissions.js

index c5bd40d84febb55ef3cb5fd3c632ee6c9f6e52b9..e563eac60bfb39c34a58e8da3da5a712a6ad14a6 100644 (file)
@@ -3,6 +3,7 @@
 namespace App\Http\Controllers;
 
 use App\Models\Tournament;
+use Illuminate\Auth\Access\AuthorizationException;
 use Illuminate\Http\Request;
 
 class TournamentController extends Controller
@@ -16,6 +17,13 @@ class TournamentController extends Controller
                        '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();
        }
 
index 3c191bc17efb200d65b1263371dec4599d93b508..18be60fe015d346a085a416e4990aee3458c7447 100644 (file)
@@ -10,6 +10,16 @@ class Round extends Model
        use HasFactory;
 
 
+       public function isComplete() {
+               if (count($this->tournament->participants) == 0) return false;
+               if (count($this->results) == 0) return false;
+               foreach ($this->tournament->getRunners() as $participant) {
+                       $result = $participant->findResult($this);
+                       if (!$result || !$result->has_finished) return false;
+               }
+               return true;
+       }
+
        public function updatePlacement() {
                $runners = [];
                foreach ($this->tournament->participants as $p) {
@@ -53,6 +63,13 @@ class Round extends Model
        }
 
 
+       public function hideResults() {
+               foreach ($this->results as $result) {
+                       $result->makeHidden(['forfeit', 'placement', 'score', 'time']);
+               }
+       }
+
+
        public function results() {
                return $this->hasMany(Result::class);
        }
index e5bb4f174b49b5f2a96446627ab0148421f7f725..0c2dcf76b0aaaf307c4591f35dc6d2f618b9019e 100644 (file)
@@ -56,6 +56,14 @@ class User extends Authenticatable
                return false;
        }
 
+       public function hasFinished(Round $round) {
+               foreach ($round->results as $result) {
+                       if ($result->user_id != $this->id) continue;
+                       return $result->has_finished;
+               }
+               return false;
+       }
+
 
        public function participation() {
                return $this->hasMany(Participant::class);
index c02d374450facecd0ab239da1aef5430b5238953..f9f2d461bd76ff0b425f1a4f8b3dc6c688e45178 100644 (file)
@@ -92,6 +92,23 @@ class RoundPolicy
                return false;
        }
 
+       /**
+        * Determine whether the user can see the results for this round.
+        *
+        * @param  \App\Models\User  $user
+        * @param  \App\Models\Round  $round
+        * @return \Illuminate\Auth\Access\Response|bool
+        */
+       public function seeResults(?User $user, Round $round)
+       {
+               return
+                       $round->locked ||
+                       ($user && $user->hasFinished($round)) ||
+                       ($user && $user->isTournamentMonitor($round->tournament)) ||
+                       ($user && $user->isTournamentAdmin($round->tournament) && !$user->isRunner($round->tournament)) ||
+                       $round->isComplete();
+       }
+
        /**
         * Determine whether the user can set the seed for this round.
         *
index b33f1c3b9889c4e8ba45702f9d1610b2921a1ad7..2ff823a955143dc166d4169b263a1d87890b5a61 100644 (file)
@@ -50,10 +50,11 @@ export const mayViewProtocol = (user, tournament) =>
        isAdmin(user) || isTournamentCrew(user, tournament);
 
 export const maySeeResults = (user, tournament, round) =>
+       round.locked ||
        hasFinished(user, round) ||
-               isTournamentMonitor(user, tournament) ||
-               (isTournamentAdmin(user, tournament) && !isRunner(user, tournament)) ||
-               Round.isComplete(tournament, round);
+       isTournamentMonitor(user, tournament) ||
+       (isTournamentAdmin(user, tournament) && !isRunner(user, tournament)) ||
+       Round.isComplete(tournament, round);
 
 // Users