1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-20 07:19:53 +08:00
Files
osu-lazer/osu.Game/Screens/SelectV2/RealmPopulatingOnlineLookupSource.cs
T
Bartłomiej Dach 8cb81974eb Add initial support for filtering by user tags in song select
The way that this works is that it plugs into the online request to
retrieve the beatmap set that the client is already performing, and
stores user tag data to the local realm database.

This means that for now user tags will only populate for beatmaps that
the user has displayed on song select which is obviously subpar. I plan
to follow this change up by adding user tag state dumps to `online.db`
and using that data for initial tag population to make the majority case
(ranked beatmaps) work.

Note that several decisions were made here that are potential discussion
points:

- `RealmPopulatingOnlineLookupSource` is set up such that it can be the
  middle man / redirection point for similar flows that we need and we
  are currently missing, such as storing guest difficulty information,
  or storing the user's current best score on a beatmap (handy for rank
  achieved sorting / filtering / etc.)

- The user tags are stored in `BeatmapMetadata` which breaks the
  longstanding assumption that you can arbitrarily pull out a metadata
  instance from any of the beatmaps in a set and get essentially the
  same object back.

  I've attempted to constrain this some by not adding user tags to
  the `IBeatmapMetadataInfo` interface through which `BeatmapSetInfo`
  exposes metadata further, but I warn in advance that this is
  a temporary state of affairs and I will make it worse in the future
  when `BeatmapMetadata.Author` becomes `Authors` plural in order to
  support guest mapper display (and direct guest difficulty submission).

- The syntax for searching via user tags is chosen to mostly match web -
  it's `tag=`, with support for all of the string matching modes song
  select already has (bare word for substring, `""` quotes for phrase
  isolated by whitespace, `""!` for exact full match).
2025-07-15 11:09:40 +02:00

82 lines
3.6 KiB
C#

// 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.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using Realms;
namespace osu.Game.Screens.SelectV2
{
/// <summary>
/// This component is designed to perform lookups of online data
/// and store portions of it for later local use to the realm database.
/// </summary>
/// <example>
/// This component is designed to locally persist potentially-volatile online information such as:
/// <list type="bullet">
/// <item>user tags assigned to difficulties of a beatmap,</item>
/// <item>guest mappers assigned to difficulties of a beatmap,</item>
/// <item>the local user's best score on a given beatmap.</item>
/// </list>
/// </example>
public partial class RealmPopulatingOnlineLookupSource : Component
{
[Resolved]
private IAPIProvider api { get; set; } = null!;
[Resolved]
private RealmAccess realm { get; set; } = null!;
public Task<APIBeatmapSet?> GetBeatmapSetAsync(int id, CancellationToken token = default)
{
var request = new GetBeatmapSetRequest(id);
var tcs = new TaskCompletionSource<APIBeatmapSet?>();
request.Success += onlineBeatmapSet =>
{
if (token.IsCancellationRequested)
{
tcs.SetCanceled(token);
return;
}
var tagsById = (onlineBeatmapSet.RelatedTags ?? []).ToDictionary(t => t.Id);
var onlineBeatmaps = onlineBeatmapSet.Beatmaps.ToDictionary(b => b.OnlineID);
realm.Write(r =>
{
foreach (var dbBeatmap in r.All<BeatmapInfo>().Filter($@"{nameof(BeatmapInfo.BeatmapSet)}.{nameof(BeatmapSetInfo.OnlineID)} == $0", id))
{
if (onlineBeatmaps.TryGetValue(dbBeatmap.OnlineID, out var onlineBeatmap))
{
string[] userTagsArray = onlineBeatmap.TopTags?
.Select(t => (topTag: t, relatedTag: tagsById.GetValueOrDefault(t.TagId)))
.Where(t => t.relatedTag != null)
// see https://github.com/ppy/osu-web/blob/bb3bd2e7c6f84f26066df5ea20a81c77ec9bb60a/resources/js/beatmapsets-show/controller.ts#L103-L106 for sort criteria
.OrderByDescending(t => t.topTag.VoteCount)
.ThenBy(t => t.relatedTag!.Name)
.Select(t => t.relatedTag!.Name)
.ToArray() ?? [];
dbBeatmap.Metadata.UserTags.Clear();
dbBeatmap.Metadata.UserTags.AddRange(userTagsArray);
}
}
});
tcs.SetResult(onlineBeatmapSet);
};
request.Failure += tcs.SetException;
api.Queue(request);
return tcs.Task;
}
}
}