Split performance calculation to its own class.

This commit is contained in:
Lucas A 2020-09-27 12:44:29 +02:00
parent 3cb9103fe0
commit ddede85704
4 changed files with 47 additions and 26 deletions

View File

@ -16,7 +16,6 @@ using osu.Framework.Lists;
using osu.Framework.Threading;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Scoring;
namespace osu.Game.Beatmaps
{
@ -115,26 +114,6 @@ namespace osu.Game.Beatmaps
return computeDifficulty(key, beatmapInfo, rulesetInfo);
}
/// <summary>
/// Calculates performance for the given <see cref="ScoreInfo"/> on a given <see cref="WorkingBeatmap"/>.
/// </summary>
/// <param name="beatmap">The <see cref="WorkingBeatmap"/> to do the calculation on. </param>
/// <param name="score">The score to do the calculation on. </param>
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
public async Task<double> CalculatePerformance([NotNull] WorkingBeatmap beatmap, [NotNull] ScoreInfo score, CancellationToken token = default)
{
return await Task.Factory.StartNew(() =>
{
if (token.IsCancellationRequested)
return default;
var calculator = score.Ruleset.CreateInstance().CreatePerformanceCalculator(beatmap, score);
var total = calculator.Calculate();
return total;
}, token, TaskCreationOptions.HideScheduler | TaskCreationOptions.RunContinuationsAsynchronously, updateScheduler);
}
private CancellationTokenSource trackedUpdateCancellationSource;
private readonly List<CancellationTokenSource> linkedCancellationSources = new List<CancellationTokenSource>();

View File

@ -58,6 +58,8 @@ namespace osu.Game
protected ScoreManager ScoreManager;
protected ScorePerformanceManager ScorePerformanceManager;
protected BeatmapDifficultyManager DifficultyManager;
protected SkinManager SkinManager;
@ -226,6 +228,9 @@ namespace osu.Game
dependencies.Cache(DifficultyManager = new BeatmapDifficultyManager());
AddInternal(DifficultyManager);
dependencies.Cache(ScorePerformanceManager = new ScorePerformanceManager());
AddInternal(ScorePerformanceManager);
dependencies.Cache(KeyBindingStore = new KeyBindingStore(contextFactory, RulesetStore));
dependencies.Cache(SettingsStore = new SettingsStore(contextFactory));
dependencies.Cache(RulesetConfigCache = new RulesetConfigCache(SettingsStore));

View File

@ -0,0 +1,39 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
namespace osu.Game.Scoring
{
public class ScorePerformanceManager : Component
{
[Resolved]
private BeatmapManager beatmapManager { get; set; }
/// <summary>
/// Calculates performance for the given <see cref="ScoreInfo"/>.
/// </summary>
/// <param name="score">The score to do the calculation on. </param>
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
public async Task<double> CalculatePerformanceAsync([NotNull] ScoreInfo score, CancellationToken token = default)
{
return await Task.Factory.StartNew(() =>
{
if (token.IsCancellationRequested)
return default;
var beatmap = beatmapManager.GetWorkingBeatmap(score.Beatmap);
var calculator = score.Ruleset.CreateInstance().CreatePerformanceCalculator(beatmap, score);
var total = calculator.Calculate();
return total;
}, token);
}
}
}

View File

@ -4,7 +4,6 @@
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
using osu.Game.Scoring;
namespace osu.Game.Screens.Ranking.Expanded.Statistics
@ -24,7 +23,7 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
}
[BackgroundDependencyLoader]
private void load(BeatmapManager beatmapManager, BeatmapDifficultyManager difficultyManager)
private void load(ScorePerformanceManager performanceManager)
{
if (score.PP.HasValue)
{
@ -32,8 +31,7 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
}
else
{
var beatmap = beatmapManager.GetWorkingBeatmap(score.Beatmap);
difficultyManager.CalculatePerformance(beatmap, score, cancellationTokenSource.Token)
performanceManager.CalculatePerformanceAsync(score, cancellationTokenSource.Token)
.ContinueWith(t => Schedule(() => performance.Value = (int)t.Result), cancellationTokenSource.Token);
}
}