1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 20:07:25 +08:00

Add SortID to RulesetInfo to allow stable ordering of rulesets for display

This commit is contained in:
Dean Herbert 2022-01-27 15:19:27 +09:00
parent de5ac7ad83
commit f30d63107a
5 changed files with 46 additions and 6 deletions

View File

@ -0,0 +1,38 @@
// 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.Linq;
using NUnit.Framework;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Catch;
using osu.Game.Rulesets.Osu;
namespace osu.Game.Tests.NonVisual
{
[TestFixture]
public class RulesetInfoOrderingTest
{
[Test]
public void TestOrdering()
{
var rulesets = new[]
{
new RulesetInfo("custom2", "Custom Ruleset 2", string.Empty, -1),
new OsuRuleset().RulesetInfo,
new RulesetInfo("custom3", "Custom Ruleset 3", string.Empty, -1),
new RulesetInfo("custom2", "Custom Ruleset 2", string.Empty, -1),
new CatchRuleset().RulesetInfo,
new RulesetInfo("custom3", "Custom Ruleset 3", string.Empty, -1),
};
var orderedRulesets = rulesets.OrderBy(r => r.SortID);
// Ensure all customs are after official.
Assert.That(orderedRulesets.Select(r => r.OnlineID), Is.EqualTo(new[] { 0, 2, -1, -1, -1, -1 }));
// Ensure customs are grouped next to each other (ie. stably sorted).
Assert.That(orderedRulesets.SkipWhile(r => r.ShortName != "custom2").Skip(1).First().ShortName, Is.EqualTo("custom2"));
Assert.That(orderedRulesets.SkipWhile(r => r.ShortName != "custom3").Skip(1).First().ShortName, Is.EqualTo("custom3"));
}
}
}

View File

@ -24,6 +24,11 @@ namespace osu.Game.Rulesets
public string InstantiationInfo { get; set; } = string.Empty;
/// <summary>
/// A best effort sort ID which provides stable ordering and puts online rulesets before non-online rulesets.
/// </summary>
public int SortID => OnlineID >= 0 ? OnlineID : Math.Abs(ShortName.GetHashCode());
public RulesetInfo(string shortName, string name, string instantiationInfo, int onlineID)
{
ShortName = shortName;

View File

@ -163,10 +163,7 @@ namespace osu.Game.Rulesets
}
}
// add known official rulesets first..
availableRulesets.AddRange(detachedRulesets.Where(r => r.OnlineID >= 0).OrderBy(r => r.OnlineID));
// .. then add any customs
availableRulesets.AddRange(detachedRulesets.Where(r => r.OnlineID < 0).OrderBy(r => r.ShortName));
availableRulesets.AddRange(detachedRulesets.OrderBy(r => r.SortID));
});
}

View File

@ -89,7 +89,7 @@ namespace osu.Game.Screens.Select.Carousel
{
default:
case SortMode.Difficulty:
int ruleset = BeatmapInfo.RulesetID.CompareTo(otherBeatmap.BeatmapInfo.RulesetID);
int ruleset = BeatmapInfo.Ruleset.SortID.CompareTo(otherBeatmap.BeatmapInfo.Ruleset.SortID);
if (ruleset != 0) return ruleset;
return BeatmapInfo.StarRating.CompareTo(otherBeatmap.BeatmapInfo.StarRating);

View File

@ -39,7 +39,7 @@ namespace osu.Game.Screens.Select.Carousel
beatmapSet.Beatmaps
.Where(b => !b.Hidden)
.OrderBy(b => b.RulesetID)
.OrderBy(b => b.Ruleset.SortID)
.ThenBy(b => b.StarRating)
.Select(b => new CarouselBeatmap(b))
.ForEach(AddChild);