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;
|
|
|
|
using System.Linq;
|
2021-08-31 21:36:31 +09:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Select.Leaderboards
|
|
|
|
{
|
|
|
|
public class BeatmapLeaderboard : Leaderboard<BeatmapLeaderboardScope, ScoreInfo>
|
|
|
|
{
|
|
|
|
public Action<ScoreInfo> ScoreSelected;
|
|
|
|
|
2019-12-03 15:28:10 +09:00
|
|
|
[Resolved]
|
|
|
|
private RulesetStore rulesets { get; set; }
|
|
|
|
|
2021-10-03 00:55:29 +09:00
|
|
|
private BeatmapInfo beatmapInfo;
|
2018-12-14 19:51:27 +09:00
|
|
|
|
2021-10-03 00:55:29 +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
|
|
|
|
{
|
2021-10-03 00:55:29 +09:00
|
|
|
if (beatmapInfo == value)
|
2018-12-14 19:51:27 +09:00
|
|
|
return;
|
|
|
|
|
2021-10-03 00:55:29 +09:00
|
|
|
beatmapInfo = value;
|
2018-12-14 19:51:27 +09:00
|
|
|
Scores = null;
|
|
|
|
|
|
|
|
UpdateScores();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 17:04:20 +05:30
|
|
|
private bool filterMods;
|
|
|
|
|
2020-05-19 16:44:22 +09:00
|
|
|
private IBindable<WeakReference<ScoreInfo>> itemRemoved;
|
|
|
|
|
2021-06-14 14:20:23 +09:00
|
|
|
private IBindable<WeakReference<ScoreInfo>> itemAdded;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
UpdateScores();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 19:51:27 +09:00
|
|
|
[Resolved]
|
|
|
|
private ScoreManager scoreManager { get; set; }
|
|
|
|
|
2021-08-31 21:36:31 +09:00
|
|
|
[Resolved]
|
|
|
|
private BeatmapDifficultyCache difficultyCache { get; set; }
|
|
|
|
|
2018-12-14 19:51:27 +09:00
|
|
|
[Resolved]
|
|
|
|
private IBindable<RulesetInfo> ruleset { get; set; }
|
|
|
|
|
2019-07-03 17:04:20 +05:30
|
|
|
[Resolved]
|
|
|
|
private IBindable<IReadOnlyList<Mod>> mods { get; set; }
|
|
|
|
|
2018-12-14 19:51:27 +09:00
|
|
|
[Resolved]
|
2019-03-13 12:56:47 +09:00
|
|
|
private IAPIProvider api { get; set; }
|
2018-12-14 19:51:27 +09:00
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
ruleset.ValueChanged += _ => UpdateScores();
|
2019-07-03 17:04:20 +05:30
|
|
|
mods.ValueChanged += _ =>
|
|
|
|
{
|
|
|
|
if (filterMods)
|
|
|
|
UpdateScores();
|
|
|
|
};
|
2019-09-19 14:52:31 +09:00
|
|
|
|
2020-05-19 16:44:22 +09:00
|
|
|
itemRemoved = scoreManager.ItemRemoved.GetBoundCopy();
|
|
|
|
itemRemoved.BindValueChanged(onScoreRemoved);
|
2021-06-14 14:20:23 +09:00
|
|
|
|
|
|
|
itemAdded = scoreManager.ItemUpdated.GetBoundCopy();
|
|
|
|
itemAdded.BindValueChanged(onScoreAdded);
|
2019-09-19 14:52:31 +09:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:23:33 +09:00
|
|
|
protected override void Reset()
|
2019-09-19 14:52:31 +09:00
|
|
|
{
|
2019-09-19 15:23:33 +09:00
|
|
|
base.Reset();
|
|
|
|
TopScore = null;
|
2018-12-14 19:51:27 +09:00
|
|
|
}
|
|
|
|
|
2021-06-14 14:26:54 +09:00
|
|
|
private void onScoreRemoved(ValueChangedEvent<WeakReference<ScoreInfo>> score) =>
|
|
|
|
scoreStoreChanged(score);
|
2021-06-14 14:20:23 +09:00
|
|
|
|
2021-06-14 14:26:54 +09:00
|
|
|
private void onScoreAdded(ValueChangedEvent<WeakReference<ScoreInfo>> score) =>
|
|
|
|
scoreStoreChanged(score);
|
2021-06-14 14:20:23 +09:00
|
|
|
|
2021-06-14 14:26:54 +09:00
|
|
|
private void scoreStoreChanged(ValueChangedEvent<WeakReference<ScoreInfo>> score)
|
2021-06-14 14:20:23 +09:00
|
|
|
{
|
|
|
|
if (Scope != BeatmapLeaderboardScope.Local)
|
|
|
|
return;
|
|
|
|
|
2021-06-14 14:26:54 +09:00
|
|
|
if (score.NewValue.TryGetTarget(out var scoreInfo))
|
|
|
|
{
|
2021-10-03 00:55:29 +09:00
|
|
|
if (BeatmapInfo?.ID != scoreInfo.BeatmapInfoID)
|
2021-06-14 14:26:54 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-14 14:26:40 +09:00
|
|
|
RefreshScores();
|
2021-06-14 14:20:23 +09:00
|
|
|
}
|
2019-12-18 19:22:42 -08:00
|
|
|
|
2019-07-21 10:42:40 +09:00
|
|
|
protected override bool IsOnlineScope => Scope != BeatmapLeaderboardScope.Local;
|
2019-07-21 03:07:27 +03:00
|
|
|
|
2021-08-31 21:36:31 +09:00
|
|
|
private CancellationTokenSource loadCancellationSource;
|
|
|
|
|
2018-12-14 19:51:27 +09:00
|
|
|
protected override APIRequest FetchScores(Action<IEnumerable<ScoreInfo>> scoresCallback)
|
|
|
|
{
|
2021-08-31 21:36:31 +09:00
|
|
|
loadCancellationSource?.Cancel();
|
|
|
|
loadCancellationSource = new CancellationTokenSource();
|
|
|
|
|
2021-10-03 00:55:29 +09:00
|
|
|
if (BeatmapInfo == null)
|
2019-09-05 05:56:21 +03:00
|
|
|
{
|
|
|
|
PlaceholderState = PlaceholderState.NoneSelected;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-14 19:51:27 +09:00
|
|
|
if (Scope == BeatmapLeaderboardScope.Local)
|
|
|
|
{
|
2019-07-03 22:28:13 +05:30
|
|
|
var scores = scoreManager
|
2021-10-04 17:35:53 +09:00
|
|
|
.QueryScores(s => !s.DeletePending && s.BeatmapInfo.ID == BeatmapInfo.ID && s.Ruleset.ID == ruleset.Value.ID);
|
2019-07-03 22:28:13 +05:30
|
|
|
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2021-09-01 15:41:52 +09:00
|
|
|
scoreManager.OrderByTotalScoreAsync(scores.ToArray(), loadCancellationSource.Token)
|
2021-08-31 21:36:31 +09:00
|
|
|
.ContinueWith(ordered => scoresCallback?.Invoke(ordered.Result), TaskContinuationOptions.OnlyOnRanToCompletion);
|
2019-07-03 22:28:13 +05:30
|
|
|
|
2018-12-14 19:51:27 +09:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-04 12:05:07 -07:00
|
|
|
if (api?.IsLoggedIn != true)
|
|
|
|
{
|
|
|
|
PlaceholderState = PlaceholderState.NotLoggedIn;
|
|
|
|
return null;
|
2019-07-04 21:39:21 -07:00
|
|
|
}
|
|
|
|
|
2021-10-03 00:55:29 +09:00
|
|
|
if (BeatmapInfo.OnlineBeatmapID == null || BeatmapInfo?.Status <= BeatmapSetOnlineStatus.Pending)
|
2019-07-04 21:39:21 -07:00
|
|
|
{
|
|
|
|
PlaceholderState = PlaceholderState.Unavailable;
|
|
|
|
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
|
|
|
{
|
|
|
|
PlaceholderState = PlaceholderState.NotSupporter;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-03 17:04:20 +05:30
|
|
|
IReadOnlyList<Mod> requestMods = null;
|
|
|
|
|
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;
|
|
|
|
|
2021-10-03 00:55:29 +09:00
|
|
|
var req = new GetScoresRequest(BeatmapInfo, ruleset.Value ?? BeatmapInfo.Ruleset, Scope, requestMods);
|
2018-12-14 19:51:27 +09:00
|
|
|
|
2019-07-14 14:03:14 +03:00
|
|
|
req.Success += r =>
|
|
|
|
{
|
2021-10-29 14:14:25 +09:00
|
|
|
scoreManager.OrderByTotalScoreAsync(r.Scores.Select(s => s.CreateScoreInfo(rulesets, BeatmapInfo)).ToArray(), loadCancellationSource.Token)
|
2021-08-31 21:36:31 +09:00
|
|
|
.ContinueWith(ordered => Schedule(() =>
|
|
|
|
{
|
|
|
|
if (loadCancellationSource.IsCancellationRequested)
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoresCallback?.Invoke(ordered.Result);
|
2021-10-29 14:14:25 +09:00
|
|
|
TopScore = r.UserScore?.CreateScoreInfo(rulesets, BeatmapInfo);
|
2021-08-31 21:36:31 +09:00
|
|
|
}), TaskContinuationOptions.OnlyOnRanToCompletion);
|
2019-07-14 14:03:14 +03:00
|
|
|
};
|
2018-12-14 19:51:27 +09:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
};
|
2018-12-14 19:51:27 +09:00
|
|
|
}
|
|
|
|
}
|