forked from ppy/osu-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScoreTransformer.php
290 lines (248 loc) · 9.51 KB
/
ScoreTransformer.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
<?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.
declare(strict_types=1);
namespace App\Transformers;
use App\Libraries\Search\ScoreSearchParams;
use App\Models\Beatmap;
use App\Models\DeletedUser;
use App\Models\LegacyMatch;
use App\Models\Multiplayer\PlaylistItemUserHighScore;
use App\Models\Multiplayer\ScoreLink as MultiplayerScoreLink;
use App\Models\Score\Best\Model as ScoreBest;
use App\Models\Score\Model as ScoreModel;
use App\Models\Solo\Score as SoloScore;
use League\Fractal\Resource\Item;
class ScoreTransformer extends TransformerAbstract
{
const MULTIPLAYER_BASE_INCLUDES = ['user.country', 'user.cover'];
// warning: the preload is actually for PlaylistItemUserHighScore, not for Score
const MULTIPLAYER_BASE_PRELOAD = [
'scoreLink.score',
'scoreLink.score.processHistory',
'scoreLink.user.country',
];
const TYPE_LEGACY = 'legacy';
const TYPE_SOLO = 'solo';
// TODO: user include is deprecated.
const USER_PROFILE_INCLUDES = ['beatmap', 'beatmapset', 'user'];
const USER_PROFILE_INCLUDES_PRELOAD = [
'beatmap',
'beatmap.beatmapset',
// it's for user profile so the user is already available
// 'user',
'processHistory',
];
protected array $availableIncludes = [
'beatmap',
'beatmapset',
'current_user_attributes',
'match',
'rank_country',
'rank_global',
'user',
'weight',
// Only for MultiplayerScoreLink
'position',
'scores_around',
];
protected array $defaultIncludes = [
'current_user_attributes',
];
private string $transformFunction;
public static function newSolo(): static
{
return new static(static::TYPE_SOLO);
}
public function __construct(?string $type = null)
{
$type ??= is_api_request() && api_version() < 20220705
? static::TYPE_LEGACY
: static::TYPE_SOLO;
switch ($type) {
case static::TYPE_LEGACY:
$this->transformFunction = 'transformLegacy';
break;
case static::TYPE_SOLO:
$this->transformFunction = 'transformSolo';
break;
}
}
public function transform(LegacyMatch\Score|MultiplayerScoreLink|ScoreModel|SoloScore $score)
{
$fn = $this->transformFunction;
return $this->$fn($score);
}
public function transformSolo(MultiplayerScoreLink|ScoreModel|SoloScore $score)
{
$extraAttributes = [];
if ($score instanceof MultiplayerScoreLink) {
$extraAttributes['playlist_item_id'] = $score->playlist_item_id;
$extraAttributes['room_id'] = $score->playlistItem->room_id;
$extraAttributes['solo_score_id'] = $score->score_id;
$score = $score->score;
}
if ($score instanceof SoloScore) {
$extraAttributes['ranked'] = $score->ranked;
$extraAttributes['preserve'] = $score->preserve;
$extraAttributes['processed'] = $score->legacy_score_id !== null || $score->processHistory !== null;
}
$hasReplay = $score->has_replay;
$isPerfectCombo = $score->is_perfect_combo;
return [
...$extraAttributes,
...$score->data->jsonSerialize(),
'beatmap_id' => $score->beatmap_id,
'best_id' => $score->best_id,
'id' => $score->getKey(),
'rank' => $score->rank,
'type' => $score->getMorphClass(),
'user_id' => $score->user_id,
'accuracy' => $score->accuracy,
'build_id' => $score->build_id,
'ended_at' => $score->ended_at_json,
'has_replay' => $hasReplay,
'is_perfect_combo' => $isPerfectCombo,
'legacy_perfect' => $score->legacy_perfect ?? $isPerfectCombo,
'legacy_score_id' => $score->legacy_score_id,
'legacy_total_score' => $score->legacy_total_score,
'max_combo' => $score->max_combo,
'passed' => $score->passed,
'pp' => $score->pp,
'ruleset_id' => $score->ruleset_id,
'started_at' => $score->started_at_json,
'total_score' => $score->total_score,
// TODO: remove this redundant field sometime after 2024-02
'replay' => $hasReplay,
];
return $ret;
}
public function transformLegacy(LegacyMatch\Score|ScoreModel|SoloScore $score)
{
$best = null;
if ($score instanceof ScoreModel) {
// this `best` relation is also used by `current_user_attributes` include.
$best = $score->best;
} elseif ($score instanceof SoloScore) {
$soloScore = $score;
$score = $soloScore->makeLegacyEntry();
$best = $score;
if ($soloScore->isLegacy()) {
$id = $soloScore->legacy_score_id;
if ($id > 0) {
// To be used later for best_id
$score->score_id = $id;
}
} else {
$id = $soloScore->getKey();
$type = $soloScore->getMorphClass();
}
}
$mode = $score->getMode();
$statistics = [
'count_100' => $score->count100,
'count_300' => $score->count300,
'count_50' => $score->count50,
'count_geki' => $score->countgeki,
'count_katu' => $score->countkatu,
'count_miss' => $score->countmiss,
];
return [
'accuracy' => $score->accuracy(),
'best_id' => $best?->getKey(),
'created_at' => $score->date_json,
'id' => $id ?? $score->getKey(),
'max_combo' => $score->maxcombo,
'mode' => $mode,
'mode_int' => Beatmap::modeInt($mode),
'mods' => $score->enabled_mods,
'passed' => $score->pass,
'perfect' => $perfect ?? $score->perfect,
'pp' => $best?->pp,
'rank' => $score->rank,
'replay' => $best?->has_replay ?? false,
'score' => $score->score,
'statistics' => $statistics,
'type' => $type ?? $score->getMorphClass(),
'user_id' => $score->user_id,
];
}
public function includeBeatmap(LegacyMatch\Score|ScoreModel|SoloScore $score)
{
$beatmap = $score->beatmap;
if ($score->getMode() !== $beatmap->mode) {
$beatmap->convert = true;
$beatmap->playmode = Beatmap::MODES[$score->getMode()];
}
return $this->item($beatmap, new BeatmapTransformer());
}
public function includeBeatmapset(LegacyMatch\Score|ScoreModel|SoloScore $score)
{
return $this->item($score->beatmap->beatmapset, new BeatmapsetCompactTransformer());
}
public function includeCurrentUserAttributes(LegacyMatch\Score|MultiplayerScoreLink|ScoreModel|SoloScore $score): Item
{
return $this->item($score, new Score\CurrentUserAttributesTransformer());
}
public function includeMatch(LegacyMatch\Score $score)
{
return $this->primitive([
'slot' => $score->slot,
'team' => $score->team,
'pass' => $score->pass,
]);
}
public function includePosition(MultiplayerScoreLink $scoreLink)
{
return $this->primitive($scoreLink->position());
}
public function includeScoresAround(MultiplayerScoreLink $scoreLink)
{
static $limit = 10;
static $transformer;
$transformer ??= static::newSolo();
return $this->primitive(array_map(
function ($item) use ($limit, $transformer) {
[$highScores, $hasMore] = $item['query']
->with(static::MULTIPLAYER_BASE_PRELOAD)
->limit($limit)
->getWithHasMore();
return [
'scores' => json_collection($highScores->pluck('scoreLink'), $transformer, static::MULTIPLAYER_BASE_INCLUDES),
'params' => ['limit' => $limit, 'sort' => $item['cursorHelper']->getSortName()],
...cursor_for_response($item['cursorHelper']->next($highScores, $hasMore)),
];
},
PlaylistItemUserHighScore::scoresAround($scoreLink),
));
}
public function includeRankCountry(ScoreBest|SoloScore $score)
{
return $this->primitive($score->userRank([
'type' => 'country',
'is_legacy' => ScoreSearchParams::showLegacyForUser(\Auth::user()),
]));
}
public function includeRankGlobal(ScoreBest|SoloScore $score)
{
return $this->primitive($score->userRank([
'is_legacy' => ScoreSearchParams::showLegacyForUser(\Auth::user()),
]));
}
public function includeUser(LegacyMatch\Score|MultiplayerScoreLink|ScoreModel|SoloScore $score)
{
return $this->item(
$score->user ?? new DeletedUser(['user_id' => $score->user_id]),
new UserCompactTransformer()
);
}
public function includeWeight(LegacyMatch\Score|ScoreModel|SoloScore $score)
{
if (($score instanceof ScoreBest || $score instanceof SoloScore) && $score->weight !== null) {
return $this->primitive([
'percentage' => $score->weight * 100,
'pp' => $score->weightedPp(),
]);
}
}
}