Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new score index for leaderboard #10890

Merged
merged 21 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 71 additions & 107 deletions app/Http/Controllers/BeatmapsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,66 @@ private static function baseScoreQuery(Beatmap $beatmap, $mode, $mods, $type = n
return $query;
}

private static function beatmapScores(string $id, ?string $scoreTransformerType, ?bool $isLegacy): array
{
$beatmap = Beatmap::findOrFail($id);
if ($beatmap->approved <= 0) {
return ['scores' => []];
}

$params = get_params(request()->all(), null, [
'limit:int',
'mode',
'mods:string[]',
'type:string',
], ['null_missing' => true]);

if ($params['mode'] !== null) {
$rulesetId = Beatmap::MODES[$params['mode']] ?? null;
if ($rulesetId === null) {
throw new InvariantException('invalid mode specified');
}
}
$rulesetId ??= $beatmap->playmode;
$mods = array_values(array_filter($params['mods'] ?? []));
$type = presence($params['type'], 'global');
$currentUser = \Auth::user();

static::assertSupporterOnlyOptions($currentUser, $type, $mods);

$esFetch = new BeatmapScores([
'beatmap_ids' => [$beatmap->getKey()],
'is_legacy' => $isLegacy,
'limit' => $params['limit'],
'mods' => $mods,
'ruleset_id' => $rulesetId,
'type' => $type,
'user' => $currentUser,
]);
$scores = $esFetch->all()->loadMissing(['beatmap', 'user.country', 'user.userProfileCustomization']);
$userScore = $esFetch->userBest();
$scoreTransformer = new ScoreTransformer($scoreTransformerType);

$results = [
'scores' => json_collection(
$scores,
$scoreTransformer,
static::DEFAULT_SCORE_INCLUDES
),
];

if (isset($userScore)) {
$results['user_score'] = [
'position' => $esFetch->rank($userScore),
'score' => json_item($userScore, $scoreTransformer, static::DEFAULT_SCORE_INCLUDES),
];
// TODO: remove this old camelCased json field
$results['userScore'] = $results['user_score'];
}

return $results;
}

