1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 17:52:54 +08:00
osu-lazer/osu.Game/Screens/Select/Leaderboards/BeatmapLeaderboard.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

239 lines
8.2 KiB
C#
Raw Normal View History

// 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
2021-08-31 20:36:31 +08:00
using System.Threading;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
2021-12-13 18:01:20 +08:00
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Leaderboards;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Scoring;
using Realms;
namespace osu.Game.Screens.Select.Leaderboards
{
public partial class BeatmapLeaderboard : Leaderboard<BeatmapLeaderboardScope, ScoreInfo>
{
public Action<ScoreInfo>? ScoreSelected;
private BeatmapInfo? beatmapInfo;
public BeatmapInfo? BeatmapInfo
{
2021-10-02 23:55:29 +08:00
get => beatmapInfo;
set
{
if (beatmapInfo == null && value == null)
return;
if (beatmapInfo?.Equals(value) == true)
return;
2021-10-02 23:55:29 +08:00
beatmapInfo = value;
// Refetch is scheduled, which can cause scores to be outdated if the leaderboard is not currently updating.
// As scores are potentially used by other components, clear them eagerly to ensure a more correct state.
SetScores(null);
2022-01-28 21:47:45 +08:00
RefetchScores();
}
}
private bool filterMods;
2019-07-04 00:58:13 +08:00
/// <summary>
/// Whether to apply the game's currently selected mods as a filter when retrieving scores.
/// </summary>
public bool FilterMods
{
get => filterMods;
set
{
if (value == filterMods)
return;
filterMods = value;
RefetchScores();
}
}
[Resolved]
private ScoreManager scoreManager { get; set; } = null!;
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; } = null!;
[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; } = null!;
[Resolved]
private IAPIProvider api { get; set; } = null!;
2022-01-28 21:47:45 +08:00
[Resolved]
private RulesetStore rulesets { get; set; } = null!;
2022-01-28 21:47:45 +08:00
[Resolved]
private RealmAccess realm { get; set; } = null!;
2022-01-28 21:47:45 +08:00
private IDisposable? scoreSubscription;
2022-01-28 21:47:45 +08:00
private GetScoresRequest? scoreRetrievalRequest;
[BackgroundDependencyLoader]
private void load()
{
ruleset.ValueChanged += _ => RefetchScores();
mods.ValueChanged += _ =>
{
if (filterMods)
RefetchScores();
};
}
2019-07-21 09:42:40 +08:00
protected override bool IsOnlineScope => Scope != BeatmapLeaderboardScope.Local;
2019-07-21 08:07:27 +08:00
protected override APIRequest? FetchScores(CancellationToken cancellationToken)
{
scoreRetrievalRequest?.Cancel();
scoreRetrievalRequest = null;
var fetchBeatmapInfo = BeatmapInfo;
if (fetchBeatmapInfo == null)
{
2022-01-31 00:12:03 +08:00
SetErrorState(LeaderboardState.NoneSelected);
return null;
}
var fetchRuleset = ruleset.Value ?? fetchBeatmapInfo.Ruleset;
if (Scope == BeatmapLeaderboardScope.Local)
{
subscribeToLocalScores(fetchBeatmapInfo, cancellationToken);
return null;
}
if (!api.IsLoggedIn)
{
2022-01-31 00:12:03 +08:00
SetErrorState(LeaderboardState.NotLoggedIn);
return null;
2019-07-05 12:39:21 +08:00
}
if (!fetchRuleset.IsLegacyRuleset())
{
SetErrorState(LeaderboardState.RulesetUnavailable);
return null;
}
if (fetchBeatmapInfo.OnlineID <= 0 || fetchBeatmapInfo.Status <= BeatmapOnlineStatus.Pending)
2019-07-05 12:39:21 +08:00
{
SetErrorState(LeaderboardState.BeatmapUnavailable);
2019-07-05 12:39:21 +08:00
return null;
}
if (!api.LocalUser.Value.IsSupporter && (Scope != BeatmapLeaderboardScope.Global || filterMods))
{
2022-01-31 00:12:03 +08:00
SetErrorState(LeaderboardState.NotSupporter);
return null;
}
IReadOnlyList<Mod>? requestMods = null;
2019-07-04 00:58:13 +08:00
if (filterMods && !mods.Value.Any())
// add nomod for the request
requestMods = new Mod[] { new ModNoMod() };
else if (filterMods)
requestMods = mods.Value;
2023-01-04 01:44:00 +08:00
var newRequest = new GetScoresRequest(fetchBeatmapInfo, fetchRuleset, Scope, requestMods);
newRequest.Success += response => Schedule(() =>
{
2023-01-04 01:44:00 +08:00
// Request may have changed since fetch request.
// Can't rely on request cancellation due to Schedule inside SetScores so let's play it safe.
if (!newRequest.Equals(scoreRetrievalRequest))
return;
SetScores(
scoreManager.OrderByTotalScore(response.Scores.Select(s => s.ToScoreInfo(rulesets, fetchBeatmapInfo))),
response.UserScore?.CreateScoreInfo(rulesets, fetchBeatmapInfo)
);
});
2023-01-04 01:44:00 +08:00
return scoreRetrievalRequest = newRequest;
}
protected override LeaderboardScore CreateDrawableScore(ScoreInfo model, int index) => new LeaderboardScore(model, index, IsOnlineScope)
{
Action = () => ScoreSelected?.Invoke(model)
};
protected override LeaderboardScore CreateDrawableTopScore(ScoreInfo model) => new LeaderboardScore(model, model.Position, false)
{
Action = () => ScoreSelected?.Invoke(model)
};
private void subscribeToLocalScores(BeatmapInfo beatmapInfo, CancellationToken cancellationToken)
2022-01-28 21:47:45 +08:00
{
Debug.Assert(beatmapInfo != null);
2022-01-28 21:47:45 +08:00
scoreSubscription?.Dispose();
scoreSubscription = null;
scoreSubscription = realm.RegisterForNotifications(r =>
r.All<ScoreInfo>().Filter($"{nameof(ScoreInfo.BeatmapInfo)}.{nameof(BeatmapInfo.ID)} == $0"
+ $" AND {nameof(ScoreInfo.Ruleset)}.{nameof(RulesetInfo.ShortName)} == $1"
+ $" AND {nameof(ScoreInfo.DeletePending)} == false"
, beatmapInfo.ID, ruleset.Value.ShortName), localScoresChanged);
void localScoresChanged(IRealmCollection<ScoreInfo> sender, ChangeSet? changes, Exception exception)
2022-01-28 22:14:26 +08:00
{
2022-01-28 22:17:06 +08:00
if (cancellationToken.IsCancellationRequested)
2022-01-28 22:14:26 +08:00
return;
2022-01-28 21:47:45 +08:00
// This subscription may fire from changes to linked beatmaps, which we don't care about.
// It's currently not possible for a score to be modified after insertion, so we can safely ignore callbacks with only modifications.
if (changes?.HasCollectionChanges() == false)
return;
2022-01-28 22:14:26 +08:00
var scores = sender.AsEnumerable();
2022-01-28 21:47:45 +08:00
2022-01-28 22:14:26 +08:00
if (filterMods && !mods.Value.Any())
{
// we need to filter out all scores that have any mods to get all local nomod scores
scores = scores.Where(s => !s.Mods.Any());
}
else if (filterMods)
{
// otherwise find all the scores that have all of the currently selected mods (similar to how web applies mod filters)
2022-11-21 02:27:40 +08:00
// we're creating and using a string HashSet representation of selected mods so that it can be translated into the DB query itself
2022-11-21 02:24:51 +08:00
var selectedMods = mods.Value.Select(m => m.Acronym).ToHashSet();
2022-11-21 02:24:51 +08:00
scores = scores.Where(s => selectedMods.SetEquals(s.Mods.Select(m => m.Acronym)));
2022-01-28 22:14:26 +08:00
}
2022-01-28 21:47:45 +08:00
2022-08-22 20:31:30 +08:00
scores = scoreManager.OrderByTotalScore(scores.Detach());
SetScores(scores);
2022-01-28 22:14:26 +08:00
}
2022-01-28 21:47:45 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
scoreSubscription?.Dispose();
scoreRetrievalRequest?.Cancel();
}
}
}