Skip to content

Commit

Permalink
judge category -> scoring category
Browse files Browse the repository at this point in the history
  • Loading branch information
venix12 committed Dec 14, 2023
1 parent 161fde9 commit 31d9727
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 71 deletions.
14 changes: 7 additions & 7 deletions app/Http/Controllers/ContestEntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function judgeResults($id)
{
$entry = ContestEntry::with('contest')
->with('contest.entries')
->with('contest.judgeCategories')
->with('contest.scoringCategories')
->with('judgeVotes')
->with('judgeVotes.scores')
->with('judgeVotes.user')
Expand All @@ -33,7 +33,7 @@ public function judgeResults($id)
abort_if(!$entry->contest->isJudged() || !$entry->contest->show_votes, 404);

$contestJson = json_item(
$entry->contest->loadSum('judgeCategories', 'max_value'),
$entry->contest->loadSum('scoringCategories', 'max_value'),
'Contest',
['max_judging_score']
);
Expand All @@ -59,7 +59,7 @@ public function judgeVote($id)
{
$entry = ContestEntry::with('contest')
->with('contest.judges')
->with('contest.judgeCategories')
->with('contest.scoringCategories')
->with('judgeVotes')
->findOrFail($id);

Expand Down Expand Up @@ -91,17 +91,17 @@ public function judgeVote($id)
]);
}

foreach ($entry->contest->judgeCategories as $category) {
foreach ($entry->contest->scoringCategories as $category) {
$score = $scores
->where('contest_judge_category_id', $category->getKey())
->where('contest_scoring_category_id', $category->getKey())
->first();

if ($score === null) {
throw new InvariantException(osu_trans('contest.judge.validation.missing_score'));
}

$currentScore = ContestJudgeScore::where('contest_judge_vote_id', $vote->getKey())
->where('contest_judge_category_id', $category->getKey())
->where('contest_scoring_category_id', $category->getKey())
->first();

$value = clamp($score['value'], 0, $category->max_value);
Expand All @@ -114,7 +114,7 @@ public function judgeVote($id)
}
} else {
ContestJudgeScore::create([
'contest_judge_category_id' => $category->getKey(),
'contest_scoring_category_id' => $category->getKey(),
'contest_judge_vote_id' => $vote->getKey(),
'value' => $value,
]);
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/ContestsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public function judge($id)
$contest = Contest::with('entries')
->with('entries.judgeVotes')
->with('entries.judgeVotes.scores')
->with('judgeCategories')
->with('scoringCategories')
->findOrFail($id);

abort_if(!$contest->isJudged(), 404);

priv_check('ContestJudge', $contest)->ensureCan();

$contestJson = json_item($contest, 'Contest', ['judge_categories']);
$contestJson = json_item($contest, 'Contest', ['scoring_categories']);
$entriesJson = json_collection($contest->entries, 'ContestEntry', [
'current_user_judge_vote.scores',
]);
Expand Down
12 changes: 6 additions & 6 deletions app/Models/Contest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
* @property string $header_url
* @property int $id
* @property mixed $link_icon
* @property-read Collection<ContestJudgeCategory> $judgeCategories
* @property-read Collection<ContestJudge> $judges
* @property int $max_entries
* @property int $max_votes
* @property string $name
* @property bool $show_votes
* @property mixed $type
* @property mixed $unmasked
* @property-read Collection<ContestJudgeCategory> $scoringCategories
* @property bool $show_names
* @property \Carbon\Carbon|null $updated_at
* @property bool $visible
Expand All @@ -62,11 +62,6 @@ public function entries()
return $this->hasMany(ContestEntry::class);
}

public function judgeCategories(): HasMany
{
return $this->hasMany(ContestJudgeCategory::class);
}

public function judges(): BelongsToMany
{
return $this->belongsToMany(User::class, ContestJudge::class);
Expand Down Expand Up @@ -169,6 +164,11 @@ public function judgeVotesFrom(User $user): HasMany
->whereHas('judgeVotes', fn ($q) => $q->where('user_id', $user->getKey()));
}

public function scoringCategories(): HasMany
{
return $this->hasMany(ContestScoringCategory::class);
}

