Skip to content

Commit

Permalink
Update to new mod format
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed Nov 13, 2024
1 parent b6fe56f commit 8634dfd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
16 changes: 11 additions & 5 deletions Difficalcy.PerformancePlus.Tests/OsuCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Difficalcy.Models;
using Difficalcy.PerformancePlus.Models;
using Difficalcy.PerformancePlus.Services;
using Difficalcy.Services;
Expand All @@ -20,18 +21,23 @@ public OsuCalculatorServiceTest()
protected override CalculatorService<OsuScore, OsuDifficulty, OsuPerformance, OsuCalculation> CalculatorService { get; }

[Theory]
[InlineData(6.578701261037768d, 288.6125590551904d, "diffcalc-test", 0)]
[InlineData(8.8180306947868328d, 722.9095478161727d, "diffcalc-test", 64)]
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, int mods)
=> TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new OsuScore { BeatmapId = beatmapId, Mods = mods });
[InlineData(6.578701261037768d, 288.6125590551904d, "diffcalc-test", new string[] { })]
[InlineData(8.8180306947868328d, 722.9095478161727d, "diffcalc-test", new string[] { "DT" })]
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, string[] mods)
=> TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new OsuScore { BeatmapId = beatmapId, Mods = mods.Select(m => new Mod { Acronym = m }).ToArray() });

[Fact]
public void TestAllParameters()
{
var score = new OsuScore
{
BeatmapId = "diffcalc-test",
Mods = 1112, // HD, HR, DT, FL
Mods = [
new Mod() { Acronym = "HD" },
new Mod() { Acronym = "HR" },
new Mod() { Acronym = "DT" },
new Mod() { Acronym = "FL" },
],
Combo = 200,
Misses = 5,
Mehs = 4,
Expand Down
19 changes: 15 additions & 4 deletions Difficalcy.PerformancePlus/Services/OsuCalculatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
using Difficalcy.Services;
using Microsoft.Extensions.Configuration;
using osu.Game.Beatmaps.Legacy;
using osu.Game.Online.API;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Difficulty;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using LazerMod = osu.Game.Rulesets.Mods.Mod;

namespace Difficalcy.PerformancePlus.Services
{
Expand Down Expand Up @@ -52,13 +54,13 @@ protected override async Task EnsureBeatmap(string beatmapId)
await beatmapProvider.EnsureBeatmap(beatmapId);
}

protected override (object, string) CalculateDifficultyAttributes(string beatmapId, int bitMods)
protected override (object, string) CalculateDifficultyAttributes(string beatmapId, Mod[] mods)
{
var workingBeatmap = GetWorkingBeatmap(beatmapId);
var mods = OsuRuleset.ConvertFromLegacyMods((LegacyMods)bitMods).ToArray();
var lazerMods = mods.Select(ModToLazerMod).ToArray();

var difficultyCalculator = OsuRuleset.CreateDifficultyCalculator(workingBeatmap);
var difficultyAttributes = difficultyCalculator.Calculate(mods) as OsuDifficultyAttributes;
var difficultyAttributes = difficultyCalculator.Calculate(lazerMods) as OsuDifficultyAttributes;

// Serialising anonymous object with same names because Mods and Skills can't be serialised
return (difficultyAttributes, JsonSerializer.Serialize(new
Expand Down Expand Up @@ -89,7 +91,7 @@ protected override OsuCalculation CalculatePerformance(OsuScore score, object di
var osuDifficultyAttributes = (OsuDifficultyAttributes)difficultyAttributes;

var workingBeatmap = GetWorkingBeatmap(score.BeatmapId);
var mods = OsuRuleset.ConvertFromLegacyMods((LegacyMods)score.Mods).ToArray();
var mods = score.Mods.Select(ModToLazerMod).ToArray();
var beatmap = workingBeatmap.GetPlayableBeatmap(OsuRuleset.RulesetInfo, mods);

var combo = score.Combo ?? beatmap.HitObjects.Count + beatmap.HitObjects.OfType<Slider>().Sum(s => s.NestedHitObjects.Count - 1);
Expand Down Expand Up @@ -122,6 +124,15 @@ private CalculatorWorkingBeatmap GetWorkingBeatmap(string beatmapId)
return new CalculatorWorkingBeatmap(OsuRuleset, beatmapStream);
}

private LazerMod ModToLazerMod(Mod mod)
{
var apiMod = new APIMod { Acronym = mod.Acronym };
foreach (var setting in mod.Settings)
apiMod.Settings.Add(setting.Key, setting.Value);

return apiMod.ToMod(OsuRuleset);
}

private static Dictionary<HitResult, int> GetHitResults(int hitResultCount, int countMiss, int countMeh, int countOk)
{
var countGreat = hitResultCount - countOk - countMeh - countMiss;
Expand Down

0 comments on commit 8634dfd

Please sign in to comment.