Skip to content

Commit

Permalink
Merge pull request #30321 from smoogipoo/bat-mods
Browse files Browse the repository at this point in the history
Make `BeatmapAttributeText` show values inclusive of mods
  • Loading branch information
bdach authored Nov 1, 2024
2 parents 44535d7 + 1b5d134 commit 9df9a97
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 8 deletions.
136 changes: 136 additions & 0 deletions osu.Game.Tests/Visual/UserInterface/TestSceneBeatmapAttributeText.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Localisation;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Models;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Difficulty;
using osu.Game.Rulesets.Difficulty.Preprocessing;
using osu.Game.Rulesets.Difficulty.Skills;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Rulesets.UI;
using osu.Game.Scoring;
using osu.Game.Skinning.Components;
using osu.Game.Tests.Beatmaps;

Expand All @@ -30,6 +44,8 @@ public TestSceneBeatmapAttributeText()
[SetUp]
public void Setup() => Schedule(() =>
{
SelectedMods.SetDefault();
Ruleset.Value = new OsuRuleset().RulesetInfo;
Beatmap.Value = CreateWorkingBeatmap(new TestBeatmap(new OsuRuleset().RulesetInfo)
{
BeatmapInfo =
Expand Down Expand Up @@ -93,6 +109,126 @@ public void TestChangeBeatmap()
AddAssert("check new title", getText, () => Is.EqualTo("Title: Another"));
}

[Test]
public void TestWithMods()
{
AddStep("set beatmap", () => Beatmap.Value = CreateWorkingBeatmap(new TestBeatmap(new OsuRuleset().RulesetInfo)
{
BeatmapInfo =
{
BPM = 100,
Length = 30000,
Difficulty =
{
ApproachRate = 10,
CircleSize = 9
}
}
}));

test(BeatmapAttribute.BPM, new OsuModDoubleTime(), "BPM: 100.00", "BPM: 150.00");
test(BeatmapAttribute.Length, new OsuModDoubleTime(), "Length: 00:30", "Length: 00:20");
test(BeatmapAttribute.ApproachRate, new OsuModDoubleTime(), "Approach Rate: 10.00", "Approach Rate: 11.00");
test(BeatmapAttribute.CircleSize, new OsuModHardRock(), "Circle Size: 9.00", "Circle Size: 10.00");

void test(BeatmapAttribute attribute, Mod mod, string before, string after)
{
AddStep($"set attribute: {attribute}", () => text.Attribute.Value = attribute);
AddAssert("check text is correct", getText, () => Is.EqualTo(before));

AddStep("add DT mod", () => SelectedMods.Value = new[] { mod });
AddAssert("check text is correct", getText, () => Is.EqualTo(after));
AddStep("clear mods", () => SelectedMods.SetDefault());
}
}

[Test]
public void TestStarRating()
{
AddStep("set test ruleset", () => Ruleset.Value = new TestRuleset().RulesetInfo);
AddStep("set star rating attribute", () => text.Attribute.Value = BeatmapAttribute.StarRating);
AddAssert("check star rating is 0", getText, () => Is.EqualTo("Star Rating: 0.00"));

// Adding mod
TestMod mod = null!;
AddStep("add mod with difficulty 1", () => SelectedMods.Value = new[] { mod = new TestMod { Difficulty = { Value = 1 } } });
AddUntilStep("check star rating is 1", getText, () => Is.EqualTo("Star Rating: 1.00"));

// Changing mod setting
AddStep("change mod difficulty to 2", () => mod.Difficulty.Value = 2);
AddUntilStep("check star rating is 2", getText, () => Is.EqualTo("Star Rating: 2.00"));
}

private string getText() => text.ChildrenOfType<SpriteText>().Single().Text.ToString();

private class TestRuleset : Ruleset
{
public override IEnumerable<Mod> GetModsFor(ModType type) => new[]
{
new TestMod()
};

public override IBeatmapConverter CreateBeatmapConverter(IBeatmap beatmap)
=> new OsuRuleset().CreateBeatmapConverter(beatmap);

public override DifficultyCalculator CreateDifficultyCalculator(IWorkingBeatmap beatmap)
=> new TestDifficultyCalculator(new TestRuleset().RulesetInfo, beatmap);

public override PerformanceCalculator CreatePerformanceCalculator()
=> new TestPerformanceCalculator(new TestRuleset());

public override DrawableRuleset CreateDrawableRulesetWith(IBeatmap beatmap, IReadOnlyList<Mod>? mods = null)
=> null!;

public override string Description => string.Empty;
public override string ShortName => string.Empty;
}

private class TestDifficultyCalculator : DifficultyCalculator
{
public TestDifficultyCalculator(IRulesetInfo ruleset, IWorkingBeatmap beatmap)
: base(ruleset, beatmap)
{
}

protected override DifficultyAttributes CreateDifficultyAttributes(IBeatmap beatmap, Mod[] mods, Skill[] skills, double clockRate)
=> new DifficultyAttributes(mods, mods.OfType<TestMod>().SingleOrDefault()?.Difficulty.Value ?? 0);

protected override IEnumerable<DifficultyHitObject> CreateDifficultyHitObjects(IBeatmap beatmap, double clockRate)
=> Array.Empty<DifficultyHitObject>();

protected override Skill[] CreateSkills(IBeatmap beatmap, Mod[] mods, double clockRate)
=> Array.Empty<Skill>();
}

private class TestPerformanceCalculator : PerformanceCalculator
{
public TestPerformanceCalculator(Ruleset ruleset)
: base(ruleset)
{
}

protected override PerformanceAttributes CreatePerformanceAttributes(ScoreInfo score, DifficultyAttributes attributes)
=> new PerformanceAttributes { Total = score.Mods.OfType<TestMod>().SingleOrDefault()?.Performance.Value ?? 0 };
}

private class TestMod : Mod
{
[SettingSource("difficulty")]
public BindableDouble Difficulty { get; } = new BindableDouble(0);

[SettingSource("performance")]
public BindableDouble Performance { get; } = new BindableDouble(0);

[JsonConstructor]
public TestMod()
{
}

public override string Name => string.Empty;
public override LocalisableString Description => string.Empty;
public override double ScoreMultiplier => 1.0;
public override string Acronym => "Test";
}
}
}
89 changes: 81 additions & 8 deletions osu.Game/Skinning/Components/BeatmapAttributeText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
Expand All @@ -18,6 +20,9 @@
using osu.Game.Localisation;
using osu.Game.Localisation.SkinComponents;
using osu.Game.Resources.Localisation.Web;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Utils;

namespace osu.Game.Skinning.Components
{
Expand All @@ -33,7 +38,20 @@ public partial class BeatmapAttributeText : FontAdjustableSkinComponent
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; } = null!;

[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; } = null!;

[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; } = null!;

[Resolved]
private BeatmapDifficultyCache difficultyCache { get; set; } = null!;

private readonly OsuSpriteText text;
private IBindable<StarDifficulty?>? difficultyBindable;
private CancellationTokenSource? difficultyCancellationSource;
private ModSettingChangeTracker? modSettingTracker;
private StarDifficulty? starDifficulty;

public BeatmapAttributeText()
{
Expand All @@ -55,7 +73,35 @@ protected override void LoadComplete()

Attribute.BindValueChanged(_ => updateText());
Template.BindValueChanged(_ => updateText());
beatmap.BindValueChanged(_ => updateText());

beatmap.BindValueChanged(b =>
{
difficultyCancellationSource?.Cancel();
difficultyCancellationSource = new CancellationTokenSource();
difficultyBindable?.UnbindAll();
difficultyBindable = difficultyCache.GetBindableDifficulty(b.NewValue.BeatmapInfo, difficultyCancellationSource.Token);
difficultyBindable.BindValueChanged(d =>
{
starDifficulty = d.NewValue;
updateText();
});
updateText();
}, true);

mods.BindValueChanged(m =>
{
modSettingTracker?.Dispose();
modSettingTracker = new ModSettingChangeTracker(m.NewValue)
{
SettingChanged = _ => updateText()
};
updateText();
}, true);

ruleset.BindValueChanged(_ => updateText());

updateText();
}
Expand Down Expand Up @@ -156,37 +202,64 @@ private LocalisableString getValueString(BeatmapAttribute attribute)
return beatmap.Value.BeatmapInfo.Metadata.Source;

case BeatmapAttribute.Length:
return TimeSpan.FromMilliseconds(beatmap.Value.BeatmapInfo.Length).ToFormattedDuration();
return Math.Round(beatmap.Value.BeatmapInfo.Length / ModUtils.CalculateRateWithMods(mods.Value)).ToFormattedDuration();

case BeatmapAttribute.RankedStatus:
return beatmap.Value.BeatmapInfo.Status.GetLocalisableDescription();

case BeatmapAttribute.BPM:
return beatmap.Value.BeatmapInfo.BPM.ToLocalisableString(@"F2");
return FormatUtils.RoundBPM(beatmap.Value.BeatmapInfo.BPM, ModUtils.CalculateRateWithMods(mods.Value)).ToLocalisableString(@"F2");

case BeatmapAttribute.CircleSize:
return ((double)beatmap.Value.BeatmapInfo.Difficulty.CircleSize).ToLocalisableString(@"F2");
return computeDifficulty().CircleSize.ToLocalisableString(@"F2");

case BeatmapAttribute.HPDrain:
return ((double)beatmap.Value.BeatmapInfo.Difficulty.DrainRate).ToLocalisableString(@"F2");
return computeDifficulty().DrainRate.ToLocalisableString(@"F2");

case BeatmapAttribute.Accuracy:
return ((double)beatmap.Value.BeatmapInfo.Difficulty.OverallDifficulty).ToLocalisableString(@"F2");
return computeDifficulty().OverallDifficulty.ToLocalisableString(@"F2");

case BeatmapAttribute.ApproachRate:
return ((double)beatmap.Value.BeatmapInfo.Difficulty.ApproachRate).ToLocalisableString(@"F2");
return computeDifficulty().ApproachRate.ToLocalisableString(@"F2");

case BeatmapAttribute.StarRating:
return beatmap.Value.BeatmapInfo.StarRating.ToLocalisableString(@"F2");
return (starDifficulty?.Stars ?? 0).ToLocalisableString(@"F2");

default:
return string.Empty;
}

BeatmapDifficulty computeDifficulty()
{
BeatmapDifficulty difficulty = new BeatmapDifficulty(beatmap.Value.BeatmapInfo.Difficulty);

foreach (var mod in mods.Value.OfType<IApplicableToDifficulty>())
mod.ApplyToDifficulty(difficulty);

if (ruleset.Value is RulesetInfo rulesetInfo)
{
double rate = ModUtils.CalculateRateWithMods(mods.Value);
difficulty = rulesetInfo.CreateInstance().GetRateAdjustedDisplayDifficulty(difficulty, rate);
}

return difficulty;
}
}

protected override void SetFont(FontUsage font) => text.Font = font.With(size: 40);

protected override void SetTextColour(Colour4 textColour) => text.Colour = textColour;

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);

difficultyCancellationSource?.Cancel();
difficultyCancellationSource?.Dispose();
difficultyCancellationSource = null;

modSettingTracker?.Dispose();
}
}

// WARNING: DO NOT ADD ANY VALUES TO THIS ENUM ANYWHERE ELSE THAN AT THE END.
Expand Down

0 comments on commit 9df9a97

Please sign in to comment.