mirror of
https://github.com/ppy/osu.git
synced 2025-02-05 09:32:54 +08:00
Simplifiy user statistics retrieval to one-time on deserialization
This commit is contained in:
parent
62514f23b5
commit
d15ffff9a5
@ -157,7 +157,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
|
|||||||
Username = $"User {i}",
|
Username = $"User {i}",
|
||||||
AllStatistics =
|
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",
|
CoverUrl = @"https://osu.ppy.sh/images/headers/profile-covers/c3.jpg",
|
||||||
});
|
});
|
||||||
@ -198,7 +198,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
|
|||||||
Username = "User 0",
|
Username = "User 0",
|
||||||
AllStatistics =
|
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",
|
CoverUrl = @"https://osu.ppy.sh/images/headers/profile-covers/c3.jpg",
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// 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.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Extensions.Color4Extensions;
|
using osu.Framework.Extensions.Color4Extensions;
|
||||||
@ -164,7 +165,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Participants
|
|||||||
|
|
||||||
var ruleset = rulesets.GetRuleset(Room.Settings.RulesetID);
|
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;
|
userRankText.Text = currentModeRank != null ? $"#{currentModeRank.Value:N0}" : string.Empty;
|
||||||
|
|
||||||
userStateDisplay.UpdateStatus(User.State, User.BeatmapAvailability);
|
userStateDisplay.UpdateStatus(User.State, User.BeatmapAvailability);
|
||||||
|
@ -5,13 +5,13 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using osu.Framework.Bindables;
|
using osu.Framework.Bindables;
|
||||||
using osu.Game.IO.Serialization;
|
using osu.Game.IO.Serialization;
|
||||||
using osu.Game.Online.API.Requests;
|
using osu.Game.Online.API.Requests;
|
||||||
using osu.Game.Rulesets;
|
|
||||||
|
|
||||||
namespace osu.Game.Users
|
namespace osu.Game.Users
|
||||||
{
|
{
|
||||||
@ -238,36 +238,26 @@ namespace osu.Game.Users
|
|||||||
[JsonProperty("replays_watched_counts")]
|
[JsonProperty("replays_watched_counts")]
|
||||||
public UserHistoryCount[] ReplaysWatchedCounts;
|
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]
|
[UsedImplicitly]
|
||||||
[JsonExtensionData]
|
[JsonExtensionData]
|
||||||
private readonly IDictionary<string, JToken> otherProperties = new Dictionary<string, JToken>();
|
private readonly IDictionary<string, JToken> otherProperties = new Dictionary<string, JToken>();
|
||||||
|
|
||||||
/// <summary>
|
[OnDeserialized]
|
||||||
/// Map for ruleset with their associated user statistics, can be altered for testing purposes.
|
private void onDeserialized(StreamingContext context)
|
||||||
/// </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)
|
|
||||||
{
|
{
|
||||||
if (AllStatistics.TryGetValue(ruleset, out var existing))
|
foreach (var kvp in otherProperties.Where(kvp => kvp.Key.StartsWith("statistics_", StringComparison.Ordinal)))
|
||||||
return existing;
|
{
|
||||||
|
var shortName = kvp.Key.Replace("statistics_", string.Empty);
|
||||||
return AllStatistics[ruleset] = parseStatisticsFor(ruleset);
|
AllStatistics[shortName] = kvp.Value.ToObject<UserStatistics>(JsonSerializer.Create(JsonSerializableExtensions.CreateGlobalSettings()));
|
||||||
}
|
}
|
||||||
|
|
||||||
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()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() => Username;
|
public override string ToString() => Username;
|
||||||
|
Loading…
Reference in New Issue
Block a user