From d15ffff9a57e6a09a1ecb6e196165eccd16c72e4 Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Tue, 9 Feb 2021 09:54:17 +0300 Subject: [PATCH] Simplifiy user statistics retrieval to one-time on deserialization --- .../TestSceneMultiplayerParticipantsList.cs | 4 +- .../Participants/ParticipantPanel.cs | 3 +- osu.Game/Users/User.cs | 42 +++++++------------ 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerParticipantsList.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerParticipantsList.cs index 5a0234e379..5da5ab74b2 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerParticipantsList.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneMultiplayerParticipantsList.cs @@ -157,7 +157,7 @@ namespace osu.Game.Tests.Visual.Multiplayer Username = $"User {i}", AllStatistics = { - { Ruleset.Value, new UserStatistics { GlobalRank = RNG.Next(1, 100000) } } + { Ruleset.Value.ShortName, new UserStatistics { GlobalRank = RNG.Next(1, 100000) } } }, CoverUrl = @"https://osu.ppy.sh/images/headers/profile-covers/c3.jpg", }); @@ -198,7 +198,7 @@ namespace osu.Game.Tests.Visual.Multiplayer Username = "User 0", AllStatistics = { - { Ruleset.Value, new UserStatistics { GlobalRank = RNG.Next(1, 100000) } } + { Ruleset.Value.ShortName, new UserStatistics { GlobalRank = RNG.Next(1, 100000) } } }, CoverUrl = @"https://osu.ppy.sh/images/headers/profile-covers/c3.jpg", }); diff --git a/osu.Game/Screens/OnlinePlay/Multiplayer/Participants/ParticipantPanel.cs b/osu.Game/Screens/OnlinePlay/Multiplayer/Participants/ParticipantPanel.cs index e78264223e..49d3bfc2dc 100644 --- a/osu.Game/Screens/OnlinePlay/Multiplayer/Participants/ParticipantPanel.cs +++ b/osu.Game/Screens/OnlinePlay/Multiplayer/Participants/ParticipantPanel.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . 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; using osu.Framework.Extensions.Color4Extensions; @@ -164,7 +165,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Participants var ruleset = rulesets.GetRuleset(Room.Settings.RulesetID); - var currentModeRank = User.User?.GetStatisticsFor(ruleset)?.GlobalRank; + var currentModeRank = User.User?.AllStatistics.GetValueOrDefault(ruleset.ShortName)?.GlobalRank; userRankText.Text = currentModeRank != null ? $"#{currentModeRank.Value:N0}" : string.Empty; userStateDisplay.UpdateStatus(User.State, User.BeatmapAvailability); diff --git a/osu.Game/Users/User.cs b/osu.Game/Users/User.cs index b8ca345f5c..2c2f293aac 100644 --- a/osu.Game/Users/User.cs +++ b/osu.Game/Users/User.cs @@ -5,13 +5,13 @@ using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Runtime.Serialization; using JetBrains.Annotations; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using osu.Framework.Bindables; using osu.Game.IO.Serialization; using osu.Game.Online.API.Requests; -using osu.Game.Rulesets; namespace osu.Game.Users { @@ -238,36 +238,26 @@ namespace osu.Game.Users [JsonProperty("replays_watched_counts")] public UserHistoryCount[] ReplaysWatchedCounts; + /// + /// All user statistics per ruleset's short name (in the case of a response). + /// Otherwise empty. Can be altered for testing purposes. + /// + // todo: this should likely be moved to a separate UserCompact class at some point. + [UsedImplicitly] + public readonly Dictionary AllStatistics = new Dictionary(); + [UsedImplicitly] [JsonExtensionData] private readonly IDictionary otherProperties = new Dictionary(); - /// - /// Map for ruleset with their associated user statistics, can be altered for testing purposes. - /// - internal readonly Dictionary AllStatistics = new Dictionary(); - - /// - /// Retrieves the user statistics for a certain ruleset. - /// If user is fetched from a , - /// this will always return empty instance, use instead. - /// - /// The ruleset to retrieve statistics for. - // todo: this should likely be moved to a separate UserCompact class at some point. - public UserStatistics GetStatisticsFor(RulesetInfo ruleset) + [OnDeserialized] + private void onDeserialized(StreamingContext context) { - if (AllStatistics.TryGetValue(ruleset, out var existing)) - return existing; - - return AllStatistics[ruleset] = parseStatisticsFor(ruleset); - } - - private UserStatistics parseStatisticsFor(RulesetInfo ruleset) - { - if (!(otherProperties.TryGetValue($"statistics_{ruleset.ShortName}", out var token))) - return new UserStatistics(); - - return token.ToObject(JsonSerializer.Create(JsonSerializableExtensions.CreateGlobalSettings())); + foreach (var kvp in otherProperties.Where(kvp => kvp.Key.StartsWith("statistics_", StringComparison.Ordinal))) + { + var shortName = kvp.Key.Replace("statistics_", string.Empty); + AllStatistics[shortName] = kvp.Value.ToObject(JsonSerializer.Create(JsonSerializableExtensions.CreateGlobalSettings())); + } } public override string ToString() => Username;