Skip to content

Commit

Permalink
Merge branches 'beatmap-pack-es', 'user-scores', 'score-pin-performan…
Browse files Browse the repository at this point in the history
…ce' and 'solo-profile-recent' into private-staging
  • Loading branch information
nanaya committed Aug 16, 2023
5 parents 38cb333 + 739822d + 4a6f752 + 06e15d3 + 4bbe38e commit 87319d3
Show file tree
Hide file tree
Showing 21 changed files with 476 additions and 631 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ CLIENT_CHECK_VERSION=false
# TWITCH_CLIENT_SECRET=

# SCORES_ES_CACHE_DURATION=
# SCORES_ES_ENABLE_LEGACY_FILTER=true
# SCORES_EXPERIMENTAL_RANK_AS_DEFAULT=false
# SCORES_EXPERIMENTAL_RANK_AS_EXTRA=false
# SCORES_RANK_CACHE_LOCAL_SERVER=0
Expand Down
231 changes: 95 additions & 136 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 @@ -24,6 +26,17 @@ class BeatmapsController extends Controller
const DEFAULT_API_INCLUDES = ['beatmapset.ratings', 'failtimes', 'max_combo'];
const DEFAULT_SCORE_INCLUDES = ['user', 'user.country', 'user.cover'];

private static function assertSupporterOnlyOptions(?User $currentUser, string $type, array $mods): void
{
$isSupporter = $currentUser?->isSupporter() ?? false;
if ($type !== 'global' && !$isSupporter) {
throw new InvariantException(osu_trans('errors.supporter_only'));
}
if (!empty($mods) && !is_api_request() && !$isSupporter) {
throw new InvariantException(osu_trans('errors.supporter_only'));
}
}

public function __construct()
{
parent::__construct();
Expand Down Expand Up @@ -247,7 +260,7 @@ public function show($id)
}

/**
* Get Beatmap scores
* Get Beatmap scores (legacy)
*
* Returns the top scores for a beatmap
*
Expand All @@ -265,61 +278,14 @@ 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();

$this->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 $this->beatmapScores($id, null, true);
}

/**
* 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 @@ -334,62 +300,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();

$this->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 $this->beatmapScores($id, ScoreTransformer::TYPE_SOLO, false);
}

public function updateOwner($id)
Expand Down Expand Up @@ -450,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 @@ -487,41 +410,77 @@ 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()),
];
}

private static function baseScoreQuery(Beatmap $beatmap, $mode, $mods, $type = null)
private function beatmapScores(string $id, ?string $scoreTransformerType, bool $isLegacy): array
{
$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()]);
$beatmap = Beatmap::findOrFail($id);
if ($beatmap->approved <= 0) {
return ['scores' => []];
}

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

private function assertSupporterOnlyOptions(?User $currentUser, string $type, array $mods): void
{
$isSupporter = $currentUser?->isSupporter() ?? false;
if ($type !== 'global' && !$isSupporter) {
throw new InvariantException(osu_trans('errors.supporter_only'));
if ($params['mode'] !== null) {
$rulesetId = Beatmap::MODES[$params['mode']] ?? null;
if ($rulesetId === null) {
throw new InvariantException('invalid mode specified');
}
}
if (!empty($mods) && !is_api_request() && !$isSupporter) {
throw new InvariantException(osu_trans('errors.supporter_only'));
$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', 'performance', '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;
}
}
24 changes: 18 additions & 6 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use App\Models\Country;
use App\Models\IpBan;
use App\Models\Log;
use App\Models\Solo\Score as SoloScore;
use App\Models\Solo\ScoreLegacyIdMap;
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 Down Expand Up @@ -786,9 +788,16 @@ private function getExtra($page, array $options, int $perPage = 10, int $offset
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'));
$soloMappingQuery = ScoreLegacyIdMap
::where('ruleset_id', Beatmap::MODES[$this->mode])
->whereIn('old_score_id', $userFirstsQuery)
->select('score_id');
$query = SoloScore
::whereIn('id', $soloMappingQuery)
->default()
->reorderBy('id', 'desc')
->with(ScoreTransformer::USER_PROFILE_INCLUDES_PRELOAD);
$userRelationColumn = 'user';
break;
Expand All @@ -807,9 +816,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
4 changes: 1 addition & 3 deletions app/Libraries/Elasticsearch/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ abstract class Search extends HasSearch implements Queryable
/**
* A tag to use when logging timing of fetches.
* FIXME: context-based tagging would be nicer.
*
* @var string|null
*/
public $loggingTag;
public ?string $loggingTag;

protected $aggregations;
protected $index;
Expand Down
8 changes: 6 additions & 2 deletions app/Libraries/Score/FetchDedupedScores.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ class FetchDedupedScores
private int $limit;
private array $result;

public function __construct(private string $dedupeColumn, private ScoreSearchParams $params)
{
public function __construct(
private string $dedupeColumn,
private ScoreSearchParams $params,
private ?string $searchLoggingTag = null
) {
$this->limit = $this->params->size;
}

public function all(): array
{
$this->params->size = $this->limit + 50;
$search = new ScoreSearch($this->params);
$search->loggingTag = $this->searchLoggingTag;

$nextCursor = null;
$hasNext = true;
Expand Down
Loading

0 comments on commit 87319d3

Please sign in to comment.