mirror of
https://github.com/ppy/osu.git
synced 2024-12-13 08:32:57 +08:00
chore: requested changes
This commit is contained in:
parent
e1c1609271
commit
d0678bfbee
@ -256,8 +256,7 @@ namespace osu.Game.Tests.NonVisual.Filtering
|
||||
const string query = "status=r";
|
||||
var filterCriteria = new FilterCriteria();
|
||||
FilterQueryParser.ApplyQueries(filterCriteria, query);
|
||||
Assert.IsNotNull(filterCriteria.OnlineStatus.Values);
|
||||
Assert.IsNotEmpty(filterCriteria.OnlineStatus.Values!);
|
||||
Assert.IsNotEmpty(filterCriteria.OnlineStatus.Values);
|
||||
Assert.Contains(BeatmapOnlineStatus.Ranked, filterCriteria.OnlineStatus.Values);
|
||||
}
|
||||
|
||||
@ -269,7 +268,7 @@ namespace osu.Game.Tests.NonVisual.Filtering
|
||||
FilterQueryParser.ApplyQueries(filterCriteria, query);
|
||||
Assert.AreEqual("I want the pp", filterCriteria.SearchText.Trim());
|
||||
Assert.AreEqual(4, filterCriteria.SearchTerms.Length);
|
||||
Assert.IsNotNull(filterCriteria.OnlineStatus.Values);
|
||||
Assert.IsNotEmpty(filterCriteria.OnlineStatus.Values);
|
||||
Assert.Contains(BeatmapOnlineStatus.Ranked, filterCriteria.OnlineStatus.Values);
|
||||
}
|
||||
|
||||
@ -279,11 +278,24 @@ namespace osu.Game.Tests.NonVisual.Filtering
|
||||
const string query = "status=ranked status=loved";
|
||||
var filterCriteria = new FilterCriteria();
|
||||
FilterQueryParser.ApplyQueries(filterCriteria, query);
|
||||
Assert.IsNotNull(filterCriteria.OnlineStatus.Values);
|
||||
Assert.IsNotEmpty(filterCriteria.OnlineStatus.Values);
|
||||
Assert.Contains(BeatmapOnlineStatus.Ranked, filterCriteria.OnlineStatus.Values);
|
||||
Assert.Contains(BeatmapOnlineStatus.Loved, filterCriteria.OnlineStatus.Values);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestApplyRangeStatusMatches()
|
||||
{
|
||||
const string query = "status>=r";
|
||||
var filterCriteria = new FilterCriteria();
|
||||
FilterQueryParser.ApplyQueries(filterCriteria, query);
|
||||
Assert.IsNotEmpty(filterCriteria.OnlineStatus.Values);
|
||||
Assert.Contains(BeatmapOnlineStatus.Ranked, filterCriteria.OnlineStatus.Values);
|
||||
Assert.Contains(BeatmapOnlineStatus.Approved, filterCriteria.OnlineStatus.Values);
|
||||
Assert.Contains(BeatmapOnlineStatus.Qualified, filterCriteria.OnlineStatus.Values);
|
||||
Assert.Contains(BeatmapOnlineStatus.Loved, filterCriteria.OnlineStatus.Values);
|
||||
}
|
||||
|
||||
[TestCase("creator")]
|
||||
[TestCase("author")]
|
||||
[TestCase("mapper")]
|
||||
|
@ -34,7 +34,7 @@ namespace osu.Game.Screens.Select
|
||||
public OptionalRange<double> Length;
|
||||
public OptionalRange<double> BPM;
|
||||
public OptionalRange<int> BeatDivisor;
|
||||
public OptionalArray<BeatmapOnlineStatus> OnlineStatus;
|
||||
public OptionalSet<BeatmapOnlineStatus> OnlineStatus = new OptionalSet<BeatmapOnlineStatus>();
|
||||
public OptionalRange<DateTimeOffset> LastPlayed;
|
||||
public OptionalTextFilter Creator;
|
||||
public OptionalTextFilter Artist;
|
||||
@ -114,24 +114,25 @@ namespace osu.Game.Screens.Select
|
||||
|
||||
public IRulesetFilterCriteria? RulesetCriteria { get; set; }
|
||||
|
||||
public struct OptionalArray<T> : IEquatable<OptionalArray<T>>
|
||||
public struct OptionalSet<T> : IEquatable<OptionalSet<T>>
|
||||
where T : struct
|
||||
{
|
||||
public bool HasFilter => Values?.Length > 0;
|
||||
public bool HasFilter => Values.Count > 0;
|
||||
|
||||
public bool IsInRange(T value)
|
||||
{
|
||||
return Values?.Contains(value) ?? false;
|
||||
return Values.Contains(value);
|
||||
}
|
||||
|
||||
public T[]? Values;
|
||||
public SortedSet<T> Values = new SortedSet<T>();
|
||||
|
||||
public bool Equals(OptionalArray<T> other)
|
||||
public OptionalSet()
|
||||
{
|
||||
if (Values is null && other.Values is null)
|
||||
return true;
|
||||
}
|
||||
|
||||
return Values?.SequenceEqual(other.Values ?? Array.Empty<T>()) ?? false;
|
||||
public bool Equals(OptionalSet<T> other)
|
||||
{
|
||||
return Values.SetEquals(other.Values);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,7 +69,7 @@ namespace osu.Game.Screens.Select
|
||||
return TryUpdateCriteriaRange(ref criteria.BeatDivisor, op, value, tryParseInt);
|
||||
|
||||
case "status":
|
||||
return TryUpdateArrayRange(ref criteria.OnlineStatus, op, value, tryParseEnum);
|
||||
return TryUpdateSetRange(ref criteria.OnlineStatus, op, value, tryParseEnum);
|
||||
|
||||
case "creator":
|
||||
case "author":
|
||||
@ -306,23 +306,55 @@ namespace osu.Game.Screens.Select
|
||||
/// If <paramref name="val"/> can be parsed into <typeparamref name="T"/> using <paramref name="parseFunction"/>, the function returns <c>true</c>
|
||||
/// and the resulting range constraint is stored into the <paramref name="range"/>'s expected values.
|
||||
/// </summary>
|
||||
/// <param name="range">The <see cref="FilterCriteria.OptionalArray{T}"/> to store the parsed data into, if successful.</param>
|
||||
/// <param name="range">The <see cref="FilterCriteria.OptionalSet{T}"/> to store the parsed data into, if successful.</param>
|
||||
/// <param name="op">The operator for the keyword filter. Currently, only <see cref="Operator.Equal"/> can be used.</param>
|
||||
/// <param name="val">The value of the keyword filter.</param>
|
||||
/// <param name="parseFunction">Function used to determine if <paramref name="val"/> can be converted to type <typeparamref name="T"/>.</param>
|
||||
public static bool TryUpdateArrayRange<T>(ref FilterCriteria.OptionalArray<T> range, Operator op, string val, TryParseFunction<T> parseFunction)
|
||||
public static bool TryUpdateSetRange<T>(ref FilterCriteria.OptionalSet<T> range, Operator op, string val, TryParseFunction<T> parseFunction)
|
||||
where T : struct
|
||||
=> parseFunction.Invoke(val, out var converted) && tryUpdateArrayRange(ref range, op, converted);
|
||||
=> parseFunction.Invoke(val, out var converted) && tryUpdateSetRange(ref range, op, converted);
|
||||
|
||||
private static bool tryUpdateArrayRange<T>(ref FilterCriteria.OptionalArray<T> range, Operator op, T value)
|
||||
private static bool tryUpdateSetRange<T>(ref FilterCriteria.OptionalSet<T> range, Operator op, T value)
|
||||
where T : struct
|
||||
{
|
||||
if (op != Operator.Equal)
|
||||
return false;
|
||||
var enumValues = (T[])Enum.GetValues(typeof(T));
|
||||
|
||||
range.Values ??= Array.Empty<T>();
|
||||
foreach (var enumValue in enumValues)
|
||||
{
|
||||
switch (op)
|
||||
{
|
||||
case Operator.Less:
|
||||
if (Comparer<T>.Default.Compare(enumValue, value) < 0)
|
||||
range.Values.Add(enumValue);
|
||||
|
||||
range.Values = range.Values.Append(value).ToArray();
|
||||
break;
|
||||
|
||||
case Operator.LessOrEqual:
|
||||
if (Comparer<T>.Default.Compare(enumValue, value) <= 0)
|
||||
range.Values.Add(enumValue);
|
||||
|
||||
break;
|
||||
|
||||
case Operator.Equal:
|
||||
range.Values.Add(value);
|
||||
break;
|
||||
|
||||
case Operator.GreaterOrEqual:
|
||||
if (Comparer<T>.Default.Compare(enumValue, value) >= 0)
|
||||
range.Values.Add(enumValue);
|
||||
|
||||
break;
|
||||
|
||||
case Operator.Greater:
|
||||
if (Comparer<T>.Default.Compare(enumValue, value) > 0)
|
||||
range.Values.Add(enumValue);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user