-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add standardised scoring for judged contests #12014
base: master
Are you sure you want to change the base?
Changes from 19 commits
b12526f
f9aae4f
f92871e
5934746
e820d60
6a34b72
81e441c
6020767
6fcaa0c
49eae94
7ab824b
341aa29
7affd2f
079a75c
efa47b1
cc9643e
a89a47f
a489e9f
691584b
44ef869
aae1c60
748ca54
a0d074f
f6b9b45
c873dce
bc6e675
e6431a5
21631a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Contest; | ||
use Illuminate\Console\Command; | ||
|
||
class ContestCalculateScores extends Command | ||
{ | ||
protected $signature = 'contest:calculate-scores {contestId}'; | ||
|
||
protected $description = 'Cleans up expired chat keep alives.'; | ||
|
||
public function handle() | ||
{ | ||
$contestId = get_int($this->argument('contestId')); | ||
if ($contestId === null) { | ||
$this->error('Contest id is required.'); | ||
return static::INVALID; | ||
} | ||
|
||
$contest = Contest::findOrFail($contestId); | ||
if (!$contest->isScoreStandardised()) { | ||
$this->error('Contest does not use standardised scoring.'); | ||
return static::FAILURE; | ||
} | ||
|
||
$contest->calculateScores(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
|
||
namespace App\Models; | ||
|
||
use Illuminate\Contracts\Database\Query\Builder as QueryBuilder; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
use Illuminate\Database\Eloquent\Relations\HasManyThrough; | ||
|
@@ -52,6 +54,59 @@ public function votes() | |
return $this->hasMany(ContestVote::class); | ||
} | ||
|
||
public function scopeForBestOf(Builder $query, User $user, string $ruleset, ?string $variant = null): Builder | ||
{ | ||
$query->whereIn('entry_url', function (QueryBuilder $beatmapsetQuery) use ($ruleset, $user, $variant) { | ||
$beatmapsetQuery->select('beatmapset_id') | ||
->from('osu_beatmaps') | ||
->where('osu_beatmaps.playmode', Beatmap::MODES[$ruleset]) | ||
->whereIn('beatmap_id', function (QueryBuilder $beatmapQuery) use ($user) { | ||
$beatmapQuery->select('beatmap_id') | ||
->from('osu_user_beatmap_playcount') | ||
->where('user_id', $user->getKey()); | ||
}); | ||
|
||
if ($ruleset === 'mania' && $variant !== null) { | ||
if ($variant === 'nk') { | ||
$beatmapsetQuery->whereNotIn('osu_beatmaps.diff_size', [4, 7]); | ||
} else { | ||
$keys = match ($variant) { | ||
'4k' => 4, | ||
'7k' => 7, | ||
}; | ||
$beatmapsetQuery->where('osu_beatmaps.diff_size', $keys); | ||
} | ||
} | ||
}); | ||
|
||
return $query; | ||
} | ||
|
||
public function scopeWithScore(Builder $query, array $options): Builder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this accept Contest model instead? So the |
||
{ | ||
$orderValue = 'votes_count'; | ||
|
||
if (isset($options['best_of'])) { | ||
$query | ||
->selectRaw('*') | ||
->selectRaw('(SELECT FLOOR(SUM(`weight`)) FROM `contest_votes` WHERE `contest_entries`.`id` = `contest_votes`.`contest_entry_id`) AS votes_count') | ||
->limit(50); // best of contests tend to have a _lot_ of entries... | ||
} else if ($options['judged'] ?? false) { | ||
$query->withSum('scores', 'value'); | ||
|
||
if ($options['is_score_standardised'] ?? false) { | ||
$query->withSum('judgeVotes as total_score_std', 'total_score_std'); | ||
$orderValue = 'total_score_std'; | ||
} else { | ||
$orderValue = 'scores_sum_value'; | ||
} | ||
} else { | ||
$query->withCount('votes'); | ||
} | ||
|
||
return $query->orderBy($orderValue, 'desc'); | ||
} | ||
|
||
public function thumbnail(): ?string | ||
{ | ||
if (!$this->contest->hasThumbnails()) { | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,11 +7,14 @@ | |||||||||||||
|
||||||||||||||
namespace App\Models; | ||||||||||||||
|
||||||||||||||
use Illuminate\Database\Eloquent\Builder; | ||||||||||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* @property-read Contest $contest | ||||||||||||||
* @property int $contest_id | ||||||||||||||
* @property ?float $mean | ||||||||||||||
* @property ?float $std_dev | ||||||||||||||
* @property-read User $user | ||||||||||||||
* @property int $user_id | ||||||||||||||
*/ | ||||||||||||||
|
@@ -27,4 +30,24 @@ public function user(): BelongsTo | |||||||||||||
{ | ||||||||||||||
return $this->belongsTo(User::class, 'user_id'); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public function calculateStdDev(): void | ||||||||||||||
{ | ||||||||||||||
[$stdDev, $mean] = $this->stdDev(); | ||||||||||||||
|
||||||||||||||
$this->update(['mean' => $mean, 'std_dev' => $stdDev]); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public function stdDev(): array | ||||||||||||||
{ | ||||||||||||||
// TODO: treat missing scores as 0? | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all judges are supposed to judge all entries and they're poked if they haven't |
||||||||||||||
$entryScores = ContestJudgeScore::scoresByEntry() | ||||||||||||||
->whereHas( | ||||||||||||||
'vote', | ||||||||||||||
fn (Builder $q) => $q->where('user_id', $this->user_id) | ||||||||||||||
->whereHas('entry', fn (Builder $qq) => $qq->where('contest_id', $this->contest_id)) | ||||||||||||||
Comment on lines
+45
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
)->pluck('total'); | ||||||||||||||
|
||||||||||||||
return std_dev($entryScores->toArray()); | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||||
|
||||||
namespace App\Models; | ||||||
|
||||||
use Illuminate\Database\Eloquent\Builder; | ||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||||||
|
||||||
/** | ||||||
|
@@ -30,4 +31,22 @@ public function vote(): BelongsTo | |||||
{ | ||||||
return $this->belongsTo(ContestJudgeVote::class, 'contest_judge_vote_id'); | ||||||
} | ||||||
|
||||||
public function scopeScoresFrom(Builder $query, User $user, Contest $contest) | ||||||
{ | ||||||
return $query->whereIn( | ||||||
'contest_judge_vote_id', | ||||||
ContestJudgeVote | ||||||
::where('user_id', $user->getKey()) | ||||||
->whereIn('contest_entry_id', $contest->entries()->select('id')) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more "laravel" (also this is what you had somewhere else)
Suggested change
|
||||||
->select('id') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's a weird indent |
||||||
); | ||||||
} | ||||||
|
||||||
public function scopeScoresByEntry(Builder $query) | ||||||
{ | ||||||
return $query->groupBy('contest_judge_vote_id') | ||||||
->select('contest_judge_vote_id') | ||||||
->addSelect(\DB::raw('SUM(value) as total')); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice description