个人资料

跳过导航链接首页 > 博客列表 > 博客正文

C# 使用 XboxLive 查询/更新 数据排行榜

分类:

在store建立对应的排行榜数据后使用

    public partial class XboxLiveHelper 
    {


        public static void WriteLeardBoard(string name, int statValue)
        {
            if (!is_online) return;

            StatisticManager statManager = StatisticManager.SingletonInstance;
            double previous = 0;
            try
            {
                previous = statManager.GetStatistic(xboxUser, name).AsNumber;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            previous += statValue;

            statManager.SetStatisticIntegerData(xboxUser, name, (int)previous);

            statManager.RequestFlushToService(xboxUser);

            statManager.DoWork();


        }

        public static async Task<LeaderboardResult> GetLeaderboardAsync(string leaderBoardName)
        {

            if (!is_online) return null;

            var statManager = StatisticManager.SingletonInstance;
            LeaderboardQuery query = new LeaderboardQuery
            {
                SkipResultToMe = true,
                //SkipResultToRank = 100,
                MaxItems = 1,
            };
            statManager.GetLeaderboard(xboxUser, leaderBoardName, query);
            int finished = 0;
            do
            {
                Debug.Write(". ");
                await Task.Delay(100);
                finished += 100;
                foreach (StatisticEvent statEvent in statManager.DoWork())
                {
                    if (statEvent.EventType == StatisticEventType.GetLeaderboardComplete
                    && statEvent.ErrorCode == 0)
                    {
                        LeaderboardResultEventArgs leaderArgs = (LeaderboardResultEventArgs)statEvent.EventArgs;
                        LeaderboardResult leaderboardResult = leaderArgs.Result;
                        return leaderboardResult;
                    }

                }
            } while (finished <= 3000);

            return null;

        }


        public static async Task<LeaderboardResult> GetLeaderboardAsync(string leaderBoardName, uint max_items)
        {

            if (!is_online) return null;

            var statManager = StatisticManager.SingletonInstance;
            LeaderboardQuery query = new LeaderboardQuery
            {
                SkipResultToMe = true,
                //SkipResultToRank = 100,
                MaxItems = max_items,
            };
            statManager.GetLeaderboard(xboxUser, leaderBoardName, query);
            int finished = 0;
            do
            {
                Debug.Write(". ");
                await Task.Delay(100);
                finished += 100;
                foreach (StatisticEvent statEvent in statManager.DoWork())
                {
                    if (statEvent.EventType == StatisticEventType.GetLeaderboardComplete
                    && statEvent.ErrorCode == 0)
                    {
                        LeaderboardResultEventArgs leaderArgs = (LeaderboardResultEventArgs)statEvent.EventArgs;
                        LeaderboardResult leaderboardResult = leaderArgs.Result;
                        return leaderboardResult;
                    }

                }
            } while (finished <= 3000);

            return null;

        }


    }

end

songshizhao
最初发表2022/5/2 0:04:07 最近更新2022/5/2 0:04:07 28
为此篇作品打分
10
   评论