Exemplo n.º 1
0
        public async Task GetSummonerID(UserModel user)
        {
            try
            {
                if (requestCount++ > maxRequestsPerSecond)
                {
                    logger.LogInformation("Max request coun exceed, waiting until continuing...");
                    Thread.Sleep(1_000);
                    requestCount = 0;
                }

                var resUser = await wrapper.GetSummonerByName(user.Server, user.Username);

                user.SummonerId = resUser.Id;
                logger.LogInformation($"Requested missing SummonerID of summoner '{user.Username}': ${user.SummonerId}");
            }
            catch (Exception e)
            {
                if (e is ResponseException &&
                    !(e as ResponseException).Response.IsSuccessStatusCode &&
                    (e as ResponseException).Response.StatusCode != HttpStatusCode.TooManyRequests)
                {
                    user.Watch = false;
                    logger.LogError("User could not be found by API - settings 'Watch' flag to false");
                }
                else
                {
                    logger.LogError($"An unexpected error occured during request: {e.Message}");
                }
            }

            dal.Update(user);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Summoner([FromRoute] string server, [FromRoute] string summonerName)
        {
            server = server.ToLower();

            UserModel user = new UserModel();

            try
            {
                user = await wrapper.GetSummonerByName(server, summonerName);
            }
            catch (ResponseException e)
            {
                if (e.Response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(NotFound());
                }
            }

            var dbUser = await dal.GetUserByNameAsync(server, summonerName);

            var userView = new UserViewModel(user)
            {
                Registered = dbUser != null,
                Watch      = dbUser?.Watch,
                Created    = dbUser?.Created,
            };

            return(Ok(userView));
        }
Exemplo n.º 3
0
        public static async Task <UserModel> GetSummoner(string server, string name, RiotAPIWrapper riotApi, ILogger logger)
        {
            try
            {
                return(await riotApi.GetSummonerByName(server, name));
            }
            catch (Exception e)
            {
                logger.LogError($"Failed getting summoner: {e}");
            }

            return(null);
        }
        public async Task <IActionResult> Watch(
            [FromRoute] string server,
            [FromRoute] string userName)
        {
            server = server.ToLower();

            var key = $"{server}/{userName}";

            if (!registrationCache.RegistrationCodeCache.ContainsKey(key))
            {
                return(Unauthorized());
            }

            var stateCode = registrationCache.RegistrationCodeCache.Get(key);

            UserModel user = new UserModel();
            string    obtainedCode;

            try
            {
                user = await wrapper.GetSummonerByName(server, userName);
            }
            catch (ResponseException e)
            {
                if (e.Response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(BadRequest("user not found"));
                }
            }

            try
            {
                obtainedCode = await wrapper.GetThirdPartyCode(server, user.Id);

                if (obtainedCode != stateCode)
                {
                    return(Unauthorized());
                }
            }
            catch (ResponseException e)
            {
                if (e.Response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(Unauthorized());
                }
            }

            var dbUser = await dal.GetUserByNameAsync(server, user.Name);

            if (dbUser == null)
            {
                dbUser = new DatabaseAccessLayer.Models.UserModel
                {
                    Server     = server,
                    Username   = user.Name,
                    SummonerId = user.Id,
                    Watch      = true,
                };

                dal.Add(dbUser);
            }
            else
            {
                dbUser.SummonerId = user.Id;
                dbUser.Watch      = true;

                dal.Update(dbUser);
            }

            await dal.CommitChangesAsync();

            return(Ok());
        }