Пример #1
0
        public LeaderboardConfig GetByName(string name)
        {
            LeaderboardConfig config = null;

            _leaderboards.TryGetValue(name, out config);
            return(config);
        }
Пример #2
0
        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));
        }
Пример #3
0
        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));
        }
Пример #4
0
        private static Dictionary <string, LeaderboardConfig> GetLeaderboardConfiguration(IConfigurationSection configuration)
        {
            Dictionary <string, LeaderboardConfig> leaderboards = new Dictionary <string, LeaderboardConfig>();

            // go over all leaderboards under "Leaderboard:Leaderboards"
            foreach (var config in configuration.GetChildren())
            {
                string            name = config.Key;
                bool              includeCurrentPlayer = bool.Parse(config["IncludeCurrentPlayer"] ?? "false");
                LeaderboardType   type = (LeaderboardType)Enum.Parse(typeof(LeaderboardType), config["Type"]);
                LeaderboardConfig leaderboardConfig = new LeaderboardConfig
                {
                    Name = name,
                    Type = type,
                    IncludeCurrentPlayer = includeCurrentPlayer
                };

                switch (type)
                {
                case LeaderboardType.Top:
                    string top = config["Top"];
                    if (top == null)
                    {
                        throw new Exception($"Leaderboard type Top must have Top value set. Leaderboard name: '{name}'");
                    }
                    else
                    {
                        leaderboardConfig.Top = int.Parse(top);
                    }
                    break;

                case LeaderboardType.AroundMe:
                    string radius = config["Radius"];
                    if (radius == null)
                    {
                        throw new Exception($"Leaderboard type AroundMe must have Radius value set. Leaderboard name: '{name}'");
                    }
                    else
                    {
                        leaderboardConfig.Radius = int.Parse(radius);
                    }
                    break;

                case LeaderboardType.All:
                    break;
                }

                leaderboards.Add(name, leaderboardConfig);
            }

            return(leaderboards);
        }
Пример #5
0
        private static Dictionary <string, LeaderboardConfig> GetLeaderboardconfiguraion(IEnumerable <IConfigurationSection> enumerable)
        {
            Dictionary <string, LeaderboardConfig> leaderboards = new Dictionary <string, LeaderboardConfig>();

            // go over all leaderboards under "Leaderboard:Leaderboards"
            foreach (var config in enumerable)
            {
                string            name = config["Name"];
                LeaderboardType   type = (LeaderboardType)Enum.Parse(typeof(LeaderboardType), config["Type"]);
                LeaderboardConfig leaderboardConfig = new LeaderboardConfig
                {
                    Name = name,
                    Type = type
                };

                switch (type)
                {
                case LeaderboardType.Top:
                    string top = config["Top"];
                    if (top == null)
                    {
                        throw new Exception($"Leaderboard type Top must have Top value set. Leaderboard name: '{name}'");
                    }
                    else
                    {
                        leaderboardConfig.Top = int.Parse(top);
                    }
                    break;

                case LeaderboardType.AroundMe:
                    string radius = config["Radius"];
                    if (radius == null)
                    {
                        throw new Exception($"Leaderboard type AroundMe must have Radius value set. Leaderboard name: '{name}'");
                    }
                    else
                    {
                        leaderboardConfig.Radius = int.Parse(radius);
                    }
                    break;

                case LeaderboardType.All:
                    break;
                }

                leaderboards.Add(name, leaderboardConfig);
            }

            return(leaderboards);
        }
Пример #6
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
                });
            }
        }