1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-19 12:23:13 +08:00

Add method for retrieving condensed user statistics

This commit is contained in:
Salman Ahmed 2021-02-08 21:13:00 +03:00
parent 75bd28eea1
commit bebff61a9d

View File

@ -2,10 +2,16 @@
// 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; using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using JetBrains.Annotations;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Game.IO.Serialization;
using osu.Game.Online.API.Requests;
using osu.Game.Rulesets;
namespace osu.Game.Users namespace osu.Game.Users
{ {
@ -178,6 +184,11 @@ namespace osu.Game.Users
private UserStatistics statistics; private UserStatistics statistics;
/// <summary>
/// The user statistics of the ruleset specified within the API request.
/// If the user is fetched from a <see cref="GetUsersRequest"/> or similar
/// (i.e. is a user compact instance), use <see cref="GetStatisticsFor"/> instead.
/// </summary>
[JsonProperty(@"statistics")] [JsonProperty(@"statistics")]
public UserStatistics Statistics public UserStatistics Statistics
{ {
@ -228,13 +239,35 @@ namespace osu.Game.Users
[JsonProperty("replays_watched_counts")] [JsonProperty("replays_watched_counts")]
public UserHistoryCount[] ReplaysWatchedCounts; public UserHistoryCount[] ReplaysWatchedCounts;
public class UserHistoryCount [UsedImplicitly]
{ [JsonExtensionData]
[JsonProperty("start_date")] private readonly IDictionary<string, JToken> otherProperties = new Dictionary<string, JToken>();
public DateTime Date;
[JsonProperty("count")] private readonly Dictionary<RulesetInfo, UserStatistics> statisticsCache = new Dictionary<RulesetInfo, UserStatistics>();
public long Count;
/// <summary>
/// Retrieves the user statistics for a certain ruleset.
/// If user is fetched from a <see cref="GetUserRequest"/>,
/// this will always return null, 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 (statisticsCache.TryGetValue(ruleset, out var existing))
return existing;
return statisticsCache[ruleset] = parseStatisticsFor(ruleset);
}
private UserStatistics parseStatisticsFor(RulesetInfo ruleset)
{
if (!(otherProperties.TryGetValue($"statistics_{ruleset.ShortName}", out var token)))
return null;
var settings = JsonSerializableExtensions.CreateGlobalSettings();
settings.DefaultValueHandling = DefaultValueHandling.Include;
return token.ToObject<UserStatistics>(JsonSerializer.Create(settings));
} }
public override string ToString() => Username; public override string ToString() => Username;
@ -249,6 +282,14 @@ namespace osu.Game.Users
Id = 0 Id = 0
}; };
public bool Equals(User other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Id == other.Id;
}
public enum PlayStyle public enum PlayStyle
{ {
[Description("Keyboard")] [Description("Keyboard")]
@ -264,12 +305,13 @@ namespace osu.Game.Users
Touch, Touch,
} }
public bool Equals(User other) public class UserHistoryCount
{ {
if (ReferenceEquals(null, other)) return false; [JsonProperty("start_date")]
if (ReferenceEquals(this, other)) return true; public DateTime Date;
return Id == other.Id; [JsonProperty("count")]
public long Count;
} }
} }
} }