Skip to content

Commit

Permalink
Improve multiplayer listing search by making it fuzzy
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Dec 4, 2024
1 parent a8963cf commit ad4df82
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions osu.Game/Screens/OnlinePlay/Lounge/Components/RoomsContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
Expand Down Expand Up @@ -80,19 +81,34 @@ private void applyFilterCriteria(FilterCriteria? criteria)
bool matchingFilter = true;

matchingFilter &= criteria.Ruleset == null || r.Room.PlaylistItemStats?.RulesetIDs.Any(id => id == criteria.Ruleset.OnlineID) != false;

if (!string.IsNullOrEmpty(criteria.SearchString))
{
// Room name isn't translatable, so ToString() is used here for simplicity.
matchingFilter &= r.FilterTerms.Any(term => term.ToString().Contains(criteria.SearchString, StringComparison.InvariantCultureIgnoreCase));
}

matchingFilter &= matchPermissions(r, criteria.Permissions);

// Room name isn't translatable, so ToString() is used here for simplicity.
string[] filterTerms = r.FilterTerms.Select(t => t.ToString()).ToArray();
string[] searchTerms = criteria.SearchString.Split(' ', StringSplitOptions.RemoveEmptyEntries);
matchingFilter &= searchTerms.All(searchTerm => filterTerms.Any(filterTerm => checkTerm(filterTerm, searchTerm)));

r.MatchingFilter = matchingFilter;
}
});

// Lifted from SearchContainer.
static bool checkTerm(string haystack, string needle)
{
int index = 0;

for (int i = 0; i < needle.Length; i++)
{
int found = CultureInfo.InvariantCulture.CompareInfo.IndexOf(haystack, needle[i], index, CompareOptions.OrdinalIgnoreCase);
if (found < 0)
return false;

index = found + 1;
}

return true;
}

static bool matchPermissions(DrawableLoungeRoom room, RoomPermissionsFilter accessType)
{
switch (accessType)
Expand Down

0 comments on commit ad4df82

Please sign in to comment.