namespace App\Http\Controllers;
use App\Models\Tournament;
+use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;
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();
}
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) {
}
+ public function hideResults() {
+ foreach ($this->results as $result) {
+ $result->makeHidden(['forfeit', 'placement', 'score', 'time']);
+ }
+ }
+
+
public function results() {
return $this->hasMany(Result::class);
}
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);
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.
*
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