コード例 #1
0
        public static List <MainCalculator.PositionGoals> GetTeamScorersByPosition(sakilaEntities4 db, int teamId, int competitionId,
                                                                                   int gamesToTake = 50)
        {
            var returnVal     = new List <MainCalculator.PositionGoals>();
            var latestMatches = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, gamesToTake);
            var matchesIds    = latestMatches.Select(x => x.CompetitionMatchID);

            var teamGoalsConceded = db.matchgoal
                                    .Where(x => matchesIds.Contains(x.MatchID) && x.TeamID == teamId)
                                    .ToList();

            var groupByPosition = teamGoalsConceded
                                  .GroupBy(x => x.player.PositionID)
                                  .OrderByDescending(x => x.Count())
                                  .ToList();


            foreach (var g in groupByPosition)
            {
                var position = db.playerposition.First(x => x.PlayerPositionID == g.Key).PlayerPositionName;
                var cnt      = g.Count();
                var newItem  = new MainCalculator.PositionGoals
                {
                    Goals    = cnt,
                    Position = position
                };
                returnVal.Add(newItem);
            }

            return(returnVal);
        }
コード例 #2
0
        public static List <string> GetTeamLetterSequence(sakilaEntities4 db, int teamId, int competitionId,
                                                          int gamesToTake = 50)
        {
            var latestMatchesLetters = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, gamesToTake)
                                       .OrderBy(x => x.MatchDate)
                                       .Select(x => GetCompetitionMatchResultLetter(x, teamId))
                                       .ToList();

            return(latestMatchesLetters);
        }
コード例 #3
0
        public static decimal GetTeamFormInLatestMatches(sakilaEntities4 db, int teamId, int competitionId, int matchesToTake = 50, DateTime?endDate = null)
        {
            if (!endDate.HasValue)
            {
                endDate = DateTime.Now;
            }

            var latestMatches    = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, matchesToTake, endDate);
            var pointsAggregated = CalculatePointsForTeamInMatches(latestMatches, teamId);

            return(Math.Round((decimal)pointsAggregated / latestMatches.Count, 2));
        }
コード例 #4
0
        public static MainCalculator.TeamStdDevAndAverage GetGoalsConcededAverageAtAway(sakilaEntities4 db, int teamId,
                                                                                        int competitionId, int matchesToTake = 50)
        {
            var relevantMatches = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, matchesToTake);
            var awayGoalsValues = relevantMatches.Where(x => x.AwayTeamID == teamId).Select(x => x.HomeGoals).ToList();
            var avg             = Math.Round(awayGoalsValues.Average(), 2);
            var result          = Math.Round(MainCalculator.CalculateStdDev(awayGoalsValues.ToList()), 2);

            return(new MainCalculator.TeamStdDevAndAverage {
                StdDev = result, Average = avg
            });
        }
コード例 #5
0
        public static TeamBalance GetTeamBalanceHome(sakilaEntities4 db, int teamId, int competitionId, int gamesToTake = 50, DateTime?endDate = null)
        {
            var latestMatches = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, gamesToTake, endDate)
                                .Where(x => x.HomeTeamID == teamId)
                                .ToList();
            var wins  = latestMatches.Count(x => x.WinnerTeamID == teamId);
            var losts = latestMatches.Count(x => x.WinnerTeamID != null && x.WinnerTeamID != teamId);
            var draws = latestMatches.Count(x => x.WinnerTeamID == null);

            return(new TeamBalance
            {
                Win = wins,
                Draw = draws,
                Lost = losts
            });
        }
コード例 #6
0
        public static decimal GetTeamFormInLatestMatchesHomeAway(sakilaEntities4 db, int teamId, int competitionId, string homeOrAway, int matchesToTake = 50)
        {
            var latestMatches = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, matchesToTake);

            if (homeOrAway == "Home")
            {
                latestMatches = latestMatches.Where(x => x.HomeTeamID == teamId).ToList();
            }
            if (homeOrAway == "Away")
            {
                latestMatches = latestMatches.Where(x => x.AwayTeamID == teamId).ToList();
            }
            var pointsAggregated = PointsCalculator.CalculatePointsForTeamInMatches(latestMatches, teamId);

            return(Math.Round((decimal)pointsAggregated / latestMatches.Count, 2));
        }
