mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 11:37:28 +08:00
Merge pull request #10258 from Game4all/results-dynamic-pp-calc
This commit is contained in:
commit
ab9e0aac58
@ -19,7 +19,6 @@ using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Containers;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Rulesets;
|
||||
using osu.Game.Rulesets.Osu;
|
||||
using osu.Game.Rulesets.Osu.Mods;
|
||||
using osu.Game.Scoring;
|
||||
using osu.Game.Screens;
|
||||
@ -193,9 +192,9 @@ namespace osu.Game.Tests.Visual.Background
|
||||
|
||||
AddStep("Transition to Results", () => player.Push(results = new FadeAccessibleResults(new ScoreInfo
|
||||
{
|
||||
Ruleset = new OsuRuleset().RulesetInfo,
|
||||
User = new User { Username = "osu!" },
|
||||
Beatmap = new TestBeatmap(new OsuRuleset().RulesetInfo).BeatmapInfo
|
||||
Beatmap = new TestBeatmap(Ruleset.Value).BeatmapInfo,
|
||||
Ruleset = Ruleset.Value,
|
||||
})));
|
||||
|
||||
AddUntilStep("Wait for results is current", () => results.IsCurrentScreen());
|
||||
|
@ -17,6 +17,7 @@ using osu.Framework.Logging;
|
||||
using osu.Framework.Threading;
|
||||
using osu.Framework.Utils;
|
||||
using osu.Game.Rulesets;
|
||||
using osu.Game.Rulesets.Difficulty;
|
||||
using osu.Game.Rulesets.Mods;
|
||||
using osu.Game.Rulesets.UI;
|
||||
|
||||
@ -238,7 +239,7 @@ namespace osu.Game.Beatmaps
|
||||
var calculator = ruleset.CreateDifficultyCalculator(beatmapManager.GetWorkingBeatmap(beatmapInfo));
|
||||
var attributes = calculator.Calculate(key.Mods);
|
||||
|
||||
return difficultyCache[key] = new StarDifficulty(attributes.StarRating, attributes.MaxCombo);
|
||||
return difficultyCache[key] = new StarDifficulty(attributes);
|
||||
}
|
||||
catch (BeatmapInvalidForRulesetException e)
|
||||
{
|
||||
@ -346,17 +347,46 @@ namespace osu.Game.Beatmaps
|
||||
|
||||
public readonly struct StarDifficulty
|
||||
{
|
||||
/// <summary>
|
||||
/// The star difficulty rating for the given beatmap.
|
||||
/// </summary>
|
||||
public readonly double Stars;
|
||||
|
||||
/// <summary>
|
||||
/// The maximum combo achievable on the given beatmap.
|
||||
/// </summary>
|
||||
public readonly int MaxCombo;
|
||||
|
||||
public StarDifficulty(double stars, int maxCombo)
|
||||
{
|
||||
Stars = stars;
|
||||
MaxCombo = maxCombo;
|
||||
/// <summary>
|
||||
/// The difficulty attributes computed for the given beatmap.
|
||||
/// Might not be available if the star difficulty is associated with a beatmap that's not locally available.
|
||||
/// </summary>
|
||||
[CanBeNull]
|
||||
public readonly DifficultyAttributes Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="StarDifficulty"/> structure based on <see cref="DifficultyAttributes"/> computed
|
||||
/// by a <see cref="DifficultyCalculator"/>.
|
||||
/// </summary>
|
||||
public StarDifficulty([NotNull] DifficultyAttributes attributes)
|
||||
{
|
||||
Stars = attributes.StarRating;
|
||||
MaxCombo = attributes.MaxCombo;
|
||||
Attributes = attributes;
|
||||
// Todo: Add more members (BeatmapInfo.DifficultyRating? Attributes? Etc...)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="StarDifficulty"/> structure with a pre-populated star difficulty and max combo
|
||||
/// in scenarios where computing <see cref="DifficultyAttributes"/> is not feasible (i.e. when working with online sources).
|
||||
/// </summary>
|
||||
public StarDifficulty(double starDifficulty, int maxCombo)
|
||||
{
|
||||
Stars = starDifficulty;
|
||||
MaxCombo = maxCombo;
|
||||
Attributes = null;
|
||||
}
|
||||
|
||||
public DifficultyRating DifficultyRating => BeatmapDifficultyManager.GetDifficultyRating(Stars);
|
||||
}
|
||||
}
|
||||
|
@ -229,6 +229,10 @@ namespace osu.Game
|
||||
dependencies.Cache(DifficultyManager = new BeatmapDifficultyManager());
|
||||
AddInternal(DifficultyManager);
|
||||
|
||||
var scorePerformanceManager = new ScorePerformanceManager();
|
||||
dependencies.Cache(scorePerformanceManager);
|
||||
AddInternal(scorePerformanceManager);
|
||||
|
||||
dependencies.Cache(KeyBindingStore = new KeyBindingStore(contextFactory, RulesetStore));
|
||||
dependencies.Cache(SettingsStore = new SettingsStore(contextFactory));
|
||||
dependencies.Cache(RulesetConfigCache = new RulesetConfigCache(SettingsStore));
|
||||
|
83
osu.Game/Scoring/ScorePerformanceManager.cs
Normal file
83
osu.Game/Scoring/ScorePerformanceManager.cs
Normal file
@ -0,0 +1,83 @@
|
||||
// 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;
|
||||
using System.Collections.Concurrent;
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// A global component which calculates and caches results of performance calculations for locally databased scores.
|
||||
/// </summary>
|
||||
public class ScorePerformanceManager : Component
|
||||
{
|
||||
// this cache will grow indefinitely per session and should be considered temporary.
|
||||
// this whole component should likely be replaced with database persistence.
|
||||
private readonly ConcurrentDictionary<PerformanceCacheLookup, double> performanceCache = new ConcurrentDictionary<PerformanceCacheLookup, double>();
|
||||
|
||||
[Resolved]
|
||||
private BeatmapDifficultyManager difficultyManager { 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 Task<double?> CalculatePerformanceAsync([NotNull] ScoreInfo score, CancellationToken token = default)
|
||||
{
|
||||
var lookupKey = new PerformanceCacheLookup(score);
|
||||
|
||||
if (performanceCache.TryGetValue(lookupKey, out double performance))
|
||||
return Task.FromResult((double?)performance);
|
||||
|
||||
return computePerformanceAsync(score, lookupKey, token);
|
||||
}
|
||||
|
||||
private async Task<double?> computePerformanceAsync(ScoreInfo score, PerformanceCacheLookup lookupKey, CancellationToken token = default)
|
||||
{
|
||||
var attributes = await difficultyManager.GetDifficultyAsync(score.Beatmap, score.Ruleset, score.Mods, token);
|
||||
|
||||
// Performance calculation requires the beatmap and ruleset to be locally available. If not, return a default value.
|
||||
if (attributes.Attributes == null)
|
||||
return null;
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
var calculator = score.Ruleset.CreateInstance().CreatePerformanceCalculator(attributes.Attributes, score);
|
||||
var total = calculator?.Calculate();
|
||||
|
||||
if (total.HasValue)
|
||||
performanceCache[lookupKey] = total.Value;
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
public readonly struct PerformanceCacheLookup
|
||||
{
|
||||
public readonly string ScoreHash;
|
||||
public readonly int LocalScoreID;
|
||||
|
||||
public PerformanceCacheLookup(ScoreInfo info)
|
||||
{
|
||||
ScoreHash = info.Hash;
|
||||
LocalScoreID = info.ID;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
var hash = new HashCode();
|
||||
|
||||
hash.Add(ScoreHash);
|
||||
hash.Add(LocalScoreID);
|
||||
|
||||
return hash.ToHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -66,7 +66,7 @@ namespace osu.Game.Screens.Ranking.Expanded
|
||||
{
|
||||
new AccuracyStatistic(score.Accuracy),
|
||||
new ComboStatistic(score.MaxCombo, !score.Statistics.TryGetValue(HitResult.Miss, out var missCount) || missCount == 0),
|
||||
new CounterStatistic("pp", (int)(score.PP ?? 0)),
|
||||
new PerformanceStatistic(score),
|
||||
};
|
||||
|
||||
var bottomStatistics = new List<HitResultStatistic>();
|
||||
|
@ -6,7 +6,6 @@ using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using osu.Game.Screens.Ranking.Expanded.Accuracy;
|
||||
using osuTK;
|
||||
|
||||
namespace osu.Game.Screens.Ranking.Expanded.Statistics
|
||||
@ -46,7 +45,7 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
|
||||
{
|
||||
AutoSizeAxes = Axes.Both,
|
||||
Direction = FillDirection.Horizontal,
|
||||
Child = counter = new Counter
|
||||
Child = counter = new StatisticCounter
|
||||
{
|
||||
Anchor = Anchor.TopCentre,
|
||||
Origin = Anchor.TopCentre
|
||||
@ -67,18 +66,5 @@ namespace osu.Game.Screens.Ranking.Expanded.Statistics
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
private class Counter : RollingCounter<int>
|
||||
{
|
||||
protected override double RollingDuration => AccuracyCircle.ACCURACY_TRANSFORM_DURATION;
|
||||
|
||||
protected override Easing RollingEasing => AccuracyCircle.ACCURACY_TRANSFORM_EASING;
|
||||
|
||||
protected override OsuSpriteText CreateSpriteText() => base.CreateSpriteText().With(s =>
|
||||
{
|
||||
s.Font = OsuFont.Torus.With(size: 20, fixedWidth: true);
|
||||
s.Spacing = new Vector2(-2, 0);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,68 @@
|
||||
// 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;
|
||||
using System.Threading;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using osu.Game.Scoring;
|
||||
|
||||
namespace osu.Game.Screens.Ranking.Expanded.Statistics
|
||||
{
|
||||
public class PerformanceStatistic : StatisticDisplay
|
||||
{
|
||||
private readonly ScoreInfo score;
|
||||
|
||||
private readonly Bindable<int> performance = new Bindable<int>();
|
||||
|
||||
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
|
||||
|
||||
private RollingCounter<int> counter;
|
||||
|
||||
public PerformanceStatistic(ScoreInfo score)
|
||||
: base("PP")
|
||||
{
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(ScorePerformanceManager performanceManager)
|
||||
{
|
||||
if (score.PP.HasValue)
|
||||
{
|
||||
setPerformanceValue(score.PP.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
performanceManager.CalculatePerformanceAsync(score, cancellationTokenSource.Token)
|
||||
.ContinueWith(t => Schedule(() => setPerformanceValue(t.Result)), cancellationTokenSource.Token);
|
||||
}
|
||||
}
|
||||
|
||||
private void setPerformanceValue(double? pp)
|
||||
{
|
||||
if (pp.HasValue)
|
||||
performance.Value = (int)Math.Round(pp.Value, MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
public override void Appear()
|
||||
{
|
||||
base.Appear();
|
||||
counter.Current.BindTo(performance);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
cancellationTokenSource?.Cancel();
|
||||
base.Dispose(isDisposing);
|
||||
}
|
||||
|
||||
protected override Drawable CreateContent() => counter = new StatisticCounter
|
||||
{
|
||||
Anchor = Anchor.TopCentre,
|
||||
Origin = Anchor.TopCentre
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// 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 osu.Framework.Graphics;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Sprites;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using osu.Game.Screens.Ranking.Expanded.Accuracy;
|
||||
using osuTK;
|
||||
|
||||
namespace osu.Game.Screens.Ranking.Expanded.Statistics
|
||||
{
|
||||
public class StatisticCounter : RollingCounter<int>
|
||||
{
|
||||
protected override double RollingDuration => AccuracyCircle.ACCURACY_TRANSFORM_DURATION;
|
||||
|
||||
protected override Easing RollingEasing => AccuracyCircle.ACCURACY_TRANSFORM_EASING;
|
||||
|
||||
protected override OsuSpriteText CreateSpriteText() => base.CreateSpriteText().With(s =>
|
||||
{
|
||||
s.Font = OsuFont.Torus.With(size: 20, fixedWidth: true);
|
||||
s.Spacing = new Vector2(-2, 0);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user