Beispiel #1
0
        //
        // ──────────────────────────────────────────────────────────────────────────────────
        //   :::::: P U B L I C   F U N C T I O N S : :  :   :    :     :        :          :
        // ──────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Get the matches from a competition
        /// </summary>
        /// <param name="token">The secret token key of the api</param>
        /// <param name="leagueId">The id of the competition in the api</param>
        /// <param name="_http">The http factory</param>
        /// <returns>The matches in the competition</returns>
        /// See <see cref="ScheduledTasks.VirtualBets.Models.CompetitionMatches"/> to know the response structure
        public async static Task <CompetitionMatches> getMatchesFromCompetition(string token, string leagueId, IHttpClientFactory _http)
        {
            string path = "competitions/" + leagueId + "/matches";

            string result = await getRequest(path, _http, token);

            try
            {
                CompetitionMatches comptMatchs = JsonConvert.DeserializeObject <CompetitionMatches>(result);

                return(comptMatchs);
            }
            catch (Exception) //Some kind of error
            {
                try           //If ok the error is that we only can do 10 requests per min
                {
                    ErrorFootballApiMessage err = JsonConvert.DeserializeObject <ErrorFootballApiMessage>(result);

                    Thread.Sleep(new TimeSpan(0, 1, 0)); //Wait a minute & retry

                    if (err.errorCode == 429)
                    {
                        return(null);
                    }

                    return(null);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
Beispiel #2
0
        //
        // ──────────────────────────────────────────────────────────────────────────────────
        //   :::::: P U B L I C   F U N C T I O N S : :  :   :    :     :        :          :
        // ──────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Initializes or updates the matchs and teams of a competition
        /// </summary>
        /// <param name="leagueId">The id in the api of the competition</param>
        public async Task InitializeAsync(string leagueId)
        {
            string token   = _configuration["footballApi:token"];
            bool   correct = true;

            CompetitionMatches comptMatchs = await APIRequest.getMatchesFromCompetition(token, leagueId, _http);

            CompetitionInfo comptInfo = await APIRequest.getCompetitionInfo(token, leagueId, _http);

            if (comptInfo == null)
            {
                return;
            }
            int actualMatchD = comptInfo.currentSeason.currentMatchday + 1;

            Competition league = FootballInitializers.initializeLeague(comptMatchs.competition.name, _context);


            comptMatchs.matches.ForEach(match =>
            {
                if (match.matchday <= actualMatchD)
                {
                    Team homeTeam = FootballInitializers.initializeTeam(match.homeTeam.name, _context);
                    Team awayTeam = FootballInitializers.initializeTeam(match.awayTeam.name, _context);

                    //There is any error inserting or updating the new matchday
                    if (!FootballInitializers.updateMatchDay(match, league, homeTeam, awayTeam, _context))
                    {
                        correct = false;
                    }
                }
            });

            league.actualMatchDay = actualMatchD;
            _context.Competitions.Update(league); //Set the actual matchday

            if (correct)
            {
                _context.SaveChanges();
            }
        }