2020-03-28 23:22:01 +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.
|
|
|
|
|
|
2021-12-17 18:09:35 +08:00
|
|
|
|
using System;
|
2020-03-28 23:22:01 +08:00
|
|
|
|
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;
|
2020-03-28 23:22:01 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
2021-12-13 18:01:20 +08:00
|
|
|
|
using osu.Game.Database;
|
2020-03-28 23:22:01 +08:00
|
|
|
|
using osu.Game.Online.API;
|
2020-04-07 14:31:22 +08:00
|
|
|
|
using osu.Game.Online.Leaderboards;
|
2020-03-28 23:22:01 +08:00
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
|
using osu.Game.Scoring;
|
2022-01-07 13:47:03 +08:00
|
|
|
|
using Realms;
|
2020-03-28 23:22:01 +08:00
|
|
|
|
|
2020-04-07 14:31:22 +08:00
|
|
|
|
namespace osu.Game.Screens.Select.Carousel
|
2020-03-28 23:22:01 +08:00
|
|
|
|
{
|
2020-04-05 03:28:36 +08:00
|
|
|
|
public class TopLocalRank : UpdateableRank
|
2020-03-28 23:22:01 +08:00
|
|
|
|
{
|
2021-10-02 23:55:29 +08:00
|
|
|
|
private readonly BeatmapInfo beatmapInfo;
|
2020-03-28 23:22:01 +08:00
|
|
|
|
|
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; }
|
2020-03-28 23:22:01 +08:00
|
|
|
|
|
2021-10-02 23:55:29 +08:00
|
|
|
|
public TopLocalRank(BeatmapInfo beatmapInfo)
|
2020-04-05 03:42:13 +08:00
|
|
|
|
: base(null)
|
2020-03-28 23:22:01 +08:00
|
|
|
|
{
|
2021-10-02 23:55:29 +08:00
|
|
|
|
this.beatmapInfo = beatmapInfo;
|
2020-03-28 23:22:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2020-04-07 13:49:24 +08:00
|
|
|
|
private void load()
|
2020-03-28 23:22:01 +08:00
|
|
|
|
{
|
2020-04-05 03:28:36 +08:00
|
|
|
|
ruleset.ValueChanged += _ => fetchAndLoadTopScore();
|
|
|
|
|
|
|
|
|
|
fetchAndLoadTopScore();
|
2020-03-28 23:22:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 18:09:35 +08:00
|
|
|
|
protected override void LoadComplete()
|
2020-04-05 03:28:36 +08:00
|
|
|
|
{
|
2021-12-17 18:09:35 +08:00
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
2022-01-07 13:47:03 +08:00
|
|
|
|
scoreSubscription = realmFactory.Context.All<ScoreInfo>()
|
|
|
|
|
.Filter($"{nameof(ScoreInfo.Beatmap)}.{nameof(BeatmapInfo.ID)} = $0", beatmapInfo.ID)
|
|
|
|
|
.QueryAsyncWithNotifications((_, changes, ___) =>
|
|
|
|
|
{
|
|
|
|
|
if (changes == null)
|
|
|
|
|
return;
|
2021-12-17 18:09:35 +08:00
|
|
|
|
|
2022-01-07 13:47:03 +08:00
|
|
|
|
fetchTopScoreRank();
|
|
|
|
|
});
|
2020-04-05 03:28:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 18:09:35 +08:00
|
|
|
|
private IDisposable scoreSubscription;
|
|
|
|
|
|
2020-04-07 14:30:06 +08:00
|
|
|
|
private ScheduledDelegate scheduledRankUpdate;
|
|
|
|
|
|
2020-04-05 03:28:36 +08:00
|
|
|
|
private void fetchAndLoadTopScore()
|
2020-03-28 23:22:01 +08:00
|
|
|
|
{
|
2021-12-17 18:09:35 +08:00
|
|
|
|
// TODO: this lookup likely isn't required, we can use the results of the subscription directly.
|
2021-12-13 18:01:20 +08:00
|
|
|
|
var rank = fetchTopScoreRank();
|
2021-12-17 18:09:35 +08:00
|
|
|
|
|
|
|
|
|
scheduledRankUpdate = Scheduler.Add(() =>
|
2020-04-07 14:30:06 +08:00
|
|
|
|
{
|
|
|
|
|
Rank = rank;
|
2020-04-05 03:28:36 +08:00
|
|
|
|
|
2020-04-07 14:30:06 +08:00
|
|
|
|
// Required since presence is changed via IsPresent override
|
|
|
|
|
Invalidate(Invalidation.Presence);
|
|
|
|
|
});
|
2020-03-28 23:22:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
2021-12-13 18:01:20 +08:00
|
|
|
|
private ScoreRank? fetchTopScoreRank()
|
2020-03-28 23:22:01 +08:00
|
|
|
|
{
|
2021-12-13 18:01:20 +08:00
|
|
|
|
if (realmFactory == null || beatmapInfo == null || ruleset?.Value == null || api?.LocalUser.Value == null)
|
2020-03-28 23:22:01 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
2021-12-13 18:01:20 +08:00
|
|
|
|
using (var realm = realmFactory.CreateContext())
|
|
|
|
|
{
|
2022-01-07 13:47:03 +08:00
|
|
|
|
return realm.All<ScoreInfo>()
|
|
|
|
|
.AsEnumerable()
|
|
|
|
|
// TODO: update to use a realm filter directly (or at least figure out the beatmap part to reduce scope).
|
|
|
|
|
.Where(s => s.UserID == api.LocalUser.Value.Id && s.BeatmapInfoID == beatmapInfo.ID && s.RulesetID == ruleset.Value.ID && !s.DeletePending)
|
2021-12-13 18:01:20 +08:00
|
|
|
|
.OrderByDescending(s => s.TotalScore)
|
|
|
|
|
.FirstOrDefault()
|
|
|
|
|
?.Rank;
|
|
|
|
|
}
|
2020-03-28 23:22:01 +08:00
|
|
|
|
}
|
2021-11-05 17:05:31 +08:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
2021-12-17 18:09:35 +08:00
|
|
|
|
scoreSubscription?.Dispose();
|
2021-11-05 17:05:31 +08:00
|
|
|
|
}
|
2020-03-28 23:22:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|