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