Skip to content

Commit

Permalink
Merge branches 'score-structure-v44', 'score-solo-index', 'beatmap-pa…
Browse files Browse the repository at this point in the history
…ck-es', 'user-scores', 'solo-profile-recent', 'solo-profile', 'search-rank-table' and 'score-legacy-toggle-ui' into private-staging-v2
  • Loading branch information
nanaya committed Jan 17, 2024
9 parents 46c4e42 + b85a695 + 5fcaa23 + 868fbd6 + f76dc8b + c98a79d + b9ab1a4 + a9c61ec + 9457af8 commit 5f68768
Show file tree
Hide file tree
Showing 44 changed files with 903 additions and 969 deletions.
8 changes: 6 additions & 2 deletions app/Http/Controllers/BeatmapPacksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace App\Http\Controllers;

use App\Libraries\Search\ScoreSearchParams;
use App\Models\Beatmap;
use App\Models\BeatmapPack;
use App\Transformers\BeatmapPackTransformer;
use Auth;

/**
* @group Beatmap Packs
Expand Down Expand Up @@ -100,7 +100,11 @@ public function show($idOrTag)
$pack = $query->where('tag', $idOrTag)->firstOrFail();
$mode = Beatmap::modeStr($pack->playmode ?? 0);
$sets = $pack->beatmapsets;
$userCompletionData = $pack->userCompletionData(Auth::user());
$currentUser = \Auth::user();
$userCompletionData = $pack->userCompletionData(
$currentUser,
ScoreSearchParams::showLegacyForUser($currentUser),
);

if (is_api_request()) {
return json_item(
Expand Down
214 changes: 97 additions & 117 deletions app/Http/Controllers/BeatmapsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 @@ -51,6 +54,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 +343,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,61 +359,19 @@ 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.
*
* ---
*
* ### Response Format
Expand All @@ -365,62 +386,7 @@ public function scores($id)
*/
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, false);
}

public function updateOwner($id)
Expand Down Expand Up @@ -481,13 +447,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' => ScoreSearchParams::showLegacyForUser(\Auth::user()),
'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 @@ -518,12 +496,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' => ScoreSearchParams::showLegacyForUser(\Auth::user()),
'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
31 changes: 23 additions & 8 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
use App\Libraries\RateLimiter;
use App\Libraries\Search\ForumSearch;
use App\Libraries\Search\ForumSearchRequestParams;
use App\Libraries\Search\ScoreSearchParams;
use App\Libraries\User\FindForProfilePage;
use App\Libraries\UserRegistration;
use App\Models\Beatmap;
use App\Models\BeatmapDiscussion;
use App\Models\Country;
use App\Models\IpBan;
use App\Models\Log;
use App\Models\Solo\Score as SoloScore;
use App\Models\User;
use App\Models\UserAccountHistory;
use App\Models\UserNotFound;
Expand Down Expand Up @@ -176,7 +178,7 @@ public function extraPages($_id, $page)
'monthly_playcounts' => json_collection($this->user->monthlyPlaycounts, new UserMonthlyPlaycountTransformer()),
'recent' => $this->getExtraSection(
'scoresRecent',
$this->user->scores($this->mode, true)->includeFails(false)->count()
$this->user->recentScoreCount($this->mode)
),
'replays_watched_counts' => json_collection($this->user->replaysWatchedCounts, new UserReplaysWatchedCountTransformer()),
];
Expand All @@ -191,7 +193,7 @@ public function extraPages($_id, $page)
return [
'best' => $this->getExtraSection(
'scoresBest',
count($this->user->beatmapBestScoreIds($this->mode))
count($this->user->beatmapBestScoreIds($this->mode, ScoreSearchParams::showLegacyForUser(\Auth::user())))
),
'firsts' => $this->getExtraSection(
'scoresFirsts',
Expand Down Expand Up @@ -787,15 +789,25 @@ private function getExtra($page, array $options, int $perPage = 10, int $offset
case 'scoresBest':
$transformer = new ScoreTransformer();
$includes = [...ScoreTransformer::USER_PROFILE_INCLUDES, 'weight'];
$collection = $this->user->beatmapBestScores($this->mode, $perPage, $offset, ScoreTransformer::USER_PROFILE_INCLUDES_PRELOAD);
$collection = $this->user->beatmapBestScores(
$this->mode,
$perPage,
$offset,
ScoreTransformer::USER_PROFILE_INCLUDES_PRELOAD,
ScoreSearchParams::showLegacyForUser(\Auth::user()),
);
$userRelationColumn = 'user';
break;
case 'scoresFirsts':
$transformer = new ScoreTransformer();
$includes = ScoreTransformer::USER_PROFILE_INCLUDES;
$query = $this->user->scoresFirst($this->mode, true)
->visibleUsers()
->reorderBy('score_id', 'desc')
$scoreQuery = $this->user->scoresFirst($this->mode, true)->unorder();
$userFirstsQuery = $scoreQuery->select($scoreQuery->qualifyColumn('score_id'));
$query = SoloScore
::whereIn('legacy_score_id', $userFirstsQuery)
->where('ruleset_id', Beatmap::MODES[$this->mode])
->default()
->reorderBy('id', 'desc')
->with(ScoreTransformer::USER_PROFILE_INCLUDES_PRELOAD);
$userRelationColumn = 'user';
break;
Expand All @@ -814,9 +826,12 @@ private function getExtra($page, array $options, int $perPage = 10, int $offset
case 'scoresRecent':
$transformer = new ScoreTransformer();
$includes = ScoreTransformer::USER_PROFILE_INCLUDES;
$query = $this->user->scores($this->mode, true)
$query = $this->user->soloScores()
->default()
->forRuleset($this->mode)
->includeFails($options['includeFails'] ?? false)
->with([...ScoreTransformer::USER_PROFILE_INCLUDES_PRELOAD, 'best']);
->reorderBy('id', 'desc')
->with(ScoreTransformer::USER_PROFILE_INCLUDES_PRELOAD);
$userRelationColumn = 'user';
break;
}
Expand Down
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
Loading

0 comments on commit 5f68768

Please sign in to comment.