Skip to content

Commit

Permalink
Use solo score index for user beatmap scores
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Aug 4, 2023
1 parent 0d6a7e3 commit 4a6f752
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
55 changes: 28 additions & 27 deletions app/Http/Controllers/BeatmapsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
use App\Jobs\Notifications\BeatmapOwnerChange;
use App\Libraries\BeatmapDifficultyAttributes;
use App\Libraries\Score\BeatmapScores;
use App\Libraries\Score\UserRank;
use App\Libraries\Search\ScoreSearch;
use App\Libraries\Search\ScoreSearchParams;
use App\Models\Beatmap;
use App\Models\BeatmapsetEvent;
use App\Models\Score\Best\Model as BestModel;
use App\Models\User;
use App\Transformers\BeatmapTransformer;
use App\Transformers\ScoreTransformer;
Expand All @@ -35,21 +37,6 @@ private static function assertSupporterOnlyOptions(?User $currentUser, string $t
}
}

private static function baseScoreQuery(Beatmap $beatmap, $mode, $mods, $type = null)
{
$query = BestModel::getClass($mode)
::default()
->where('beatmap_id', $beatmap->getKey())
->with(['beatmap', 'user.country', 'user.userProfileCustomization'])
->withMods($mods);

if ($type !== null) {
$query->withType($type, ['user' => auth()->user()]);
}

return $query;
}

public function __construct()
{
parent::__construct();
Expand Down Expand Up @@ -374,13 +361,25 @@ public function userScore($beatmapId, $userId)
$mode = presence($params['mode'] ?? null, $beatmap->mode);
$mods = array_values(array_filter($params['mods'] ?? []));

$score = static::baseScoreQuery($beatmap, $mode, $mods)
->visibleUsers()
->where('user_id', $userId)
->firstOrFail();
$baseParams = ScoreSearchParams::fromArray([
'beatmap_ids' => [$beatmap->getKey()],
'is_legacy' => true,
'limit' => 1,
'mods' => $mods,
'ruleset_id' => Beatmap::MODES[$mode],
'sort' => 'score_desc',
'user_id' => (int) $userId,
]);
$score = (new ScoreSearch($baseParams))->records()->first();
abort_if($score === null, 404);

$rankParams = clone $baseParams;
$rankParams->beforeScore = $score;
$rankParams->userId = null;
$rank = UserRank::getRank($rankParams);

return [
'position' => $score->userRank(compact('mods')),
'position' => $rank,
'score' => json_item(
$score,
new ScoreTransformer(),
Expand Down Expand Up @@ -411,12 +410,14 @@ public function userScoreAll($beatmapId, $userId)
{
$beatmap = Beatmap::scoreable()->findOrFail($beatmapId);
$mode = presence(get_string(request('mode'))) ?? $beatmap->mode;
$scores = BestModel::getClass($mode)
::default()
->where([
'beatmap_id' => $beatmap->getKey(),
'user_id' => $userId,
])->get();
$params = ScoreSearchParams::fromArray([
'beatmap_ids' => [$beatmap->getKey()],
'is_legacy' => true,
'ruleset_id' => Beatmap::MODES[$mode],
'sort' => 'score_desc',
'user_id' => (int) $userId,
]);
$scores = (new ScoreSearch($params))->records();

return [
'scores' => json_collection($scores, new ScoreTransformer()),
Expand Down
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@
Route::get('lookup', 'BeatmapsController@lookup')->name('lookup');

Route::group(['prefix' => '{beatmap}'], function () {
Route::get('scores/users/{user}', 'BeatmapsController@userScore');
Route::get('scores/users/{user}/all', 'BeatmapsController@userScoreAll');
Route::get('scores/users/{user}', 'BeatmapsController@userScore')->name('user.score');
Route::get('scores/users/{user}/all', 'BeatmapsController@userScoreAll')->name('user.scores');
Route::get('scores', 'BeatmapsController@scores')->name('scores');
Route::get('solo-scores', 'BeatmapsController@soloScores')->name('solo-scores');

Expand Down
37 changes: 37 additions & 0 deletions tests/Controllers/BeatmapsControllerSoloScoresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Models\Genre;
use App\Models\Group;
use App\Models\Language;
use App\Models\OAuth;
use App\Models\Solo\Score;
use App\Models\User;
use App\Models\UserGroup;
Expand Down Expand Up @@ -176,6 +177,8 @@ public static function tearDownAfterClass(): void
Genre::truncate();
Group::truncate();
Language::truncate();
OAuth\Client::truncate();
OAuth\Token::truncate();
Score::truncate();
User::truncate();
UserGroup::truncate();
Expand All @@ -201,6 +204,40 @@ public function testQuery(array $scoreKeys, array $params, string $route)
}
}

public function testUserScore()
{
$url = route('api.beatmaps.user.score', [
'beatmap' => static::$beatmap->getKey(),
'mods' => ['DT', 'HD'],
'user' => static::$user->getKey(),
]);
$this->actAsScopedUser(static::$user);
$this
->json('GET', $url)
->assertJsonPath('score.id', static::$scores['legacy:userMods']->getKey());
}

public function testUserScoreAll()
{
$url = route('api.beatmaps.user.scores', [
'beatmap' => static::$beatmap->getKey(),
'user' => static::$user->getKey(),
]);
$this->actAsScopedUser(static::$user);
$this
->json('GET', $url)
->assertJsonCount(4, 'scores')
->assertJsonPath(
'scores.*.id',
array_map(fn (string $key): int => static::$scores[$key]->getKey(), [
'legacy:user',
'legacy:userMods',
'legacy:userModsNC',
'legacy:userModsLowerScore',
])
);
}

public function dataProviderForTestQuery(): array
{
$ret = [];
Expand Down

0 comments on commit 4a6f752

Please sign in to comment.