Skip to content

Commit

Permalink
Simplify nesting of OrderByTotalScoreAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Sep 7, 2021
1 parent 92f59c1 commit 3d8faea
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions osu.Game/Scoring/ScoreManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,20 @@ public async Task<ScoreInfo[]> OrderByTotalScoreAsync(ScoreInfo[] scores, Cancel
{
var difficultyCache = difficulties?.Invoke();

if (difficultyCache == null)
return orderByTotalScore(scores);

// Compute difficulties asynchronously first to prevent blocking via the GetTotalScore() call below.
foreach (var s in scores)
if (difficultyCache != null)
{
await difficultyCache.GetDifficultyAsync(s.Beatmap, s.Ruleset, s.Mods, cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
// Compute difficulties asynchronously first to prevent blocking via the GetTotalScore() call below.
foreach (var s in scores)
{
await difficultyCache.GetDifficultyAsync(s.Beatmap, s.Ruleset, s.Mods, cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
}
}

return orderByTotalScore(scores);

ScoreInfo[] orderByTotalScore(IEnumerable<ScoreInfo> incoming)
{
// We're calling .Result, but this should not be a blocking call due to the above GetDifficultyAsync() calls.
return incoming.OrderByDescending(s => GetTotalScoreAsync(s, cancellationToken: cancellationToken).Result)
.ThenBy(s => s.OnlineScoreID)
.ToArray();
}
// We're calling .Result, but this should not be a blocking call due to the above GetDifficultyAsync() calls.
return scores.OrderByDescending(s => GetTotalScoreAsync(s, cancellationToken: cancellationToken).Result)
.ThenBy(s => s.OnlineScoreID)
.ToArray();
}

/// <summary>
Expand Down

0 comments on commit 3d8faea

Please sign in to comment.