protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                using (var scope = _serviceProvider.CreateScope())
                {
                    using (var dbContext = scope.ServiceProvider.GetRequiredService <DatabaseContext>())
                    {
                        try
                        {
                            var challengerPlayers = await _throttledRequestHelper.SendThrottledRequest(async() =>
                                                                                                       await _riotApi.League.GetChallengerLeagueAsync(Region.euw, LeagueQueue.RankedSolo));

                            var mastersPlayers = await _throttledRequestHelper.SendThrottledRequest(async() =>
                                                                                                    await _riotApi.League.GetMasterLeagueAsync(Region.euw, LeagueQueue.RankedSolo));

                            foreach (var highEloPlayer in challengerPlayers.Entries.Concat(mastersPlayers.Entries))
                            {
                                try
                                {
                                    var riotSummoner = await _throttledRequestHelper.SendThrottledRequest(async() =>
                                                                                                          await _riotApi.Summoner.GetSummonerBySummonerIdAsync(Region.euw, long.Parse(highEloPlayer.PlayerOrTeamId)));

                                    var dbSummoner = await dbContext.Summoners.FirstOrDefaultAsync(x => x.AccountId == riotSummoner.AccountId);

                                    if (dbSummoner == null)
                                    {
                                        var newDbSummoner = new Models.Summoner.Summoner
                                        {
                                            SummonerId      = riotSummoner.Id,
                                            AccountId       = riotSummoner.AccountId,
                                            ProfileIconId   = riotSummoner.ProfileIconId,
                                            Level           = riotSummoner.Level,
                                            RevisionDate    = riotSummoner.RevisionDate,
                                            SummonerName    = riotSummoner.Name,
                                            LastUpdatedDate = new DateTime()
                                        };

                                        dbContext.Summoners.Add(newDbSummoner);
                                        await dbContext.SaveChangesAsync();

                                        dbSummoner = newDbSummoner;
                                    }

                                    // If the summoner has had updates post the date what we have on our records
                                    // RevisionDate can be anything from a level up to new games played
                                    if (riotSummoner.RevisionDate > dbSummoner.LastUpdatedDate)
                                    {
                                        DateTime?collectionFromDate;
                                        DateTime?collectionToDate;

                                        if (dbSummoner.LastUpdatedDate == DateTime.MinValue)
                                        {
                                            collectionFromDate = null;
                                            collectionToDate   = null;
                                        }
                                        else
                                        {
                                            collectionFromDate = dbSummoner.LastUpdatedDate;
                                            collectionToDate   = DateTime.Now;
                                        }

                                        var riotMatchList = await _throttledRequestHelper.SendThrottledRequest(
                                            async() =>
                                            await _riotApi.Match.GetMatchListAsync(
                                                Region.euw,
                                                riotSummoner.AccountId,
                                                null,
                                                null,
                                                null,
                                                collectionFromDate, //No filter if null on these dates
                                                collectionToDate,
                                                0,                  //starting index
                                                25));               //ending index

                                        if (riotMatchList?.Matches != null)
                                        {
                                            foreach (var match in riotMatchList?.Matches)
                                            {
                                                if (!dbContext.Matches.Any(x => x.GameId == match.GameId))
                                                {
                                                    var newDbMatch = await ConvertRiotMatchReferenceToDbMatch(match);

                                                    if (newDbMatch != null)
                                                    {
                                                        dbContext.Matches.Add(newDbMatch);

                                                        var riotMatchTimeline = await _throttledRequestHelper.SendThrottledRequest(
                                                            async() => await _riotApi.Match.GetMatchTimelineAsync(Region.euw, newDbMatch.GameId));

                                                        if (riotMatchTimeline != null)
                                                        {
                                                            dbContext.MatchTimelines.Add(ConvertRiotMatchTimelineToDbMatchTimeline(riotMatchTimeline, newDbMatch.GameId));
                                                        }

                                                        await dbContext.SaveChangesAsync();
                                                    }
                                                }
                                            }
                                        }

                                        dbSummoner.LastUpdatedDate = riotSummoner.RevisionDate;
                                    }
                                }
                                catch (RiotSharpException)
                                {
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }
                        catch (RiotSharpException)
                        {
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
        }
Beispiel #2
0
        // This task is not optmised in any way and has lots of code duplication
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                _logging.LogEvent("MatchDataCollectionService started.");

                using (var scope = _serviceScopeFactory.CreateScope())
                {
                    using (var matchupInformationRepository = scope.ServiceProvider.GetRequiredService <IBasicMatchupInformationRepository>())
                        using (var summonerRepository = scope.ServiceProvider.GetRequiredService <ISummonerRepository>())
                        {
                            try
                            {
                                _logging.LogEvent("Current matches count - " + matchupInformationRepository.GetAllMatchups().Count());

                                League challengerPlayers = await _throttledRequestHelper.SendThrottledRequest(async() => await _riotApi.League.GetChallengerLeagueAsync(Region.euw, LeagueQueue.RankedSolo));

                                League mastersPlayers = await _throttledRequestHelper.SendThrottledRequest(async() => await _riotApi.League.GetMasterLeagueAsync(Region.euw, LeagueQueue.RankedSolo));

                                IEnumerable <LeaguePosition> highEloPlayerEntires = challengerPlayers.Entries.Concat(mastersPlayers.Entries);

                                int totalPlayers = highEloPlayerEntires.Count();
                                int currentCount = 0;

                                foreach (var highEloPlayer in highEloPlayerEntires)
                                {
                                    _logging.LogEvent(++currentCount + "/" + totalPlayers + " - " + highEloPlayer.PlayerOrTeamName);

                                    Summoner summoner = await _throttledRequestHelper.SendThrottledRequest(async() => await _riotApi.Summoner.GetSummonerBySummonerIdAsync(RiotSharp.Misc.Region.euw, Convert.ToInt64(highEloPlayer.PlayerOrTeamId)));

                                    Db_LccSummoner summonerInDatabase = summonerRepository.GetSummonerByAccountId(summoner.AccountId);
                                    if (summoner != null && summonerInDatabase == null)
                                    {
                                        summonerRepository.InsertSummoner(
                                            new Db_LccSummoner()
                                        {
                                            AccountId       = summoner.AccountId,
                                            SummonerName    = summoner.Name,
                                            LastUpdatedTime = DateTime.Now
                                        });

                                        newSummonersAddedToDatabaseTotal++;
                                        newSummonersAddedThisSession++;

                                        MatchList matchList = await _throttledRequestHelper.SendThrottledRequest(async() => await _riotApi.Match.GetMatchListAsync(Region.euw, summoner.AccountId, null, null, null, null, null, 0, 75));

                                        if (matchList != null && matchList?.Matches != null)
                                        {
                                            await GetRiotMatchupInformationAndAddIfNotExisting(matchupInformationRepository, matchList, highEloPlayerEntires);
                                        }
                                    }
                                    else
                                    {
                                        DateTime lastUpdatedDate          = summonerInDatabase.LastUpdatedTime;
                                        DateTime lastRevisionDateFromRiot = summoner.RevisionDate;

                                        if (lastRevisionDateFromRiot > lastUpdatedDate)
                                        {
                                            summonerInDatabase.LastUpdatedTime = summoner.RevisionDate;
                                            summonerRepository.UpdateSummoner(summonerInDatabase);

                                            MatchList newMatches = await _throttledRequestHelper.SendThrottledRequest(async() => await _riotApi.Match.GetMatchListAsync(RiotSharp.Misc.Region.euw, summoner.AccountId, null, null, null, lastUpdatedDate, DateTime.Now, 0, 25));

                                            if (newMatches != null && newMatches?.Matches != null)
                                            {
                                                await GetRiotMatchupInformationAndAddIfNotExisting(matchupInformationRepository, newMatches, highEloPlayerEntires);
                                            }
                                        }
                                    }

                                    summonerRepository.Save();
                                    matchupInformationRepository.Save();
                                }
                            }
                            catch (RiotSharpException e)
                            {
                                _logging.LogEvent("RiotSharpException encountered - " + e.Message + ".");
                                if (e.HttpStatusCode == (HttpStatusCode)429)
                                {
                                    _logging.LogEvent("RateLimitExceeded exception - Sleeping for 50 seconds.");
                                    await Task.Run(() => Thread.Sleep(50 * 1000));
                                }
                            }
                            catch (Exception e)
                            {
                                _logging.LogEvent("Exception encountered - " + e.Message + ".");
                            }
                        }
                }

                PrintSummary();

                matchesUpdatedThisSession    = 0;
                newSummonersAddedThisSession = 0;

                _logging.LogEvent("MatchDataCollectionService finished, will wait 10 minutes and start again.");
                await Task.Run(() => Thread.Sleep(600000));
            }
        }