コード例 #7
0
        public TeamReportStats(Dictionary <string, List <Helper.LetterDistribution> > lettersDict,
                               Dictionary <int, List <DataObjects.AttributeType> > attributesDict,
                               int competitionId, string teamName, sakilaEntities4 db, bool isHomeTeam)
        {
            Team = db.team.First(x => x.TeamName == teamName);
            var relevantMatches = MainCalculator.GetTeamLatesMatches(db, Team.TeamID, competitionId);

            TeamForm             = LettersSequenceCalculator.GetTeamLatestSequence(db, Team.TeamID, competitionId);
            TeamOverallBalance   = new TeamOverallBalance(Team, competitionId, isHomeTeam, relevantMatches);
            TeamGoalsStats       = new TeamGoalsStats(Team, competitionId, isHomeTeam, db, relevantMatches);
            TeamAttributes       = Helper.GetTeamAttributeFromDict(Team.TeamID, attributesDict);
            TeamAttributesString = Helper.GetTeamAttributesList((int)Team.TeamID, attributesDict);
            string homeTeamSeq;

            LettersDistribution = LettersSequenceCalculator.BuildSequenceStringExpectation(db, Team.TeamID,
                                                                                           competitionId, lettersDict, out homeTeamSeq);
            TeamLastThreeResults = homeTeamSeq;
        }
コード例 #8
0
        public static MainCalculator.TeamStdDevAndAverage GetGoalsScoringAverage(sakilaEntities4 db, int teamId,
                                                                                 int competitionId, int matchesToTake = 50, DateTime?endDate = null)
        {
            if (!endDate.HasValue)
            {
                endDate = DateTime.Now;
            }

            var relevantMatches = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, matchesToTake, endDate);
            var homeGoalsValues = relevantMatches.Where(x => x.HomeTeamID == teamId).Select(x => x.HomeGoals).ToList();
            var awayGoalsValues = relevantMatches.Where(x => x.AwayTeamID == teamId).Select(x => x.AwayGoals).ToList();
            var allValues       = homeGoalsValues.Concat(awayGoalsValues).ToList();
            var avg             = Math.Round(allValues.Average(), 2);
            var result          = Math.Round(MainCalculator.CalculateStdDev(allValues.ToList()), 2);

            return(new MainCalculator.TeamStdDevAndAverage {
                StdDev = result, Average = avg
            });
        }
コード例 #9
0
        public static double GetStdEventValuePerTeamMatches(sakilaEntities4 db, int teamId, int competitionId, int eventTypeId, int gamesToTake = 50)
        {
            var matchesIds = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, gamesToTake)
                             .Select(x => x.CompetitionMatchID)
                             .ToList();

            var values =
                db.matchevent.Where(
                    x =>
                    x.EventTypeID == eventTypeId &&
                    matchesIds.Contains(x.MatchID) &&
                    x.competitionmatch.CompetitionID == competitionId &&
                    x.eventvalue != null)
                .OrderByDescending(x => x.competitionmatch.MatchDate)
                .Take(gamesToTake)
                .Select(x => (int)x.eventvalue)
                .ToList();

            return(Math.Round(MainCalculator.CalculateStdDev(values), 2));
        }
コード例 #10
0
        public static string GetTeamLatestSequence(sakilaEntities4 db, int teamId, int competitionId, int gamesToTake = 50)
        {
            var latestMatches = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, gamesToTake)
                                .OrderBy(x => x.MatchDate)
                                .Reverse()
                                .Select(x => GetCompetitionMatchResultLetter(x, teamId))
                                .ToList();
            var firstLetter = latestMatches.First();
            var sequence    = firstLetter;
            var curLetter   = latestMatches[1];
            var i           = 1;

            while (curLetter == firstLetter && i < latestMatches.Count - 1)
            {
                sequence += " " + curLetter;
                i++;
                curLetter = latestMatches[i];
            }

            return(sequence);
        }
