2020-09-27 18:44:29 +08: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-29 01:04:39 +08:00
|
|
|
|
using System;
|
2020-09-27 18:44:29 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Game.Beatmaps;
|
2020-11-06 12:26:18 +08:00
|
|
|
|
using osu.Game.Database;
|
2020-09-27 18:44:29 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Scoring
|
|
|
|
|
{
|
2020-11-02 13:50:44 +08:00
|
|
|
|
/// <summary>
|
2020-11-06 12:15:33 +08: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 13:50:44 +08:00
|
|
|
|
/// </summary>
|
2020-11-06 12:50:51 +08:00
|
|
|
|
public class ScorePerformanceCache : MemoryCachingComponent<ScorePerformanceCache.PerformanceCacheLookup, double?>
|
2020-09-27 18:44:29 +08:00
|
|
|
|
{
|
2020-09-30 00:32:02 +08:00
|
|
|
|
[Resolved]
|
2020-11-06 12:14:23 +08:00
|
|
|
|
private BeatmapDifficultyCache difficultyCache { get; set; }
|
2020-09-30 00:32:02 +08:00
|
|
|
|
|
2020-11-06 12:50:51 +08:00
|
|
|
|
protected override bool CacheNullValues => false;
|
|
|
|
|
|
2020-09-27 18:44:29 +08: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-11-06 12:50:51 +08:00
|
|
|
|
public Task<double?> CalculatePerformanceAsync([NotNull] ScoreInfo score, CancellationToken token = default) =>
|
|
|
|
|
GetAsync(new PerformanceCacheLookup(score), token);
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
2020-11-06 12:50:51 +08:00
|
|
|
|
protected override async Task<double?> ComputeValueAsync(PerformanceCacheLookup lookup, CancellationToken token = default)
|
2020-09-29 01:04:39 +08:00
|
|
|
|
{
|
2020-11-06 12:50:51 +08:00
|
|
|
|
var score = lookup.ScoreInfo;
|
|
|
|
|
|
2021-10-04 16:35:53 +08:00
|
|
|
|
var attributes = await difficultyCache.GetDifficultyAsync(score.BeatmapInfo, score.Ruleset, score.Mods, token).ConfigureAwait(false);
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
2020-10-10 00:32:03 +08:00
|
|
|
|
// Performance calculation requires the beatmap and ruleset to be locally available. If not, return a default value.
|
2021-11-20 23:54:58 +08:00
|
|
|
|
if (attributes?.Attributes == null)
|
2020-10-11 01:16:21 +08:00
|
|
|
|
return null;
|
2020-10-10 00:32:03 +08:00
|
|
|
|
|
2020-10-11 01:16:21 +08:00
|
|
|
|
token.ThrowIfCancellationRequested();
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
2021-11-20 23:54:58 +08:00
|
|
|
|
var calculator = score.Ruleset.CreateInstance().CreatePerformanceCalculator(attributes.Value.Attributes, score);
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
2020-11-06 12:50:51 +08:00
|
|
|
|
return calculator?.Calculate();
|
2020-09-29 01:04:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public readonly struct PerformanceCacheLookup
|
|
|
|
|
{
|
2020-11-06 12:50:51 +08:00
|
|
|
|
public readonly ScoreInfo ScoreInfo;
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
|
|
|
|
public PerformanceCacheLookup(ScoreInfo info)
|
|
|
|
|
{
|
2020-11-06 12:50:51 +08:00
|
|
|
|
ScoreInfo = info;
|
2020-09-29 01:04:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
var hash = new HashCode();
|
|
|
|
|
|
2020-11-06 12:50:51 +08:00
|
|
|
|
hash.Add(ScoreInfo.Hash);
|
|
|
|
|
hash.Add(ScoreInfo.ID);
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
|
|
|
|
return hash.ToHashCode();
|
|
|
|
|
}
|
2020-09-27 18:44:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|