2022-12-22 08:04:53 +00: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 System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
2024-02-09 09:41:36 +00:00
|
|
|
using osu.Framework.Bindables;
|
2022-12-22 08:04:53 +00:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
|
|
|
using osu.Framework.Graphics;
|
2022-12-24 10:28:46 +00:00
|
|
|
using osu.Game.Extensions;
|
2022-12-22 08:04:53 +00:00
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
|
|
|
using osu.Game.Online.Spectator;
|
|
|
|
using osu.Game.Scoring;
|
|
|
|
using osu.Game.Users;
|
|
|
|
|
2024-02-28 22:13:31 +00:00
|
|
|
namespace osu.Game.Online
|
2022-12-22 08:04:53 +00: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>
|
2024-02-22 18:50:46 +00:00
|
|
|
public partial class UserStatisticsWatcher : Component
|
2022-12-22 08:04:53 +00:00
|
|
|
{
|
2024-02-22 18:50:46 +00:00
|
|
|
public IBindable<UserStatisticsUpdate?> LatestUpdate => latestUpdate;
|
|
|
|
private readonly Bindable<UserStatisticsUpdate?> latestUpdate = new Bindable<UserStatisticsUpdate?>();
|
2024-02-09 09:41:36 +00:00
|
|
|
|
2022-12-22 08:04:53 +00:00
|
|
|
[Resolved]
|
|
|
|
private SpectatorClient spectatorClient { get; set; } = null!;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; } = null!;
|
|
|
|
|
2024-02-09 09:41:36 +00:00
|
|
|
private readonly Dictionary<long, ScoreInfo> watchedScores = new Dictionary<long, ScoreInfo>();
|
2022-12-22 08:04:53 +00:00
|
|
|
|
2022-12-24 12:42:32 +00:00
|
|
|
private Dictionary<string, UserStatistics>? latestStatistics;
|
2022-12-22 08:04:53 +00:00
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
api.LocalUser.BindValueChanged(user => onUserChanged(user.NewValue), true);
|
|
|
|
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>
|
2024-02-09 09:41:36 +00:00
|
|
|
public void RegisterForStatisticsUpdateAfter(ScoreInfo score)
|
2022-12-22 08:04:53 +00:00
|
|
|
{
|
2022-12-28 06:50:10 +00:00
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
if (!api.IsLoggedIn)
|
|
|
|
return;
|
2022-12-22 08:04:53 +00:00
|
|
|
|
2022-12-28 06:50:10 +00:00
|
|
|
if (!score.Ruleset.IsLegacyRuleset() || score.OnlineID <= 0)
|
|
|
|
return;
|
2022-12-24 10:28:46 +00:00
|
|
|
|
2024-02-09 09:41:36 +00:00
|
|
|
watchedScores.Add(score.OnlineID, score);
|
2022-12-28 06:50:10 +00:00
|
|
|
});
|
|
|
|
}
|
2022-12-22 08:04:53 +00:00
|
|
|
|
|
|
|
private void onUserChanged(APIUser? localUser) => Schedule(() =>
|
|
|
|
{
|
2022-12-24 12:42:32 +00:00
|
|
|
latestStatistics = null;
|
2022-12-22 08:04:53 +00:00
|
|
|
|
2022-12-24 12:28:25 +00:00
|
|
|
if (localUser == null || localUser.OnlineID <= 1)
|
2022-12-22 08:04:53 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
var userRequest = new GetUsersRequest(new[] { localUser.OnlineID });
|
2022-12-28 06:28:18 +00:00
|
|
|
userRequest.Success += initialiseUserStatistics;
|
|
|
|
api.Queue(userRequest);
|
|
|
|
});
|
|
|
|
|
|
|
|
private void initialiseUserStatistics(GetUsersResponse response) => Schedule(() =>
|
|
|
|
{
|
|
|
|
var user = response.Users.SingleOrDefault();
|
|
|
|
|
|
|
|
// possible if the user is restricted or similar.
|
|
|
|
if (user == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
latestStatistics = new Dictionary<string, UserStatistics>();
|
|
|
|
|
|
|
|
if (user.RulesetsStatistics != null)
|
2022-12-22 08:04:53 +00:00
|
|
|
{
|
2022-12-28 06:28:18 +00:00
|
|
|
foreach (var rulesetStats in user.RulesetsStatistics)
|
2022-12-22 08:04:53 +00:00
|
|
|
latestStatistics.Add(rulesetStats.Key, rulesetStats.Value);
|
2022-12-28 06:28:18 +00:00
|
|
|
}
|
2022-12-22 08:04:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
private void userScoreProcessed(int userId, long scoreId)
|
|
|
|
{
|
|
|
|
if (userId != api.LocalUser.Value?.OnlineID)
|
|
|
|
return;
|
|
|
|
|
2024-02-09 09:41:36 +00:00
|
|
|
if (!watchedScores.Remove(scoreId, out var scoreInfo))
|
2022-12-22 08:04:53 +00:00
|
|
|
return;
|
|
|
|
|
2024-02-09 09:41:36 +00:00
|
|
|
requestStatisticsUpdate(userId, scoreInfo);
|
2022-12-22 08:04:53 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 09:41:36 +00:00
|
|
|
private void requestStatisticsUpdate(int userId, ScoreInfo scoreInfo)
|
2022-12-22 08:04:53 +00:00
|
|
|
{
|
2024-02-09 09:41:36 +00:00
|
|
|
var request = new GetUserRequest(userId, scoreInfo.Ruleset);
|
|
|
|
request.Success += user => Schedule(() => dispatchStatisticsUpdate(scoreInfo, user.Statistics));
|
2022-12-22 08:04:53 +00:00
|
|
|
api.Queue(request);
|
|
|
|
}
|
|
|
|
|
2024-02-09 09:41:36 +00:00
|
|
|
private void dispatchStatisticsUpdate(ScoreInfo scoreInfo, UserStatistics updatedStatistics)
|
2022-12-22 08:04:53 +00:00
|
|
|
{
|
2024-02-09 09:41:36 +00:00
|
|
|
string rulesetName = scoreInfo.Ruleset.ShortName;
|
2022-12-22 08:04:53 +00:00
|
|
|
|
2024-01-03 08:37:57 +00:00
|
|
|
api.UpdateStatistics(updatedStatistics);
|
|
|
|
|
2022-12-24 12:42:32 +00:00
|
|
|
if (latestStatistics == null)
|
2022-12-22 08:04:53 +00:00
|
|
|
return;
|
|
|
|
|
2022-12-24 12:42:32 +00:00
|
|
|
latestStatistics.TryGetValue(rulesetName, out UserStatistics? latestRulesetStatistics);
|
|
|
|
latestRulesetStatistics ??= new UserStatistics();
|
|
|
|
|
2024-02-22 18:50:46 +00:00
|
|
|
latestUpdate.Value = new UserStatisticsUpdate(scoreInfo, latestRulesetStatistics, updatedStatistics);
|
2022-12-22 08:04:53 +00:00
|
|
|
latestStatistics[rulesetName] = updatedStatistics;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
if (spectatorClient.IsNotNull())
|
|
|
|
spectatorClient.OnUserScoreProcessed -= userScoreProcessed;
|
|
|
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|