1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 15:52:55 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/TopLocalRank.cs

92 lines
2.8 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.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2020-04-07 14:30:06 +08:00
using osu.Framework.Graphics;
using osu.Framework.Threading;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
2020-04-07 14:31:22 +08:00
using osu.Game.Online.Leaderboards;
using osu.Game.Rulesets;
using osu.Game.Scoring;
2020-04-07 14:31:22 +08:00
namespace osu.Game.Screens.Select.Carousel
{
public class TopLocalRank : UpdateableRank
{
2021-10-02 23:55:29 +08:00
private readonly BeatmapInfo beatmapInfo;
2020-04-07 13:49:24 +08:00
[Resolved]
private ScoreManager scores { get; set; }
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
[Resolved]
private IAPIProvider api { get; set; }
2021-10-02 23:55:29 +08:00
public TopLocalRank(BeatmapInfo beatmapInfo)
2020-04-05 03:42:13 +08:00
: base(null)
{
2021-10-02 23:55:29 +08:00
this.beatmapInfo = beatmapInfo;
}
[BackgroundDependencyLoader]
2020-04-07 13:49:24 +08:00
private void load()
{
scores.ItemUpdated += scoreChanged;
scores.ItemRemoved += scoreChanged;
2020-05-19 15:44:22 +08:00
ruleset.ValueChanged += _ => fetchAndLoadTopScore();
fetchAndLoadTopScore();
}
private void scoreChanged(ScoreInfo score)
{
if (score.BeatmapInfoID == beatmapInfo.ID)
fetchAndLoadTopScore();
}
2020-04-07 14:30:06 +08:00
private ScheduledDelegate scheduledRankUpdate;
private void fetchAndLoadTopScore()
{
2020-04-07 13:50:11 +08:00
var rank = fetchTopScore()?.Rank;
2020-04-07 14:30:06 +08:00
scheduledRankUpdate = Schedule(() =>
{
Rank = rank;
2020-04-07 14:30:06 +08:00
// Required since presence is changed via IsPresent override
Invalidate(Invalidation.Presence);
});
}
2020-04-07 14:30:06 +08:00
// We're present if a rank is set, or if there is a pending rank update (IsPresent = true is required for the scheduler to run).
public override bool IsPresent => base.IsPresent && (Rank != null || scheduledRankUpdate?.Completed == false);
private ScoreInfo fetchTopScore()
{
2021-10-02 23:55:29 +08:00
if (scores == null || beatmapInfo == null || ruleset?.Value == null || api?.LocalUser.Value == null)
return null;
2021-10-02 23:55:29 +08:00
return scores.QueryScores(s => s.UserID == api.LocalUser.Value.Id && s.BeatmapInfoID == beatmapInfo.ID && s.RulesetID == ruleset.Value.ID && !s.DeletePending)
.OrderByDescending(s => s.TotalScore)
.FirstOrDefault();
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (scores != null)
{
scores.ItemUpdated -= scoreChanged;
scores.ItemRemoved -= scoreChanged;
}
}
}
}