using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using LeanCloud.Storage.Internal;
using System.IO;
using System.Text;
namespace LeanCloud {
///
/// 排行榜顺序
///
public enum AVLeaderboardOrder {
///
/// 升序
///
ASCENDING,
///
/// 降序
///
DESCENDING
}
///
/// 排行榜更新策略
///
public enum AVLeaderboardUpdateStrategy {
///
/// 更好地
///
BETTER,
///
/// 最近的
///
LAST,
///
/// 总和
///
SUM,
}
///
/// 排行榜刷新频率
///
public enum AVLeaderboardVersionChangeInterval {
///
/// 从不
///
NEVER,
///
/// 每天
///
DAY,
///
/// 每周
///
WEEK,
///
/// 每月
///
MONTH
}
///
/// 排行榜类
///
public class AVLeaderboard {
///
/// 成绩名字
///
/// The name of the statistic.
public string StatisticName {
get; private set;
}
///
/// 排行榜顺序
///
/// The order.
public AVLeaderboardOrder Order {
get; private set;
}
///
/// 排行榜更新策略
///
/// The update strategy.
public AVLeaderboardUpdateStrategy UpdateStrategy {
get; private set;
}
///
/// 排行榜版本更新频率
///
/// The version change intervak.
public AVLeaderboardVersionChangeInterval VersionChangeInterval {
get; private set;
}
///
/// 版本号
///
/// The version.
public int Version {
get; private set;
}
///
/// 下次重置时间
///
/// The next reset at.
public DateTime NextResetAt {
get; private set;
}
///
/// 创建时间
///
/// The created at.
public DateTime CreatedAt {
get; private set;
}
///
/// Leaderboard 构造方法
///
/// 成绩名称
AVLeaderboard(string statisticName) {
StatisticName = statisticName;
}
AVLeaderboard() {
}
///
/// 创建排行榜对象
///
/// 排行榜对象
/// 名称
/// 排序方式
/// 版本更新频率
/// 成绩更新策略
public static Task CreateLeaderboard(string statisticName,
AVLeaderboardOrder order = AVLeaderboardOrder.DESCENDING,
AVLeaderboardUpdateStrategy updateStrategy = AVLeaderboardUpdateStrategy.BETTER,
AVLeaderboardVersionChangeInterval versionChangeInterval = AVLeaderboardVersionChangeInterval.WEEK) {
if (string.IsNullOrEmpty(statisticName)) {
throw new ArgumentNullException(nameof(statisticName));
}
var data = new Dictionary {
{ "statisticName", statisticName },
{ "order", order.ToString().ToLower() },
{ "versionChangeInterval", versionChangeInterval.ToString().ToLower() },
{ "updateStrategy", updateStrategy.ToString().ToLower() },
};
var command = new AVCommand("leaderboard/leaderboards", "POST", data: data);
return AVPlugins.Instance.CommandRunner.RunCommandAsync(command).OnSuccess(t => {
try {
var leaderboard = Parse(t.Result.Item2);
return leaderboard;
} catch (Exception e) {
throw new AVException(AVException.ErrorCode.InvalidJSON, e.Message);
}
});
}
///
/// 创建排行榜对象
///
/// 排行榜对象
/// 名称
public static AVLeaderboard CreateWithoutData(string statisticName) {
if (string.IsNullOrEmpty(statisticName)) {
throw new ArgumentNullException(nameof(statisticName));
}
return new AVLeaderboard(statisticName);
}
///
/// 获取排行榜对象
///
/// 排行榜对象
/// 名称
public static Task GetLeaderboard(string statisticName) {
return CreateWithoutData(statisticName).Fetch();
}
///
/// 更新用户成绩
///
/// 更新的成绩
/// 用户
/// 成绩
/// 是否强行覆盖
public static Task> UpdateStatistics(AVUser user, Dictionary statistics, bool overwrite = false) {
if (user == null) {
throw new ArgumentNullException(nameof(user));
}
if (statistics == null || statistics.Count == 0) {
throw new ArgumentNullException(nameof(statistics));
}
var data = new List