-
Notifications
You must be signed in to change notification settings - Fork 394
/
Copy pathContestEntry.php
118 lines (101 loc) · 3.86 KB
/
ContestEntry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?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\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;
/**
* @property-read Contest $contest
* @property int $contest_id
* @property \Carbon\Carbon|null $created_at
* @property string|null $entry_url
* @property string|null $thumbnail_url
* @property int $id
* @property-read Collection<ContestJudgeVote> $judgeVotes
* @property string $masked_name
* @property string $name
* @property-read Collection<ContestJudgeScore> $scores
* @property \Carbon\Carbon|null $updated_at
* @property-read User $user
* @property int|null $user_id
* @property-read Collection<ContestVote> $votes
*/
class ContestEntry extends Model
{
public function scores(): HasManyThrough
{
return $this->hasManyThrough(ContestJudgeScore::class, ContestJudgeVote::class);
}
public function contest()
{
return $this->belongsTo(Contest::class);
}
public function judgeVotes(): HasMany
{
return $this->hasMany(ContestJudgeVote::class);
}
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
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
{
$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()) {
return null;
}
return presence($this->thumbnail_url) ?? $this->entry_url;
}
}