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 Sep 8, 2023
1 parent 411e016 commit 1870026
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
39 changes: 28 additions & 11 deletions app/Http/Controllers/BeatmapsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
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;
Expand Down Expand Up @@ -476,13 +479,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 @@ -513,12 +528,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 @@ -399,8 +399,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
43 changes: 43 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 as SoloScore;
use App\Models\User;
use App\Models\UserGroup;
Expand Down Expand Up @@ -166,6 +167,8 @@ public static function tearDownAfterClass(): void
Genre::truncate();
Group::truncate();
Language::truncate();
OAuth\Client::truncate();
OAuth\Token::truncate();
SoloScore::truncate();
User::truncate();
UserGroup::truncate();
Expand Down Expand Up @@ -203,6 +206,46 @@ public function testQuery(array $scoreKeys, array $params)
}
}

/**
* @group RequiresScoreIndexer
*/
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());
}

/**
* @group RequiresScoreIndexer
*/
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
{
return [
Expand Down

0 comments on commit 1870026

Please sign in to comment.