1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 07:27:51 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/TopLocalRank.cs

94 lines
3.1 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.Framework.Graphics.Containers;
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 osuTK;
using Realms;
2020-04-07 14:31:22 +08:00
namespace osu.Game.Screens.Select.Carousel
{
public class TopLocalRank : CompositeDrawable
{
2021-10-02 23:55:29 +08:00
private readonly BeatmapInfo beatmapInfo;
2020-04-07 13:49:24 +08:00
[Resolved]
2022-07-23 14:25:06 +08:00
private IBindable<RulesetInfo> ruleset { get; set; } = null!;
2020-04-07 13:49:24 +08:00
2021-12-13 18:01:20 +08:00
[Resolved]
2022-07-23 14:25:06 +08:00
private RealmAccess realm { get; set; } = null!;
2021-12-13 18:01:20 +08:00
[Resolved]
2022-07-23 14:25:06 +08:00
private ScoreManager scoreManager { get; set; } = null!;
2020-04-07 13:49:24 +08:00
[Resolved]
2022-07-23 14:25:06 +08:00
private IAPIProvider api { get; set; } = null!;
2022-07-23 14:25:06 +08:00
private IDisposable? scoreSubscription;
private readonly UpdateableRank updateable;
public ScoreRank? DisplayedRank => updateable.Rank;
2021-10-02 23:55:29 +08:00
public TopLocalRank(BeatmapInfo beatmapInfo)
{
2021-10-02 23:55:29 +08:00
this.beatmapInfo = beatmapInfo;
AutoSizeAxes = Axes.Both;
InternalChild = updateable = new UpdateableRank
{
Size = new Vector2(40, 20),
Alpha = 0,
};
}
protected override void LoadComplete()
{
base.LoadComplete();
ruleset.BindValueChanged(_ =>
2020-04-07 14:30:06 +08:00
{
scoreSubscription?.Dispose();
scoreSubscription = realm.RegisterForNotifications(r =>
r.All<ScoreInfo>()
.Filter($"{nameof(ScoreInfo.User)}.{nameof(RealmUser.OnlineID)} == $0"
+ $" && {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),
localScoresChanged);
}, true);
2022-07-23 14:25:06 +08:00
void localScoresChanged(IRealmCollection<ScoreInfo> sender, ChangeSet? changes, Exception _)
{
// This subscription may fire from changes to linked beatmaps, which we don't care about.
// It's currently not possible for a score to be modified after insertion, so we can safely ignore callbacks with only modifications.
if (changes?.HasCollectionChanges() == false)
return;
ScoreInfo? topScore = scoreManager.OrderByTotalScore(sender.Detach()).FirstOrDefault();
updateable.Rank = topScore?.Rank;
updateable.Alpha = topScore != null ? 1 : 0;
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
scoreSubscription?.Dispose();
}
}
}