Skip to content

Commit

Permalink
Use solo scores for profile page recent scores
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Aug 4, 2023
1 parent 16ece1e commit 4bbe38e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
9 changes: 6 additions & 3 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,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 @@ -816,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
12 changes: 12 additions & 0 deletions app/Models/Solo/Score.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ public function scopeDefault(Builder $query): Builder
return $query->whereHas('beatmap');
}

public function scopeForRuleset(Builder $query, string $ruleset): Builder
{
return $query->where('ruleset_id', Beatmap::MODES[$ruleset]);
}

public function scopeIncludeFails(Builder $query, bool $includeFails): Builder
{
return $includeFails
? $query
: $query->where('data->passed', true);
}

/**
* This should match the one used in osu-elastic-indexer.
*/
Expand Down
18 changes: 18 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ public function getAttribute($key)
'scoresMania',
'scoresOsu',
'scoresTaiko',
'soloScores',
'statisticsFruits',
'statisticsMania',
'statisticsOsu',
Expand Down Expand Up @@ -1427,6 +1428,11 @@ public function scoresBest(string $mode, bool $returnQuery = false)
return $returnQuery ? $this->$relation() : $this->$relation;
}

public function soloScores(): HasMany
{
return $this->hasMany(Solo\Score::class);
}

public function topicWatches()
{
return $this->hasMany(TopicWatch::class);
Expand Down Expand Up @@ -1776,6 +1782,18 @@ public function authHash(): string
return hash('sha256', $this->user_email).':'.hash('sha256', $this->user_password);
}

public function recentScoreCount(string $ruleset): int
{
return $this->soloScores()
->default()
->forRuleset($ruleset)
->includeFails(false)
->select('id')
->limit(100)
->get()
->count();
}

public function resetSessions(?string $excludedSessionId = null): void
{
SessionStore::destroy($this->getKey(), $excludedSessionId);
Expand Down
2 changes: 1 addition & 1 deletion app/Transformers/UserCompactTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public function includeScoresPinnedCount(User $user)

public function includeScoresRecentCount(User $user)
{
return $this->primitive($user->scores($this->mode, true)->includeFails(false)->count());
return $this->primitive($user->recentScoreCount($this->mode));
}

public function includeStatistics(User $user)
Expand Down

0 comments on commit 4bbe38e

Please sign in to comment.