From ddede857043f8a7a189cd69dcffde9503bccdf88 Mon Sep 17 00:00:00 2001 From: Lucas A Date: Sun, 27 Sep 2020 12:44:29 +0200 Subject: [PATCH] Split performance calculation to its own class. --- osu.Game/Beatmaps/BeatmapDifficultyManager.cs | 21 ---------- osu.Game/OsuGameBase.cs | 5 +++ osu.Game/Scoring/ScorePerformanceManager.cs | 39 +++++++++++++++++++ .../Statistics/PerformanceStatistic.cs | 8 ++-- 4 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 osu.Game/Scoring/ScorePerformanceManager.cs diff --git a/osu.Game/Beatmaps/BeatmapDifficultyManager.cs b/osu.Game/Beatmaps/BeatmapDifficultyManager.cs index 1acc4b290f..e9d26683c3 100644 --- a/osu.Game/Beatmaps/BeatmapDifficultyManager.cs +++ b/osu.Game/Beatmaps/BeatmapDifficultyManager.cs @@ -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); } - /// - /// Calculates performance for the given on a given . - /// - /// The to do the calculation on. - /// The score to do the calculation on. - /// An optional to cancel the operation. - public async Task 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 linkedCancellationSources = new List(); diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index b1269e9300..9a4710d576 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -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)); diff --git a/osu.Game/Scoring/ScorePerformanceManager.cs b/osu.Game/Scoring/ScorePerformanceManager.cs new file mode 100644 index 0000000000..c8fec3b40c --- /dev/null +++ b/osu.Game/Scoring/ScorePerformanceManager.cs @@ -0,0 +1,39 @@ +// Copyright (c) ppy Pty Ltd . 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; } + + /// + /// Calculates performance for the given . + /// + /// The score to do the calculation on. + /// An optional to cancel the operation. + public async Task 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); + } + } +} diff --git a/osu.Game/Screens/Ranking/Expanded/Statistics/PerformanceStatistic.cs b/osu.Game/Screens/Ranking/Expanded/Statistics/PerformanceStatistic.cs index b84d0b7ff7..e014258fd4 100644 --- a/osu.Game/Screens/Ranking/Expanded/Statistics/PerformanceStatistic.cs +++ b/osu.Game/Screens/Ranking/Expanded/Statistics/PerformanceStatistic.cs @@ -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,9 +31,8 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics } else { - var beatmap = beatmapManager.GetWorkingBeatmap(score.Beatmap); - difficultyManager.CalculatePerformance(beatmap, score, cancellationTokenSource.Token) - .ContinueWith(t => Schedule(() => performance.Value = (int)t.Result), cancellationTokenSource.Token); + performanceManager.CalculatePerformanceAsync(score, cancellationTokenSource.Token) + .ContinueWith(t => Schedule(() => performance.Value = (int)t.Result), cancellationTokenSource.Token); } }