1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-14 00:53:19 +08:00

Merge pull request #16641 from peppy/ruleset-ordering-fix

Add more encompassing stable ordering of rulesets
This commit is contained in:
Dan Balasescu 2022-01-27 22:41:07 +09:00 committed by GitHub
commit 99bc679a22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 7 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);
// 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

@ -42,6 +42,8 @@ namespace osu.Game.Rulesets
public bool Equals(EFRulesetInfo other) => other != null && ID == other.ID && Available == other.Available && Name == other.Name && InstantiationInfo == other.InstantiationInfo;
public int CompareTo(RulesetInfo other) => OnlineID.CompareTo(other.OnlineID);
public override bool Equals(object obj) => obj is EFRulesetInfo rulesetInfo && Equals(rulesetInfo);
public bool Equals(IRulesetInfo other) => other is RulesetInfo b && Equals(b);

View File

@ -11,7 +11,7 @@ namespace osu.Game.Rulesets
/// <summary>
/// A representation of a ruleset's metadata.
/// </summary>
public interface IRulesetInfo : IHasOnlineID<int>, IEquatable<IRulesetInfo>
public interface IRulesetInfo : IHasOnlineID<int>, IEquatable<IRulesetInfo>, IComparable<RulesetInfo>
{
/// <summary>
/// The user-exposed name of this ruleset.

View File

@ -49,6 +49,20 @@ namespace osu.Game.Rulesets
public bool Equals(IRulesetInfo? other) => other is RulesetInfo b && Equals(b);
public int CompareTo(RulesetInfo other)
{
if (OnlineID >= 0 && other.OnlineID >= 0)
return OnlineID.CompareTo(other.OnlineID);
// Official rulesets are always given precedence for the time being.
if (OnlineID >= 0)
return -1;
if (other.OnlineID >= 0)
return 1;
return string.Compare(ShortName, other.ShortName, StringComparison.Ordinal);
}
public override int GetHashCode()
{
// Importantly, ignore the underlying realm hash code, as it will usually not match.

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));
});
}

View File

@ -89,7 +89,8 @@ namespace osu.Game.Screens.Select.Carousel
{
default:
case SortMode.Difficulty:
int ruleset = BeatmapInfo.Ruleset.OnlineID.CompareTo(otherBeatmap.BeatmapInfo.Ruleset.OnlineID);
int ruleset = BeatmapInfo.Ruleset.CompareTo(otherBeatmap.BeatmapInfo.Ruleset);
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.Ruleset.OnlineID)
.OrderBy(b => b.Ruleset)
.ThenBy(b => b.StarRating)
.Select(b => new CarouselBeatmap(b))
.ForEach(AddChild);