osu/osu.Game/Screens/Select/Carousel/TopLocalRank.cs

91 lines
2.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.
2020-05-19 07:44:22 +00:00
using System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2020-04-07 06:30:06 +00:00
using osu.Framework.Graphics;
using osu.Framework.Threading;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
2020-04-07 06:31:22 +00:00
using osu.Game.Online.Leaderboards;
using osu.Game.Rulesets;
using osu.Game.Scoring;
2020-04-07 06:31:22 +00:00
namespace osu.Game.Screens.Select.Carousel
{
public class TopLocalRank : UpdateableRank
{
private readonly BeatmapInfo beatmap;
2020-04-07 05:49:24 +00:00
[Resolved]
private ScoreManager scores { get; set; }
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
[Resolved]
private IAPIProvider api { get; set; }
2020-05-19 07:44:22 +00:00
private IBindable<WeakReference<ScoreInfo>> itemAdded;
private IBindable<WeakReference<ScoreInfo>> itemRemoved;
2020-04-04 19:42:13 +00:00
public TopLocalRank(BeatmapInfo beatmap)
: base(null)
{
this.beatmap = beatmap;
}
[BackgroundDependencyLoader]
2020-04-07 05:49:24 +00:00
private void load()
{
2020-05-19 07:44:22 +00:00
itemAdded = scores.ItemAdded.GetBoundCopy();
itemAdded.BindValueChanged(scoreChanged);
itemRemoved = scores.ItemRemoved.GetBoundCopy();
itemRemoved.BindValueChanged(scoreChanged);
ruleset.ValueChanged += _ => fetchAndLoadTopScore();
fetchAndLoadTopScore();
}
2020-05-19 07:44:22 +00:00
private void scoreChanged(ValueChangedEvent<WeakReference<ScoreInfo>> weakScore)
{
2020-05-19 07:44:22 +00:00
if (weakScore.NewValue.TryGetTarget(out var score))
{
if (score.BeatmapInfoID == beatmap.ID)
fetchAndLoadTopScore();
}
}
2020-04-07 06:30:06 +00:00
private ScheduledDelegate scheduledRankUpdate;
private void fetchAndLoadTopScore()
{
2020-04-07 05:50:11 +00:00
var rank = fetchTopScore()?.Rank;
2020-04-07 06:30:06 +00:00
scheduledRankUpdate = Schedule(() =>
{
Rank = rank;
2020-04-07 06:30:06 +00:00
// Required since presence is changed via IsPresent override
Invalidate(Invalidation.Presence);
});
}
2020-04-07 06:30:06 +00: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()
{
if (scores == null || beatmap == null || ruleset?.Value == null || api?.LocalUser.Value == null)
return null;
return scores.QueryScores(s => s.UserID == api.LocalUser.Value.Id && s.BeatmapInfoID == beatmap.ID && s.RulesetID == ruleset.Value.ID && !s.DeletePending)
.OrderByDescending(s => s.TotalScore)
.FirstOrDefault();
}
}
}