Skip to content

Commit

Permalink
entries controller improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
venix12 committed Jan 26, 2024
1 parent acb8e11 commit 027ab3c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions app/Http/Controllers/ContestEntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,22 @@ public function judgeVote($id)
'comment',
]);

$categoryIds = new Set(array_pluck($params['scores'], 'contest_scoring_category_id'));
$categoryIds->intersect(new Set($entry->contest->scoringCategories->pluck('id')));
$scoresByCategoryId = collect($params['scores'])
->keyBy('contest_scoring_category_id');

if ($categoryIds->count() !== $entry->contest->scoringCategories->count()) {
$expectedCategoryIds = new Set($entry->contest->scoringCategories->pluck('id'));
$givenCategoryIds = new Set($scoresByCategoryId->keys());

if ($expectedCategoryIds->diff($givenCategoryIds)->count() > 0) {
throw new InvariantException(osu_trans('contest.judge.validation.missing_score'));
}

DB::transaction(function () use ($entry, $params) {
DB::transaction(function () use ($entry, $params, $scoresByCategoryId) {
$vote = $entry->judgeVotes()->firstOrNew(['user_id' => Auth::user()->getKey()]);
$vote->fill(['comment' => $params['comment']])->save();

foreach ($params['scores'] as $score) {
$category = $entry->contest->scoringCategories
->find($score['contest_scoring_category_id']);

foreach ($entry->contest->scoringCategories as $category) {
$score = $scoresByCategoryId[$category->getKey()];
$value = clamp($score['value'], 0, $category->max_value);

$vote->scores()->firstOrNew([
Expand Down

0 comments on commit 027ab3c

Please sign in to comment.