public async Task <IActionResult> Get(string name)
        {
            var gamertag = User.GetGamerTag();

            _appMonitor.LogEvent("Leaderboard", properties: new Dictionary <string, string> {
                { "Name", name }
            });

            LeaderboardConfig config = _leaderboardProvider.GetByName(name);

            if (config == null)
            {
                return(NotFound());
            }
            LeaderboardType  type = config.Type;
            List <GameScore> scores;

            switch (type)
            {
            case LeaderboardType.AroundMe:
                if (gamertag == null)
                {
                    return(this.ValidationFailed(new ErrorDetail("gamertag", "Must be signed in as a player with a gamertag to retrive this leaderboard")));
                }
                scores = await _store.GetScoresAroundMeAsync(gamertag, config.Radius);

                break;

            case LeaderboardType.Top:
                scores = await _store.GetTopHighScoresAsync(config.Top);

                break;

            default:
                scores = await _store.GetAllHighScoresAsync();

                break;
            }

            GameScore currentPlayer = null;

            if (config.IncludeCurrentPlayer && gamertag != null)
            {
                currentPlayer = (await _store.GetScoresAroundMeAsync(gamertag, 0)).FirstOrDefault();
            }

            // Format response model
            var resultModel = new LeaderboardGetResponseModel
            {
                Entries = scores
                          ?.Select(s => LeaderboardGetResponseModel.LeaderboardEntry.Map(s, gamertag))
                          ?.ToList(),
                CurrentPlayer = LeaderboardGetResponseModel.LeaderboardEntry.Map(currentPlayer, null)
            };

            // Return result
            return(Ok(resultModel));
        }
        public async Task <IActionResult> Get(string name)
        {
            var gamertag = User.GetGamerTag();

            LeaderboardConfig config = _leaderboardConfiguration.GetByName(name);

            if (config == null)
            {
                return(NotFound());
            }
            LeaderboardType  type = config.Type;
            List <GameScore> scores;

            switch (type)
            {
            case LeaderboardType.AroundMe:
                if (gamertag == null)
                {
                    return(BadRequest());    // need a gamertag to get scores!
                }
                scores = await _store.GetScoresAroundMeAsync(gamertag, config.Radius);

                break;

            case LeaderboardType.Top:
                scores = await _store.GetTopHighScoresAsync(config.Top);

                break;

            default:
                scores = await _store.GetAllHighScoresAsync();

                break;
            }

            GameScore currentPlayer = null;

            if (config.IncludeCurrentPlayer && gamertag != null)
            {
                currentPlayer = (await _store.GetScoresAroundMeAsync(gamertag, 0)).FirstOrDefault();
            }

            // Format response model
            var resultModel = new LeaderboardGetResponseModel
            {
                Entries = scores
                          ?.Select(s => LeaderboardGetResponseModel.LeaderboardEntry.Map(s, gamertag))
                          ?.ToList(),
                CurrentPlayer = LeaderboardGetResponseModel.LeaderboardEntry.Map(currentPlayer, null)
            };

            // Return result
            return(Ok(resultModel));
        }
Exemple #3
0
        public async Task <ActionResult> Get(string name)
        {
            //TODO
            var gamerTag = User.GetGamerTag();

            LeaderboardConfig config = _configuration.GetLeaderboardConfig(name);
            LeaderboardType   type   = config.Type;
            List <GameScore>  scores;

            switch (type)
            {
            case LeaderboardType.AroundMe:
                scores = await _store.GetScoresAroundMeAsync(gamerTag, config.Radius);

                break;

            case LeaderboardType.Top:
                scores = await _store.GetTopHighScoresAsync(config.Top);

                break;

            default:
                scores = await _store.GetAllHighScoresAsync();

                break;
            }

            // Format response model
            var resultModel = new LeaderboardGetResponseModel
            {
                Entries = scores == null ? null : scores.Select(s => (LeaderboardGetResponseModel.LeaderboardEntry)s).ToList()
            };

            // Return result
            return(Ok(resultModel));
        }
        public async Task <IActionResult> Get(string name)
        {
            var userId = User.GetId();

            _appMonitor.LogEvent("Leaderboard", properties: new Dictionary <string, string> {
                { "Name", name }
            });

            LeaderboardConfig config = _leaderboardProvider.GetByName(name);

            if (config == null)
            {
                return(NotFound());
            }
            LeaderboardType  type = config.Type;
            List <GameScore> scores;

            switch (type)
            {
            case LeaderboardType.AroundMe:
                if (userId == null)
                {
                    return(this.ValidationFailed(new ErrorDetail("userId", "Must be signed in as a player with a userId to retrive this leaderboard")));
                }
                scores = await _store.GetScoresAroundUserAsync(userId, config.Radius);

                break;

            case LeaderboardType.Top:
                scores = await _store.GetTopHighScoresAsync(config.Top);

                break;

            default:
                scores = await _store.GetAllHighScoresAsync();

                break;
            }
            var userIdsToLookUp = scores.Select(s => s.UserId).ToList();

            GameScore currentPlayer = null;

            if (config.IncludeCurrentPlayer && userId != null)
            {
                currentPlayer = (await _store.GetScoresAroundUserAsync(userId, 0)).FirstOrDefault();
                if (currentPlayer != null && !userIdsToLookUp.Contains(currentPlayer.UserId))
                {
                    userIdsToLookUp.Add(currentPlayer.UserId);
                }
            }

            var gamertags = await _playerManagementClient.GetGamertagsForUserIdsAsync(userIdsToLookUp.ToArray());

            var resultModel = new LeaderboardGetResponseModel
            {
                Entries = scores
                          ?.Select(s => Map(s, userId))
                          ?.ToList(),
                CurrentPlayer = Map(currentPlayer, null)
            };

            return(Ok(resultModel));

            // local function to map to response types
            LeaderboardGetResponseModel.LeaderboardEntry Map(GameScore score, string currentUserId)
            {
                if (score == null)
                {
                    return(null);
                }

                //find gamertag for userid

                return(new LeaderboardGetResponseModel.LeaderboardEntry
                {
                    Gamertag = gamertags.Single(g => g.UserId == score.UserId).Gamertag,
                    Score = score.Score,
                    Rank = score.Rank,
                    IsCurrentPlayer = currentUserId == score.UserId
                });
            }
        }