/// <summary> /// Gets the statistics of the user. /// </summary> /// <param name="user"></param> /// <param name="statisticNames"></param> /// <returns></returns> public static async Task <ReadOnlyCollection <LCStatistic> > GetStatistics(LCUser user, IEnumerable <string> statisticNames = null) { if (user == null) { throw new ArgumentNullException(nameof(user)); } string path = $"leaderboard/users/{user.ObjectId}/statistics"; if (statisticNames != null && statisticNames.Count() > 0) { string names = string.Join(",", statisticNames); path = $"{path}?statistics={names}"; } Dictionary <string, object> result = await LCCore.HttpClient.Get <Dictionary <string, object> >(path); if (result.TryGetValue("results", out object results) && results is List <object> list) { List <LCStatistic> statistics = new List <LCStatistic>(); foreach (object item in list) { LCStatistic statistic = LCStatistic.Parse(item as Dictionary <string, object>); statistics.Add(statistic); } return(statistics.AsReadOnly()); } return(null); }
internal static LCRanking Parse(IDictionary <string, object> data) { LCRanking ranking = new LCRanking(); if (data.TryGetValue("rank", out object rank)) { ranking.Rank = Convert.ToInt32(rank); } if (data.TryGetValue("user", out object user)) { LCObjectData objectData = LCObjectData.Decode(user as System.Collections.IDictionary); ranking.User = LCUser.GenerateUser(objectData); } if (data.TryGetValue("statisticName", out object statisticName)) { ranking.StatisticName = statisticName as string; } if (data.TryGetValue("statisticValue", out object value)) { ranking.Value = Convert.ToDouble(value); } if (data.TryGetValue("statistics", out object statistics) && statistics is List <object> list) { List <LCStatistic> statisticList = new List <LCStatistic>(); foreach (object item in list) { LCStatistic statistic = LCStatistic.Parse(item as IDictionary <string, object>); statisticList.Add(statistic); } ranking.IncludedStatistics = statisticList.AsReadOnly(); } return(ranking); }
/// <summary> /// Updates the statistic of the user. /// </summary> /// <param name="user"></param> /// <param name="statistics"></param> /// <param name="overwrite"></param> /// <returns></returns> public static async Task <ReadOnlyCollection <LCStatistic> > UpdateStatistics(LCUser user, Dictionary <string, double> statistics, bool overwrite = false) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (statistics == null || statistics.Count == 0) { throw new ArgumentNullException(nameof(statistics)); } List <Dictionary <string, object> > data = statistics.Select(statistic => new Dictionary <string, object> { { "statisticName", statistic.Key }, { "statisticValue", statistic.Value }, }).ToList(); string path = $"leaderboard/users/{user.ObjectId}/statistics"; if (overwrite) { path = $"{path}?overwrite=1"; } Dictionary <string, object> result = await LCCore.HttpClient.Post <Dictionary <string, object> >(path, data : data); if (result.TryGetValue("results", out object results) && results is List <object> list) { List <LCStatistic> statisticList = new List <LCStatistic>(); foreach (object item in list) { LCStatistic statistic = LCStatistic.Parse(item as IDictionary <string, object>); statisticList.Add(statistic); } return(statisticList.AsReadOnly()); } return(null); }