public function __construct()
{
parent::__construct();
Expand Down Expand Up @@ -280,7 +340,7 @@ public function show($id)
/**
* Get Beatmap scores
*
* Returns the top scores for a beatmap
* Returns the top scores for a beatmap. Depending on user preferences, this may only show legacy scores.
*
* ---
*
Expand All @@ -296,60 +356,18 @@ public function show($id)
*/
public function scores($id)
{
$beatmap = Beatmap::findOrFail($id);
if ($beatmap->approved <= 0) {
return ['scores' => []];
}

$params = get_params(request()->all(), null, [
'limit:int',
'mode:string',
'mods:string[]',
'type:string',
], ['null_missing' => true]);

$mode = presence($params['mode']) ?? $beatmap->mode;
$mods = array_values(array_filter($params['mods'] ?? []));
$type = presence($params['type']) ?? 'global';
$currentUser = auth()->user();

static::assertSupporterOnlyOptions($currentUser, $type, $mods);

$query = static::baseScoreQuery($beatmap, $mode, $mods, $type);

if ($currentUser !== null) {
// own score shouldn't be filtered by visibleUsers()
$userScore = (clone $query)->where('user_id', $currentUser->user_id)->first();
}

$scoreTransformer = new ScoreTransformer();

$results = [
'scores' => json_collection(
$query->visibleUsers()->forListing($params['limit']),
$scoreTransformer,
static::DEFAULT_SCORE_INCLUDES
),
];

if (isset($userScore)) {
$results['user_score'] = [
'position' => $userScore->userRank(compact('type', 'mods')),
'score' => json_item($userScore, $scoreTransformer, static::DEFAULT_SCORE_INCLUDES),
];
// TODO: remove this old camelCased json field
$results['userScore'] = $results['user_score'];
}

return $results;
return static::beatmapScores(
$id,
null,
// TODO: change to imported name after merge with other PRs
\App\Libraries\Search\ScoreSearchParams::showLegacyForUser(\Auth::user()),
);
}

/**
* Get Beatmap scores (temp)
* Get Beatmap scores (non-legacy)
*
* Returns the top scores for a beatmap from newer client.
*
* This is a temporary endpoint.
* Returns the top scores for a beatmap.
*
* ---
*
Expand All @@ -359,68 +377,14 @@ public function scores($id)
*
* @urlParam beatmap integer required Id of the [Beatmap](#beatmap).
*
* @queryParam legacy_only Set to true to only return legacy scores. Example: 0
* @queryParam mode The [Ruleset](#ruleset) to get scores for.
* @queryParam mods An array of matching Mods, or none // TODO.
* @queryParam type Beatmap score ranking type // TODO.
*/
public function soloScores($id)
{
$beatmap = Beatmap::findOrFail($id);
if ($beatmap->approved <= 0) {
return ['scores' => []];
}

$params = get_params(request()->all(), null, [
'limit:int',
'mode',
'mods:string[]',
'type:string',
], ['null_missing' => true]);

if ($params['mode'] !== null) {
$rulesetId = Beatmap::MODES[$params['mode']] ?? null;
if ($rulesetId === null) {
throw new InvariantException('invalid mode specified');
}
}
$rulesetId ??= $beatmap->playmode;
$mods = array_values(array_filter($params['mods'] ?? []));
$type = presence($params['type'], 'global');
$currentUser = auth()->user();

static::assertSupporterOnlyOptions($currentUser, $type, $mods);

$esFetch = new BeatmapScores([
'beatmap_ids' => [$beatmap->getKey()],
'is_legacy' => false,
'limit' => $params['limit'],
'mods' => $mods,
'ruleset_id' => $rulesetId,
'type' => $type,
'user' => $currentUser,
]);
$scores = $esFetch->all()->loadMissing(['beatmap', 'performance', 'user.country', 'user.userProfileCustomization']);
$userScore = $esFetch->userBest();
$scoreTransformer = new ScoreTransformer(ScoreTransformer::TYPE_SOLO);

$results = [
'scores' => json_collection(
$scores,
$scoreTransformer,
static::DEFAULT_SCORE_INCLUDES
),
];

if (isset($userScore)) {
$results['user_score'] = [
'position' => $esFetch->rank($userScore),
'score' => json_item($userScore, $scoreTransformer, static::DEFAULT_SCORE_INCLUDES),
];
// TODO: remove this old camelCased json field
$results['userScore'] = $results['user_score'];
}

return $results;
return static::beatmapScores($id, ScoreTransformer::TYPE_SOLO, null);
}

public function updateOwner($id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,13 @@ public function update($roomId, $playlistItemId, $tokenId)
});

$score = $scoreLink->score;
$transformer = ScoreTransformer::newSolo();
if ($score->wasRecentlyCreated) {
$scoreJson = json_item($score, $transformer);
$score::queueForProcessing($scoreJson);
$score->queueForProcessing();
}

return json_item(
$scoreLink,
$transformer,
ScoreTransformer::newSolo(),
[
...ScoreTransformer::MULTIPLAYER_BASE_INCLUDES,
'position',
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Controllers/Solo/ScoresController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ public function store($beatmapId, $tokenId)
return $score;
});

$scoreJson = json_item($score, new ScoreTransformer(ScoreTransformer::TYPE_SOLO));
if ($score->wasRecentlyCreated) {
$score::queueForProcessing($scoreJson);
$score->queueForProcessing();
}

return $scoreJson;
return json_item($score, new ScoreTransformer(ScoreTransformer::TYPE_SOLO));
}
}
2 changes: 0 additions & 2 deletions app/Jobs/RemoveBeatmapsetSoloScores.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use App\Models\Beatmap;
use App\Models\Beatmapset;
use App\Models\Solo\Score;
use App\Models\Solo\ScorePerformance;
use DB;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -68,7 +67,6 @@ private function deleteScores(Collection $scores): void
$scoresQuery->update(['preserve' => false]);
$this->scoreSearch->queueForIndex($this->schemas, $ids);
DB::transaction(function () use ($ids, $scoresQuery): void {
ScorePerformance::whereKey($ids)->delete();
$scoresQuery->delete();
});
}
Expand Down
14 changes: 8 additions & 6 deletions app/Libraries/Search/ScoreSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,20 @@ public function getQuery(): BoolQuery

