From c04dd02405c44fea3ab5b1756789bb51d6712088 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Tue, 16 Jul 2024 00:07:46 -0500 Subject: [PATCH] Bonus Score Use #20 Implement bonus scores Bonus score is now included in total scores for seating. --- .../Tabulation/AllowForOlympicScoring.php | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/app/Actions/Tabulation/AllowForOlympicScoring.php b/app/Actions/Tabulation/AllowForOlympicScoring.php index a5346cf..a4fda12 100644 --- a/app/Actions/Tabulation/AllowForOlympicScoring.php +++ b/app/Actions/Tabulation/AllowForOlympicScoring.php @@ -5,20 +5,27 @@ namespace App\Actions\Tabulation; use App\Exceptions\TabulationException; +use App\Models\BonusScore; use App\Models\Entry; use App\Services\AuditionService; use App\Services\EntryService; use Illuminate\Support\Facades\Cache; + use function auditionSetting; class AllowForOlympicScoring implements CalculateEntryScore { protected CalculateScoreSheetTotal $calculator; + protected AuditionService $auditionService; + protected EntryService $entryService; - public function __construct(CalculateScoreSheetTotal $calculator, AuditionService $auditionService, EntryService $entryService) - { + public function __construct( + CalculateScoreSheetTotal $calculator, + AuditionService $auditionService, + EntryService $entryService + ) { $this->calculator = $calculator; $this->auditionService = $auditionService; $this->entryService = $entryService; @@ -28,6 +35,7 @@ class AllowForOlympicScoring implements CalculateEntryScore { $cacheKey = 'entryScore-'.$entry->id.'-'.$mode; + return Cache::remember($cacheKey, 10, function () use ($mode, $entry) { $this->basicValidation($mode, $entry); $this->areAllJudgesIn($entry); @@ -38,7 +46,7 @@ class AllowForOlympicScoring implements CalculateEntryScore } - protected function getJudgeTotals($mode, Entry $entry) + protected function getJudgeTotals($mode, Entry $entry): array { $scores = []; @@ -55,7 +63,7 @@ class AllowForOlympicScoring implements CalculateEntryScore // remove the highest and lowest scores array_pop($scores); array_shift($scores); - } + } $sums = []; // Sum each subscore from the judges foreach ($scores as $score) { @@ -66,9 +74,33 @@ class AllowForOlympicScoring implements CalculateEntryScore $index++; } } + // add the bonus points for a seating mode + if ($mode === 'seating') { + $sums[0] += $this->getBonusPoints($entry); + } + return $sums; } + protected function getBonusPoints(Entry $entry) + { + + $bonusScoreDefinition = $entry->audition->bonusScore()->first(); + if (! $bonusScoreDefinition) { + return 0; + } + $bonusJudges = $bonusScoreDefinition->judges; + $bonusScoreSheets = BonusScore::where('entry_id', $entry->id)->get(); + foreach ($bonusScoreSheets as $sheet) { + if (! $bonusJudges->contains($sheet->user_id)) { + throw new TabulationException('Entry has a bonus score from unassigned judge'); + } + } + + // sum the score property of the $bonusScoreSheets + return $bonusScoreSheets->sum('score'); + } + protected function basicValidation($mode, $entry): void { if ($mode !== 'seating' && $mode !== 'advancement') {