Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/Console/Commands/GeneratePdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class GeneratePdf extends Command
{
public function handle(ScoresheetGenerator $generator): void
{
/** @var Game $game */
$game = Game::query()->first();
$game = Game::current();

$pdf = $generator->generate($game);

Expand Down
38 changes: 38 additions & 0 deletions app/Enums/MatchPhase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Enums;

enum MatchPhase: string
{
case Setup = 'setup';
case Ready = 'ready';
case InProgress = 'in_progress';
case Completed = 'completed';
case PdfGenerated = 'pdf_generated';

public function allowsSetupEdits(): bool
{
return match ($this) {
self::Setup, self::Ready => true,
self::InProgress, self::Completed, self::PdfGenerated => false,
};
}

public function allowsGameplayRecording(): bool
{
return match ($this) {
self::Ready, self::InProgress => true,
self::Setup, self::Completed, self::PdfGenerated => false,
};
}

public function allowsPdfGeneration(): bool
{
return match ($this) {
self::Completed, self::PdfGenerated => true,
self::Setup, self::Ready, self::InProgress => false,
};
}
}
10 changes: 3 additions & 7 deletions app/Jobs/RecalculateGameStateSnapshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Jobs;

use App\Models\Game;
use App\Services\CurrentMatchResolver;
use App\Services\GameState\GameStateRecalculator;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -15,22 +15,18 @@ class RecalculateGameStateSnapshots implements ShouldQueue
use Queueable;

public function __construct(
public int $gameId,
public ?string $upTo = null,
) {}

/**
* Execute the job.
*/
public function handle(GameStateRecalculator $recalculator): void
public function handle(GameStateRecalculator $recalculator, CurrentMatchResolver $currentMatchResolver): void
{
/** @var Game $game */
$game = Game::query()->findOrFail($this->gameId);

$upTo = $this->upTo === null
? null
: CarbonImmutable::parse($this->upTo);

$recalculator->recalculate($game, $upTo);
$recalculator->recalculate($currentMatchResolver->currentOrFail(), $upTo);
}
}
44 changes: 23 additions & 21 deletions app/Livewire/Court.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Models\GameEvent;
use App\Models\Player;
use App\Models\Staff;
use App\Services\CurrentMatchResolver;
use App\Services\GameSideResolver;
use Flux\Flux;
use Illuminate\Contracts\View\View;
Expand All @@ -23,9 +24,6 @@

class Court extends Component
{
#[Reactive]
public ?int $gameId = null;

#[Reactive]
public ?GameState $gameState = null;

Expand All @@ -47,11 +45,6 @@ class Court extends Component

public ?string $pendingMisconductSubjectLabel = null;

public function mount(?int $gameId = null): void
{
$this->gameId = $gameId;
}

public function render(): View
{
return view('livewire.court', $this->courtContext());
Expand Down Expand Up @@ -101,12 +94,17 @@ public function selectMisconductSubject(string $subjectType, int $subjectId): vo

public function recordPendingDelayWarning(): void
{
if ($this->pendingDelayWarningTeam === null || $this->gameId === null) {
if ($this->pendingDelayWarningTeam === null) {
return;
}

$team = TeamAB::from($this->pendingDelayWarningTeam);
$game = Game::query()->findSole($this->gameId);
$game = $this->activeGame();

if ($game === null) {
return;
}

$game->recordDelayWarning($team);

$this->pendingDelayWarningTeam = null;
Expand All @@ -121,12 +119,17 @@ public function recordPendingDelayWarning(): void

public function recordPendingDelayPenalty(): void
{
if ($this->pendingDelayPenaltyTeam === null || $this->gameId === null) {
if ($this->pendingDelayPenaltyTeam === null) {
return;
}

$team = TeamAB::from($this->pendingDelayPenaltyTeam);
$game = Game::query()->findSole($this->gameId);
$game = $this->activeGame();

if ($game === null) {
return;
}

$game->recordDelayPenalty($team);

$this->pendingDelayPenaltyTeam = null;
Expand All @@ -146,15 +149,18 @@ public function recordPendingMisconduct(): void
|| $this->pendingMisconductSanction === null
|| $this->pendingMisconductSubjectType === null
|| $this->pendingMisconductSubjectId === null
|| $this->gameId === null
) {
return;
}

$team = TeamAB::from($this->pendingMisconductTeam);
$sanction = MisconductSanction::from($this->pendingMisconductSanction);
$subjectType = MisconductSubjectType::from($this->pendingMisconductSubjectType);
$game = Game::query()->findSole($this->gameId);
$game = $this->activeGame();

if ($game === null) {
return;
}

$game->recordMisconduct($team, $subjectType, $this->pendingMisconductSubjectId, $sanction);

Expand Down Expand Up @@ -253,11 +259,7 @@ private function isBeforeInitialToss(GameState $state, ?Game $game): bool

private function activeGame(): ?Game
{
if ($this->gameId === null) {
return null;
}

return Game::query()->whereKey($this->gameId)->first();
return app(CurrentMatchResolver::class)->current();
}

private function gameSideResolver(): GameSideResolver
Expand Down Expand Up @@ -292,7 +294,7 @@ private function pendingMisconductSubjects(): array

return [
'players' => $players
->orderByPivot('number')
->orderBy('number')
->get()
->map(function (Player $player) use ($game, $team, $sanction): array {
$recordedSanction = $sanction === null
Expand All @@ -309,7 +311,7 @@ private function pendingMisconductSubjects(): array
];
})
->all(),
'staff' => $this->staffMisconductSubjects($staff->orderByPivot('id')->get()->all(), $game, $team, $sanction),
'staff' => $this->staffMisconductSubjects($staff->orderBy('id')->get()->all(), $game, $team, $sanction),
];
}

Expand Down
39 changes: 33 additions & 6 deletions app/Livewire/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Enums\TeamAB;
use App\Enums\TeamSide;
use App\Models\Game as GameModel;
use App\Services\CurrentMatchResolver;
use App\Services\GameSideResolver;
use Flux\Flux;
use Illuminate\Contracts\View\View;
Expand All @@ -16,8 +17,6 @@

class Game extends Component
{
public int $gameId;

public GameState $gameState;

public ?int $justEndedSetNumber = null;
Expand All @@ -30,23 +29,41 @@ class Game extends Component

public bool $showFifthSetSideChangePrompt = false;

public function mount(GameModel $game): void
public function mount(CurrentMatchResolver $currentMatchResolver): void
{
$this->gameId = $game->getKey();
$resolvedGame = $currentMatchResolver->current();

if ($resolvedGame === null) {
$this->redirectRoute('match.setup', navigate: true);

return;
}

if (! $currentMatchResolver->isSetupComplete($resolvedGame)) {
$this->redirectRoute('match.setup', navigate: true);

return;
}

$this->synchronizeGameContext();
}

#[On('game-event-recorded')]
public function synchronizeGameContext(): void
{
$game = $this->activeGame();

if ($game === null) {
return;
}

$wasSetInProgress = isset($this->gameState) && $this->gameState->setInProgress;
$previousSetNumber = isset($this->gameState) ? $this->gameState->setNumber : 0;
$previousSetsWonTeamA = isset($this->gameState) ? $this->gameState->setsWonTeamA : 0;
$previousScoreTeamA = isset($this->gameState) ? $this->gameState->scoreTeamA : 0;
$previousScoreTeamB = isset($this->gameState) ? $this->gameState->scoreTeamB : 0;
$previousShowFifthSetSideChangePrompt = $this->showFifthSetSideChangePrompt;

$game = GameModel::query()->findSole($this->gameId);
$this->gameState = $game->stateAt();
$this->showFifthSetSideChangePrompt = $this->gameSideResolver()->shouldPromptForFifthSetSideSwap($this->gameState);

Expand Down Expand Up @@ -80,7 +97,12 @@ public function acknowledgeSetEnd(): void

public function acknowledgeFifthSetSideChange(): void
{
$game = GameModel::query()->findSole($this->gameId);
$game = $this->activeGame();

if ($game === null) {
return;
}

$game->recordCourtSidesSwapped();
$this->synchronizeGameContext();
$this->dispatch('game-event-recorded');
Expand Down Expand Up @@ -117,4 +139,9 @@ private function isBeforeInitialToss(): bool
return ! $this->gameSideResolver()->hasRequiredToss($this->gameState)
&& ! $this->gameSideResolver()->requiresFifthSetToss($this->gameState);
}

private function activeGame(): ?GameModel
{
return app(CurrentMatchResolver::class)->current();
}
}
17 changes: 5 additions & 12 deletions app/Livewire/LineupSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@
use App\Enums\TeamSide;
use App\Exceptions\InvalidGameEventTransition;
use App\Models\Game;
use App\Services\CurrentMatchResolver;
use App\Services\GameSideResolver;
use Flux\Flux;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Validator;
use InvalidArgumentException;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Reactive;
use Livewire\Component;

class LineupSubmission extends Component
{
#[Reactive]
#[Locked]
public int $gameId;

#[Reactive]
public TeamAB $team = TeamAB::TeamA;

Expand All @@ -37,12 +33,9 @@ class LineupSubmission extends Component
/** @var array<int, string> */
public array $lineup = [];

public function mount(TeamAB $team, ?int $gameId = null, string $courtSide = 'left'): void
public function mount(TeamAB $team, string $courtSide = 'left'): void
{
abort_if(is_null($gameId), 404);

$this->team = $team;
$this->gameId = $gameId;
$this->courtSide = $courtSide;
$this->lineup = $this->defaultLineup();
}
Expand Down Expand Up @@ -116,7 +109,7 @@ public function render(): View
#[Computed]
public function activeGame(): ?Game
{
return Game::query()->whereKey($this->gameId)->first();
return app(CurrentMatchResolver::class)->current();
}

/**
Expand Down Expand Up @@ -170,8 +163,8 @@ private function eligibleRosterNumbers(Game $game): array
}

$rosterNumbers = $teamSide === TeamSide::Home
? $game->homePlayers()->wherePivot('is_libero', false)->orderByPivot('number')->pluck('game_player.number')
: $game->awayPlayers()->wherePivot('is_libero', false)->orderByPivot('number')->pluck('game_player.number');
? $game->homePlayers()->where('is_libero', false)->orderBy('number')->pluck('players.number')
: $game->awayPlayers()->where('is_libero', false)->orderBy('number')->pluck('players.number');

return $rosterNumbers
->map(fn (mixed $number): int => (int) $number)
Expand Down
Loading