Skip to content

Commit

Permalink
Update syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed Apr 29, 2024
1 parent 8c2f700 commit 73b10f2
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 97 deletions.
4 changes: 2 additions & 2 deletions Difficalcy.Catch.Tests/CatchCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CatchCalculatorServiceTest : CalculatorServiceTest<CatchScore, Catc
[InlineData(4.0505463516206195d, 164.5770866821372d, "diffcalc-test", 0)]
[InlineData(5.1696411260785498d, 291.43480971713944d, "diffcalc-test", 64)]
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, int mods)
=> base.TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new CatchScore { BeatmapId = beatmapId, Mods = mods });
=> TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new CatchScore { BeatmapId = beatmapId, Mods = mods });

[Fact]
public void TestAllParameters()
Expand All @@ -27,6 +27,6 @@ public void TestAllParameters()
LargeDroplets = 18,
SmallDroplets = 200,
};
base.TestGetCalculationReturnsCorrectValues(5.739025024925009d, 241.19384779497875d, score);
TestGetCalculationReturnsCorrectValues(5.739025024925009d, 241.19384779497875d, score);
}
}
36 changes: 15 additions & 21 deletions Difficalcy.Catch/Services/CatchCalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

namespace Difficalcy.Catch.Services
{
public class CatchCalculatorService : CalculatorService<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation>
public class CatchCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : CalculatorService<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation>(cache)
{
private readonly IBeatmapProvider _beatmapProvider;
private CatchRuleset CatchRuleset { get; } = new CatchRuleset();

public override CalculatorInfo Info
Expand All @@ -39,19 +38,14 @@ public override CalculatorInfo Info
}
}

public CatchCalculatorService(ICache cache, IBeatmapProvider beatmapProvider) : base(cache)
{
_beatmapProvider = beatmapProvider;
}

protected override async Task EnsureBeatmap(string beatmapId)
{
await _beatmapProvider.EnsureBeatmap(beatmapId);
await beatmapProvider.EnsureBeatmap(beatmapId);
}

