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;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Scoring
|
|
|
|
|
{
|
2020-11-02 05:50:44 +00:00
|
|
|
|
/// <summary>
|
2020-11-06 04:15:33 +00:00
|
|
|
|
/// A component which performs and acts as a central cache for performance calculations of locally databased scores.
|
|
|
|
|
/// Currently not persisted between game sessions.
|
2020-11-02 05:50:44 +00:00
|
|
|
|
/// </summary>
|
2020-11-06 04:15:33 +00:00
|
|
|
|
public class ScorePerformanceCache : Component
|
2020-09-27 10:44:29 +00:00
|
|
|
|
{
|
2020-11-02 05:51:19 +00:00
|
|
|
|
// this cache will grow indefinitely per session and should be considered temporary.
|
|
|
|
|
// this whole component should likely be replaced with database persistence.
|
2020-09-28 17:04:39 +00:00
|
|
|
|
private readonly ConcurrentDictionary<PerformanceCacheLookup, double> performanceCache = new ConcurrentDictionary<PerformanceCacheLookup, double>();
|
|
|
|
|
|
2020-09-29 16:32:02 +00:00
|
|
|
|
[Resolved]
|
2020-11-06 04:14:23 +00:00
|
|
|
|
private BeatmapDifficultyCache difficultyCache { get; set; }
|
2020-09-29 16:32:02 +00:00
|
|
|
|
|
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>
|
2020-10-27 12:45:21 +00:00
|
|
|
|
public Task<double?> CalculatePerformanceAsync([NotNull] ScoreInfo score, CancellationToken token = default)
|
2020-09-27 10:44:29 +00:00
|
|
|
|
{
|
2020-10-12 20:10:02 +00:00
|
|
|
|
var lookupKey = new PerformanceCacheLookup(score);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
2020-10-12 20:10:02 +00:00
|
|
|
|
if (performanceCache.TryGetValue(lookupKey, out double performance))
|
2020-10-27 12:45:21 +00:00
|
|
|
|
return Task.FromResult((double?)performance);
|
2020-09-27 10:44:29 +00:00
|
|
|
|
|
2020-10-27 12:45:21 +00:00
|
|
|
|
return computePerformanceAsync(score, lookupKey, token);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
}
|
2020-09-27 10:44:29 +00:00
|
|
|
|
|
2020-10-10 17:16:21 +00:00
|
|
|
|
private async Task<double?> computePerformanceAsync(ScoreInfo score, PerformanceCacheLookup lookupKey, CancellationToken token = default)
|
2020-09-28 17:04:39 +00:00
|
|
|
|
{
|
2020-11-06 04:14:23 +00:00
|
|
|
|
var attributes = await difficultyCache.GetDifficultyAsync(score.Beatmap, score.Ruleset, score.Mods, token);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
2020-10-09 16:32:03 +00:00
|
|
|
|
// Performance calculation requires the beatmap and ruleset to be locally available. If not, return a default value.
|
|
|
|
|
if (attributes.Attributes == null)
|
2020-10-10 17:16:21 +00:00
|
|
|
|
return null;
|
2020-10-09 16:32:03 +00:00
|
|
|
|
|
2020-10-10 17:16:21 +00:00
|
|
|
|
token.ThrowIfCancellationRequested();
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
2020-10-07 12:10:25 +00:00
|
|
|
|
var calculator = score.Ruleset.CreateInstance().CreatePerformanceCalculator(attributes.Attributes, score);
|
2020-10-10 17:16:21 +00:00
|
|
|
|
var total = calculator?.Calculate();
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
2020-10-10 17:16:21 +00:00
|
|
|
|
if (total.HasValue)
|
|
|
|
|
performanceCache[lookupKey] = total.Value;
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public readonly struct PerformanceCacheLookup
|
|
|
|
|
{
|
2020-10-08 16:31:29 +00:00
|
|
|
|
public readonly string ScoreHash;
|
2020-10-10 17:19:24 +00:00
|
|
|
|
public readonly int LocalScoreID;
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
|
|
|
|
public PerformanceCacheLookup(ScoreInfo info)
|
|
|
|
|
{
|
2020-10-08 16:31:29 +00:00
|
|
|
|
ScoreHash = info.Hash;
|
2020-10-10 17:19:24 +00:00
|
|
|
|
LocalScoreID = info.ID;
|
2020-09-28 17:04:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
var hash = new HashCode();
|
|
|
|
|
|
2020-10-08 16:31:29 +00:00
|
|
|
|
hash.Add(ScoreHash);
|
2020-10-10 17:19:24 +00:00
|
|
|
|
hash.Add(LocalScoreID);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
|
|
|
|
return hash.ToHashCode();
|
|
|
|
|
}
|
2020-09-27 10:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|