Exemplo n.º 1
0
        // match hasn't been generated before so isn't in database, get it from api instead, generate prediction then add to database
        private async Task <Match> getMatchFromApi(Match match)
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("X-Auth-Token", "7830c352850f4acda78aa61d1666d45b");
            using (HttpResponseMessage response = await client.GetAsync("https://api.football-data.org/v2/matches/" + match.MatchId))
            {
                if (response.IsSuccessStatusCode)
                {
                    JObject jsonObject = JObject.Parse(await response.Content.ReadAsStringAsync());

                    if (jsonObject["match"]["status"].ToString() == "FINISHED")
                    {
                        match = await populateFinishedMatch(jsonObject, match);
                    }
                    else
                    {
                        match = await populateFutureMatch(jsonObject, match);

                        match.HomeStats = await leagueService.getStats(match.LeagueId, match.HomeTeamId, match.MatchId);

                        match.AwayStats = await leagueService.getStats(match.LeagueId, match.AwayTeamId, match.MatchId, false);

                        MatchUtilities.getWDLString(match.HomeStats);
                        MatchUtilities.getWDLString(match.AwayStats);
                        match = await getRecentFormFromApi(match);
                    }
                    Predictor.generatePrediction(match);
                    addMatchToDatabase(match);
                    return(match);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }
Exemplo n.º 2
0
        // returns a match object, either from databse or API
        public async Task <Match> getMatch(Match match)
        {
            //check if match is in database already
            if (databaseCheck(match))
            {
                match = await getMatchFromDatabase(match);

                //if match is in database as an unfinished match, check if it has since finished and update
                if (DateTime.Parse(match.UtcDate).AddHours(2) < DateTime.Now && !match.predictionResultRecorded)
                {
                    using (HttpResponseMessage response = await client.GetAsync("https://api.football-data.org/v2/matches/" + match.MatchId))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            JObject jsonObject = JObject.Parse(await response.Content.ReadAsStringAsync());

                            if (jsonObject["match"]["status"].ToString() == "FINISHED")
                            {
                                match.finished = true;
                                match          = await populateFinishedMatch(jsonObject, match);

                                updateFinishedMatchInDatabase(match);
                                incrementPredictions(match);
                                addGoalScorersToDatabase(match.homeGoalScorers, match.HomeTeamId, match.MatchId);
                                addGoalScorersToDatabase(match.awayGoalScorers, match.AwayTeamId, match.MatchId);
                            }
                        }
                    }
                }
                return(match);
            }
            else
            // match not in database, get from api instead
            {
                using (HttpResponseMessage response = await client.GetAsync("https://api.football-data.org/v2/matches/" + match.MatchId))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        JObject jsonObject = JObject.Parse(await response.Content.ReadAsStringAsync());

                        //if match is finished, get from api as a finished match
                        if (jsonObject["match"]["status"].ToString() == "FINISHED")
                        {
                            await populateFinishedMatch(jsonObject, match);
                        }
                        else
                        // else get a scheduled match object
                        {
                            await populateFutureMatch(jsonObject, match);

                            match.HomeStats = await leagueService.getStats(match.LeagueId, match.HomeTeamId, match.MatchId);

                            match.AwayStats = await leagueService.getStats(match.LeagueId, match.AwayTeamId, match.MatchId, false);

                            match.HomeStats.homeOrAwayWDL = MatchUtilities.getWDLString(match.HomeStats);
                            match.AwayStats.homeOrAwayWDL = MatchUtilities.getWDLString(match.AwayStats);
                            match = await getRecentFormFromApi(match);

                            match = Predictor.generatePrediction(match);
                        }
                        addMatchToDatabase(match);
                        return(match);
                    }
                    else
                    {
                        throw new Exception(response.ReasonPhrase);
                    }
                }
            }
        }