protected override (object, string) CalculateDifficultyAttributes(CatchScore score)
{
var workingBeatmap = getWorkingBeatmap(score.BeatmapId);
var workingBeatmap = GetWorkingBeatmap(score.BeatmapId);
var mods = CatchRuleset.ConvertFromLegacyMods((LegacyMods)score.Mods).ToArray();

var difficultyCalculator = CatchRuleset.CreateDifficultyCalculator(workingBeatmap);
Expand All @@ -60,9 +54,9 @@ protected override (object, string) CalculateDifficultyAttributes(CatchScore sco
// Serialising anonymous object with same names because some properties can't be serialised, and the built-in JsonProperty fields aren't on all required fields
return (difficultyAttributes, JsonSerializer.Serialize(new
{
StarRating = difficultyAttributes.StarRating,
MaxCombo = difficultyAttributes.MaxCombo,
ApproachRate = difficultyAttributes.ApproachRate
difficultyAttributes.StarRating,
difficultyAttributes.MaxCombo,
difficultyAttributes.ApproachRate
}));
}

Expand All @@ -75,13 +69,13 @@ protected override CatchCalculation CalculatePerformance(CatchScore score, objec
{
var catchDifficultyAttributes = (CatchDifficultyAttributes)difficultyAttributes;

var workingBeatmap = getWorkingBeatmap(score.BeatmapId);
var workingBeatmap = GetWorkingBeatmap(score.BeatmapId);
var mods = CatchRuleset.ConvertFromLegacyMods((LegacyMods)score.Mods).ToArray();
var beatmap = workingBeatmap.GetPlayableBeatmap(CatchRuleset.RulesetInfo, mods);

var combo = score.Combo ?? beatmap.HitObjects.Count(h => h is Fruit) + beatmap.HitObjects.OfType<JuiceStream>().SelectMany(j => j.NestedHitObjects).Count(h => !(h is TinyDroplet));
var statistics = getHitResults(beatmap, score.Misses, score.LargeDroplets, score.SmallDroplets);
var accuracy = calculateAccuracy(statistics);
var statistics = GetHitResults(beatmap, score.Misses, score.LargeDroplets, score.SmallDroplets);
var accuracy = CalculateAccuracy(statistics);

var scoreInfo = new ScoreInfo(beatmap.BeatmapInfo, CatchRuleset.RulesetInfo)
{
Expand All @@ -103,13 +97,13 @@ protected override CatchCalculation CalculatePerformance(CatchScore score, objec
};
}

private CalculatorWorkingBeatmap getWorkingBeatmap(string beatmapId)
private CalculatorWorkingBeatmap GetWorkingBeatmap(string beatmapId)
{
using var beatmapStream = _beatmapProvider.GetBeatmapStream(beatmapId);
using var beatmapStream = beatmapProvider.GetBeatmapStream(beatmapId);
return new CalculatorWorkingBeatmap(CatchRuleset, beatmapStream, beatmapId);
}

private Dictionary<HitResult, int> getHitResults(IBeatmap beatmap, int countMiss, int? countDroplet, int? countTinyDroplet)
private static Dictionary<HitResult, int> GetHitResults(IBeatmap beatmap, int countMiss, int? countDroplet, int? countTinyDroplet)
{
var maxTinyDroplets = beatmap.HitObjects.OfType<JuiceStream>().Sum(s => s.NestedHitObjects.OfType<TinyDroplet>().Count());
var maxDroplets = beatmap.HitObjects.OfType<JuiceStream>().Sum(s => s.NestedHitObjects.OfType<Droplet>().Count()) - maxTinyDroplets;
Expand All @@ -135,23 +129,23 @@ private Dictionary<HitResult, int> getHitResults(IBeatmap beatmap, int countMiss
};
}

private double calculateAccuracy(Dictionary<HitResult, int> statistics)
private static double CalculateAccuracy(Dictionary<HitResult, int> statistics)
{
double hits = statistics[HitResult.Great] + statistics[HitResult.LargeTickHit] + statistics[HitResult.SmallTickHit];
double total = hits + statistics[HitResult.Miss] + statistics[HitResult.SmallTickMiss];

return hits / total;
}

private CatchDifficulty GetDifficultyFromDifficultyAttributes(CatchDifficultyAttributes difficultyAttributes)
private static CatchDifficulty GetDifficultyFromDifficultyAttributes(CatchDifficultyAttributes difficultyAttributes)
{
return new CatchDifficulty()
{
Total = difficultyAttributes.StarRating
};
}

private CatchPerformance GetPerformanceFromPerformanceAttributes(CatchPerformanceAttributes performanceAttributes)
private static CatchPerformance GetPerformanceFromPerformanceAttributes(CatchPerformanceAttributes performanceAttributes)
{
return new CatchPerformance()
{
Expand Down
4 changes: 2 additions & 2 deletions Difficalcy.Mania.Tests/ManiaCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ManiaCalculatorServiceTest : CalculatorServiceTest<ManiaScore, Mani
[InlineData(2.3493769750220914d, 45.76140071089439d, "diffcalc-test", 0)]
[InlineData(2.797245912537965d, 68.79984443279172d, "diffcalc-test", 64)]
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, int mods)
=> base.TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new ManiaScore { BeatmapId = beatmapId, Mods = mods });
=> TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new ManiaScore { BeatmapId = beatmapId, Mods = mods });

[Fact]
public void TestAllParameters()
Expand All @@ -28,6 +28,6 @@ public void TestAllParameters()
Goods = 2,
Greats = 1,
};
base.TestGetCalculationReturnsCorrectValues(2.797245912537965d, 43.17076331130473d, score);
TestGetCalculationReturnsCorrectValues(2.797245912537965d, 43.17076331130473d, score);
}
}
24 changes: 12 additions & 12 deletions Difficalcy.Mania/Services/ManiaCalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override async Task EnsureBeatmap(string beatmapId)

