Add abnormal difficulty settings checks

This commit is contained in:
Arthur Araujo 2024-03-20 09:31:58 -03:00
parent 8ee391530f
commit 4904c49c38
3 changed files with 179 additions and 1 deletions

View File

@ -0,0 +1,93 @@
// 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.
using osu.Game.Rulesets.Edit.Checks;
using NUnit.Framework;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Edit;
using osu.Game.Tests.Beatmaps;
using System.Linq;
namespace osu.Game.Tests.Editing.Checks
{
[TestFixture]
public class CheckAbnormalDifficultySettingsTest
{
private CheckAbnormalDifficultySettings check = null!;
private IBeatmap beatmap = new Beatmap<HitObject>();
[SetUp]
public void Setup()
{
check = new CheckAbnormalDifficultySettings();
beatmap.Difficulty = new();
}
[Test]
public void TestSettingsNormal()
{
var context = getContext();
var issues = check.Run(context).ToList();
Assert.That(issues, Has.Count.EqualTo(0));
}
[Test]
public void TestAllSettingsMoreThanOneDecimal()
{
beatmap.Difficulty = new()
{
ApproachRate = 5.55f,
OverallDifficulty = 7.7777f,
CircleSize = 4.444f,
DrainRate = 1.1111111111f,
};
var context = getContext();
var issues = check.Run(context).ToList();
Assert.That(issues, Has.Count.EqualTo(4));
}
[Test]
public void TestAllSettingsLessThanZero()
{
beatmap.Difficulty = new()
{
ApproachRate = -1,
OverallDifficulty = -20,
CircleSize = -11,
DrainRate = -34,
};
var context = getContext();
var issues = check.Run(context).ToList();
Assert.That(issues, Has.Count.EqualTo(4));
}
[Test]
public void TestAllSettingsHigherThanTen()
{
beatmap.Difficulty = new()
{
ApproachRate = 14,
OverallDifficulty = 24,
CircleSize = 30,
DrainRate = 90,
};
var context = getContext();
var issues = check.Run(context).ToList();
Assert.That(issues, Has.Count.EqualTo(4));
}
private BeatmapVerifierContext getContext()
{
return new BeatmapVerifierContext(beatmap, new TestWorkingBeatmap(beatmap));
}
}
}

View File

@ -41,7 +41,10 @@ public class BeatmapVerifier : IBeatmapVerifier
new CheckPreviewTime(),
// Events
new CheckBreaks()
new CheckBreaks(),
// Settings
new CheckAbnormalDifficultySettings(),
};
public IEnumerable<Issue> Run(BeatmapVerifierContext context)

View File

@ -0,0 +1,82 @@
// 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.
using System.Collections.Generic;
using osu.Game.Rulesets.Edit.Checks.Components;
namespace osu.Game.Rulesets.Edit.Checks
{
public class CheckAbnormalDifficultySettings : ICheck
{
public CheckMetadata Metadata => new CheckMetadata(CheckCategory.Settings, "Abnormal difficulty settings");
public IEnumerable<IssueTemplate> PossibleTemplates => new IssueTemplate[]
{
new IssueTemplateMoreThanOneDecimal(this),
new IssueTemplateOutOfRange(this),
};
public IEnumerable<Issue> Run(BeatmapVerifierContext context)
{
var diff = context.Beatmap.Difficulty;
if (hasMoreThanOneDecimalPlace(diff.ApproachRate))
yield return new IssueTemplateMoreThanOneDecimal(this).Create("Approach rate", diff.ApproachRate);
if (isOutOfRange(diff.ApproachRate))
yield return new IssueTemplateOutOfRange(this).Create("Approach rate", diff.ApproachRate);
if (hasMoreThanOneDecimalPlace(diff.OverallDifficulty))
yield return new IssueTemplateMoreThanOneDecimal(this).Create("Overall difficulty", diff.OverallDifficulty);
if (isOutOfRange(diff.OverallDifficulty))
yield return new IssueTemplateOutOfRange(this).Create("Overall difficulty", diff.OverallDifficulty);
if (hasMoreThanOneDecimalPlace(diff.CircleSize))
yield return new IssueTemplateMoreThanOneDecimal(this).Create("Circle size", diff.CircleSize);
if (isOutOfRange(diff.CircleSize))
yield return new IssueTemplateOutOfRange(this).Create("Circle size", diff.CircleSize);
if (hasMoreThanOneDecimalPlace(diff.DrainRate))
yield return new IssueTemplateMoreThanOneDecimal(this).Create("Drain rate", diff.DrainRate);
if (isOutOfRange(diff.DrainRate))
yield return new IssueTemplateOutOfRange(this).Create("Drain rate", diff.DrainRate);
}
private bool isOutOfRange(float setting)
{
return setting < 0f || setting > 10f;
}
private bool hasMoreThanOneDecimalPlace(float setting)
{
return float.Round(setting, 1) != setting;
}
public class IssueTemplateMoreThanOneDecimal : IssueTemplate
{
public IssueTemplateMoreThanOneDecimal(ICheck check)
: base(check, IssueType.Problem, "{0} {1} has more than one decimal place.")
{
}
public Issue Create(string settingName, float settingValue) => new Issue(this, settingName, settingValue);
}
public class IssueTemplateOutOfRange : IssueTemplate
{
public IssueTemplateOutOfRange(ICheck check)
: base(check, IssueType.Warning, "{0} is {1} although it is capped between 0 to 10 in-game.")
{
}
public Issue Create(string settingName, float settingValue) => new Issue(this, settingName, settingValue);
}
}
}