1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-05 10:33:22 +08:00

Make UserStatisticsWatcher fully rely on LocalUserStatisticsProvider

This commit is contained in:
Salman Alshamrani 2024-10-25 03:06:41 -04:00
parent 3a57b21c89
commit 44dd81363a
2 changed files with 12 additions and 44 deletions

View File

@ -33,6 +33,13 @@ namespace osu.Game.Online
private readonly Dictionary<string, UserStatistics> allStatistics = new Dictionary<string, UserStatistics>(); private readonly Dictionary<string, UserStatistics> allStatistics = new Dictionary<string, UserStatistics>();
/// <summary>
/// Returns the <see cref="UserStatistics"/> currently available for the given ruleset.
/// This may return null if the requested statistics has not been fetched yet.
/// </summary>
/// <param name="ruleset">The ruleset to return the corresponding <see cref="UserStatistics"/> for.</param>
internal UserStatistics? GetStatisticsFor(RulesetInfo ruleset) => allStatistics.GetValueOrDefault(ruleset.ShortName);
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions; using osu.Framework.Extensions.ObjectExtensions;
@ -10,7 +9,6 @@ using osu.Framework.Graphics;
using osu.Game.Extensions; using osu.Game.Extensions;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Spectator; using osu.Game.Online.Spectator;
using osu.Game.Scoring; using osu.Game.Scoring;
using osu.Game.Users; using osu.Game.Users;
@ -34,8 +32,6 @@ namespace osu.Game.Online
private readonly Dictionary<long, ScoreInfo> watchedScores = new Dictionary<long, ScoreInfo>(); private readonly Dictionary<long, ScoreInfo> watchedScores = new Dictionary<long, ScoreInfo>();
private Dictionary<string, UserStatistics>? latestStatistics;
public UserStatisticsWatcher(LocalUserStatisticsProvider? statisticsProvider = null) public UserStatisticsWatcher(LocalUserStatisticsProvider? statisticsProvider = null)
{ {
this.statisticsProvider = statisticsProvider; this.statisticsProvider = statisticsProvider;
@ -44,8 +40,6 @@ namespace osu.Game.Online
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
api.LocalUser.BindValueChanged(user => onUserChanged(user.NewValue), true);
spectatorClient.OnUserScoreProcessed += userScoreProcessed; spectatorClient.OnUserScoreProcessed += userScoreProcessed;
} }
@ -67,35 +61,6 @@ namespace osu.Game.Online
}); });
} }
private void onUserChanged(APIUser? localUser) => Schedule(() =>
{
latestStatistics = null;
if (localUser == null || localUser.OnlineID <= 1)
return;
var userRequest = new GetUsersRequest(new[] { localUser.OnlineID });
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)
{
foreach (var rulesetStats in user.RulesetsStatistics)
latestStatistics.Add(rulesetStats.Key, rulesetStats.Value);
}
});
private void userScoreProcessed(int userId, long scoreId) private void userScoreProcessed(int userId, long scoreId)
{ {
if (userId != api.LocalUser.Value?.OnlineID) if (userId != api.LocalUser.Value?.OnlineID)
@ -116,18 +81,14 @@ namespace osu.Game.Online
private void dispatchStatisticsUpdate(ScoreInfo scoreInfo, UserStatistics updatedStatistics) private void dispatchStatisticsUpdate(ScoreInfo scoreInfo, UserStatistics updatedStatistics)
{ {
string rulesetName = scoreInfo.Ruleset.ShortName; if (statisticsProvider == null)
statisticsProvider?.UpdateStatistics(updatedStatistics, scoreInfo.Ruleset);
if (latestStatistics == null)
return; return;
latestStatistics.TryGetValue(rulesetName, out UserStatistics? latestRulesetStatistics); var latestRulesetStatistics = statisticsProvider.GetStatisticsFor(scoreInfo.Ruleset);
latestRulesetStatistics ??= new UserStatistics(); statisticsProvider.UpdateStatistics(updatedStatistics, scoreInfo.Ruleset);
latestUpdate.Value = new UserStatisticsUpdate(scoreInfo, latestRulesetStatistics, updatedStatistics); if (latestRulesetStatistics != null)
latestStatistics[rulesetName] = updatedStatistics; latestUpdate.Value = new UserStatisticsUpdate(scoreInfo, latestRulesetStatistics, updatedStatistics);
} }
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)