protected override (object, string) CalculateDifficultyAttributes(ManiaScore score)
{
var workingBeatmap = getWorkingBeatmap(score.BeatmapId);
var workingBeatmap = GetWorkingBeatmap(score.BeatmapId);
var mods = ManiaRuleset.ConvertFromLegacyMods((LegacyMods)score.Mods).ToArray();

var difficultyCalculator = ManiaRuleset.CreateDifficultyCalculator(workingBeatmap);
Expand All @@ -59,9 +59,9 @@ protected override (object, string) CalculateDifficultyAttributes(ManiaScore sco
// Serialising anonymous object with same names because some properties can't be serialised, and the built-in JsonProperty fields aren't on all required fields
return (difficultyAttributes, JsonSerializer.Serialize(new
{
StarRating = difficultyAttributes.StarRating,
MaxCombo = difficultyAttributes.MaxCombo,
GreatHitWindow = difficultyAttributes.GreatHitWindow
difficultyAttributes.StarRating,
difficultyAttributes.MaxCombo,
difficultyAttributes.GreatHitWindow
}));
}

Expand All @@ -73,14 +73,14 @@ protected override object DeserialiseDifficultyAttributes(string difficultyAttri
protected override ManiaCalculation CalculatePerformance(ManiaScore score, object difficultyAttributes)
{
var maniaDifficultyAttributes = (ManiaDifficultyAttributes)difficultyAttributes;
var workingBeatmap = getWorkingBeatmap(score.BeatmapId);
var workingBeatmap = GetWorkingBeatmap(score.BeatmapId);
var mods = ManiaRuleset.ConvertFromLegacyMods((LegacyMods)score.Mods).ToArray();
var beatmap = workingBeatmap.GetPlayableBeatmap(ManiaRuleset.RulesetInfo, mods);

var hitObjectCount = beatmap.HitObjects.Count;
var holdNoteTailCount = beatmap.HitObjects.OfType<HoldNote>().Count();
var statistics = getHitResults(hitObjectCount + holdNoteTailCount, score.Misses, score.Mehs, score.Oks, score.Goods, score.Greats);
var accuracy = calculateAccuracy(statistics);
var statistics = GetHitResults(hitObjectCount + holdNoteTailCount, score.Misses, score.Mehs, score.Oks, score.Goods, score.Greats);
var accuracy = CalculateAccuracy(statistics);

var scoreInfo = new ScoreInfo(beatmap.BeatmapInfo, ManiaRuleset.RulesetInfo)
{
Expand All @@ -101,13 +101,13 @@ protected override ManiaCalculation CalculatePerformance(ManiaScore score, objec
};
}

private CalculatorWorkingBeatmap getWorkingBeatmap(string beatmapId)
private CalculatorWorkingBeatmap GetWorkingBeatmap(string beatmapId)
{
using var beatmapStream = _beatmapProvider.GetBeatmapStream(beatmapId);
return new CalculatorWorkingBeatmap(ManiaRuleset, beatmapStream, beatmapId);
}

private Dictionary<HitResult, int> getHitResults(int hitResultCount, int countMiss, int countMeh, int countOk, int countGood, int countGreat)
private static Dictionary<HitResult, int> GetHitResults(int hitResultCount, int countMiss, int countMeh, int countOk, int countGood, int countGreat)
{
var countPerfect = hitResultCount - (countMiss + countMeh + countOk + countGood + countGreat);

Expand All @@ -122,7 +122,7 @@ private Dictionary<HitResult, int> getHitResults(int hitResultCount, int countMi
};
}

private double calculateAccuracy(Dictionary<HitResult, int> statistics)
private static double CalculateAccuracy(Dictionary<HitResult, int> statistics)
{
var countPerfect = statistics[HitResult.Perfect];
var countGreat = statistics[HitResult.Great];
Expand All @@ -135,15 +135,15 @@ private double calculateAccuracy(Dictionary<HitResult, int> statistics)
return (double)((6 * countPerfect) + (6 * countGreat) + (4 * countGood) + (2 * countOk) + countMeh) / (6 * total);
}

private ManiaDifficulty GetDifficultyFromDifficultyAttributes(ManiaDifficultyAttributes difficultyAttributes)
private static ManiaDifficulty GetDifficultyFromDifficultyAttributes(ManiaDifficultyAttributes difficultyAttributes)
{
return new ManiaDifficulty()
{
Total = difficultyAttributes.StarRating
};
}

private ManiaPerformance GetPerformanceFromPerformanceAttributes(ManiaPerformanceAttributes performanceAttributes)
private static ManiaPerformance GetPerformanceFromPerformanceAttributes(ManiaPerformanceAttributes performanceAttributes)
{
return new ManiaPerformance()
{
Expand Down
4 changes: 2 additions & 2 deletions Difficalcy.Osu.Tests/OsuCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class OsuCalculatorServiceTest : CalculatorServiceTest<OsuScore, OsuDiffi
[InlineData(6.710442985146793d, 288.27290484349686d, "diffcalc-test", 0)]
[InlineData(8.9742952703071666d, 710.7304138915342d, "diffcalc-test", 64)]
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, int mods)
=> base.TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new OsuScore { BeatmapId = beatmapId, Mods = mods });
=> TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new OsuScore { BeatmapId = beatmapId, Mods = mods });

[Fact]
public void TestAllParameters()
Expand All @@ -27,6 +27,6 @@ public void TestAllParameters()
Mehs = 4,
Oks = 3,
};
base.TestGetCalculationReturnsCorrectValues(10.07270907570737d, 553.1423675531603d, score);
TestGetCalculationReturnsCorrectValues(10.07270907570737d, 553.1423675531603d, score);
}
}
Loading

0 comments on commit 73b10f2

Please sign in to comment.