private Task <IEnumerable <LeaderboardScore> > InternalGetScores(string leaderboardCode, int count, bool socialOnly, bool centered)
        {
            var tcs = new TaskCompletionSource <IEnumerable <LeaderboardScore> >();

            UIKit.UIApplication.SharedApplication.InvokeOnMainThread(async() =>
            {
                var result = Enumerable.Empty <LeaderboardScore>();

                var leaderboards        = await GKLeaderboard.LoadLeaderboardsAsync() ?? new GKLeaderboard[] { };
                var selectedLeaderboard = leaderboards.FirstOrDefault(l => l.Identifier == leaderboardCode);

                if (selectedLeaderboard != null)
                {
                    selectedLeaderboard.PlayerScope = socialOnly ? GKLeaderboardPlayerScope.FriendsOnly : GKLeaderboardPlayerScope.Global;
                    selectedLeaderboard.Range       = new Foundation.NSRange(1, count);
                    var scores = await selectedLeaderboard.LoadScoresAsync();

                    if (centered && selectedLeaderboard.LocalPlayerScore != null)
                    {
                        var playerRank = selectedLeaderboard.LocalPlayerScore.Rank;
                        var fromIndex  = playerRank - (count / 2);
                        fromIndex      = fromIndex < 1 ? 1 : fromIndex;

                        selectedLeaderboard.Range = new Foundation.NSRange(fromIndex, count);
                        scores = await selectedLeaderboard.LoadScoresAsync();
                    }

                    result = await IOSMapper.MapLeaderBoards(scores);
                }

                tcs.SetResult(result);
            });

            return(tcs.Task);
        }
Example #2
0
        /// <summary>
        /// Gets the top scores from leaderboard.
        /// </summary>
        /// <param name="leaderboardCode">The leaderboard code.</param>
        /// <returns>
        /// Task
        /// </returns>
        public async Task <Dictionary <string, List <WaveEngine.Social.LeaderboardScore> > > GetTopScoresFromLeaderboard(string leaderboardCode)
        {
            var scores = new Dictionary <string, List <GKScore> >();

            var leaderboards = await GKLeaderboard.LoadLeaderboardsAsync() ?? new GKLeaderboard[]
            {
            };

            foreach (var leaderboard in leaderboards)
            {
                var leaderboardScores = await leaderboard.LoadScoresAsync() ?? new GKScore[]
                {
                };
                var leaderboardScoresList = leaderboardScores.ToList();

                var boardId = leaderboard.Identifier;

                scores.Add(boardId, leaderboardScoresList);
            }

            var waveScores = Mapper.MapLeaderBoards(scores);

            return(waveScores);
        }