2019-01-24 08:43:03 +00:00
|
|
|
// 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.
|
2018-12-14 10:51:27 +00:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2022-07-01 06:42:35 +00:00
|
|
|
using System.Diagnostics;
|
2018-12-14 10:51:27 +00:00
|
|
|
using System.Linq;
|
2021-08-31 12:36:31 +00:00
|
|
|
using System.Threading;
|
2018-12-14 10:51:27 +00:00
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 10:04:31 +00:00
|
|
|
using osu.Framework.Bindables;
|
2018-12-14 10:51:27 +00:00
|
|
|
using osu.Game.Beatmaps;
|
2021-12-13 10:01:20 +00:00
|
|
|
using osu.Game.Database;
|
2022-03-03 05:15:25 +00:00
|
|
|
using osu.Game.Extensions;
|
2018-12-14 10:51:27 +00:00
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
using osu.Game.Online.Leaderboards;
|
|
|
|
using osu.Game.Rulesets;
|
2019-07-03 11:34:20 +00:00
|
|
|
using osu.Game.Rulesets.Mods;
|
2018-12-14 10:51:27 +00:00
|
|
|
using osu.Game.Scoring;
|
2022-01-07 05:47:03 +00:00
|
|
|
using Realms;
|
2018-12-14 10:51:27 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Select.Leaderboards
|
|
|
|
{
|
2022-09-16 09:15:17 +00:00
|
|
|
public partial class BeatmapLeaderboard : Leaderboard<BeatmapLeaderboardScope, ScoreInfo>
|
2018-12-14 10:51:27 +00:00
|
|
|
{
|
2022-09-26 07:15:18 +00:00
|
|
|
public Action<ScoreInfo>? ScoreSelected;
|
2018-12-14 10:51:27 +00:00
|
|
|
|
2022-09-26 07:15:18 +00:00
|
|
|
private BeatmapInfo? beatmapInfo;
|
2018-12-14 10:51:27 +00:00
|
|
|
|
2022-09-26 07:15:18 +00:00
|
|
|
public BeatmapInfo? BeatmapInfo
|
2018-12-14 10:51:27 +00:00
|
|
|
{
|
2021-10-02 15:55:29 +00:00
|
|
|
get => beatmapInfo;
|
2018-12-14 10:51:27 +00:00
|
|
|
set
|
|
|
|
{
|
2022-01-28 05:18:10 +00:00
|
|
|
if (beatmapInfo == null && value == null)
|
|
|
|
return;
|
|
|
|
|
2021-12-17 10:39:04 +00:00
|
|
|
if (beatmapInfo?.Equals(value) == true)
|
2018-12-14 10:51:27 +00:00
|
|
|
return;
|
|
|
|
|
2021-10-02 15:55:29 +00:00
|
|
|
beatmapInfo = value;
|
2022-09-21 06:21:32 +00:00
|
|
|
|
|
|
|
// 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 13:47:45 +00:00
|
|
|
RefetchScores();
|
2018-12-14 10:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 11:34:20 +00:00
|
|
|
private bool filterMods;
|
|
|
|
|
2019-07-03 16:58:13 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Whether to apply the game's currently selected mods as a filter when retrieving scores.
|
|
|
|
/// </summary>
|
2019-07-03 11:34:20 +00:00
|
|
|
public bool FilterMods
|
|
|
|
{
|
|
|
|
get => filterMods;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == filterMods)
|
|
|
|
return;
|
|
|
|
|
|
|
|
filterMods = value;
|
|
|
|
|
2022-01-28 12:22:09 +00:00
|
|
|
RefetchScores();
|
2019-07-03 11:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 10:51:27 +00:00
|
|
|
[Resolved]
|
2022-09-26 07:15:18 +00:00
|
|
|
private ScoreManager scoreManager { get; set; } = null!;
|
2018-12-14 10:51:27 +00:00
|
|
|
|
|
|
|
[Resolved]
|
2022-09-26 07:15:18 +00:00
|
|
|
private IBindable<RulesetInfo> ruleset { get; set; } = null!;
|
2018-12-14 10:51:27 +00:00
|
|
|
|
2019-07-03 11:34:20 +00:00
|
|
|
[Resolved]
|
2022-09-26 07:15:18 +00:00
|
|
|
private IBindable<IReadOnlyList<Mod>> mods { get; set; } = null!;
|
2019-07-03 11:34:20 +00:00
|
|
|
|
2018-12-14 10:51:27 +00:00
|
|
|
[Resolved]
|
2022-09-26 07:15:18 +00:00
|
|
|
private IAPIProvider api { get; set; } = null!;
|
2018-12-14 10:51:27 +00:00
|
|
|
|
2022-01-28 13:47:45 +00:00
|
|
|
[Resolved]
|
2022-09-26 07:15:18 +00:00
|
|
|
private RulesetStore rulesets { get; set; } = null!;
|
2022-01-28 13:47:45 +00:00
|
|
|
|
|
|
|
[Resolved]
|
2022-09-26 07:15:18 +00:00
|
|
|
private RealmAccess realm { get; set; } = null!;
|
2022-01-28 13:47:45 +00:00
|
|
|
|
2022-09-26 07:15:18 +00:00
|
|
|
private IDisposable? scoreSubscription;
|
2022-01-28 13:47:45 +00:00
|
|
|
|
2022-09-26 07:15:18 +00:00
|
|
|
private GetScoresRequest? scoreRetrievalRequest;
|
2022-09-26 07:02:33 +00:00
|
|
|
|
2018-12-14 10:51:27 +00:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2022-01-28 12:22:09 +00:00
|
|
|
ruleset.ValueChanged += _ => RefetchScores();
|
2019-07-03 11:34:20 +00:00
|
|
|
mods.ValueChanged += _ =>
|
|
|
|
{
|
|
|
|
if (filterMods)
|
2022-01-28 12:22:09 +00:00
|
|
|
RefetchScores();
|
2019-07-03 11:34:20 +00:00
|
|
|
};
|
2021-12-17 09:37:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 01:42:40 +00:00
|
|
|
protected override bool IsOnlineScope => Scope != BeatmapLeaderboardScope.Local;
|
2019-07-21 00:07:27 +00:00
|
|
|
|
2022-09-26 07:15:18 +00:00
|
|
|
protected override APIRequest? FetchScores(CancellationToken cancellationToken)
|
2018-12-14 10:51:27 +00:00
|
|
|
{
|
2021-12-22 08:24:01 +00:00
|
|
|
var fetchBeatmapInfo = BeatmapInfo;
|
|
|
|
|
|
|
|
if (fetchBeatmapInfo == null)
|
2019-09-05 02:56:21 +00:00
|
|
|
{
|
2022-01-30 16:12:03 +00:00
|
|
|
SetErrorState(LeaderboardState.NoneSelected);
|
2019-09-05 02:56:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-26 07:15:18 +00:00
|
|
|
var fetchRuleset = ruleset.Value ?? fetchBeatmapInfo.Ruleset;
|
|
|
|
|
2018-12-14 10:51:27 +00:00
|
|
|
if (Scope == BeatmapLeaderboardScope.Local)
|
|
|
|
{
|
2022-07-01 06:42:35 +00:00
|
|
|
subscribeToLocalScores(fetchBeatmapInfo, cancellationToken);
|
2022-01-21 08:08:20 +00:00
|
|
|
return null;
|
2018-12-14 10:51:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 07:15:18 +00:00
|
|
|
if (!api.IsLoggedIn)
|
2019-07-04 19:05:07 +00:00
|
|
|
{
|
2022-01-30 16:12:03 +00:00
|
|
|
SetErrorState(LeaderboardState.NotLoggedIn);
|
2019-07-04 19:05:07 +00:00
|
|
|
return null;
|
2019-07-05 04:39:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 05:15:25 +00:00
|
|
|
if (!fetchRuleset.IsLegacyRuleset())
|
2022-03-02 05:10:59 +00:00
|
|
|
{
|
|
|
|
SetErrorState(LeaderboardState.RulesetUnavailable);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-24 03:16:08 +00:00
|
|
|
if (fetchBeatmapInfo.OnlineID <= 0 || fetchBeatmapInfo.Status <= BeatmapOnlineStatus.Pending)
|
2019-07-05 04:39:21 +00:00
|
|
|
{
|
2022-03-02 05:10:59 +00:00
|
|
|
SetErrorState(LeaderboardState.BeatmapUnavailable);
|
2019-07-05 04:39:21 +00:00
|
|
|
return null;
|
2019-07-04 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 03:12:44 +00:00
|
|
|
if (!api.LocalUser.Value.IsSupporter && (Scope != BeatmapLeaderboardScope.Global || filterMods))
|
2018-12-14 10:51:27 +00:00
|
|
|
{
|
2022-01-30 16:12:03 +00:00
|
|
|
SetErrorState(LeaderboardState.NotSupporter);
|
2018-12-14 10:51:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-26 07:15:18 +00:00
|
|
|
IReadOnlyList<Mod>? requestMods = null;
|
2019-07-03 11:34:20 +00:00
|
|
|
|
2019-07-03 16:58:13 +00:00
|
|
|
if (filterMods && !mods.Value.Any())
|
2019-07-03 11:34:20 +00:00
|
|
|
// add nomod for the request
|
|
|
|
requestMods = new Mod[] { new ModNoMod() };
|
|
|
|
else if (filterMods)
|
|
|
|
requestMods = mods.Value;
|
|
|
|
|
2023-01-03 17:44:00 +00:00
|
|
|
scoreRetrievalRequest?.Cancel();
|
2018-12-14 10:51:27 +00:00
|
|
|
|
2023-01-03 17:44:00 +00:00
|
|
|
var newRequest = new GetScoresRequest(fetchBeatmapInfo, fetchRuleset, Scope, requestMods);
|
|
|
|
newRequest.Success += response => Schedule(() =>
|
2023-01-02 16:55:05 +00:00
|
|
|
{
|
2023-01-03 17:44:00 +00: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))
|
2023-01-02 16:55:05 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
SetScores(
|
|
|
|
scoreManager.OrderByTotalScore(response.Scores.Select(s => s.ToScoreInfo(rulesets, fetchBeatmapInfo))),
|
|
|
|
response.UserScore?.CreateScoreInfo(rulesets, fetchBeatmapInfo)
|
|
|
|
);
|
|
|
|
});
|
2018-12-14 10:51:27 +00:00
|
|
|
|
2023-01-03 17:44:00 +00:00
|
|
|
return scoreRetrievalRequest = newRequest;
|
2018-12-14 10:51:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 08:32:24 +00:00
|
|
|
protected override LeaderboardScore CreateDrawableScore(ScoreInfo model, int index) => new LeaderboardScore(model, index, IsOnlineScope)
|
2019-12-17 20:56:30 +00:00
|
|
|
{
|
2020-01-06 08:32:24 +00:00
|
|
|
Action = () => ScoreSelected?.Invoke(model)
|
|
|
|
};
|
2020-08-31 10:54:22 +00:00
|
|
|
|
|
|
|
protected override LeaderboardScore CreateDrawableTopScore(ScoreInfo model) => new LeaderboardScore(model, model.Position, false)
|
|
|
|
{
|
|
|
|
Action = () => ScoreSelected?.Invoke(model)
|
|
|
|
};
|
2021-11-05 09:05:31 +00:00
|
|
|
|
2022-07-01 06:42:35 +00:00
|
|
|
private void subscribeToLocalScores(BeatmapInfo beatmapInfo, CancellationToken cancellationToken)
|
2022-01-28 13:47:45 +00:00
|
|
|
{
|
2022-07-01 06:42:35 +00:00
|
|
|
Debug.Assert(beatmapInfo != null);
|
|
|
|
|
2022-01-28 13:47:45 +00: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);
|
|
|
|
|
2022-09-26 07:15:18 +00:00
|
|
|
void localScoresChanged(IRealmCollection<ScoreInfo> sender, ChangeSet? changes, Exception exception)
|
2022-01-28 14:14:26 +00:00
|
|
|
{
|
2022-01-28 14:17:06 +00:00
|
|
|
if (cancellationToken.IsCancellationRequested)
|
2022-01-28 14:14:26 +00:00
|
|
|
return;
|
2022-01-28 13:47:45 +00:00
|
|
|
|
2022-03-08 05:43:14 +00: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 14:14:26 +00:00
|
|
|
var scores = sender.AsEnumerable();
|
2022-01-28 13:47:45 +00:00
|
|
|
|
2022-01-28 14:14:26 +00: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)
|
|
|
|
{
|
2022-11-20 16:39:46 +00:00
|
|
|
// otherwise find all the scores that have all of the currently selected mods (similar to how web applies mod filters)
|
2022-11-20 18:27:40 +00: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-20 18:24:51 +00:00
|
|
|
var selectedMods = mods.Value.Select(m => m.Acronym).ToHashSet();
|
2022-11-20 16:39:46 +00:00
|
|
|
|
2022-11-20 18:24:51 +00:00
|
|
|
scores = scores.Where(s => selectedMods.SetEquals(s.Mods.Select(m => m.Acronym)));
|
2022-01-28 14:14:26 +00:00
|
|
|
}
|
2022-01-28 13:47:45 +00:00
|
|
|
|
2022-08-22 12:31:30 +00:00
|
|
|
scores = scoreManager.OrderByTotalScore(scores.Detach());
|
2022-01-31 04:50:53 +00:00
|
|
|
|
2022-09-20 08:01:44 +00:00
|
|
|
SetScores(scores);
|
2022-01-28 14:14:26 +00:00
|
|
|
}
|
2022-01-28 13:47:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 09:05:31 +00:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
2022-09-26 07:02:33 +00:00
|
|
|
|
2021-12-17 09:37:28 +00:00
|
|
|
scoreSubscription?.Dispose();
|
2022-09-26 07:02:33 +00:00
|
|
|
scoreRetrievalRequest?.Cancel();
|
2021-11-05 09:05:31 +00:00
|
|
|
}
|
2018-12-14 10:51:27 +00:00
|
|
|
}
|
|
|
|
}
|