forked from ppy/osu-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeatmap.php
439 lines (367 loc) · 12.1 KB
/
Beatmap.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?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 App\Exceptions\InvariantException;
use App\Jobs\EsDocument;
use App\Libraries\Transactions\AfterCommit;
use DB;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* @property int $approved
* @property-read Collection<BeatmapDiscussion> $beatmapDiscussions
* @property-read Collection<BeatmapOwner> $beatmapOwners
* @property int $beatmap_id
* @property Beatmapset $beatmapset
* @property int|null $beatmapset_id
* @property float $bpm
* @property string|null $checksum
* @property int $countNormal
* @property int $countSlider
* @property int $countSpinner
* @property int $countTotal
* @property \Carbon\Carbon|null $deleted_at
* @property float $diff_approach
* @property float $diff_drain
* @property float $diff_overall
* @property float $diff_size
* @property-read Collection<BeatmapDifficulty> $difficulty
* @property-read Collection<BeatmapDifficultyAttrib> $difficultyAttribs
* @property float $difficultyrating
* @property-read Collection<BeatmapFailtimes> $failtimes
* @property string|null $filename
* @property int $hit_length
* @property \Carbon\Carbon $last_update
* @property int $max_combo
* @property mixed $mode
* @property-read Collection<User> $owners
* @property int $passcount
* @property int $playcount
* @property int $playmode
* @property int $score_version
* @property int $total_length
* @property User $user
* @property int $user_id
* @property string $version
* @property string|null $youtube_preview
*/
class Beatmap extends Model implements AfterCommit
{
use SoftDeletes;
public $convert = false;
protected $table = 'osu_beatmaps';
protected $primaryKey = 'beatmap_id';
protected $casts = [
'last_update' => 'datetime',
];
public $timestamps = false;
const MODES = [
'osu' => 0,
'taiko' => 1,
'fruits' => 2,
'mania' => 3,
];
const VARIANTS = [
'mania' => ['4k', '7k'],
];
public static function isModeValid(?string $mode)
{
return array_key_exists($mode, static::MODES);
}
public static function isVariantValid(?string $mode, ?string $variant)
{
return $variant === null || in_array($variant, static::VARIANTS[$mode] ?? [], true);
}
public static function modeInt($str): ?int
{
return static::MODES[$str] ?? null;
}
public static function modeStr($int): ?string
{
static $lookupMap;
$lookupMap ??= array_flip(static::MODES);
return $lookupMap[$int] ?? null;
}
public function baseDifficultyRatings()
{
return $this->difficulty()->where('mods', 0);
}
public function baseMaxCombo()
{
return $this->difficultyAttribs()->noMods()->maxCombo();
}
public function beatmapOwners()
{
return $this->hasMany(BeatmapOwner::class);
}
public function beatmapset()
{
return $this->belongsTo(Beatmapset::class, 'beatmapset_id')->withTrashed();
}
public function beatmapDiscussions()
{
return $this->hasMany(BeatmapDiscussion::class);
}
public function beatmapTags()
{
return $this->hasMany(BeatmapTag::class);
}
public function difficulty()
{
return $this->hasMany(BeatmapDifficulty::class);
}
public function difficultyAttribs()
{
return $this->hasMany(BeatmapDifficultyAttrib::class);
}
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function scopeDefault($query)
{
return $query
->orderBy('playmode', 'ASC')
->orderBy('difficultyrating', 'ASC');
}
public function scopeIncreasesStatistics(Builder $query): Builder
{
return $query->whereHas('beatmapset', fn ($q) => $q->withTrashed(false));
}
public function scopeScoreable($query)
{
return $query->where('approved', '>', 0);
}
public function scopeWithMaxCombo($query)
{
$mods = BeatmapDifficultyAttrib::NO_MODS;
$attrib = BeatmapDifficultyAttrib::MAX_COMBO;
$attribTable = (new BeatmapDifficultyAttrib())->tableName();
$mode = $this->qualifyColumn('playmode');
$id = $this->qualifyColumn('beatmap_id');
return $query
->select(DB::raw("*, (
SELECT value
FROM {$attribTable}
WHERE beatmap_id = {$id}
AND mode = {$mode}
AND mods = {$mods}
AND attrib_id = {$attrib}
) AS attrib_max_combo"));
}
public function failtimes()
{
return $this->hasMany(BeatmapFailtimes::class);
}
public function scores($mode = null)
{
return $this->getScores(Score::class, $mode);
}
public function scoresBest($mode = null)
{
return $this->getScores(Score\Best::class, $mode);
}
public function scoresBestOsu()
{
return $this->hasMany(Score\Best\Osu::class);
}
public function scoresBestTaiko()
{
return $this->hasMany(Score\Best\Taiko::class);
}
public function scoresBestFruits()
{
return $this->hasMany(Score\Best\Fruits::class);
}
public function scoresBestMania()
{
return $this->hasMany(Score\Best\Mania::class);
}
public function afterCommit()
{
$beatmapset = $this->beatmapset;
if ($beatmapset !== null) {
dispatch(new EsDocument($beatmapset));
}
}
public function isScoreable()
{
return $this->approved > 0;
}
public function canBeConvertedTo(int $rulesetId)
{
return $this->playmode === static::MODES['osu'] || $this->playmode === $rulesetId;
}
public function getAttribute($key)
{
return match ($key) {
'approved',
'beatmap_id',
'beatmapset_id',
'bpm',
'checksum',
'countNormal',
'countSlider',
'countSpinner',
'countTotal',
'diff_approach',
'diff_drain',
'diff_overall',
'filename',
'hit_length',
'max_combo',
'passcount',
'playcount',
'playmode',
'score_version',
'total_length',
'user_id',
'youtube_preview' => $this->getRawAttribute($key),
'deleted_at',
'last_update' => $this->getTimeFast($key),
'deleted_at_json',
'last_update_json' => $this->getJsonTimeFast($key),
'diff_size' => $this->getDiffSize(),
'difficultyrating' => $this->getDifficultyrating(),
'mode' => $this->getMode(),
'version' => $this->getVersion(),
'baseDifficultyRatings',
'baseMaxCombo',
'beatmapDiscussions',
'beatmapOwners',
'beatmapset',
'difficulty',
'difficultyAttribs',
'failtimes',
'scoresBestFruits',
'scoresBestMania',
'scoresBestOsu',
'scoresBestTaiko',
'user' => $this->getRelationValue($key),
};
}
/**
* @return Collection<User>
*/
public function getOwners(): Collection
{
$owners = $this->beatmapOwners->loadMissing('user')->map(
fn ($beatmapOwner) => $beatmapOwner->user ?? new DeletedUser(['user_id' => $beatmapOwner->user_id])
);
// TODO: remove when everything writes to beatmap_owners.
if (!$owners->contains(fn ($beatmapOwner) => $beatmapOwner->user_id === $this->user_id)) {
$owners->prepend($this->user ?? new DeletedUser(['user_id' => $this->user_id]));
}
return $owners;
}
public function isOwner(User $user): bool
{
if ($this->user_id === $user->getKey()) {
return true;
}
return $this->relationLoaded('beatmapOwners')
? $this->beatmapOwners->contains('user_id', $user->getKey())
: $this->beatmapOwners()->where('user_id', $user->getKey())->exists();
}
public function maxCombo()
{
if (!$this->convert) {
$rowMaxCombo = $this->max_combo;
if ($rowMaxCombo > 0) {
return $rowMaxCombo;
}
if (array_key_exists('attrib_max_combo', $this->attributes)) {
return $this->attributes['attrib_max_combo'];
}
}
if ($this->relationLoaded('baseMaxCombo')) {
$maxCombo = $this->baseMaxCombo->firstWhere('mode', $this->playmode);
} else {
$maxCombo = $this->difficultyAttribs()
->mode($this->playmode)
->noMods()
->maxCombo()
->first();
}
return $maxCombo?->value;
}
public function status()
{
return array_search($this->approved, Beatmapset::STATES, true);
}
private function getDifficultyrating()
{
if ($this->convert) {
$value = (
$this->relationLoaded('baseDifficultyRatings')
? $this->baseDifficultyRatings
: $this->baseDifficultyRatings()
)->firstWhere('mode', $this->playmode)
?->diff_unified ?? 0;
} else {
$value = $this->getRawAttribute('difficultyrating');
}
return round($value, 2);
}
private function getDiffSize()
{
/*
* Matches client implementation.
* all round()s here use PHP_ROUND_HALF_EVEN to match C# default Math.Round.
* References:
* - (implementation) https://github.com/ppy/osu/blob/6bbc23c831cd73bf126b31edb0bb4fa729f947d1/osu.Game.Rulesets.Mania/Beatmaps/ManiaBeatmapConverter.cs#L40
* - (rounding) https://msdn.microsoft.com/en-us/library/wyk4d9cy(v=vs.110).aspx
*/
$value = $this->getRawAttribute('diff_size');
if ($this->playmode === static::MODES['mania']) {
$roundedValue = (int) round($value, 0, PHP_ROUND_HALF_EVEN);
if ($this->convert) {
$sliderOrSpinner = ($this->countSlider ?? 0) + ($this->countSpinner ?? 0);
$total = max(1, $sliderOrSpinner + ($this->countNormal ?? 0));
$percentSliderOrSpinner = $sliderOrSpinner / $total;
$accuracy = (int) round($this->diff_overall ?? 0, 0, PHP_ROUND_HALF_EVEN);
if ($percentSliderOrSpinner < 0.2) {
return 7;
} elseif ($percentSliderOrSpinner < 0.3 || $roundedValue >= 5) {
return $accuracy > 5 ? 7 : 6;
} elseif ($percentSliderOrSpinner > 0.6) {
return $accuracy > 4 ? 5 : 4;
} else {
return \Number::clamp($accuracy + 1, 4, 7);
}
} else {
return max(1, $roundedValue);
}
}
return $value;
}
private function getMode()
{
return static::modeStr($this->playmode);
}
private function getScores($modelPath, $mode)
{
$mode ?? ($mode = $this->mode);
if (!static::isModeValid($mode)) {
throw new InvariantException(osu_trans('errors.beatmaps.invalid_mode'));
}
if ($this->mode !== 'osu' && $this->mode !== $mode) {
throw new InvariantException(osu_trans('errors.beatmaps.standard_converts_only'));
}
$mode = studly_case($mode);
return $this->hasMany("{$modelPath}\\{$mode}");
}
private function getVersion()
{
$value = $this->getRawAttribute('version');
if ($this->mode === 'mania') {
$keys = $this->getDiffSize();
if (strpos($value, "{$keys}k") === false && strpos($value, "{$keys}K") === false) {
return "[{$keys}K] {$value}";
}
}
return $value;
}
}