From: Daniel Karbach Date: Fri, 1 Apr 2022 09:57:50 +0000 (+0200) Subject: simple result hiding on initial tournament request X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=1c91e5dcedd930bea5fe44ea95a77a9ecd0177a4;p=alttp.git simple result hiding on initial tournament request --- diff --git a/app/Http/Controllers/TournamentController.php b/app/Http/Controllers/TournamentController.php index c5bd40d..e563eac 100644 --- a/app/Http/Controllers/TournamentController.php +++ b/app/Http/Controllers/TournamentController.php @@ -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(); } diff --git a/app/Models/Round.php b/app/Models/Round.php index 3c191bc..18be60f 100644 --- a/app/Models/Round.php +++ b/app/Models/Round.php @@ -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); } diff --git a/app/Models/User.php b/app/Models/User.php index e5bb4f1..0c2dcf7 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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); diff --git a/app/Policies/RoundPolicy.php b/app/Policies/RoundPolicy.php index c02d374..f9f2d46 100644 --- a/app/Policies/RoundPolicy.php +++ b/app/Policies/RoundPolicy.php @@ -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. * diff --git a/resources/js/helpers/permissions.js b/resources/js/helpers/permissions.js index b33f1c3..2ff823a 100644 --- a/resources/js/helpers/permissions.js +++ b/resources/js/helpers/permissions.js @@ -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