Rewrite RankAuditionEntries action and use it in the new seat audition form controller.

This commit is contained in:
Matt Young 2025-06-15 15:44:05 -05:00
parent b8ce2bc6db
commit 349da644b7
2 changed files with 67 additions and 105 deletions

View File

@ -4,105 +4,82 @@
namespace App\Actions\Tabulation; namespace App\Actions\Tabulation;
use App\Exceptions\TabulationException; use App\Exceptions\AuditionAdminException;
use App\Models\Audition; use App\Models\Audition;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use function is_numeric;
class RankAuditionEntries class RankAuditionEntries
{ {
protected CalculateEntryScore $calculator;
public function __construct(CalculateEntryScore $calculator)
{
$this->calculator = $calculator;
}
public function rank(string $mode, Audition $audition): Collection
{
$cacheKey = 'audition'.$audition->id.$mode;
return Cache::remember($cacheKey, 300, function () use ($mode, $audition) {
return $this->calculateRank($mode, $audition);
});
}
/** /**
* For a given audition, return a collection of entries ranked by total score. Each entry will have a * Get ranked entries for the provided audition for either seating or advancement.
* property rank that either is their rank or a flag reflecting no-show, declined, or failed-prelim status
* *
* @throws TabulationException * If the rank_type is seating, the ranked entries are returned in descending order of seating total.
* If the rank_type is advancement, the ranked entries are returned in descending order of advancement total.
*
* The ranked entries are returned as a Collection of Entry objects.
*
* @param string $rank_type advancement|seating
* @return Collection|void
*
* @throws AuditionAdminException
*/ */
public function calculateRank(string $mode, Audition $audition): Collection public function __invoke(Audition $audition, string $rank_type)
{ {
$this->basicValidation($mode, $audition); if ($rank_type !== 'seating' && $rank_type !== 'advancement') {
$entries = match ($mode) { throw new AuditionAdminException('Invalid rank type: '.$rank_type.' (must be seating or advancement)');
'seating' => $audition->entries()->forSeating()->with('scoreSheets')->withCount('bonusScores')->get(), }
'advancement' => $audition->entries()->forAdvancement()->with('scoreSheets')->get(),
}; $cache_duration = 15;
foreach ($entries as $entry) { if ($rank_type === 'seating') {
$entry->setRelation('audition', $audition); return cache()->remember('rank_seating_'.$audition->id, $cache_duration, function () use ($audition) {
try { return $this->get_seating_ranks($audition);
$entry->score_totals = $this->calculator->calculate($mode, $entry); });
} catch (TabulationException $ex) {
$entry->score_totals = [-1];
$entry->score_message = $ex->getMessage();
}
}
// Sort entries based on their total score, then by subscores in tiebreak order
$entries = $entries->sort(function ($a, $b) {
for ($i = 0; $i < count($a->score_totals); $i++) {
if (! array_key_exists($i, $a->score_totals)) {
return -1;
}
if (! array_key_exists($i, $b->score_totals)) {
return -1;
}
if ($a->score_totals[$i] > $b->score_totals[$i]) {
return -1;
} elseif ($a->score_totals[$i] < $b->score_totals[$i]) {
return 1;
}
}
return 0;
});
$rank = 1;
$rawRank = 1;
foreach ($entries as $entry) {
$entry->rank = $rank;
$entry->raw_rank = $rawRank;
// We don't really get a rank for seating if we have certain flags
if ($mode === 'seating') {
if ($entry->hasFlag('failed_prelim')) {
$entry->rank = 'Failed Prelim';
} elseif ($entry->hasFlag('declined')) {
$entry->rank = 'Declined';
} elseif ($entry->hasFlag('no_show')) {
$entry->rank = 'No Show';
}
}
if (is_numeric($entry->rank)) {
$rank++;
}
$rawRank++;
} }
return $entries;
} }
protected function basicValidation($mode, Audition $audition): void private function get_seating_ranks(Audition $audition): Collection
{ {
if ($mode !== 'seating' && $mode !== 'advancement') { return $audition->entries()
throw new TabulationException('Mode must be seating or advancement'); ->whereHas('totalScore')
} ->with('totalScore')
if (! $audition->exists()) { ->with('student.school')
throw new TabulationException('Invalid audition provided'); ->join('entry_total_scores', 'entries.id', '=', 'entry_total_scores.entry_id')
} ->orderBy('entry_total_scores.seating_total', 'desc')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[0]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[1]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[2]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[3]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[4]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[5]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[6]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[7]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[8]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[9]"), -999999) DESC')
->select('entries.*')
->get();
}
private function get_advancement_ranks(Audition $audition): Collection
{
return $audition->entries()
->whereHas('totalScore')
->with('totalScore')
->with('student.school')
->join('entry_total_scores', 'entries.id', '=', 'entry_total_scores.entry_id')
->orderBy('entry_total_scores.advancement_total', 'desc')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[0]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[1]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[2]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[3]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[4]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[5]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[6]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[7]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[8]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.advancement_subscore_totals, "$[9]"), -999999) DESC')
->select('entries.*')
->get();
} }
} }

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Tabulation; namespace App\Http\Controllers\Tabulation;
use App\Actions\Tabulation\GetAuditionSeats; use App\Actions\Tabulation\GetAuditionSeats;
use App\Actions\Tabulation\RankAuditionEntries;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Audition; use App\Models\Audition;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -11,25 +12,9 @@ class SeatAuditionFormController extends Controller
{ {
public function __invoke(Request $request, Audition $audition) public function __invoke(Request $request, Audition $audition)
{ {
$ranker = app(RankAuditionEntries::class);
// Get scored entries in order // Get scored entries in order
$scored_entries = $audition->entries() $scored_entries = $ranker($audition, 'seating');
->whereHas('totalScore')
->with('totalScore')
->with('student.school')
->join('entry_total_scores', 'entries.id', '=', 'entry_total_scores.entry_id')
->orderBy('entry_total_scores.seating_total', 'desc')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[0]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[1]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[2]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[3]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[4]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[5]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[6]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[7]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[8]"), -999999) DESC')
->orderByRaw('COALESCE(JSON_EXTRACT(entry_total_scores.seating_subscore_totals, "$[9]"), -999999) DESC')
->select('entries.*')
->get();
// Get unscored entries sorted by draw number // Get unscored entries sorted by draw number
$unscored_entries = $audition->entries() $unscored_entries = $audition->entries()