private void AnalyzeGameCompetitoresH2H(Game game)
        {
            var analyzedGames = systemRepo.GetHeadToHeadStatsAnalyzedGames();

            if (game.Result != 0 || analyzedGames.FindIndex(g => g.GameId == game.GameId) >= 0)
            {
                Console.WriteLine($"Game '{game.GameId}' already have been analyzed.");
                return;
            }

            var teamsHeadToHead   = analysisRepo.GetTeamsHeadToHead(game.HomeTeamId, game.AwayTeamId);
            var updatedHeadToHead = new HeadToHeadStats
            {
                Team1Id = teamsHeadToHead.Team1Id,
                Team2Id = teamsHeadToHead.Team2Id
            };

            var teamsHistory = leagueRepo.GetTeamsHistory(teamsHeadToHead.Team1Id, teamsHeadToHead.Team2Id);

            foreach (var historyGame in teamsHistory)
            {
                if (!ComputeH2hForGame(updatedHeadToHead, historyGame))
                {
                    return;
                }
            }

            analysisRepo.UpdateHeadToHeadStats(updatedHeadToHead);
            systemRepo.AddAnalyzedGames_HeadToHeadStats(new List <int> {
                game.GameId
            }, game.SeasonRound.LeagueSeasonId);
        }
Ejemplo n.º 2
0
        public void UpdateHeadToHeadStats(HeadToHeadStats stats)
        {
            OpenConnection();

            var h2hStats = Db.HeadToHeadStats.Where(s => s.Team1Id == stats.Team1Id && s.Team2Id == stats.Team2Id).FirstOrDefault();

            if (h2hStats != null)
            {
                h2hStats.Copy(stats);
                var repo = new LogRepository();
                repo.WriteLog(Database.SystemData.Severity.Information, "Update record in HeadToHeadStats table", nameof(AnalysisDataRepository),
                              "localhost", "[teamId1 = " + h2hStats.Team1Id + "][teamId2 = " + h2hStats.Team2Id + "]", "");
                Db.SaveChanges();
            }

            Db.Database.Connection.Close();
        }
Ejemplo n.º 3
0
        private static CyberFootballBet[] GetBets(CyberFootballMatch match, HeadToHeadStats stats)
        {
            var bets    = new List <CyberFootballBet>();
            var lastWin = stats.LastResult;

            if (lastWin == "x")
            {
                return(new CyberFootballBet[0]);
            }

            if (match.Player1.Name == lastWin)
            {
                bets.Add(new CyberFootballBet
                {
                    Match  = match,
                    Amount = 3,
                    Bet    = "win" + match.Player2.Name,
                    BetKey = match.LiveMatch.Win2betKey,
                    Odds   = match.LiveMatch.Win2Odds
                });
            }
            else
            {
                bets.Add(new CyberFootballBet
                {
                    Match  = match,
                    Amount = 3,
                    Bet    = "win" + match.Player1.Name,
                    BetKey = match.LiveMatch.Win1betKey,
                    Odds   = match.LiveMatch.Win1Odds
                });
            }

            bets.Add(new CyberFootballBet
            {
                Match  = match,
                Amount = 3,
                Bet    = "draw",
                BetKey = match.LiveMatch.DrawbetKey,
                Odds   = match.LiveMatch.DrawbetKey
            });

            return(bets.ToArray());
        }
