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
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
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;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
2019-07-14 19:03:14 +08:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2018-12-14 18:51:27 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Select.Leaderboards
|
|
|
|
{
|
|
|
|
public class BeatmapLeaderboard : Leaderboard<BeatmapLeaderboardScope, ScoreInfo>
|
|
|
|
{
|
|
|
|
public Action<ScoreInfo> ScoreSelected;
|
|
|
|
|
|
|
|
private BeatmapInfo beatmap;
|
|
|
|
|
|
|
|
public BeatmapInfo Beatmap
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
get => beatmap;
|
2018-12-14 18:51:27 +08:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (beatmap == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
beatmap = value;
|
|
|
|
Scores = null;
|
|
|
|
|
|
|
|
UpdateScores();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 14:23:33 +08:00
|
|
|
public APILegacyUserTopScoreInfo TopScore
|
|
|
|
{
|
|
|
|
get => topScoreContainer.Score.Value;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == null)
|
|
|
|
topScoreContainer.Hide();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
topScoreContainer.Show();
|
|
|
|
topScoreContainer.Score.Value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:34:20 +08:00
|
|
|
private bool filterMods;
|
|
|
|
|
2019-09-19 14:23:33 +08:00
|
|
|
private UserTopScoreContainer topScoreContainer;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
UpdateScores();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
ruleset.ValueChanged += _ => UpdateScores();
|
2019-07-03 19:34:20 +08:00
|
|
|
mods.ValueChanged += _ =>
|
|
|
|
{
|
|
|
|
if (filterMods)
|
|
|
|
UpdateScores();
|
|
|
|
};
|
2019-09-19 13:52:31 +08:00
|
|
|
|
2019-09-19 19:47:33 +08:00
|
|
|
Content.Add(topScoreContainer = new UserTopScoreContainer
|
|
|
|
{
|
|
|
|
ScoreSelected = s => ScoreSelected?.Invoke(s)
|
|
|
|
});
|
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;
|
2018-12-14 18:51:27 +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
|
|
|
|
2018-12-14 18:51:27 +08:00
|
|
|
protected override APIRequest FetchScores(Action<IEnumerable<ScoreInfo>> scoresCallback)
|
|
|
|
{
|
2019-09-05 10:56:21 +08:00
|
|
|
if (Beatmap == null)
|
|
|
|
{
|
|
|
|
PlaceholderState = PlaceholderState.NoneSelected;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-14 18:51:27 +08:00
|
|
|
if (Scope == BeatmapLeaderboardScope.Local)
|
|
|
|
{
|
2019-07-04 00:58:13 +08:00
|
|
|
var scores = scoreManager
|
|
|
|
.QueryScores(s => !s.DeletePending && s.Beatmap.ID == Beatmap.ID && s.Ruleset.ID == ruleset.Value.ID);
|
|
|
|
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Scores = scores.OrderByDescending(s => s.TotalScore).ToArray();
|
2018-12-14 18:51:27 +08:00
|
|
|
PlaceholderState = Scores.Any() ? PlaceholderState.Successful : PlaceholderState.NoScores;
|
2019-07-04 00:58:13 +08:00
|
|
|
|
2018-12-14 18:51:27 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:05:07 +08:00
|
|
|
if (api?.IsLoggedIn != true)
|
|
|
|
{
|
|
|
|
PlaceholderState = PlaceholderState.NotLoggedIn;
|
|
|
|
return null;
|
2019-07-05 12:39:21 +08:00
|
|
|
}
|
|
|
|
|
2019-09-05 10:56:21 +08:00
|
|
|
if (Beatmap.OnlineBeatmapID == null || Beatmap?.Status <= BeatmapSetOnlineStatus.Pending)
|
2019-07-05 12:39:21 +08:00
|
|
|
{
|
|
|
|
PlaceholderState = PlaceholderState.Unavailable;
|
|
|
|
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
|
|
|
{
|
|
|
|
PlaceholderState = PlaceholderState.NotSupporter;
|
|
|
|
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;
|
|
|
|
|
|
|
|
var req = new GetScoresRequest(Beatmap, ruleset.Value ?? Beatmap.Ruleset, Scope, requestMods);
|
2018-12-14 18:51:27 +08:00
|
|
|
|
2019-07-14 19:03:14 +08:00
|
|
|
req.Success += r =>
|
|
|
|
{
|
|
|
|
scoresCallback?.Invoke(r.Scores);
|
2019-09-19 14:23:33 +08:00
|
|
|
TopScore = r.UserScore;
|
2019-07-14 19:03:14 +08:00
|
|
|
};
|
2018-12-14 18:51:27 +08:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:19:15 +08:00
|
|
|
protected override LeaderboardScore CreateDrawableScore(ScoreInfo model, int index) => new LeaderboardScore(model, index, IsOnlineScope)
|
2018-12-14 18:51:27 +08:00
|
|
|
{
|
|
|
|
Action = () => ScoreSelected?.Invoke(model)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|