Audition Service Updates

This commit is contained in:
Matt Young 2024-07-11 00:46:39 -05:00
parent e11741a0a1
commit ee8003fd45
3 changed files with 34 additions and 50 deletions

View File

@ -6,15 +6,17 @@ namespace App\Actions\Tabulation;
use App\Exceptions\TabulationException; use App\Exceptions\TabulationException;
use App\Models\Entry; use App\Models\Entry;
use App\Services\AuditionService;
class AllJudgesCount implements CalculateEntryScore class AllJudgesCount implements CalculateEntryScore
{ {
protected CalculateScoreSheetTotal $calculator; protected CalculateScoreSheetTotal $calculator;
protected AuditionService $auditionService;
public function __construct() public function __construct()
{ {
#$this->calculator = new CalculateScoreSheetTotal();
$this->calculator = app(CalculateScoreSheetTotal::class); $this->calculator = app(CalculateScoreSheetTotal::class);
$this->auditionService = app(AuditionService::class);
} }
public function calculate(string $mode, Entry $entry): array public function calculate(string $mode, Entry $entry): array
@ -29,7 +31,7 @@ class AllJudgesCount implements CalculateEntryScore
protected function getJudgeTotals($mode, Entry $entry) protected function getJudgeTotals($mode, Entry $entry)
{ {
$scores = []; $scores = [];
foreach ($entry->audition->judges as $judge) { foreach ($this->auditionService->getJudges($entry->audition) as $judge) {
$scores[] = $this->calculator->__invoke($mode, $entry, $judge); $scores[] = $this->calculator->__invoke($mode, $entry, $judge);
} }
$sums = []; $sums = [];
@ -59,7 +61,7 @@ class AllJudgesCount implements CalculateEntryScore
protected function areAllJudgesIn(Entry $entry): void protected function areAllJudgesIn(Entry $entry): void
{ {
$assignedJudgeCount = $entry->audition->judges->count(); $assignedJudgeCount = $this->auditionService->getJudges($entry->audition)->count();
if ($entry->scoreSheets->count() !== $assignedJudgeCount) { if ($entry->scoreSheets->count() !== $assignedJudgeCount) {
throw new TabulationException('Not all score sheets are in'); throw new TabulationException('Not all score sheets are in');
} }
@ -67,7 +69,7 @@ class AllJudgesCount implements CalculateEntryScore
protected function areAllJudgesValid(Entry $entry): void protected function areAllJudgesValid(Entry $entry): void
{ {
$validJudgeIds = $entry->audition->judges->sort()->pluck('id')->toArray(); $validJudgeIds = $this->auditionService->getJudges($entry->audition)->sort()->pluck('id')->toArray();
$existingJudgeIds = $entry->scoreSheets->sort()->pluck('user_id')->toArray(); $existingJudgeIds = $entry->scoreSheets->sort()->pluck('user_id')->toArray();
if ($validJudgeIds !== $existingJudgeIds) { if ($validJudgeIds !== $existingJudgeIds) {
throw new TabulationException('Score exists from a judge not assigned to this audition'); throw new TabulationException('Score exists from a judge not assigned to this audition');

View File

@ -5,7 +5,6 @@
namespace App\Actions\Tabulation; namespace App\Actions\Tabulation;
use App\Exceptions\TabulationException; use App\Exceptions\TabulationException;
use App\Models\Audition;
use App\Models\Entry; use App\Models\Entry;
use App\Models\ScoreSheet; use App\Models\ScoreSheet;
use App\Models\User; use App\Models\User;
@ -14,6 +13,7 @@ use App\Services\AuditionService;
class CalculateScoreSheetTotal class CalculateScoreSheetTotal
{ {
protected AuditionService $auditionService; protected AuditionService $auditionService;
public function __construct(AuditionService $auditionService) public function __construct(AuditionService $auditionService)
{ {
$this->auditionService = $auditionService; $this->auditionService = $auditionService;
@ -26,7 +26,6 @@ class CalculateScoreSheetTotal
if (! $scoreSheet) { if (! $scoreSheet) {
throw new TabulationException('No score sheet by that judge for that entry'); throw new TabulationException('No score sheet by that judge for that entry');
} }
#$subscores = $this->getSubscores($mode, $entry->audition);
$subscores = $this->auditionService->getSubscores($entry->audition, $mode); $subscores = $this->auditionService->getSubscores($entry->audition, $mode);
$scoreTotal = 0; $scoreTotal = 0;
$weightsTotal = 0; $weightsTotal = 0;
@ -57,29 +56,4 @@ class CalculateScoreSheetTotal
throw new TabulationException('Invalid judge provided'); throw new TabulationException('Invalid judge provided');
} }
} }
protected function getSubscores($mode, Audition $audition)
{
static $seatingSubscores = [];
static $advancementSubscores = [];
if ($mode === 'seating') {
if (! isset($seatingSubscores[$audition->id])) {
$seatingSubscores[$audition->id] = $audition->scoringGuide->subscores->where('for_seating',
true)->sortBy('tiebreak_order');
}
return $seatingSubscores[$audition->id];
}
if ($mode === 'advancement') {
if (! isset($advancementSubscores[$audition->id])) {
$advancementSubscores[$audition->id] = $audition->scoringGuide->subscores->where('for_advance',
true)->sortBy('tiebreak_order');
}
return $advancementSubscores[$audition->id];
}
throw new TabulationException('Invalid mode requested. Mode must be seating or advancement');
}
} }

View File

@ -4,7 +4,7 @@ namespace App\Services;
use App\Exceptions\AuditionServiceException; use App\Exceptions\AuditionServiceException;
use App\Models\Audition; use App\Models\Audition;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Cache;
class AuditionService class AuditionService
{ {
@ -21,7 +21,9 @@ class AuditionService
*/ */
public function getSubscores(Audition $audition, $mode = 'seating', $sort = 'tiebreak') public function getSubscores(Audition $audition, $mode = 'seating', $sort = 'tiebreak')
{ {
static $auditionSubscores = []; $cacheKey = 'auditionSubscores-'.$audition->id.'-'.$mode.'-'.$sort;
return Cache::remember($cacheKey, 10, function () use ($audition, $mode, $sort) {
$this->validateAudition($audition); $this->validateAudition($audition);
$this->validateMode($mode); $this->validateMode($mode);
$this->validateSort($sort); $this->validateSort($sort);
@ -34,13 +36,19 @@ class AuditionService
'seating' => 'for_seating', 'seating' => 'for_seating',
'advancement' => 'for_advance', 'advancement' => 'for_advance',
}; };
if (! isset($auditionSubscores[$mode][$sort])) {
$auditionSubscores[$mode][$sort] = $audition->scoringGuide->subscores->where($modeColumn, true)->sortBy($sortColumn); return $audition->scoringGuide->subscores->where($modeColumn, true)->sortBy($sortColumn);
} else { });
Log::debug('Using cached subscores');
} }
return $auditionSubscores[$mode][$sort]; public function getJudges(Audition $audition)
{
$cacheKey = 'auditionJudges-'.$audition->id;
return Cache::remember($cacheKey, 10, function () use ($audition) {
$this->validateAudition($audition);
return $audition->judges;
});
} }
protected function validateAudition($audition) protected function validateAudition($audition)