Esempio n. 1
0
        public static void CheckH2H(PredictionData data, Game game)
        {
            var leagueRepo   = new LeagueDataRepository();
            var analysisRepo = new AnalysisDataRepository();

            var homeTeamId   = game.HomeTeamId;
            var awayTeamId   = game.AwayTeamId;
            var h2hStats     = analysisRepo.GetTeamsHeadToHead(homeTeamId, awayTeamId);
            var teamsHistory = leagueRepo.GetTeamsHistory(homeTeamId, awayTeamId).Where(g => g.Result != 0)
                               .OrderByDescending(th => th.Date).ToList();

            bool homeTeamMapsToTeam1 = h2hStats.Team1Id == homeTeamId;

            int lastHomeTeamWinGamesAgo = -1, lastAwayTeamWinGamesAgo = -1, lastDrawGamesAgo = -1;
            int lastBttsYes = -1, lastBttsNo = -1;
            int lastTotalOver = -1, lastTotalUnder = -1;

            for (int i = 0; i < teamsHistory.Count; i++)
            {
                if (lastHomeTeamWinGamesAgo > -1 && lastAwayTeamWinGamesAgo > -1 && lastDrawGamesAgo > -1 &&
                    lastTotalOver > -1 && lastTotalUnder > -1 && lastBttsYes > -1 && lastBttsNo > -1)
                {
                    break;
                }
                var tempGame = teamsHistory[i];
                if (tempGame.HomeTeamId == homeTeamId)
                {
                    switch (tempGame.Result)
                    {
                    case GameResult.HomeWin:
                        if (lastHomeTeamWinGamesAgo == -1)
                        {
                            lastHomeTeamWinGamesAgo = i;
                        }
                        break;

                    case GameResult.Draw:
                        if (lastDrawGamesAgo == -1)
                        {
                            lastDrawGamesAgo = i;
                        }
                        break;

                    case GameResult.AwayWin:
                        if (lastAwayTeamWinGamesAgo == -1)
                        {
                            lastAwayTeamWinGamesAgo = i;
                        }
                        break;
                    }
                }
                else
                {
                    switch (tempGame.Result)
                    {
                    case GameResult.HomeWin:
                        if (lastAwayTeamWinGamesAgo == -1)
                        {
                            lastAwayTeamWinGamesAgo = i;
                        }
                        break;

                    case GameResult.Draw:
                        if (lastDrawGamesAgo == -1)
                        {
                            lastDrawGamesAgo = i;
                        }
                        break;

                    case GameResult.AwayWin:
                        if (lastHomeTeamWinGamesAgo == -1)
                        {
                            lastHomeTeamWinGamesAgo = i;
                        }
                        break;
                    }
                }

                var totalOver = (tempGame.AwayTeamGoals + tempGame.HomeTeamGoals) > 2;
                if (totalOver && (lastTotalOver == -1))
                {
                    lastTotalOver = i;
                }
                if (!totalOver && (lastTotalUnder == -1))
                {
                    lastTotalUnder = i;
                }

                var bttsYes = (tempGame.AwayTeamGoals > 0) && (tempGame.HomeTeamGoals > 0);
                if (bttsYes && (lastBttsYes == -1))
                {
                    lastBttsYes = i;
                }
                if (!bttsYes && (lastBttsNo == -1))
                {
                    lastBttsNo = i;
                }
            }

            double averageHomeTeamWinFreq, averageAwayTeamWinFreq, averageDrawsFreq;
            double averageBttsYesFreq, averageBttsNoFreq;
            double averageTotalOverFreq, averageTotalUnderFreq;

            if (homeTeamMapsToTeam1)
            {
                averageHomeTeamWinFreq = 1 / h2hStats.Team1WinsPercentage;
                averageAwayTeamWinFreq = 1 / h2hStats.Team2WinsPercentage;
            }
            else
            {
                averageAwayTeamWinFreq = 1 / h2hStats.Team1WinsPercentage;
                averageHomeTeamWinFreq = 1 / h2hStats.Team2WinsPercentage;
            }
            averageDrawsFreq = 1 / h2hStats.DrawsPercentage;

            averageBttsYesFreq = (double)h2hStats.GamePlayed / h2hStats.BTTS_Yes;
            averageBttsNoFreq  = (double)h2hStats.GamePlayed / h2hStats.BTTS_No;

            averageTotalOverFreq  = (double)h2hStats.GamePlayed / h2hStats.GamesOver2_5;
            averageTotalUnderFreq = (double)h2hStats.GamePlayed / h2hStats.GamesUnder2_5;

            var minFreq = averageHomeTeamWinFreq;

            if (averageAwayTeamWinFreq < minFreq)
            {
                minFreq = averageAwayTeamWinFreq;
            }
            if (averageDrawsFreq < minFreq)
            {
                minFreq = averageDrawsFreq;
            }

            if (minFreq == averageHomeTeamWinFreq)
            {
                data.CalculatedProbabilities.HomeWinProbability *= H2HCoef;
            }
            if (minFreq == averageDrawsFreq)
            {
                data.CalculatedProbabilities.DrawProbability *= H2HCoef;
            }
            if (minFreq == averageAwayTeamWinFreq)
            {
                data.CalculatedProbabilities.AwayWinProbability *= H2HCoef;
            }

            minFreq = averageBttsYesFreq;
            if (averageBttsNoFreq < minFreq)
            {
                minFreq = averageBttsNoFreq;
            }

            if (minFreq == averageBttsNoFreq)
            {
                data.CalculatedProbabilities.BttsNoProbability *= H2HCoef;
            }
            if (minFreq == averageBttsYesFreq)
            {
                data.CalculatedProbabilities.BttsYesProbability *= H2HCoef;
            }

            minFreq = averageTotalOverFreq;
            if (averageTotalUnderFreq < minFreq)
            {
                minFreq = averageTotalUnderFreq;
            }

            if (minFreq == averageTotalOverFreq)
            {
                data.CalculatedProbabilities.TotalOverProbability *= H2HCoef;
            }
            if (minFreq == averageTotalUnderFreq)
            {
                data.CalculatedProbabilities.TotalUnderProbability *= H2HCoef;
            }

            //differences between last time some result happend and average result frequency
            var diffHomeTeamWins = lastHomeTeamWinGamesAgo - averageHomeTeamWinFreq;
            var diffAwayTeamWins = lastAwayTeamWinGamesAgo - averageAwayTeamWinFreq;
            var diffDraws = lastDrawGamesAgo - averageDrawsFreq;

            var diffBttsYes = lastBttsYes - averageBttsYesFreq;
            var diffBttsNo  = lastBttsNo - averageBttsNoFreq;

            var diffTotalOver  = lastTotalOver - averageTotalOverFreq;
            var diffTotalUnder = lastTotalUnder - averageTotalUnderFreq;

            var maxDiff = diffHomeTeamWins;

            if (diffAwayTeamWins > maxDiff)
            {
                maxDiff = diffAwayTeamWins;
            }
            if (diffDraws > maxDiff)
            {
                maxDiff = diffDraws;
            }

            if (maxDiff == diffHomeTeamWins)
            {
                data.CalculatedProbabilities.HomeWinProbability *= H2HCoefLastGames;
            }
            if (maxDiff == diffDraws)
            {
                data.CalculatedProbabilities.DrawProbability *= H2HCoefLastGames;
            }
            if (maxDiff == diffAwayTeamWins)
            {
                data.CalculatedProbabilities.AwayWinProbability *= H2HCoefLastGames;
            }

            maxDiff = diffBttsYes;
            if (diffBttsNo > maxDiff)
            {
                maxDiff = diffBttsNo;
            }

            if (maxDiff == diffBttsYes)
            {
                data.CalculatedProbabilities.BttsYesProbability *= H2HCoefLastGames;
            }
            if (maxDiff == diffBttsNo)
            {
                data.CalculatedProbabilities.BttsNoProbability *= H2HCoefLastGames;
            }

            maxDiff = diffTotalOver;
            if (diffTotalUnder > maxDiff)
            {
                maxDiff = diffTotalUnder;
            }

            if (maxDiff == diffTotalOver)
            {
                data.CalculatedProbabilities.TotalOverProbability *= H2HCoefLastGames;
            }
            if (maxDiff == diffTotalUnder)
            {
                data.CalculatedProbabilities.TotalUnderProbability *= H2HCoefLastGames;
            }
        }