Skip to content

Commit

Permalink
Refactor to clean up calclation services a tad
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed Nov 23, 2024
1 parent 0f12054 commit def381f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 60 deletions.
36 changes: 20 additions & 16 deletions Difficalcy.Catch/Services/CatchCalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -98,7 +115,7 @@ object difficultyAttributes
+ beatmap
.HitObjects.OfType<JuiceStream>()
.SelectMany(j => j.NestedHitObjects)
.Count(h => !(h is TinyDroplet));
.Count(h => h is not TinyDroplet);
var statistics = GetHitResults(
beatmap,
score.Misses,
Expand All @@ -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)
Expand Down
33 changes: 19 additions & 14 deletions Difficalcy.Mania/Services/ManiaCalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
34 changes: 19 additions & 15 deletions Difficalcy.Osu/Services/OsuCalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 19 additions & 15 deletions Difficalcy.Taiko/Services/TaikoCalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down

0 comments on commit def381f

Please sign in to comment.