1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-19 14:23:14 +08:00
osu-lazer/osu.Game/Online/UserStatisticsWatcher.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
3.0 KiB
C#
Raw Normal View History

2022-12-22 16:04:53 +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.
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2022-12-22 16:04:53 +08:00
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Game.Extensions;
2022-12-22 16:04:53 +08:00
using osu.Game.Online.API;
using osu.Game.Online.Spectator;
using osu.Game.Scoring;
2024-02-29 06:13:31 +08:00
namespace osu.Game.Online
2022-12-22 16:04:53 +08:00
{
/// <summary>
/// A persistent component that binds to the spectator server and API in order to deliver updates about the logged in user's gameplay statistics.
/// </summary>
public partial class UserStatisticsWatcher : Component
2022-12-22 16:04:53 +08:00
{
private readonly LocalUserStatisticsProvider statisticsProvider;
public IBindable<ScoreBasedUserStatisticsUpdate?> LatestUpdate => latestUpdate;
private readonly Bindable<ScoreBasedUserStatisticsUpdate?> latestUpdate = new Bindable<ScoreBasedUserStatisticsUpdate?>();
2022-12-22 16:04:53 +08:00
[Resolved]
private SpectatorClient spectatorClient { get; set; } = null!;
[Resolved]
private IAPIProvider api { get; set; } = null!;
private readonly Dictionary<long, ScoreInfo> watchedScores = new Dictionary<long, ScoreInfo>();
2022-12-22 16:04:53 +08:00
public UserStatisticsWatcher(LocalUserStatisticsProvider statisticsProvider)
{
this.statisticsProvider = statisticsProvider;
}
2022-12-22 16:04:53 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
2022-12-22 16:04:53 +08:00
spectatorClient.OnUserScoreProcessed += userScoreProcessed;
}
/// <summary>
/// Registers for a user statistics update after the given <paramref name="score"/> has been processed server-side.
/// </summary>
/// <param name="score">The score to listen for the statistics update for.</param>
public void RegisterForStatisticsUpdateAfter(ScoreInfo score)
2022-12-22 16:04:53 +08:00
{
Schedule(() =>
{
if (!api.IsLoggedIn)
return;
2022-12-22 16:04:53 +08:00
if (!score.Ruleset.IsLegacyRuleset() || score.OnlineID <= 0)
return;
watchedScores.Add(score.OnlineID, score);
});
}
2022-12-22 16:04:53 +08:00
private void userScoreProcessed(int userId, long scoreId)
{
if (userId != api.LocalUser.Value?.OnlineID)
return;
if (!watchedScores.Remove(scoreId, out var scoreInfo))
2022-12-22 16:04:53 +08:00
return;
statisticsProvider.RefetchStatistics(scoreInfo.Ruleset, u => Schedule(() =>
{
if (u.OldStatistics != null)
latestUpdate.Value = new ScoreBasedUserStatisticsUpdate(scoreInfo, u.OldStatistics, u.NewStatistics);
}));
2022-12-22 16:04:53 +08:00
}
protected override void Dispose(bool isDisposing)
{
if (spectatorClient.IsNotNull())
spectatorClient.OnUserScoreProcessed -= userScoreProcessed;
base.Dispose(isDisposing);
}
}
}