Ejemplo n.º 4
0
        public HeadToHeadStats GetTeamsHeadToHead(int teamId1, int teamId2)
        {
            OpenConnection();
            var repo = new LogRepository();

            var headToheadStats = Db.HeadToHeadStats.Where(h2h =>
                                                           (h2h.Team1Id == teamId1 && h2h.Team2Id == teamId2)
                                                           ||
                                                           (h2h.Team1Id == teamId2 && h2h.Team2Id == teamId1)).ToList();

            if (headToheadStats.Count > 1)
            {
                repo.WriteLog(Database.SystemData.Severity.Error, "HeadToHeadStats has more then 1 record for teams", nameof(AnalysisDataRepository),
                              "localhost", "[teamId1 = " + teamId1 + "][teamId2 = " + teamId2 + "]", "");
            }

            if (headToheadStats.Count == 0)
            {
                var newH2h = new HeadToHeadStats
                {
                    Team1Id    = teamId1,
                    Team2Id    = teamId2,
                    LastUpdate = DateTime.Now
                };

                Db.HeadToHeadStats.Add(newH2h);
                Db.SaveChanges();
                headToheadStats.Add(newH2h);


                repo.WriteLog(Database.SystemData.Severity.Information, "Insert to HeadToHeadStats table new record", nameof(AnalysisDataRepository),
                              "localhost", "[teamId1 = " + teamId1 + "][teamId2 = " + teamId2 + "]", "");
            }

            Db.Database.Connection.Close();
            return(headToheadStats.First());
        }
        private bool ComputeH2hForGame(HeadToHeadStats stats, Game game)
        {
            if (game.HomeTeamId == stats.Team1Id && game.AwayTeamId == stats.Team2Id)
            {
                //game results
                if (game.Result == GameResult.HomeWin)
                {
                    stats.Team1WinsCount++;
                }
                else if (game.Result == GameResult.AwayWin)
                {
                    stats.Team2WinsCount++;
                }
                else
                {
                    stats.DrawsCount++;
                }

                //teamGoals
                stats.Team1Goals += game.HomeTeamGoals;
                stats.Team2Goals += game.AwayTeamGoals;
            }
            else if (game.HomeTeamId == stats.Team2Id && game.AwayTeamId == stats.Team1Id)
            {
                //game results
                if (game.Result == GameResult.HomeWin)
                {
                    stats.Team2WinsCount++;
                }
                else if (game.Result == GameResult.AwayWin)
                {
                    stats.Team1WinsCount++;
                }
                else
                {
                    stats.DrawsCount++;
                }

                //teamGoals
                stats.Team1Goals += game.AwayTeamGoals;
                stats.Team2Goals += game.HomeTeamGoals;
            }
            else
            {
                Console.WriteLine($"Error accuared while processing game [gameId={game.GameId}]. teamIDs = [{game.HomeTeamId},{game.AwayTeamId}] [{stats.Team1Id},{stats.Team2Id}]");
                return(false);
            }


            //total
            var total = game.AwayTeamGoals + game.HomeTeamGoals;

            stats.Goals += total;
            if (total > 2.5)
            {
                stats.GamesOver2_5++;
            }
            else
            {
                stats.GamesUnder2_5++;
            }

            //btts
            if (game.AwayTeamGoals > 0 && game.HomeTeamGoals > 0)
            {
                stats.BTTS_Yes++;
            }
            else
            {
                stats.BTTS_No++;
            }

            stats.GamePlayed++;
            //per game
            stats.GoalsPerGame      = (double)stats.Goals / stats.GamePlayed;
            stats.Team1GoalsPerGame = (double)stats.Team1Goals / stats.GamePlayed;
            stats.Team2GoalsPerGame = (double)stats.Team2Goals / stats.GamePlayed;

            stats.Team1WinsPercentage = (double)stats.Team1WinsCount / stats.GamePlayed;
            stats.Team2WinsPercentage = (double)stats.Team2WinsCount / stats.GamePlayed;
            stats.DrawsPercentage     = (double)stats.DrawsCount / stats.GamePlayed;
            return(true);
        }
Ejemplo n.º 6
0
        private static HeadToHeadStats CalculateHead2Head(HeadToHead h2h, int take = 20)
        {
            var r = new HeadToHeadStats
            {
                Player1 = h2h.Player1,
                Player2 = h2h.Player2
            };

            if (h2h.Results == null)
            {
                return(r);
            }

            foreach (var q in h2h.Results.Where(v => v.Status != "cancelled").OrderByDescending(x => x.Date).Take(take))
            {
                var p1     = q.Player1.Name;
                var p2     = q.Player2.Name;
                var result = q.FT.Split(':').Select(int.Parse).ToList();

                if (result[0] == result[1])
                {
                    r.DrawsCount++;
                    continue;
                }

                if (result[0] > result[1])
                {
                    if (p1 == r.Player1)
                    {
                        r.P1WinsCount++;
                    }
                    else
                    {
                        r.P2WinsCount++;
                    }
                    continue;
                }

                if (result[0] < result[1])
                {
                    if (p2 == r.Player2)
                    {
                        r.P2WinsCount++;
                    }
                    else
                    {
                        r.P1WinsCount++;
                    }
                }
            }

            var lastMatch = h2h.Results.Last();
            var lastRes   = lastMatch.FT.Split(':').Select(int.Parse).ToList();

            if (lastRes[0] == lastRes[1])
            {
                r.LastResult = "x";
            }
            else
            {
                if (lastRes[0] > lastRes[1])
                {
                    r.LastResult = lastMatch.Player1.Name;
                }
                else
                {
                    r.LastResult = lastMatch.Player2.Name;
                }
            }

            return(r);
        }