From def381f236334c22997927bb93c9eebc4e94f1ed Mon Sep 17 00:00:00 2001 From: Samuel Cattini-Schultz Date: Sat, 23 Nov 2024 14:58:06 +1100 Subject: [PATCH] Refactor to clean up calclation services a tad --- .../Services/CatchCalculatorService.cs | 36 ++++++++++--------- .../Services/ManiaCalculatorService.cs | 33 +++++++++-------- .../Services/OsuCalculatorService.cs | 34 ++++++++++-------- .../Services/TaikoCalculatorService.cs | 34 ++++++++++-------- 4 files changed, 77 insertions(+), 60 deletions(-) diff --git a/Difficalcy.Catch/Services/CatchCalculatorService.cs b/Difficalcy.Catch/Services/CatchCalculatorService.cs index 0bb008c..2b11f50 100644 --- a/Difficalcy.Catch/Services/CatchCalculatorService.cs +++ b/Difficalcy.Catch/Services/CatchCalculatorService.cs @@ -8,7 +8,6 @@ using Difficalcy.Models; using Difficalcy.Services; using osu.Game.Beatmaps; -using osu.Game.Beatmaps.Legacy; using osu.Game.Online.API; using osu.Game.Rulesets.Catch; using osu.Game.Rulesets.Catch.Difficulty; @@ -88,6 +87,24 @@ object difficultyAttributes { var catchDifficultyAttributes = (CatchDifficultyAttributes)difficultyAttributes; + var scoreInfo = GetScoreInfo(score); + + var performanceCalculator = CatchRuleset.CreatePerformanceCalculator(); + var performanceAttributes = + performanceCalculator.Calculate(scoreInfo, catchDifficultyAttributes) + as CatchPerformanceAttributes; + + return new CatchCalculation() + { + Difficulty = GetDifficultyFromDifficultyAttributes(catchDifficultyAttributes), + Performance = GetPerformanceFromPerformanceAttributes(performanceAttributes), + Accuracy = scoreInfo.Accuracy, + Combo = scoreInfo.MaxCombo, + }; + } + + private ScoreInfo GetScoreInfo(CatchScore score) + { var workingBeatmap = GetWorkingBeatmap(score.BeatmapId); var mods = score.Mods.Select(ModToLazerMod).ToArray(); var beatmap = workingBeatmap.GetPlayableBeatmap(CatchRuleset.RulesetInfo, mods); @@ -98,7 +115,7 @@ object difficultyAttributes + beatmap .HitObjects.OfType() .SelectMany(j => j.NestedHitObjects) - .Count(h => !(h is TinyDroplet)); + .Count(h => h is not TinyDroplet); var statistics = GetHitResults( beatmap, score.Misses, @@ -107,26 +124,13 @@ object difficultyAttributes ); var accuracy = CalculateAccuracy(statistics); - var scoreInfo = new ScoreInfo(beatmap.BeatmapInfo, CatchRuleset.RulesetInfo) + return new ScoreInfo(beatmap.BeatmapInfo, CatchRuleset.RulesetInfo) { Accuracy = accuracy, MaxCombo = combo, Statistics = statistics, Mods = mods, }; - - var performanceCalculator = CatchRuleset.CreatePerformanceCalculator(); - var performanceAttributes = - performanceCalculator.Calculate(scoreInfo, catchDifficultyAttributes) - as CatchPerformanceAttributes; - - return new CatchCalculation() - { - Difficulty = GetDifficultyFromDifficultyAttributes(catchDifficultyAttributes), - Performance = GetPerformanceFromPerformanceAttributes(performanceAttributes), - Accuracy = accuracy, - Combo = combo, - }; } private CalculatorWorkingBeatmap GetWorkingBeatmap(string beatmapId) diff --git a/Difficalcy.Mania/Services/ManiaCalculatorService.cs b/Difficalcy.Mania/Services/ManiaCalculatorService.cs index 0a30b93..956276c 100644 --- a/Difficalcy.Mania/Services/ManiaCalculatorService.cs +++ b/Difficalcy.Mania/Services/ManiaCalculatorService.cs @@ -7,7 +7,6 @@ using Difficalcy.Mania.Models; using Difficalcy.Models; using Difficalcy.Services; -using osu.Game.Beatmaps.Legacy; using osu.Game.Online.API; using osu.Game.Rulesets.Mania; using osu.Game.Rulesets.Mania.Difficulty; @@ -87,6 +86,24 @@ object difficultyAttributes ) { var maniaDifficultyAttributes = (ManiaDifficultyAttributes)difficultyAttributes; + + var scoreInfo = GetScoreInfo(score); + + var performanceCalculator = ManiaRuleset.CreatePerformanceCalculator(); + var performanceAttributes = + performanceCalculator.Calculate(scoreInfo, maniaDifficultyAttributes) + as ManiaPerformanceAttributes; + + return new ManiaCalculation() + { + Difficulty = GetDifficultyFromDifficultyAttributes(maniaDifficultyAttributes), + Performance = GetPerformanceFromPerformanceAttributes(performanceAttributes), + Accuracy = scoreInfo.Accuracy, + }; + } + + private ScoreInfo GetScoreInfo(ManiaScore score) + { var workingBeatmap = GetWorkingBeatmap(score.BeatmapId); var mods = score.Mods.Select(ModToLazerMod).ToArray(); var beatmap = workingBeatmap.GetPlayableBeatmap(ManiaRuleset.RulesetInfo, mods); @@ -103,25 +120,13 @@ object difficultyAttributes ); var accuracy = CalculateAccuracy(statistics); - var scoreInfo = new ScoreInfo(beatmap.BeatmapInfo, ManiaRuleset.RulesetInfo) + return new ScoreInfo(beatmap.BeatmapInfo, ManiaRuleset.RulesetInfo) { Accuracy = accuracy, MaxCombo = 0, Statistics = statistics, Mods = mods, }; - - var performanceCalculator = ManiaRuleset.CreatePerformanceCalculator(); - var performanceAttributes = - performanceCalculator.Calculate(scoreInfo, maniaDifficultyAttributes) - as ManiaPerformanceAttributes; - - return new ManiaCalculation() - { - Difficulty = GetDifficultyFromDifficultyAttributes(maniaDifficultyAttributes), - Performance = GetPerformanceFromPerformanceAttributes(performanceAttributes), - Accuracy = accuracy, - }; } private CalculatorWorkingBeatmap GetWorkingBeatmap(string beatmapId) diff --git a/Difficalcy.Osu/Services/OsuCalculatorService.cs b/Difficalcy.Osu/Services/OsuCalculatorService.cs index 474376d..39fde0a 100644 --- a/Difficalcy.Osu/Services/OsuCalculatorService.cs +++ b/Difficalcy.Osu/Services/OsuCalculatorService.cs @@ -6,7 +6,6 @@ using Difficalcy.Models; using Difficalcy.Osu.Models; using Difficalcy.Services; -using osu.Game.Beatmaps; using osu.Game.Online.API; using osu.Game.Rulesets.Osu; using osu.Game.Rulesets.Osu.Difficulty; @@ -99,6 +98,24 @@ object difficultyAttributes { var osuDifficultyAttributes = (OsuDifficultyAttributes)difficultyAttributes; + var scoreInfo = GetScoreInfo(score); + + var performanceCalculator = OsuRuleset.CreatePerformanceCalculator(); + var performanceAttributes = + performanceCalculator.Calculate(scoreInfo, osuDifficultyAttributes) + as OsuPerformanceAttributes; + + return new OsuCalculation() + { + Difficulty = GetDifficultyFromDifficultyAttributes(osuDifficultyAttributes), + Performance = GetPerformanceFromPerformanceAttributes(performanceAttributes), + Accuracy = scoreInfo.Accuracy, + Combo = scoreInfo.MaxCombo, + }; + } + + private ScoreInfo GetScoreInfo(OsuScore score) + { var workingBeatmap = GetWorkingBeatmap(score.BeatmapId); var mods = score.Mods.Select(ModToLazerMod).ToArray(); var beatmap = workingBeatmap.GetPlayableBeatmap(OsuRuleset.RulesetInfo, mods); @@ -143,26 +160,13 @@ object difficultyAttributes accuracy = CalculateLazerAccuracy(statistics, maxSliderTails, maxSliderTicks); } - var scoreInfo = new ScoreInfo(beatmap.BeatmapInfo, OsuRuleset.RulesetInfo) + return new ScoreInfo(beatmap.BeatmapInfo, OsuRuleset.RulesetInfo) { Accuracy = accuracy, MaxCombo = combo, Statistics = statistics, Mods = mods, }; - - var performanceCalculator = OsuRuleset.CreatePerformanceCalculator(); - var performanceAttributes = - performanceCalculator.Calculate(scoreInfo, osuDifficultyAttributes) - as OsuPerformanceAttributes; - - return new OsuCalculation() - { - Difficulty = GetDifficultyFromDifficultyAttributes(osuDifficultyAttributes), - Performance = GetPerformanceFromPerformanceAttributes(performanceAttributes), - Accuracy = accuracy, - Combo = combo, - }; } private CalculatorWorkingBeatmap GetWorkingBeatmap(string beatmapId) diff --git a/Difficalcy.Taiko/Services/TaikoCalculatorService.cs b/Difficalcy.Taiko/Services/TaikoCalculatorService.cs index dd9959e..6d5f20f 100644 --- a/Difficalcy.Taiko/Services/TaikoCalculatorService.cs +++ b/Difficalcy.Taiko/Services/TaikoCalculatorService.cs @@ -7,7 +7,6 @@ using Difficalcy.Models; using Difficalcy.Services; using Difficalcy.Taiko.Models; -using osu.Game.Beatmaps.Legacy; using osu.Game.Online.API; using osu.Game.Rulesets.Scoring; using osu.Game.Rulesets.Taiko; @@ -94,6 +93,24 @@ object difficultyAttributes { var taikoDifficultyAttributes = (TaikoDifficultyAttributes)difficultyAttributes; + var scoreInfo = GetScoreInfo(score); + + var performanceCalculator = TaikoRuleset.CreatePerformanceCalculator(); + var performanceAttributes = + performanceCalculator.Calculate(scoreInfo, taikoDifficultyAttributes) + as TaikoPerformanceAttributes; + + return new TaikoCalculation() + { + Difficulty = GetDifficultyFromDifficultyAttributes(taikoDifficultyAttributes), + Performance = GetPerformanceFromPerformanceAttributes(performanceAttributes), + Accuracy = scoreInfo.Accuracy, + Combo = scoreInfo.MaxCombo, + }; + } + + private ScoreInfo GetScoreInfo(TaikoScore score) + { var workingBeatmap = GetWorkingBeatmap(score.BeatmapId); var mods = score.Mods.Select(ModToLazerMod).ToArray(); var beatmap = workingBeatmap.GetPlayableBeatmap(TaikoRuleset.RulesetInfo, mods); @@ -103,26 +120,13 @@ object difficultyAttributes var statistics = GetHitResults(hitResultCount, score.Misses, score.Oks); var accuracy = CalculateAccuracy(statistics); - var scoreInfo = new ScoreInfo(beatmap.BeatmapInfo, TaikoRuleset.RulesetInfo) + return new ScoreInfo(beatmap.BeatmapInfo, TaikoRuleset.RulesetInfo) { Accuracy = accuracy, MaxCombo = combo, Statistics = statistics, Mods = mods, }; - - var performanceCalculator = TaikoRuleset.CreatePerformanceCalculator(); - var performanceAttributes = - performanceCalculator.Calculate(scoreInfo, taikoDifficultyAttributes) - as TaikoPerformanceAttributes; - - return new TaikoCalculation() - { - Difficulty = GetDifficultyFromDifficultyAttributes(taikoDifficultyAttributes), - Performance = GetPerformanceFromPerformanceAttributes(performanceAttributes), - Accuracy = accuracy, - Combo = combo, - }; } private CalculatorWorkingBeatmap GetWorkingBeatmap(string beatmapId)