2020-09-27 10:44:29 +00:00
|
|
|
|
// 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.
|
|
|
|
|
|
2020-09-28 17:04:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2020-09-27 10:44:29 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Game.Beatmaps;
|
2020-09-28 17:04:39 +00:00
|
|
|
|
using osu.Game.Rulesets.Mods;
|
2020-09-27 10:44:29 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Scoring
|
|
|
|
|
{
|
|
|
|
|
public class ScorePerformanceManager : Component
|
|
|
|
|
{
|
2020-09-28 17:04:39 +00:00
|
|
|
|
private readonly ConcurrentDictionary<PerformanceCacheLookup, double> performanceCache = new ConcurrentDictionary<PerformanceCacheLookup, double>();
|
|
|
|
|
|
2020-09-27 10:44:29 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private BeatmapManager beatmapManager { get; set; }
|
|
|
|
|
|
2020-09-29 16:32:02 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private BeatmapDifficultyManager difficultyManager { get; set; }
|
|
|
|
|
|
2020-09-27 10:44:29 +00:00
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2020-09-28 17:04:39 +00:00
|
|
|
|
if (tryGetExisting(score, out var perf, out var lookupKey))
|
|
|
|
|
return perf;
|
|
|
|
|
|
2020-09-29 16:32:02 +00:00
|
|
|
|
return await computePerformanceAsync(score, lookupKey, token);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
}
|
2020-09-27 10:44:29 +00:00
|
|
|
|
|
2020-09-28 17:04:39 +00:00
|
|
|
|
private bool tryGetExisting(ScoreInfo score, out double performance, out PerformanceCacheLookup lookupKey)
|
|
|
|
|
{
|
|
|
|
|
lookupKey = new PerformanceCacheLookup(score);
|
2020-09-27 10:44:29 +00:00
|
|
|
|
|
2020-09-28 17:04:39 +00:00
|
|
|
|
return performanceCache.TryGetValue(lookupKey, out performance);
|
|
|
|
|
}
|
2020-09-27 10:44:29 +00:00
|
|
|
|
|
2020-09-29 16:32:02 +00:00
|
|
|
|
private async Task<double> computePerformanceAsync(ScoreInfo score, PerformanceCacheLookup lookupKey, CancellationToken token = default)
|
2020-09-28 17:04:39 +00:00
|
|
|
|
{
|
|
|
|
|
var beatmap = beatmapManager.GetWorkingBeatmap(score.Beatmap);
|
2020-09-29 16:32:02 +00:00
|
|
|
|
var attributes = await difficultyManager.GetDifficultyAsync(score.Beatmap, score.Ruleset, score.Mods, token);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
return default;
|
|
|
|
|
|
2020-09-29 16:32:02 +00:00
|
|
|
|
var calculator = score.Ruleset.CreateInstance().CreatePerformanceCalculator(beatmap, score, attributes.Attributes);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
var total = calculator.Calculate();
|
|
|
|
|
|
|
|
|
|
performanceCache[lookupKey] = total;
|
|
|
|
|
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public readonly struct PerformanceCacheLookup
|
|
|
|
|
{
|
|
|
|
|
public readonly double Accuracy;
|
|
|
|
|
public readonly int BeatmapId;
|
|
|
|
|
public readonly long TotalScore;
|
|
|
|
|
public readonly int Combo;
|
|
|
|
|
public readonly Mod[] Mods;
|
|
|
|
|
public readonly int RulesetId;
|
|
|
|
|
|
|
|
|
|
public PerformanceCacheLookup(ScoreInfo info)
|
|
|
|
|
{
|
|
|
|
|
Accuracy = info.Accuracy;
|
|
|
|
|
BeatmapId = info.Beatmap.ID;
|
|
|
|
|
TotalScore = info.TotalScore;
|
|
|
|
|
Combo = info.Combo;
|
|
|
|
|
Mods = info.Mods;
|
2020-09-29 16:32:02 +00:00
|
|
|
|
RulesetId = info.Ruleset.ID ?? 0;
|
2020-09-28 17:04:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
var hash = new HashCode();
|
|
|
|
|
|
|
|
|
|
hash.Add(Accuracy);
|
|
|
|
|
hash.Add(BeatmapId);
|
|
|
|
|
hash.Add(TotalScore);
|
|
|
|
|
hash.Add(Combo);
|
|
|
|
|
hash.Add(RulesetId);
|
|
|
|
|
foreach (var mod in Mods)
|
|
|
|
|
hash.Add(mod);
|
|
|
|
|
|
|
|
|
|
return hash.ToHashCode();
|
|
|
|
|
}
|
2020-09-27 10:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|