2022-01-19 07:33:33 +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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2022-01-23 03:01:30 +00:00
|
|
|
using System.Collections.Generic;
|
2022-01-19 07:33:33 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Rulesets.Mods;
|
2022-01-23 03:01:30 +00:00
|
|
|
using osu.Game.Rulesets.Objects;
|
2022-01-19 07:33:33 +00:00
|
|
|
using osu.Game.Rulesets.Scoring;
|
|
|
|
using osu.Game.Scoring;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Difficulty
|
|
|
|
{
|
|
|
|
public class PerformanceBreakdownCalculator
|
|
|
|
{
|
2022-02-05 13:36:34 +00:00
|
|
|
private readonly IBeatmap playableBeatmap;
|
2022-01-19 07:33:33 +00:00
|
|
|
private readonly BeatmapDifficultyCache difficultyCache;
|
|
|
|
private readonly ScorePerformanceCache performanceCache;
|
|
|
|
|
2022-02-05 13:36:34 +00:00
|
|
|
public PerformanceBreakdownCalculator(IBeatmap playableBeatmap, BeatmapDifficultyCache difficultyCache, ScorePerformanceCache performanceCache)
|
2022-01-19 07:33:33 +00:00
|
|
|
{
|
2022-02-05 13:36:34 +00:00
|
|
|
this.playableBeatmap = playableBeatmap;
|
2022-01-19 07:33:33 +00:00
|
|
|
this.difficultyCache = difficultyCache;
|
|
|
|
this.performanceCache = performanceCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
[ItemCanBeNull]
|
|
|
|
public async Task<PerformanceBreakdown> CalculateAsync(ScoreInfo score, CancellationToken cancellationToken = default)
|
|
|
|
{
|
2022-01-20 04:50:28 +00:00
|
|
|
PerformanceAttributes[] performanceArray = await Task.WhenAll(
|
|
|
|
// compute actual performance
|
|
|
|
performanceCache.CalculatePerformanceAsync(score, cancellationToken),
|
|
|
|
// compute performance for perfect play
|
|
|
|
getPerfectPerformance(score, cancellationToken)
|
|
|
|
).ConfigureAwait(false);
|
2022-01-19 07:33:33 +00:00
|
|
|
|
2022-01-20 04:50:28 +00:00
|
|
|
return new PerformanceBreakdown { Performance = performanceArray[0], PerfectPerformance = performanceArray[1] };
|
|
|
|
}
|
|
|
|
|
|
|
|
[ItemCanBeNull]
|
2022-01-20 05:06:00 +00:00
|
|
|
private Task<PerformanceAttributes> getPerfectPerformance(ScoreInfo score, CancellationToken cancellationToken = default)
|
2022-01-19 07:33:33 +00:00
|
|
|
{
|
2022-01-20 04:50:28 +00:00
|
|
|
return Task.Run(async () =>
|
2022-01-19 07:33:33 +00:00
|
|
|
{
|
2022-01-20 05:06:00 +00:00
|
|
|
Ruleset ruleset = score.Ruleset.CreateInstance();
|
2022-01-19 07:33:33 +00:00
|
|
|
ScoreInfo perfectPlay = score.DeepClone();
|
|
|
|
perfectPlay.Accuracy = 1;
|
|
|
|
perfectPlay.Passed = true;
|
|
|
|
|
|
|
|
// calculate max combo
|
2022-02-06 03:22:12 +00:00
|
|
|
// todo: Get max combo from difficulty calculator instead when diffcalc properly supports lazer-first scores
|
2022-02-06 02:59:53 +00:00
|
|
|
perfectPlay.MaxCombo = calculateMaxCombo(playableBeatmap);
|
2022-01-19 07:33:33 +00:00
|
|
|
|
|
|
|
// create statistics assuming all hit objects have perfect hit result
|
2022-02-05 13:36:34 +00:00
|
|
|
var statistics = playableBeatmap.HitObjects
|
|
|
|
.SelectMany(getPerfectHitResults)
|
|
|
|
.GroupBy(hr => hr, (hr, list) => (hitResult: hr, count: list.Count()))
|
|
|
|
.ToDictionary(pair => pair.hitResult, pair => pair.count);
|
2022-01-19 07:33:33 +00:00
|
|
|
perfectPlay.Statistics = statistics;
|
2023-05-18 08:47:25 +00:00
|
|
|
perfectPlay.MaximumStatistics = statistics;
|
2022-01-19 07:33:33 +00:00
|
|
|
|
|
|
|
// calculate total score
|
2022-01-20 05:06:00 +00:00
|
|
|
ScoreProcessor scoreProcessor = ruleset.CreateScoreProcessor();
|
2022-01-19 08:08:45 +00:00
|
|
|
scoreProcessor.Mods.Value = perfectPlay.Mods;
|
2023-05-18 08:47:25 +00:00
|
|
|
scoreProcessor.ApplyBeatmap(playableBeatmap);
|
2023-05-19 05:37:26 +00:00
|
|
|
perfectPlay.TotalScore = scoreProcessor.MaximumTotalScore;
|
2022-01-19 07:33:33 +00:00
|
|
|
|
|
|
|
// compute rank achieved
|
|
|
|
// default to SS, then adjust the rank with mods
|
|
|
|
perfectPlay.Rank = ScoreRank.X;
|
|
|
|
|
|
|
|
foreach (IApplicableToScoreProcessor mod in perfectPlay.Mods.OfType<IApplicableToScoreProcessor>())
|
|
|
|
{
|
|
|
|
perfectPlay.Rank = mod.AdjustRank(perfectPlay.Rank, 1);
|
|
|
|
}
|
|
|
|
|
2022-01-20 05:06:00 +00:00
|
|
|
// calculate performance for this perfect score
|
2022-02-06 02:59:53 +00:00
|
|
|
var difficulty = await difficultyCache.GetDifficultyAsync(
|
|
|
|
playableBeatmap.BeatmapInfo,
|
|
|
|
score.Ruleset,
|
|
|
|
score.Mods,
|
|
|
|
cancellationToken
|
|
|
|
).ConfigureAwait(false);
|
|
|
|
|
2022-01-20 05:06:00 +00:00
|
|
|
// ScorePerformanceCache is not used to avoid caching multiple copies of essentially identical perfect performance attributes
|
2022-03-14 05:25:26 +00:00
|
|
|
return difficulty == null ? null : ruleset.CreatePerformanceCalculator()?.Calculate(perfectPlay, difficulty.Value.Attributes);
|
2022-01-19 07:33:33 +00:00
|
|
|
}, cancellationToken);
|
|
|
|
}
|
2022-01-23 03:01:30 +00:00
|
|
|
|
2022-02-06 02:59:53 +00:00
|
|
|
private int calculateMaxCombo(IBeatmap beatmap)
|
|
|
|
{
|
|
|
|
return beatmap.HitObjects.SelectMany(getPerfectHitResults).Count(r => r.AffectsCombo());
|
|
|
|
}
|
|
|
|
|
2022-01-23 03:01:30 +00:00
|
|
|
private IEnumerable<HitResult> getPerfectHitResults(HitObject hitObject)
|
|
|
|
{
|
|
|
|
foreach (HitObject nested in hitObject.NestedHitObjects)
|
|
|
|
yield return nested.CreateJudgement().MaxResult;
|
|
|
|
|
|
|
|
yield return hitObject.CreateJudgement().MaxResult;
|
|
|
|
}
|
2022-01-19 07:33:33 +00:00
|
|
|
}
|
|
|
|
}
|