コード例 #11
0
        public static double?GetAverageEventValuePerTeamMatches(sakilaEntities4 db, int teamId, int competitionId, int eventTypeId, int gamesToTake = 50)
        {
            var matchesIds = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, gamesToTake)
                             .Select(x => x.CompetitionMatchID)
                             .ToList();



            var average =
                db.matchevent.Where(
                    x =>
                    x.EventTypeID == eventTypeId &&
                    matchesIds.Contains(x.MatchID) &&
                    x.competitionmatch.CompetitionID == competitionId &&
                    x.eventvalue != null)
                .OrderByDescending(x => x.competitionmatch.MatchDate)
                .Take(gamesToTake)
                .Select(x => x.eventvalue)
                .Average() * 2;

            return(average);
        }
コード例 #12
0
        public static string GetTeamTopScorersAgainsePosition(sakilaEntities4 db, int teamId, int competitionId,
                                                              int gamesToTake = 50)
        {
            var latestMatches = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, gamesToTake);
            var matchesIds    = latestMatches.Select(x => x.CompetitionMatchID);

            var teamGoalsConceded = db.matchgoal
                                    .Where(x => matchesIds.Contains(x.MatchID) && x.TeamID != teamId)
                                    .ToList();

            var groupByPosition = teamGoalsConceded
                                  .GroupBy(x => x.player.PositionID)
                                  .OrderByDescending(x => x.Count())
                                  .Select(x => x.First().player.PositionID)
                                  .First();

            var positionName = db.playerposition.First(x => x.PlayerPositionID == groupByPosition).PlayerPositionName;
            var percent      = (decimal)teamGoalsConceded.Where(x => x.player.PositionID == groupByPosition).Count() /
                               teamGoalsConceded.Count;

            return(positionName + " (" + Math.Round(percent * 100, 2) + ")");
        }
コード例 #13
0
        public static TopScorer GetTeamTopScorer(sakilaEntities4 db, int teamId, int competitionId, int gamesToTake = 50)
        {
            var latestMatches = MainCalculator.GetTeamLatesMatches(db, teamId, competitionId, gamesToTake);
            var matchesIds    = latestMatches.Select(x => x.CompetitionMatchID);

            var teamGoals = db.matchgoal
                            .Where(x => matchesIds.Contains(x.MatchID) && x.TeamID == teamId)
                            .ToList();

            if (!teamGoals.Any())
            {
                return(new TopScorer
                {
                    Name = "NA",
                    Goals = 0,
                    Position = "NA"
                });
            }

            var mostGoals = teamGoals
                            .GroupBy(x => x.ScorerID)
                            .OrderByDescending(x => x.Count());

            var topScorerID = mostGoals
                              .Select(x => x.First().ScorerID)
                              .First();

            var player = db.player.First(x => x.PlayerID == topScorerID);

            var positionName = db.playerposition.First(x => x.PlayerPositionID == player.PositionID).PlayerPositionName;

            return(new TopScorer
            {
                Name = player.PlayerName,
                Goals = mostGoals.First().Count(),
                Position = positionName
            });
        }
