Skip to content

Commit 3d0aaf7

Browse files
committed
Simplify score transformer variation parameter
1 parent cd87242 commit 3d0aaf7

File tree

7 files changed

+18
-41
lines changed

7 files changed

+18
-41
lines changed

app/Http/Controllers/BeatmapsController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static function assertSupporterOnlyOptions(?User $currentUser, string $t
4545
}
4646

4747
// TODO: move this to scores() and remove soloScores(). Probably sometime after October 2025.
48-
private static function beatmapScores(string $id, ?string $scoreTransformerType, ?bool $isLegacy): array
48+
private static function beatmapScores(string $id, ?bool $legacyFormat, ?bool $isLegacy): array
4949
{
5050
$beatmap = Beatmap::findOrFail($id);
5151
if ($beatmap->approved <= 0) {
@@ -77,7 +77,7 @@ private static function beatmapScores(string $id, ?string $scoreTransformerType,
7777
]);
7878
$scores = $esFetch->all()->loadMissing(['beatmap', 'user.country', 'user.team', 'processHistory']);
7979
$userScore = $esFetch->userBest();
80-
$scoreTransformer = new ScoreTransformer($scoreTransformerType);
80+
$scoreTransformer = new ScoreTransformer($legacyFormat);
8181

8282
$results = [
8383
'score_count' => UserRank::getCount($esFetch->baseParams),
@@ -386,7 +386,7 @@ public function scores($id)
386386
*/
387387
public function soloScores($id)
388388
{
389-
return static::beatmapScores($id, ScoreTransformer::TYPE_SOLO, null);
389+
return static::beatmapScores($id, false, null);
390390
}
391391

392392
public function updateOwner($id)

app/Http/Controllers/Multiplayer/Rooms/Playlist/ScoresController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function index($roomId, $playlistId)
6464
->limit($limit)
6565
->getWithHasMore();
6666

67-
$transformer = ScoreTransformer::newSolo();
67+
$transformer = new ScoreTransformer(false);
6868
$scoresJson = json_collection(
6969
$highScores->pluck('scoreLink'),
7070
$transformer,
@@ -127,7 +127,7 @@ public function show($roomId, $playlistId, $id)
127127

128128
return json_item(
129129
$scoreLink,
130-
ScoreTransformer::newSolo(),
130+
new ScoreTransformer(false),
131131
[
132132
...ScoreTransformer::MULTIPLAYER_BASE_INCLUDES,
133133
'position',
@@ -159,7 +159,7 @@ public function showUser($roomId, $playlistId, $userId)
159159

160160
return json_item(
161161
$scoreLink,
162-
ScoreTransformer::newSolo(),
162+
new ScoreTransformer(false),
163163
[
164164
...ScoreTransformer::MULTIPLAYER_BASE_INCLUDES,
165165
'position',
@@ -225,7 +225,7 @@ public function update($roomId, $playlistItemId, $tokenId)
225225

226226
return json_item(
227227
$scoreLink,
228-
ScoreTransformer::newSolo(),
228+
new ScoreTransformer(false),
229229
[
230230
...ScoreTransformer::MULTIPLAYER_BASE_INCLUDES,
231231
'position',

app/Http/Controllers/ScoresController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function () use ($cursor, $isOldScores, $rulesetId) {
197197
}
198198

199199
return [
200-
'scores' => json_collection($filteredScores, new ScoreTransformer(ScoreTransformer::TYPE_SOLO)),
200+
'scores' => json_collection($filteredScores, new ScoreTransformer(false)),
201201
// return previous cursor if no result, assuming there's no new scores yet
202202
...cursor_for_response($cursorHelper->next($filteredScores) ?? $cursor),
203203
];

app/Http/Controllers/Solo/ScoresController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ public function store($beatmapId, $tokenId)
5353
$score->queueForProcessing();
5454
}
5555

56-
return json_item($score, new ScoreTransformer(ScoreTransformer::TYPE_SOLO));
56+
return json_item($score, new ScoreTransformer(false));
5757
}
5858
}

app/Transformers/LegacyMatch/GameTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function includeScores(Game $game)
4545
{
4646
return $this->collection(
4747
$game->scores,
48-
new ScoreTransformer(ScoreTransformer::TYPE_LEGACY)
48+
new ScoreTransformer(true)
4949
);
5050
}
5151
}

app/Transformers/Multiplayer/PlaylistItemTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function includeScores(PlaylistItem $item)
4646
{
4747
return $this->collection(
4848
$item->scoreLinks,
49-
ScoreTransformer::newSolo()
49+
new ScoreTransformer(false)
5050
);
5151
}
5252
}

app/Transformers/ScoreTransformer.php

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ class ScoreTransformer extends TransformerAbstract
2929
'scoreLink.user.country',
3030
];
3131

32-
const TYPE_LEGACY = 'legacy';
33-
const TYPE_SOLO = 'solo';
34-
3532
// TODO: user include is deprecated.
3633
const USER_PROFILE_INCLUDES = ['beatmap', 'beatmapset', 'user'];
3734
const USER_PROFILE_INCLUDES_PRELOAD = [
@@ -61,38 +58,19 @@ class ScoreTransformer extends TransformerAbstract
6158
'current_user_attributes',
6259
];
6360

64-
private string $transformFunction;
61+
private bool $legacyFormat;
6562

66-
public static function newSolo(): static
63+
public function __construct(?bool $legacyFormat = null)
6764
{
68-
return new static(static::TYPE_SOLO);
69-
}
70-
71-
public function __construct(?string $type = null)
72-
{
73-
$type ??= is_api_request() && api_version() < 20220705
74-
? static::TYPE_LEGACY
75-
: static::TYPE_SOLO;
76-
77-
switch ($type) {
78-
case static::TYPE_LEGACY:
79-
$this->transformFunction = 'transformLegacy';
80-
break;
81-
case static::TYPE_SOLO:
82-
$this->transformFunction = 'transformSolo';
83-
break;
84-
}
65+
$this->legacyFormat = $legacyFormat ?? (is_api_request() && api_version() < 20220705);
8566
}
8667

8768
public function transform(LegacyMatch\Score|MultiplayerScoreLink|ScoreModel|SoloScore $score)
8869
{
89-
$fn = $this->transformFunction;
90-
91-
return $this->$fn($score);
92-
}
70+
if ($this->legacyFormat) {
71+
return $this->transformLegacy($score);
72+
}
9373

94-
public function transformSolo(MultiplayerScoreLink|ScoreModel|SoloScore $score)
95-
{
9674
$extraAttributes = [];
9775

9876
if ($score instanceof MultiplayerScoreLink) {
@@ -237,8 +215,7 @@ public function includePosition(MultiplayerScoreLink $scoreLink)
237215
public function includeScoresAround(MultiplayerScoreLink $scoreLink)
238216
{
239217
static $limit = 10;
240-
static $transformer;
241-
$transformer ??= static::newSolo();
218+
static $transformer = new static(false);
242219

243220
return $this->primitive(array_map(
244221
function ($item) use ($limit, $transformer) {

0 commit comments

Comments
 (0)