From 30c2813ecfe0e93129a9faa040995c8456640c77 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Thu, 20 Jun 2024 23:54:02 -0500 Subject: [PATCH] ScoreService now honors the setting of for_seating on the scoring guides --- app/Events/ScoreSheetChange.php | 36 ++++++++++++++++++++ app/Listeners/RefreshScoreSheetCache.php | 35 ++++++++++++++++++++ app/Listeners/RefreshScoringGuideCache.php | 1 + app/Observers/ScoreSheetObserver.php | 11 ++++--- app/Providers/AppServiceProvider.php | 7 ++++ app/Services/ScoreService.php | 38 ++++++++++++++++++++-- 6 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 app/Events/ScoreSheetChange.php create mode 100644 app/Listeners/RefreshScoreSheetCache.php diff --git a/app/Events/ScoreSheetChange.php b/app/Events/ScoreSheetChange.php new file mode 100644 index 0000000..7178c49 --- /dev/null +++ b/app/Events/ScoreSheetChange.php @@ -0,0 +1,36 @@ +entryId = $entryId; + } + + /** + * Get the channels the event should broadcast on. + * + * @return array + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('channel-name'), + ]; + } +} diff --git a/app/Listeners/RefreshScoreSheetCache.php b/app/Listeners/RefreshScoreSheetCache.php new file mode 100644 index 0000000..1604550 --- /dev/null +++ b/app/Listeners/RefreshScoreSheetCache.php @@ -0,0 +1,35 @@ +scoreService = $scoreService; + } + + /** + * Handle the event. + */ + public function handle(ScoreSheetChange $event): void + { + $this->scoreService->clearScoreSheetCountCache(); + if ($event->entryId) { + $this->scoreService->clearEntryTotalScoresCache($event->entryId); + } + // If we are in local environment, send a success flash message + if (config('app.env') === 'local') { + session()->flash('success','Cleared cache for entry ID ' . $event->entryId); + } + } +} diff --git a/app/Listeners/RefreshScoringGuideCache.php b/app/Listeners/RefreshScoringGuideCache.php index 4d6e7d3..3a38cc4 100644 --- a/app/Listeners/RefreshScoringGuideCache.php +++ b/app/Listeners/RefreshScoringGuideCache.php @@ -24,5 +24,6 @@ class RefreshScoringGuideCache public function handle(ScoringGuideChange $event): void { $this->scoreService->clearScoringGuideCache(); + $this->scoreService->clearAllCachedTotalScores(); } } diff --git a/app/Observers/ScoreSheetObserver.php b/app/Observers/ScoreSheetObserver.php index a8637f7..533763e 100644 --- a/app/Observers/ScoreSheetObserver.php +++ b/app/Observers/ScoreSheetObserver.php @@ -2,6 +2,7 @@ namespace App\Observers; +use App\Events\ScoreSheetChange; use App\Models\ScoreSheet; class ScoreSheetObserver @@ -11,7 +12,7 @@ class ScoreSheetObserver */ public function created(ScoreSheet $scoreSheet): void { - // + ScoreSheetChange::dispatch($scoreSheet->entry_id); } /** @@ -19,7 +20,7 @@ class ScoreSheetObserver */ public function updated(ScoreSheet $scoreSheet): void { - // + ScoreSheetChange::dispatch($scoreSheet->entry_id); } /** @@ -27,7 +28,7 @@ class ScoreSheetObserver */ public function deleted(ScoreSheet $scoreSheet): void { - // + ScoreSheetChange::dispatch($scoreSheet->entry_id); } /** @@ -35,7 +36,7 @@ class ScoreSheetObserver */ public function restored(ScoreSheet $scoreSheet): void { - // + ScoreSheetChange::dispatch($scoreSheet->entry_id); } /** @@ -43,6 +44,6 @@ class ScoreSheetObserver */ public function forceDeleted(ScoreSheet $scoreSheet): void { - // + ScoreSheetChange::dispatch($scoreSheet->entry_id); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index a74ab04..11fb104 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,9 +4,11 @@ namespace App\Providers; use App\Events\AuditionChange; use App\Events\EntryChange; +use App\Events\ScoreSheetChange; use App\Events\ScoringGuideChange; use App\Listeners\RefreshAuditionCache; use App\Listeners\RefreshEntryCache; +use App\Listeners\RefreshScoreSheetCache; use App\Listeners\RefreshScoringGuideCache; use App\Models\Audition; use App\Models\Entry; @@ -104,5 +106,10 @@ class AppServiceProvider extends ServiceProvider ScoringGuideChange::class, RefreshScoringGuideCache::class ); + + Event::listen( + ScoreSheetChange::class, + RefreshScoreSheetCache::class + ); } } diff --git a/app/Services/ScoreService.php b/app/Services/ScoreService.php index 8b9970d..42dc109 100644 --- a/app/Services/ScoreService.php +++ b/app/Services/ScoreService.php @@ -74,8 +74,6 @@ class ScoreService } - - /** * Get final scores array for the requested entry. The first element is the total score. The following elements are sums * of each subscore in tiebreaker order @@ -101,7 +99,7 @@ class ScoreService $scoringGuideId = $audition->scoring_guide_id; // $entries = Entry::where('audition_id',$auditionId)->with('scoreSheets')->get(); $entries = $this->entryCache->getEntriesForAudition($auditionId); - $entries->load('scoreSheets'); + $entries->load('scoreSheets'); // TODO Cache this somehow, it's expensive and repetitive on the seating page foreach ($entries as $entry) { $cacheKey = 'entry' . $entry->id . 'totalScores'; @@ -111,6 +109,27 @@ class ScoreService } } + public function clearScoreSheetCountCache() + { + $cacheKey = 'entryScoreSheetCounts'; + Cache::forget($cacheKey); + } + + public function clearEntryTotalScoresCache($entryId) + { + $cacheKey = 'entry' . $entryId . 'totalScores'; + Cache::forget($cacheKey); + } + + public function clearAllCachedTotalScores() + { + foreach ($this->entryCache->getAllEntries() as $entry) + { + $cacheKey = 'entry' . $entry->id . 'totalScores'; + Cache::forget($cacheKey); + } + } + /** * Calculate final score using the provided scoring guide and score sheets. Returns an array of scores * The first element is the total score. The following elements are the sum of each subscore @@ -127,14 +146,24 @@ class ScoreService // TODO cache the scoring guides with their subscores $subscores = $sg->subscores->sortBy('tiebreak_order'); + $ignoredSubscores = []; // This will be subscores not used for seating + + // Init final scores array $finalScoresArray = []; foreach ($subscores as $subscore) { + if (! $subscore->for_seating) { // Ignore scores that are not for seating + $ignoredSubscores[] = $subscore->id; + continue; + } $finalScoresArray[$subscore->id] = 0; } foreach($scoreSheets as $sheet) { foreach ($sheet->subscores as $ss) { + if (in_array($ss['subscore_id'], $ignoredSubscores)) { // Ignore scores that are not for seating + continue; + } $finalScoresArray[$ss['subscore_id']] += $ss['score']; } } @@ -143,6 +172,9 @@ class ScoreService $totalScore = 0; $totalWeight = 0; foreach ($subscores as $subscore) { + if (in_array($subscore->id, $ignoredSubscores)) { // Ignore scores that are not for seating + continue; + } $totalScore += ($finalScoresArray[$subscore->id] * $subscore->weight); $totalWeight += $subscore->weight; }