1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 20:07:24 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/TopLocalRank.cs

81 lines
3.0 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.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2020-04-07 14:30:06 +08:00
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
2021-12-13 18:01:20 +08:00
using osu.Game.Database;
using osu.Game.Models;
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;
using Realms;
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 IBindable<RulesetInfo> ruleset { get; set; }
2021-12-13 18:01:20 +08:00
[Resolved]
private RealmContextFactory realmFactory { get; set; }
2020-04-07 13:49:24 +08:00
[Resolved]
private IAPIProvider api { get; set; }
private IDisposable scoreSubscription;
private bool rankUpdatePending;
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;
}
protected override void LoadComplete()
{
base.LoadComplete();
ruleset.BindValueChanged(_ =>
2020-04-07 14:30:06 +08:00
{
rankUpdatePending = true;
2020-04-07 14:30:06 +08:00
// Required since presence is changed via IsPresent override
Invalidate(Invalidation.Presence);
scoreSubscription?.Dispose();
scoreSubscription = realmFactory.Context.All<ScoreInfo>()
.Filter($"{nameof(ScoreInfo.User)}.{nameof(RealmUser.OnlineID)} == $0"
2022-01-18 18:28:09 +08:00
+ $" && {nameof(ScoreInfo.BeatmapInfo)}.{nameof(BeatmapInfo.ID)} == $1"
+ $" && {nameof(ScoreInfo.Ruleset)}.{nameof(RulesetInfo.ShortName)} == $2"
+ $" && {nameof(ScoreInfo.DeletePending)} == false", api.LocalUser.Value.Id, beatmapInfo.ID, ruleset.Value.ShortName)
.OrderByDescending(s => s.TotalScore)
.QueryAsyncWithNotifications((items, changes, ___) =>
{
if (changes == null)
rankUpdatePending = false;
Rank = items.FirstOrDefault()?.Rank;
});
}, true);
}
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 || rankUpdatePending);
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
scoreSubscription?.Dispose();
}
}
}