From f7364de01af250ec7e292702086544b6b4fe8f36 Mon Sep 17 00:00:00 2001 From: Joseph Madamba Date: Tue, 10 Dec 2024 18:18:34 -0800 Subject: [PATCH] Add test and null protections --- .../TestSceneBeatmapRecommendations.cs | 44 +++++++++++++++++++ osu.Game/Beatmaps/DifficultyRecommender.cs | 3 +- .../BeatmapSearchGeneralFilterRow.cs | 12 ++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/osu.Game.Tests/Visual/SongSelect/TestSceneBeatmapRecommendations.cs b/osu.Game.Tests/Visual/SongSelect/TestSceneBeatmapRecommendations.cs index bd5c43d242..4c8c1d7ad2 100644 --- a/osu.Game.Tests/Visual/SongSelect/TestSceneBeatmapRecommendations.cs +++ b/osu.Game.Tests/Visual/SongSelect/TestSceneBeatmapRecommendations.cs @@ -13,9 +13,12 @@ using osu.Framework.Testing; using osu.Game.Beatmaps; using osu.Game.Database; using osu.Game.Extensions; +using osu.Game.Graphics.Sprites; using osu.Game.Online.API; using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests.Responses; +using osu.Game.Overlays; +using osu.Game.Overlays.BeatmapListing; using osu.Game.Rulesets; using osu.Game.Rulesets.Catch; using osu.Game.Rulesets.Mania; @@ -23,6 +26,8 @@ using osu.Game.Rulesets.Osu; using osu.Game.Rulesets.Taiko; using osu.Game.Tests.Resources; using osu.Game.Users; +using osu.Game.Utils; +using osuTK.Input; namespace osu.Game.Tests.Visual.SongSelect { @@ -170,6 +175,45 @@ namespace osu.Game.Tests.Visual.SongSelect presentAndConfirm(() => maniaSet, 5); } + [Test] + public void TestBeatmapListingFilter() + { + AddStep("set playmode to taiko", () => ((DummyAPIAccess)API).LocalUser.Value.PlayMode = "taiko"); + + AddStep("open beatmap listing", () => + { + InputManager.PressKey(Key.ControlLeft); + InputManager.PressKey(Key.B); + InputManager.ReleaseKey(Key.B); + InputManager.ReleaseKey(Key.ControlLeft); + }); + + AddUntilStep("wait for load", () => Game.ChildrenOfType().SingleOrDefault()?.IsLoaded, () => Is.True); + + checkRecommendedDifficulty(3); + + AddStep("change mode filter to osu!", () => Game.ChildrenOfType().Single().ChildrenOfType>().ElementAt(1).TriggerClick()); + + checkRecommendedDifficulty(2); + + AddStep("change mode filter to osu!taiko", () => Game.ChildrenOfType().Single().ChildrenOfType>().ElementAt(2).TriggerClick()); + + checkRecommendedDifficulty(3); + + AddStep("change mode filter to osu!catch", () => Game.ChildrenOfType().Single().ChildrenOfType>().ElementAt(3).TriggerClick()); + + checkRecommendedDifficulty(4); + + AddStep("change mode filter to osu!mania", () => Game.ChildrenOfType().Single().ChildrenOfType>().ElementAt(4).TriggerClick()); + + checkRecommendedDifficulty(5); + + void checkRecommendedDifficulty(double starRating) + => AddAssert($"recommended difficulty is {starRating}", + () => Game.ChildrenOfType().Single().ChildrenOfType().ElementAt(1).Text.ToString(), + () => Is.EqualTo($"Recommended difficulty ({starRating.FormatStarRating()})")); + } + private BeatmapSetInfo importBeatmapSet(IEnumerable difficultyRulesets) { var rulesets = difficultyRulesets.ToArray(); diff --git a/osu.Game/Beatmaps/DifficultyRecommender.cs b/osu.Game/Beatmaps/DifficultyRecommender.cs index bd864422d1..a5c7371b4d 100644 --- a/osu.Game/Beatmaps/DifficultyRecommender.cs +++ b/osu.Game/Beatmaps/DifficultyRecommender.cs @@ -83,7 +83,8 @@ namespace osu.Game.Beatmaps StarRatingUpdated?.Invoke(); } - public double GetRecommendedStarRatingFor(RulesetInfo ruleset) => recommendedDifficultyMapping[ruleset.ShortName]; + public double? GetRecommendedStarRatingFor(RulesetInfo ruleset) + => recommendedDifficultyMapping.TryGetValue(ruleset.ShortName, out double starRating) ? starRating : null; /// /// Find the recommended difficulty from a selection of available difficulties for the current local user. diff --git a/osu.Game/Overlays/BeatmapListing/BeatmapSearchGeneralFilterRow.cs b/osu.Game/Overlays/BeatmapListing/BeatmapSearchGeneralFilterRow.cs index 42d788dad7..66a0a16549 100644 --- a/osu.Game/Overlays/BeatmapListing/BeatmapSearchGeneralFilterRow.cs +++ b/osu.Game/Overlays/BeatmapListing/BeatmapSearchGeneralFilterRow.cs @@ -91,8 +91,16 @@ namespace osu.Game.Overlays.BeatmapListing { // fallback to profile default game mode if beatmap listing mode filter is set to Any // TODO: find a way to update `PlayMode` when the profile default game mode has changed - var ruleset = Ruleset.Value.IsLegacyRuleset() ? Ruleset.Value : rulesets.GetRuleset(api.LocalUser.Value.PlayMode)!; - Text.Text = LocalisableString.Interpolate($"{Value.GetLocalisableDescription()} ({recommender?.GetRecommendedStarRatingFor(ruleset).FormatStarRating()})"); + RulesetInfo? ruleset = Ruleset.Value.IsLegacyRuleset() ? Ruleset.Value : rulesets.GetRuleset(api.LocalUser.Value.PlayMode); + + if (ruleset == null) return; + + double? starRating = recommender?.GetRecommendedStarRatingFor(ruleset); + + if (starRating != null) + Text.Text = LocalisableString.Interpolate($"{Value.GetLocalisableDescription()} ({starRating.Value.FormatStarRating()})"); + else + Text.Text = Value.GetLocalisableDescription(); } }