public function state()
{
if ($this->entry_starts_at === null || $this->entry_starts_at->isFuture()) {
Expand Down
4 changes: 2 additions & 2 deletions app/Models/ContestJudgeScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property-read ContestJudgeCategory $category
* @property-read ContestScoringCategory $category
* @property int $contest_judge_category_id
* @property int $contest_judge_vote_id
* @property \Carbon\Carbon|null $created_at
Expand All @@ -23,7 +23,7 @@ class ContestJudgeScore extends Model
{
public function category(): BelongsTo
{
return $this->belongsTo(ContestJudgeCategory::class, 'contest_judge_category_id');
return $this->belongsTo(ContestScoringCategory::class, 'contest_scoring_category_id');
}

public function vote(): BelongsTo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
* @property string $name
* @property \Carbon\Carbon|null $updated_at
*/
class ContestJudgeCategory extends Model
class ContestScoringCategory extends Model
{
}
23 changes: 0 additions & 23 deletions app/Transformers/ContestJudgeCategoryTransformer.php

This file was deleted.

4 changes: 2 additions & 2 deletions app/Transformers/ContestJudgeScoreTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class ContestJudgeScoreTransformer extends TransformerAbstract
public function transform(ContestJudgeScore $score): array
{
return [
'contest_judge_category_id' => $score->contest_judge_category_id,
'contest_scoring_category_id' => $score->contest_scoring_category_id,
'id' => $score->getKey(),
'value' => $score->value,
];
}

public function includeCategory(ContestJudgeScore $score): Item
{
return $this->item($score->category, new ContestJudgeCategoryTransformer());
return $this->item($score->category, new ContestScoringCategoryTransformer());
}
}
23 changes: 23 additions & 0 deletions app/Transformers/ContestScoringCategoryTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace App\Transformers;

use App\Models\ContestScoringCategory;

class ContestScoringCategoryTransformer extends TransformerAbstract
{
public function transform(ContestScoringCategory $scoringCategory): array
{
return [
'description' => $scoringCategory->description,
'id' => $scoringCategory->getKey(),
'max_value' => $scoringCategory->max_value,
'name' => $scoringCategory->name,
];
}
}
8 changes: 4 additions & 4 deletions app/Transformers/ContestTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ContestTransformer extends TransformerAbstract
{
protected array $availableIncludes = [
'entries',
'judge_categories',
'scoring_categories',
'max_judging_score',
'users_voted_count',
];
Expand Down Expand Up @@ -50,14 +50,14 @@ public function includeEntries(Contest $contest)
return $this->collection($contest->entriesByType(Auth::user()), new ContestEntryTransformer());
}

public function includeJudgeCategories(Contest $contest): Collection
public function includeScoringCategories(Contest $contest): Collection
{
return $this->collection($contest->judgeCategories, new ContestJudgeCategoryTransformer());
return $this->collection($contest->scoringCategories, new ContestScoringCategoryTransformer());
}

public function includeMaxJudgingScore(Contest $contest): Primitive
{
return $this->primitive((int) $contest->judge_categories_sum_max_value);
return $this->primitive((int) $contest->scoring_categories_sum_max_value);
}

public function includeUsersVotedCount(Contest $contest): ResourceInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public function up(): void
{
Schema::create('contest_judge_categories', function (Blueprint $table) {
Schema::create('contest_scoring_categories', function (Blueprint $table) {
$table->id();
$table->integer('contest_id')->unsigned();
$table->string('name');
Expand All @@ -33,6 +33,6 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists('contest_judge_categories');
Schema::dropIfExists('contest_scoring_categories');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public function up(): void
Schema::create('contest_judge_scores', function (Blueprint $table) {
$table->id();
$table->integer('contest_judge_vote_id')->unsigned();
$table->integer('contest_judge_category_id')->unsigned();
$table->integer('contest_scoring_category_id')->unsigned();
$table->tinyInteger('value');
$table->timestamps();

$table->index('contest_judge_category_id');
$table->unique(['contest_judge_vote_id', 'contest_judge_category_id'], 'vote_category');
$table->index('contest_scoring_category_id');
$table->unique(['contest_judge_vote_id', 'contest_scoring_category_id'], 'vote_category');
});
}

Expand Down
19 changes: 9 additions & 10 deletions resources/js/contest-judge/entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import BigButton from 'components/big-button';
import ContestEntryJson from 'interfaces/contest-entry-json';
import ContestJudgeCategory from 'interfaces/contest-judge-category-json';
import ContestJudgeCategoryJson from 'interfaces/contest-judge-category-json';
import ContestScoringCategoryJson from 'interfaces/contest-scoring-category-json';
import ContestJudgeScoreJson from 'interfaces/contest-judge-score-json';
import ContestJudgeVoteJson from 'interfaces/contest-judge-vote-json';
import { route } from 'laroute';
Expand All @@ -18,7 +17,7 @@ import { trans } from 'utils/lang';

interface Props {
entry: ContestEntry;
judgeCategories: ContestJudgeCategory[];
scoringCategories: ContestScoringCategoryJson[];
store: ContestEntryStore;
}

Expand All @@ -44,12 +43,12 @@ export default class Entry extends React.Component<Props> {
private get disabled() {
let scoresHaveChanged = false;

for (const category of this.props.judgeCategories) {
for (const category of this.props.scoringCategories) {
const score = this.score(category.id);
if (score == null) return true;

if (!scoresHaveChanged) {
const initialScore = this.initialVote?.scores?.find((x) => x.contest_judge_category_id === category.id);
const initialScore = this.initialVote?.scores?.find((x) => x.contest_scoring_category_id === category.id);
if (initialScore?.value !== score.value) scoresHaveChanged = true;
}
}
Expand All @@ -74,7 +73,7 @@ export default class Entry extends React.Component<Props> {
{this.props.entry.title}
</div>

{this.props.judgeCategories.map((category) => {
{this.props.scoringCategories.map((category) => {
const currentScore = this.score(category.id);

return (
Expand Down Expand Up @@ -133,21 +132,21 @@ export default class Entry extends React.Component<Props> {
const categoryId = Number(e.currentTarget.getAttribute('data-category-id'));
const value = Number(e.currentTarget.value);

const score = { contest_judge_category_id: categoryId, value };
const score: ContestJudgeScoreJson = { contest_scoring_category_id: categoryId, value };
const { scores } = this;

if (this.score(categoryId) == null) {
scores?.push(score);
} else {
const index = scores?.findIndex((x) => x.contest_judge_category_id === categoryId);
const index = scores?.findIndex((x) => x.contest_scoring_category_id === categoryId);
// that should never happen
if (index === -1) return;

scores?.splice(index, 1, score);
}
};

private renderRangeInput(category: ContestJudgeCategoryJson, initialValue: number) {
private renderRangeInput(category: ContestScoringCategoryJson, initialValue: number) {
return (
<div className='contest-judge-entry-range-input'>
<input
Expand All @@ -162,7 +161,7 @@ export default class Entry extends React.Component<Props> {
}

private score(categoryId: number) {
return this.scores.find((x) => x.contest_judge_category_id === categoryId);
return this.scores.find((x) => x.contest_scoring_category_id === categoryId);
}

@action
Expand Down
6 changes: 3 additions & 3 deletions resources/js/contest-judge/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class Main extends React.Component<Props> {
}

render() {
const judgeCategories = this.props.contest?.judge_categories;
const scoringCategories = this.props.contest?.scoring_categories;

return (
<>
Expand All @@ -46,11 +46,11 @@ export default class Main extends React.Component<Props> {
</div>

<div className='contest-judge contest-judge--items'>
{judgeCategories && this.filteredEntries.map((entry) => (
{scoringCategories && this.filteredEntries.map((entry) => (
<Entry
key={entry.id}
entry={entry}
judgeCategories={judgeCategories}
scoringCategories={scoringCategories}
store={this.props.store}
/>
))}
Expand Down
4 changes: 2 additions & 2 deletions resources/js/interfaces/contest-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// See the LICENCE file in the repository root for full licence text.

import ContestEntryJson from './contest-entry-json';
import ContestJudgeCategoryJson from './contest-judge-category-json';
import ContestScoringCategoryJson from './contest-scoring-category-json';

export default interface ContestJson {
entries?: ContestEntryJson[];
id: number;
judge_categories?: ContestJudgeCategoryJson[];
scoring_categories?: ContestScoringCategoryJson[];
max_judging_score?: number;
name: string;
}
6 changes: 3 additions & 3 deletions resources/js/interfaces/contest-judge-score-json.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

import ContestJudgeCategoryJson from './contest-judge-category-json';
import ContestScoringCategoryJson from './contest-scoring-category-json';

export default interface ContestJudgeScoreJson {
category?: ContestJudgeCategoryJson;
contest_judge_category_id: number;
category?: ContestScoringCategoryJson;
contest_scoring_category_id: number;
id?: number;
value: number;
}
Loading

0 comments on commit 31d9727

Please sign in to comment.