$beforeTotalScore = $this->params->beforeTotalScore;
if ($beforeTotalScore === null && $this->params->beforeScore !== null) {
$beforeTotalScore = $this->params->beforeScore->isLegacy()
? $this->params->beforeScore->data->legacyTotalScore
: $this->params->beforeScore->data->totalScore;
$beforeTotalScore = $this->params->isLegacy
? $this->params->beforeScore->legacy_total_score
: $this->params->beforeScore->total_score;
}
if ($beforeTotalScore !== null) {
$scoreQuery = (new BoolQuery())->shouldMatch(1);
$scoreField = $this->params->isLegacy ? 'legacy_total_score' : 'total_score';
$scoreQuery->should((new BoolQuery())->filter(['range' => [
'total_score' => ['gt' => $beforeTotalScore],
$scoreField => ['gt' => $beforeTotalScore],
]]));
if ($this->params->beforeScore !== null) {
$scoreQuery->should((new BoolQuery())
->filter(['range' => ['id' => ['lt' => $this->params->beforeScore->getKey()]]])
->filter(['term' => ['total_score' => $beforeTotalScore]]));
->filter(['term' => [$scoreField => $beforeTotalScore]]));
}

$query->must($scoreQuery);
Expand Down Expand Up @@ -142,7 +143,8 @@ private function addModsFilter(BoolQuery $query): void
$allMods = $this->params->rulesetId === null
? $modsHelper->allIds
: new Set(array_keys($modsHelper->mods[$this->params->rulesetId]));
$allMods->remove('PF', 'SD', 'MR');
// CL is currently considered a "preference" mod
$allMods->remove('CL', 'PF', 'SD', 'MR');

$allSearchMods = [];
foreach ($mods as $mod) {
Expand Down
33 changes: 28 additions & 5 deletions app/Libraries/Search/ScoreSearchParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,33 @@ public static function fromArray(array $rawParams): static
}

/**
* This returns value for isLegacy based on user preference
* This returns value for isLegacy based on user preference, request type, and `legacy_only` parameter
*/
public static function showLegacyForUser(?User $user): null | true
{
public static function showLegacyForUser(
?User $user = null,
?bool $legacyOnly = null,
?bool $isApiRequest = null
): null | true {
$isApiRequest ??= is_api_request();
// `null` is actual parameter value for the other two parameters so
// only try filling them up if not passed at all.
$argLen = func_num_args();
if ($argLen < 2) {
$legacyOnly = get_bool(Request('legacy_only'));

if ($argLen < 1) {
$user = \Auth::user();
}
}

if ($legacyOnly !== null) {
return $legacyOnly ? true : null;
}

if ($isApiRequest) {
return null;
}

return $user?->userProfileCustomization?->legacy_score_only ?? UserProfileCustomization::DEFAULT_LEGACY_ONLY_ATTRIBUTE
? true
: null;
Expand Down Expand Up @@ -93,9 +116,9 @@ public function setSort(?string $sort): void
{
switch ($sort) {
case 'score_desc':
$sortColumn = $this->isLegacy ? 'legacy_total_score' : 'total_score';
$this->sorts = [
new Sort('is_legacy', 'asc'),
new Sort('total_score', 'desc'),
new Sort($sortColumn, 'desc'),
new Sort('id', 'asc'),
];
break;
Expand Down
6 changes: 3 additions & 3 deletions app/Models/Multiplayer/PlaylistItemUserHighScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function scoresAround(ScoreLink $scoreLink): array
{
$placeholder = new static([
'score_id' => $scoreLink->getKey(),
'total_score' => $scoreLink->score->data->totalScore,
'total_score' => $scoreLink->score->total_score,
]);

static $typeOptions = [
Expand Down Expand Up @@ -117,10 +117,10 @@ public function updateWithScoreLink(ScoreLink $scoreLink): void
$score = $scoreLink->score;

$this->fill([
'accuracy' => $score->data->accuracy,
'accuracy' => $score->accuracy,
'pp' => $score->pp,
'score_id' => $scoreLink->getKey(),
'total_score' => $score->data->totalScore,
'total_score' => $score->total_score,
])->save();
}
}
2 changes: 1 addition & 1 deletion app/Models/Multiplayer/ScoreLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function position(): ?int
$query = PlaylistItemUserHighScore
::where('playlist_item_id', $this->playlist_item_id)
->cursorSort('score_asc', [
'total_score' => $score->data->totalScore,
'total_score' => $score->total_score,
'score_id' => $this->getKey(),
]);

Expand Down
Loading
Loading