From e79e7e222d9153d3e8668820aca0a9667aa41a13 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Thu, 12 Jun 2025 01:11:08 -0500 Subject: [PATCH] Add action to total an entries scores. --- app/Actions/Tabulation/TotalEntryScores.php | 72 +++++++++++++++++++++ app/Models/EntryTotalScore.php | 5 ++ 2 files changed, 77 insertions(+) create mode 100644 app/Actions/Tabulation/TotalEntryScores.php diff --git a/app/Actions/Tabulation/TotalEntryScores.php b/app/Actions/Tabulation/TotalEntryScores.php new file mode 100644 index 0000000..be6e305 --- /dev/null +++ b/app/Actions/Tabulation/TotalEntryScores.php @@ -0,0 +1,72 @@ +id)->delete(); + } + // bail out if a total score is already calculated + if (EntryTotalScore::where('entry_id', $entry->id)->count() > 0) { + return; + } + $requiredSubscores = $entry->audition->scoringGuide->subscores; + $newTotaledScore = EntryTotalScore::make(); + $newTotaledScore->entry_id = $entry->id; + + // deal with seating scores + $scoreSheets = ScoreSheet::where('entry_id', $entry->id)->orderBy('seating_total', 'desc')->get(); + if (auditionSetting('olympic_scoring' && $scoreSheets->count() > 2)) { + // under olympic scoring, drop the first and last element + $scoreSheets->shift(); + $scoreSheets->pop(); + } + $newTotaledScore->seating_total = $scoreSheets->avg('seating_total'); + $seatingSubscores = $requiredSubscores + ->filter(fn ($subscore) => $subscore->for_seating == true) + ->sortBy('tiebreak_order'); + $total_seating_subscores = []; + foreach ($seatingSubscores as $subscore) { + $runningTotal = 0; + foreach ($scoreSheets as $scoreSheet) { + $runningTotal += $scoreSheet->subscores[$subscore->id]['score']; + } + $total_seating_subscores[] = $runningTotal / $scoreSheets->count(); + } + $newTotaledScore->seating_subscore_totals = $total_seating_subscores; + + // deal with advancement scores + $scoreSheets = ScoreSheet::where('entry_id', $entry->id)->orderBy('advancement_total', 'desc')->get(); + if (auditionSetting('olympic_scoring' && $scoreSheets->count() > 2)) { + // under olympic scoring, drop the first and last element + $scoreSheets->shift(); + $scoreSheets->pop(); + } + $newTotaledScore->advancement_total = $scoreSheets->avg('advancement_total'); + $advancement_subscores = $requiredSubscores + ->filter(fn ($subscore) => $subscore->for_advance == true) + ->sortBy('tiebreak_order'); + $total_advancement_subscores = []; + foreach ($advancement_subscores as $subscore) { + $runningTotal = 0; + foreach ($scoreSheets as $scoreSheet) { + $runningTotal += $scoreSheet->subscores[$subscore->id]['score']; + } + $total_advancement_subscores[] = $runningTotal / $scoreSheets->count(); + } + $newTotaledScore->advancement_subscore_totals = $total_advancement_subscores; + $newTotaledScore->save(); + dd($newTotaledScore); + } +} diff --git a/app/Models/EntryTotalScore.php b/app/Models/EntryTotalScore.php index 2a5c598..ca07e2b 100644 --- a/app/Models/EntryTotalScore.php +++ b/app/Models/EntryTotalScore.php @@ -8,4 +8,9 @@ use Illuminate\Database\Eloquent\Model; class EntryTotalScore extends Model { use HasFactory; + + protected $casts = [ + 'seating_subscore_totals' => 'json', + 'advancement_subscore_totals' => 'json', + ]; }