Skip to content

Commit

Permalink
Use new table for user played filter on map search
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Jan 16, 2024
1 parent 64a6ff3 commit a9c61ec
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions app/Libraries/Search/BeatmapsetSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
use App\Models\Beatmap;
use App\Models\Beatmapset;
use App\Models\Follow;
use App\Models\Score;
use App\Models\Solo;
use App\Models\User;
use Ds\Set;

class BeatmapsetSearch extends RecordSearch
{
Expand Down Expand Up @@ -423,38 +424,36 @@ private function addTextFilter(BoolQuery $query, string $paramField, array $fiel

private function getPlayedBeatmapIds(?array $rank = null)
{
$unionQuery = null;
$query = Solo\Score
::where('user_id', $this->params->user->getKey())
->whereIn('ruleset_id', $this->getSelectedModes());

$select = $rank === null ? 'beatmap_id' : ['beatmap_id', 'score', 'rank'];
if ($rank === null) {
return $query->distinct('beatmap_id')->pluck('beatmap_id');
}

foreach ($this->getSelectedModes() as $mode) {
$newQuery = Score\Best\Model::getClassByRulesetId($mode)
::forUser($this->params->user)
->select($select);
$topScores = [];
$scoreField = ScoreSearchParams::showLegacyForUser($this->params->user)
? 'legacy_total_score'
: 'total_score';
foreach ($query->get() as $score) {
$prevScore = $topScores[$score->beatmap_id] ?? null;

if ($unionQuery === null) {
$unionQuery = $newQuery;
} else {
$unionQuery->union($newQuery);
$scoreValue = $score->$scoreField;
if ($scoreValue !== null && ($prevScore === null || $prevScore->$scoreField < $scoreValue)) {
$topScores[$score->beatmap_id] = $score;
}
}

if ($rank === null) {
return model_pluck($unionQuery, 'beatmap_id');
} else {
$allScores = $unionQuery->get();
$beatmapRank = collect();

foreach ($allScores as $score) {
$prevScore = $beatmapRank[$score->beatmap_id] ?? null;

if ($prevScore === null || $prevScore->score < $score->score) {
$beatmapRank[$score->beatmap_id] = $score;
}
$ret = [];
$rankSet = new Set($rank);
foreach ($topScores as $beatmapId => $score) {
if ($rankSet->contains($score->rank)) {
$ret[] = $beatmapId;
}

return $beatmapRank->whereInStrict('rank', $rank)->pluck('beatmap_id')->all();
}

return $ret;
}

private function getSelectedModes()
Expand Down

0 comments on commit a9c61ec

Please sign in to comment.