예제 #1
0
        public async Task <GenericResponse <IEnumerable <LolLeaderboardResponse> > > GetLeaderboard(string sortBy)
        {
            GenericResponse <IEnumerable <TLolStat> > leaderboards = await _lolDataAccess.GetLeaderboard(sortBy);

            if (leaderboards.Success)
            {
                // Convert to correct model
                IEnumerable <LolLeaderboardResponse> convertedResult = leaderboards.Result.Select(i =>
                {
                    LolStatsResponse baseObject = i.ConvertToResponse();
                    return(new LolLeaderboardResponse
                    {
                        GameUsername = i.UserGame.Username,
                        Username = i.UserGame.User.Username,
                        UserId = i.UserGame.UserId,
                        FlexQ = baseObject.FlexQ,
                        SoloQ = baseObject.SoloQ
                    });
                });
                return(new GenericResponse <IEnumerable <LolLeaderboardResponse> >(convertedResult));
            }
            else
            {
                return(new GenericResponse <IEnumerable <LolLeaderboardResponse> >(leaderboards.ErrorMessage, leaderboards.Exception));
            }
        }
예제 #2
0
        public static LolStatsResponse NoRankResponse()
        {
            LolStatsResponse lolResponse = new LolStatsResponse
            {
                SoloQ = new LolListStatsResponse
                {
                    LeagueName   = "Wukong's knight",
                    Wins         = 1337,
                    Losses       = 42,
                    LeaguePoints = 100,
                    Tier         = "I",
                    Rank         = "Challenger"
                },
                FlexQ = new LolListStatsResponse
                {
                    LeagueName   = "Wukong's knight",
                    Wins         = 1337,
                    Losses       = 42,
                    LeaguePoints = 100,
                    Tier         = "I",
                    Rank         = "Challenger"
                }
            };

            return(lolResponse);
        }
예제 #3
0
        public static LolStatsResponse ConvertToResponse(this TLolStat lolDbStats)
        {
            if (lolDbStats == null)
            {
                return(null);
            }
            LolStatsResponse lolResponse = new LolStatsResponse
            {
                SoloQ = new LolListStatsResponse
                {
                    LeagueName   = lolDbStats.SoloNameLeague,
                    Wins         = lolDbStats.SoloWins,
                    Losses       = lolDbStats.SoloLosses,
                    LeaguePoints = lolDbStats.SoloLp,
                    Tier         = lolDbStats.SoloTier,
                    Rank         = lolDbStats.SoloRank
                },
                FlexQ = new LolListStatsResponse
                {
                    LeagueName   = lolDbStats.FlexNameLeague,
                    Wins         = lolDbStats.FlexWins,
                    Losses       = lolDbStats.FlexLosses,
                    LeaguePoints = lolDbStats.FlexLp,
                    Tier         = lolDbStats.FlexTier,
                    Rank         = lolDbStats.FlexRank
                }
            };

            return(lolResponse);
        }
예제 #4
0
        public async Task <GenericResponse <BooleanResponse> > UpdateCache(LolStatsResponse lolResponse, TUserGame userGame)
        {
            TLolStat dbStats = _databaseContext.TLolStat.FirstOrDefault(s => s.UserGame.Username == userGame.Username);

            bool newStats = false;

            if (dbStats == null)
            {
                newStats = true;
                dbStats  = new TLolStat {
                    UserGameId = userGame.Id
                };
            }


            dbStats.FlexTier       = lolResponse.FlexQ.Tier;
            dbStats.FlexLosses     = lolResponse.FlexQ.Losses;
            dbStats.FlexLp         = lolResponse.FlexQ.LeaguePoints;
            dbStats.FlexNameLeague = lolResponse.FlexQ.LeagueName;
            dbStats.FlexWins       = lolResponse.FlexQ.Wins;
            dbStats.FlexRank       = lolResponse.FlexQ.Rank;

            dbStats.SoloTier       = lolResponse.SoloQ.Tier;
            dbStats.SoloLosses     = lolResponse.SoloQ.Losses;
            dbStats.SoloLp         = lolResponse.SoloQ.LeaguePoints;
            dbStats.SoloNameLeague = lolResponse.SoloQ.LeagueName;
            dbStats.SoloWins       = lolResponse.SoloQ.Wins;
            dbStats.SoloRank       = lolResponse.SoloQ.Rank;

            try
            {
                if (newStats)
                {
                    _databaseContext.TLolStat.Add(dbStats);
                }
                else
                {
                    _databaseContext.TLolStat.Update(dbStats);
                }
                await _databaseContext.SaveChangesAsync();

                return(new GenericResponse <BooleanResponse>(new BooleanResponse {
                    Success = true
                }));
            }
            catch (DbUpdateException e)
            {
                return(new GenericResponse <BooleanResponse>("Error while saving in cache", e));
            }
        }
예제 #5
0
        public async Task <GenericResponse <LolStatsResponse> > GetStatsFromSite(string gameUsername)
        {
            var requestId = _requestFactory.GenerateRequest($"lol/summoner/v3/summoners/by-name/{gameUsername}", Method.GET, "lol");
            var lolId     = _restClient.Execute <LolResponse>(requestId);

            if (lolId.StatusCode.IsStatusOk())
            {
                var id      = new GenericResponse <LolResponse>(lolId.Data).Result.Id;
                var request = _requestFactory.GenerateRequest($"lol/league/v3/positions/by-summoner/{id}", Method.GET, "lol");
                var lolUser = _restClient.Execute <List <LolListStatsResponse> >(request);

                if (lolUser.StatusCode.IsStatusOk())
                {
                    if (lolUser.Data.Count() == 2)
                    {
                        var lolStats = new LolStatsResponse
                        {
                            SoloQ = lolUser.Data[1],
                            FlexQ = lolUser.Data[0]
                        };
                        return(new GenericResponse <LolStatsResponse>(lolStats));
                    }
                    else if (lolUser.Data.Count() < 2)
                    {
                        return(new GenericResponse <LolStatsResponse>(LolTools.NoRankResponse()));
                    }
                    else
                    {
                        var lolStats = new LolStatsResponse
                        {
                            SoloQ = lolUser.Data[1],
                            FlexQ = lolUser.Data[2]
                        };
                        return(new GenericResponse <LolStatsResponse>(lolStats));
                    }
                }
                else
                {
                    return(new GenericResponse <LolStatsResponse>($"Cannot get stats for {gameUsername} (error {lolUser.StatusCode})", null));
                }
            }
            else
            {
                return(new GenericResponse <LolStatsResponse>($"Cannot get stats for {gameUsername} (error {lolId.StatusCode})", null));
            }
        }