コード例 #14
0
            public bool AggregateStats(sakilaEntities4 db, bool isTest = false)
            {
                var homeTeam = db.team.First(x => x.TeamID == HomeTeamID);
                var awayTeam = db.team.First(x => x.TeamID == AwayTeamID);

                HomeTeamValue = homeTeam.MarketValue.Value;
                AwayTeamValue = awayTeam.MarketValue.Value;
                HomeTeam      = homeTeam.TeamName;
                AwayTeam      = awayTeam.TeamName;

                HomeTeamMatches = MainCalculator.GetTeamLatesMatches(db, HomeTeamID, CompetitionID, 50, Date);
                AwayTeamMatches = MainCalculator.GetTeamLatesMatches(db, AwayTeamID, CompetitionID, 50, Date);

                if (HomeTeamMatches.Count < 4 || HomeTeamMatches.Count > 34 || AwayTeamMatches.Count < 4 || AwayTeamMatches.Count > 34)
                {
                    return(false);
                }

                HomeTotalShotsAvg = SecondaryStatsCalculator.GetAverageEventValue(db, HomeTeamID, CompetitionID, (int)DataObjects.EventType.TotalShots).Value;
                AwayTotalShotsAvg = SecondaryStatsCalculator.GetAverageEventValue(db, AwayTeamID, CompetitionID, (int)DataObjects.EventType.TotalShots).Value;

                HomeShotsOnTargetAvg = SecondaryStatsCalculator.GetAverageEventValue(db, HomeTeamID, CompetitionID, (int)DataObjects.EventType.ShotsOnTarget).Value;
                AwayShotsOnTargetAvg = SecondaryStatsCalculator.GetAverageEventValue(db, AwayTeamID, CompetitionID, (int)DataObjects.EventType.ShotsOnTarget).Value;

                HomeTotalShotsAgainstAvg = SecondaryStatsCalculator.GetAverageEventValueAgainstTeam(db, HomeTeamID, CompetitionID, (int)DataObjects.EventType.TotalShots).Value;
                AwayTotalShotsAgainstAvg = SecondaryStatsCalculator.GetAverageEventValueAgainstTeam(db, AwayTeamID, CompetitionID, (int)DataObjects.EventType.TotalShots).Value;

                HomeShotsOnTargetAgainstAvg = SecondaryStatsCalculator.GetAverageEventValueAgainstTeam(db, HomeTeamID, CompetitionID, (int)DataObjects.EventType.ShotsOnTarget).Value;
                AwayShotsOnTargetAgainstAvg = SecondaryStatsCalculator.GetAverageEventValueAgainstTeam(db, AwayTeamID, CompetitionID, (int)DataObjects.EventType.ShotsOnTarget).Value;

                HomeTotalShotsExpectedVsTeam = (HomeTotalShotsAvg + AwayTotalShotsAgainstAvg) / 2;
                AwayTotalShotsExpectedVsTeam = (AwayTotalShotsAvg + HomeTotalShotsAgainstAvg) / 2;

                HomeShotsOnTargetExpectedVsTeam = (HomeShotsOnTargetAvg + AwayShotsOnTargetAgainstAvg) / 2;
                AwayShotsOnTargetExpectedVsTeam = (AwayShotsOnTargetAvg + HomeShotsOnTargetAgainstAvg) / 2;

                HomePossessionAvg = SecondaryStatsCalculator.GetAverageEventValue(db, HomeTeamID, CompetitionID, (int)DataObjects.EventType.Possession).Value;
                AwayPossessionAvg = SecondaryStatsCalculator.GetAverageEventValue(db, AwayTeamID, CompetitionID, (int)DataObjects.EventType.Possession).Value;

                HomeFoulsAvg = SecondaryStatsCalculator.GetAverageEventValue(db, HomeTeamID, CompetitionID, (int)DataObjects.EventType.Fouls).Value;
                AwayFoulsAvg = SecondaryStatsCalculator.GetAverageEventValue(db, AwayTeamID, CompetitionID, (int)DataObjects.EventType.Fouls).Value;

                HomePointsSeasonal = PointsCalculator.CalculatePointPace(PointsCalculator.GetTeamBalance(HomeTeamMatches, HomeTeamID, CompetitionID));
                AwayPointsSeasonal = PointsCalculator.CalculatePointPace(PointsCalculator.GetTeamBalance(AwayTeamMatches, AwayTeamID, CompetitionID));

                HomePointsLast3 = PointsCalculator.CalculatePointPace(PointsCalculator.GetTeamBalance(HomeTeamMatches, HomeTeamID, CompetitionID, 3));
                AwayPointsLast3 = PointsCalculator.CalculatePointPace(PointsCalculator.GetTeamBalance(AwayTeamMatches, AwayTeamID, CompetitionID, 3));

                HomePointsAtHome = PointsCalculator.CalculatePointPace(PointsCalculator.GetTeamBalanceHome(HomeTeamMatches, HomeTeamID, CompetitionID));
                AwayPointsAtAway = PointsCalculator.CalculatePointPace(PointsCalculator.GetTeamBalanceAway(AwayTeamMatches, AwayTeamID, CompetitionID));

                HomeGoalsAvgSeasonal = GoalsCalculator.GetGoalsScoringAverage(HomeTeamMatches, HomeTeamID, CompetitionID).Average;
                AwayGoalsAvgSeasonal = GoalsCalculator.GetGoalsScoringAverage(AwayTeamMatches, AwayTeamID, CompetitionID).Average;

                HomeGoalsAvgLast3 = GoalsCalculator.GetGoalsScoringAverage(HomeTeamMatches, HomeTeamID, CompetitionID, 3).Average;
                AwayGoalsAvgLast3 = GoalsCalculator.GetGoalsScoringAverage(AwayTeamMatches, AwayTeamID, CompetitionID, 3).Average;

                HomeGoalsAvgAtHome = GoalsCalculator.GetGoalsScoringAverageAtHome(HomeTeamMatches, HomeTeamID, CompetitionID).Average;
                AwayGoalsAvgAtAway = GoalsCalculator.GetGoalsScoringAverageAtAway(AwayTeamMatches, AwayTeamID, CompetitionID).Average;

                HomeGoalsExpected = (HomeGoalsAvgSeasonal + HomeGoalsAvgLast3 + HomeGoalsAvgAtHome) / 3;
                AwayGoalsExpected = (AwayGoalsAvgSeasonal + AwayGoalsAvgLast3 + AwayGoalsAvgAtAway) / 3;

                HomeGoalsConcededAvgSeasonal = GoalsCalculator.GetGoalsConcededAverage(HomeTeamMatches, HomeTeamID, CompetitionID).Average;
                AwayGoalsConcededAvgSeasonal = GoalsCalculator.GetGoalsConcededAverage(AwayTeamMatches, AwayTeamID, CompetitionID).Average;

                HomeGoalsConcededAvgLast3 = GoalsCalculator.GetGoalsConcededAverage(HomeTeamMatches, HomeTeamID, CompetitionID, 3).Average;
                AwayGoalsConcededAvgLast3 = GoalsCalculator.GetGoalsConcededAverage(AwayTeamMatches, AwayTeamID, CompetitionID, 3).Average;

                HomeGoalsConcededAvgAtHome = GoalsCalculator.GetGoalsConcededAverageAtHome(HomeTeamMatches, HomeTeamID, CompetitionID).Average;
                AwayGoalsConcededAvgAtAway = GoalsCalculator.GetGoalsConcededAverageAtAway(AwayTeamMatches, AwayTeamID, CompetitionID).Average;

                HomeGoalsExpectedConcede = (HomeGoalsConcededAvgSeasonal + HomeGoalsConcededAvgLast3 + HomeGoalsConcededAvgAtHome) / 3;
                AwayGoalsExpectedConcede = (AwayGoalsConcededAvgSeasonal + AwayGoalsConcededAvgLast3 + AwayGoalsConcededAvgAtAway) / 3;

                HomeGoalsExpectedVsTeam = (HomeGoalsExpected + AwayGoalsExpectedConcede) / 2;
                AwayGoalsExpectedVsTeam = (AwayGoalsExpected + HomeGoalsExpectedConcede) / 2;

                HomeTrend = LettersSequenceCalculator.GetTeamLetterScore(HomeTeamMatches, HomeTeamID, CompetitionID);
                AwayTrend = LettersSequenceCalculator.GetTeamLetterScore(AwayTeamMatches, AwayTeamID, CompetitionID);

                HomePointsWeightedSeasonal = PointsCalculator.GetWeightedBalance(db, HomeTeamMatches, HomeTeamID, CompetitionID);
                AwayPointsWeightedSeasonal = (PointsCalculator.GetWeightedBalance(db, AwayTeamMatches, AwayTeamID, CompetitionID));

                ChanceCreationHome = HomePossessionAvg / HomeTotalShotsAvg;
                ChanceCreationAway = AwayPossessionAvg / AwayTotalShotsAvg;

                ShotsAccuracyHome = HomeTotalShotsAvg == 0 || HomeShotsOnTargetAvg == 0 ? 0 : HomeTotalShotsAvg / HomeShotsOnTargetAvg;
                ShotsAccuracyAway = AwayTotalShotsAvg == 0 || AwayShotsOnTargetAvg == 0 ? 0 : AwayTotalShotsAvg / AwayShotsOnTargetAvg;

                ScoringRateHome = HomeShotsOnTargetAvg == 0 || HomeGoalsAvgSeasonal == 0 ? 0 : HomeShotsOnTargetAvg / HomeGoalsAvgSeasonal;
                ScoringRateAway = AwayShotsOnTargetAvg == 0 || AwayGoalsAvgSeasonal == 0 ? 0 : AwayShotsOnTargetAvg / AwayGoalsAvgSeasonal;

                KeeperStrengthHome = HomeShotsOnTargetAgainstAvg == 0 || HomeGoalsConcededAvgSeasonal == 0 ? 0 : HomeShotsOnTargetAgainstAvg / HomeGoalsConcededAvgSeasonal;
                KeeperStrengthAway = AwayShotsOnTargetAgainstAvg == 0 || AwayGoalsConcededAvgSeasonal == 0 ? 0 : AwayShotsOnTargetAgainstAvg / AwayGoalsConcededAvgSeasonal;

                if (!isTest)
                {
                    var relevantMatchOdds = db.matchodds.FirstOrDefault(x => x.MatchID == CompetitionMatchId);
                    if (relevantMatchOdds != null)
                    {
                        HomeOddsRatio  = relevantMatchOdds.HomeTeamOdds;
                        DrawOddsRatio  = relevantMatchOdds.DrawOdds;
                        AwayOddsRatio  = relevantMatchOdds.AwayTeamOdds;
                        IsHomeFavorite = 0;
                        if (HomeOddsRatio < AwayOddsRatio)
                        {
                            IsHomeFavorite = 1;
                        }
                    }

                    else
                    {
                        Console.WriteLine("Failed to fing odds for match: " + HomeTeamID + " VS. " + AwayTeamID);
                        HomeOddsRatio  = 0.00M;
                        DrawOddsRatio  = 0.00M;
                        AwayOddsRatio  = 0.00M;
                        IsHomeFavorite = 0;
                    }
                }

                else
                {
                    HomeOddsRatio  = 0.00M;
                    DrawOddsRatio  = 0.00M;
                    AwayOddsRatio  = 0.00M;
                    IsHomeFavorite = 0;
                }


                var homeTeamSeq      = LettersSequenceCalculator.GetTeamLetterSequence(db, HomeTeamID, CompetitionID, 3);
                var homeSeqString    = string.Join("", homeTeamSeq);
                var homeTeamExpected = LettersDict.First(x => x.Key.Equals(homeSeqString)).Value;

                HomeExpectedWin = Math.Round(homeTeamExpected.First(x => x.Letter == "W").Percent, 2);

                var awayTeamSeq      = LettersSequenceCalculator.GetTeamLetterSequence(db, AwayTeamID, CompetitionID, 3);
                var awaySeqString    = string.Join("", awayTeamSeq);
                var awayTeamExpected = LettersDict.First(x => x.Key.Equals(awaySeqString)).Value;

                AwayExpectedWin = Math.Round(awayTeamExpected.First(x => x.Letter == "W").Percent, 2);

                Round = (decimal)(HomeTeamMatches.Count + AwayTeamMatches.Count) / 2;

                if (AwayPossessionAvg == 0.0 || HomePossessionAvg == 0.0)
                {
                    return(false);
                }

                return(true);
            }