Skip to content

Commit 44ef869

Browse files
committed
simpler query (also applies soft deletes global scope)
1 parent 691584b commit 44ef869

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

app/Models/ContestEntry.php

+15-22
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,23 @@ public function votes()
5656

5757
public function scopeForBestOf(Builder $query, User $user, string $ruleset, ?string $variant = null): Builder
5858
{
59-
$query->whereIn('entry_url', function (QueryBuilder $beatmapsetQuery) use ($ruleset, $user, $variant) {
60-
$beatmapsetQuery->select('beatmapset_id')
61-
->from('osu_beatmaps')
62-
->where('osu_beatmaps.playmode', Beatmap::MODES[$ruleset])
63-
->whereIn('beatmap_id', function (QueryBuilder $beatmapQuery) use ($user) {
64-
$beatmapQuery->select('beatmap_id')
65-
->from('osu_user_beatmap_playcount')
66-
->where('user_id', $user->getKey());
67-
});
68-
69-
if ($ruleset === 'mania' && $variant !== null) {
70-
if ($variant === 'nk') {
71-
$beatmapsetQuery->whereNotIn('osu_beatmaps.diff_size', [4, 7]);
72-
} else {
73-
$keys = match ($variant) {
74-
'4k' => 4,
75-
'7k' => 7,
76-
};
77-
$beatmapsetQuery->where('osu_beatmaps.diff_size', $keys);
78-
}
59+
$beatmapsetIdsQuery = Beatmap::select('beatmapset_id')
60+
->where('playmode', Beatmap::MODES[$ruleset])
61+
->whereIn('beatmap_id', $user->beatmapPlaycounts()->select('beatmap_id'));
62+
63+
if ($ruleset === 'mania' && $variant !== null) {
64+
if ($variant === 'nk') {
65+
$beatmapsetIdsQuery->whereNotIn('osu_beatmaps.diff_size', [4, 7]);
66+
} else {
67+
$keys = match ($variant) {
68+
'4k' => 4,
69+
'7k' => 7,
70+
};
71+
$beatmapsetIdsQuery->where('osu_beatmaps.diff_size', $keys);
7972
}
80-
});
73+
}
8174

82-
return $query;
75+
return $query->whereIn('entry_url', $beatmapsetIdsQuery);
8376
}
8477

8578
public function scopeWithScore(Builder $query, array $options): Builder

0 commit comments

Comments
 (0)