1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 23:43:03 +08:00

Simplifiy user statistics retrieval to one-time on deserialization

This commit is contained in:
Salman Ahmed 2021-02-09 09:54:17 +03:00
parent 62514f23b5
commit d15ffff9a5
3 changed files with 20 additions and 29 deletions

View File

@ -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",
});

View File

@ -1,6 +1,7 @@
// 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;
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);

View File

@ -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;
/// <summary>
/// All user statistics per ruleset's short name (in the case of a <see cref="GetUsersRequest"/> response).
/// Otherwise empty. Can be altered for testing purposes.
/// </summary>
// todo: this should likely be moved to a separate UserCompact class at some point.
[UsedImplicitly]
public readonly Dictionary<string, UserStatistics> AllStatistics = new Dictionary<string, UserStatistics>();
[UsedImplicitly]
[JsonExtensionData]
private readonly IDictionary<string, JToken> otherProperties = new Dictionary<string, JToken>();
/// <summary>
/// Map for ruleset with their associated user statistics, can be altered for testing purposes.
/// </summary>
internal readonly Dictionary<RulesetInfo, UserStatistics> AllStatistics = new Dictionary<RulesetInfo, UserStatistics>();
/// <summary>
/// Retrieves the user statistics for a certain ruleset.
/// If user is fetched from a <see cref="GetUserRequest"/>,
/// this will always return empty instance, use <see cref="Statistics"/> instead.
/// </summary>
/// <param name="ruleset">The ruleset to retrieve statistics for.</param>
// 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<UserStatistics>(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<UserStatistics>(JsonSerializer.Create(JsonSerializableExtensions.CreateGlobalSettings()));
}
}
public override string ToString() => Username;