1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 20:32:55 +08:00
osu-lazer/osu.Game/Screens/Select/Leaderboards/BeatmapLeaderboard.cs

214 lines
6.9 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.Linq;
2021-08-31 20:36:31 +08:00
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
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;
namespace osu.Game.Screens.Select.Leaderboards
{
public class BeatmapLeaderboard : Leaderboard<BeatmapLeaderboardScope, ScoreInfo>
{
public Action<ScoreInfo> ScoreSelected;
[Resolved]
private RulesetStore rulesets { get; set; }
2021-10-02 23:55:29 +08:00
private BeatmapInfo beatmapInfo;
2021-10-02 23:55:29 +08:00
public BeatmapInfo BeatmapInfo
{
2021-10-02 23:55:29 +08:00
get => beatmapInfo;
set
{
2021-10-02 23:55:29 +08:00
if (beatmapInfo == value)
return;
2021-10-02 23:55:29 +08:00
beatmapInfo = value;
Scores = null;
UpdateScores();
}
}
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;
UpdateScores();
}
}
[Resolved]
private ScoreManager scoreManager { get; set; }
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; }
[Resolved]
private IAPIProvider api { get; set; }
[BackgroundDependencyLoader]
private void load()
{
ruleset.ValueChanged += _ => UpdateScores();
mods.ValueChanged += _ =>
{
if (filterMods)
UpdateScores();
};
2019-09-19 13:52:31 +08:00
scoreManager.ItemRemoved += scoreStoreChanged;
scoreManager.ItemUpdated += scoreStoreChanged;
2019-09-19 13:52:31 +08:00
}
2019-09-19 14:23:33 +08:00
protected override void Reset()
2019-09-19 13:52:31 +08:00
{
2019-09-19 14:23:33 +08:00
base.Reset();
TopScore = null;
}
private void scoreStoreChanged(ScoreInfo score)
{
if (Scope != BeatmapLeaderboardScope.Local)
return;
if (BeatmapInfo?.ID != score.BeatmapInfoID)
return;
2021-06-14 13:26:40 +08:00
RefreshScores();
}
2019-07-21 09:42:40 +08:00
protected override bool IsOnlineScope => Scope != BeatmapLeaderboardScope.Local;
2019-07-21 08:07:27 +08:00
2021-08-31 20:36:31 +08:00
private CancellationTokenSource loadCancellationSource;
protected override APIRequest FetchScores(Action<IEnumerable<ScoreInfo>> scoresCallback)
{
2021-08-31 20:36:31 +08:00
loadCancellationSource?.Cancel();
loadCancellationSource = new CancellationTokenSource();
var cancellationToken = loadCancellationSource.Token;
2021-10-02 23:55:29 +08:00
if (BeatmapInfo == null)
{
PlaceholderState = PlaceholderState.NoneSelected;
return null;
}
if (Scope == BeatmapLeaderboardScope.Local)
{
2019-07-04 00:58:13 +08:00
var scores = scoreManager
.QueryScores(s => !s.DeletePending && s.BeatmapInfo.ID == BeatmapInfo.ID && s.Ruleset.ID == ruleset.Value.ID);
2019-07-04 00:58:13 +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)));
}
scoreManager.OrderByTotalScoreAsync(scores.ToArray(), cancellationToken)
2021-08-31 20:36:31 +08:00
.ContinueWith(ordered => scoresCallback?.Invoke(ordered.Result), TaskContinuationOptions.OnlyOnRanToCompletion);
2019-07-04 00:58:13 +08:00
return null;
}
if (api?.IsLoggedIn != true)
{
PlaceholderState = PlaceholderState.NotLoggedIn;
return null;
2019-07-05 12:39:21 +08:00
}
if (BeatmapInfo.OnlineID == null || BeatmapInfo?.Status <= BeatmapOnlineStatus.Pending)
2019-07-05 12:39:21 +08:00
{
PlaceholderState = PlaceholderState.Unavailable;
return null;
}
if (!api.LocalUser.Value.IsSupporter && (Scope != BeatmapLeaderboardScope.Global || filterMods))
{
PlaceholderState = PlaceholderState.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;
2021-10-02 23:55:29 +08:00
var req = new GetScoresRequest(BeatmapInfo, ruleset.Value ?? BeatmapInfo.Ruleset, Scope, requestMods);
req.Success += r =>
{
scoreManager.OrderByTotalScoreAsync(r.Scores.Select(s => s.CreateScoreInfo(rulesets, BeatmapInfo)).ToArray(), cancellationToken)
2021-08-31 20:36:31 +08:00
.ContinueWith(ordered => Schedule(() =>
{
if (cancellationToken.IsCancellationRequested)
2021-08-31 20:36:31 +08:00
return;
scoresCallback?.Invoke(ordered.Result);
TopScore = r.UserScore?.CreateScoreInfo(rulesets, BeatmapInfo);
2021-08-31 20:36:31 +08:00
}), TaskContinuationOptions.OnlyOnRanToCompletion);
};
return req;
}
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)
};
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (scoreManager != null)
{
scoreManager.ItemRemoved -= scoreStoreChanged;
scoreManager.ItemUpdated -= scoreStoreChanged;
}
}
}
}