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