private void AnalyzeLeagueSeason(LeagueSeason season, StandingTableAnalysis standingTableRecord) { var allSeasonGames = leagueRepo.GetAllSeasonGames(season.LeagueSeasonId); var analyzedGames = systemRepo.GetStandingTableAnalyzedGames(season.LeagueSeasonId); var allTeams = allSeasonGames.Select(g => g.AwayTeamId) .Union(allSeasonGames.Select(g => g.HomeTeamId)) .Distinct().OrderBy(i => i).ToList(); var processedGames = new List <int>(); var allSeasonTeams = analysisRepo.GetRangeOfTeams(allTeams, season); foreach (var game in allSeasonGames) { if (game.Result == 0 || analyzedGames.FindIndex(g => g.GameId == game.GameId) >= 0) { continue; } var homeTeam = allSeasonTeams.Where(n => n.FootballTeamId == game.HomeTeamId).FirstOrDefault(); var awayTeam = allSeasonTeams.Where(n => n.FootballTeamId == game.AwayTeamId).FirstOrDefault(); homeTeam.GamePlayed++; awayTeam.GamePlayed++; homeTeam.GoalsFor += game.HomeTeamGoals; homeTeam.GoalsAgainst += game.AwayTeamGoals; awayTeam.GoalsFor += game.AwayTeamGoals; awayTeam.GoalsAgainst += game.HomeTeamGoals; if (game.Result == GameResult.HomeWin) { homeTeam.WinsCount++; awayTeam.LossesCount++; homeTeam.Points += 3; } else if (game.Result == GameResult.AwayWin) { awayTeam.WinsCount++; homeTeam.LossesCount++; awayTeam.Points += 3; } else { awayTeam.DrawsCount++; homeTeam.DrawsCount++; awayTeam.Points += 1; homeTeam.Points += 1; } processedGames.Add(game.GameId); } //set table places allSeasonTeams = allSeasonTeams.OrderBy(i => i.Points).ThenBy(i => (i.GoalsFor - i.GoalsAgainst)).ToList(); int place = 20; bool finishedSeason = true; foreach (var team in allSeasonTeams) { team.TablePlace = place; if (team.GamePlayed < 38) { finishedSeason = false; } Console.WriteLine($"[{place}] TeamID = {team.FootballTeamId} GamePlayed = {team.GamePlayed} Points = {team.Points} GoalDiff = {team.GoalsFor - team.GoalsAgainst}"); place--; } analysisRepo.UpdateSeasonTeams(allSeasonTeams); systemRepo.AddAnalyzedGames_StandingTable(processedGames, season.LeagueSeasonId); if (finishedSeason) { standingTableRecord.AnalysisDone = true; systemRepo.UpdateStandingTable(standingTableRecord); } }
public LeagueSeasonModule() { Get[SharedRoutes.StandingTable] = parameters => { var seasonId = (int)parameters.seasonId; var season = leagueRepo.GetLeagueSeasonById(seasonId); var seasonTeams = analysisRepo.GetSeasonTeams(seasonId); var teamsDb = leagueRepo.GetFootballTeamsByIds(seasonTeams.Select(t => t.FootballTeamId).ToArray()); var standingTable = new StandingTableDto { LeagueSeasonId = seasonId, StartDate = season.StartDate, EndDate = season.EndDate, IsFinished = season.EndDate < DateTime.Now, Teams = seasonTeams.Select(Mapper.Map <LeagueSeasonTeamDto>).OrderBy(t => t.TablePlace).ToList() }; foreach (var team in standingTable.Teams) { var teamDb = teamsDb.Where(t => t.TeamId == team.FootballTeamId).FirstOrDefault(); team.FootballTeamName = teamDb.Name; } return(Response.AsJson(standingTable)); }; Get[SharedRoutes.SeasonGames] = parameters => { var seasonId = (int)parameters.seasonId; var seasonGames = leagueRepo.GetAllSeasonGames(seasonId).Select(Mapper.Map <GameDto>).ToList(); return(Response.AsJson(seasonGames)); }; Get[SharedRoutes.GameStats] = parameters => { var seasonId = (int)parameters.seasonId; var matchId = (int)parameters.id; if (!leagueRepo.GetAllSeasonGames(seasonId).Any(g => g.GameId == matchId)) { return(HttpStatusCode.NotFound); } bool onlyCreated; var gameStatsDb = leagueRepo.GetGameStats(matchId, out onlyCreated); var gameStats = Mapper.Map <GameInfoDto>(gameStatsDb); return(Response.AsJson(gameStats)); }; Get[SharedRoutes.SeasonTotalStats] = parameters => { var seasonId = (int)parameters.seasonId; var season = leagueRepo.GetLeagueSeasonById(seasonId); var seasonTeams = analysisRepo.GetSeasonTeams(seasonId); var teamsFormDb_allGames = analysisRepo.GetSeasonTeamForms_AllGames(seasonId); var teamsFormDb_homeGames = analysisRepo.GetSeasonTeamForms_HomeGames(seasonId); var teamsFormDb_awayGames = analysisRepo.GetSeasonTeamForms_AwayGames(seasonId); var teamTotals = new List <SeasonTeamTotalDto>(); foreach (var team in seasonTeams) { var teamFormDb_allGames = teamsFormDb_allGames.Where(t => t.TeamId == team.FootballTeamId).FirstOrDefault(); var teamFormDb_homeGames = teamsFormDb_homeGames.Where(t => t.TeamId == team.FootballTeamId).FirstOrDefault(); var teamFormDb_awayGames = teamsFormDb_awayGames.Where(t => t.TeamId == team.FootballTeamId).FirstOrDefault(); var teamTotal = new SeasonTeamTotalDto { FootballTeamId = team.FootballTeamId, FootballTeamName = team.FootballTeam.Name, GamePlayed = team.GamePlayed, TotalOver = teamFormDb_allGames?.GamesOver2_5 ?? 0, TotalUnder = teamFormDb_allGames?.GamesUnder2_5 ?? 0, TotalOverHome = teamFormDb_homeGames?.GamesOver2_5 ?? 0, TotalUnderHome = teamFormDb_homeGames?.GamesUnder2_5 ?? 0, TotalOverAway = teamFormDb_awayGames?.GamesOver2_5 ?? 0, TotalUnderAway = teamFormDb_awayGames?.GamesUnder2_5 ?? 0 }; teamTotals.Add(teamTotal); } var totalTable = new TotalTableDto { LeagueSeasonId = seasonId, StartDate = season.StartDate, EndDate = season.EndDate, IsFinished = season.EndDate < DateTime.Now, Teams = teamTotals.OrderBy(t => t.TotalOver).ToList() }; return(Response.AsJson(totalTable)); }; Get[SharedRoutes.SeasonBttsStats] = parameters => { var seasonId = (int)parameters.seasonId; var season = leagueRepo.GetLeagueSeasonById(seasonId); var seasonTeams = analysisRepo.GetSeasonTeams(seasonId); var teamsFormDb_allGames = analysisRepo.GetSeasonTeamForms_AllGames(seasonId); var teamsFormDb_homeGames = analysisRepo.GetSeasonTeamForms_HomeGames(seasonId); var teamsFormDb_awayGames = analysisRepo.GetSeasonTeamForms_AwayGames(seasonId); var teamTotals = new List <SeasonTeamBttsDto>(); foreach (var team in seasonTeams) { var teamFormDb_allGames = teamsFormDb_allGames.Where(t => t.TeamId == team.FootballTeamId).FirstOrDefault(); var teamFormDb_homeGames = teamsFormDb_homeGames.Where(t => t.TeamId == team.FootballTeamId).FirstOrDefault(); var teamFormDb_awayGames = teamsFormDb_awayGames.Where(t => t.TeamId == team.FootballTeamId).FirstOrDefault(); var teamTotal = new SeasonTeamBttsDto { FootballTeamId = team.FootballTeamId, FootballTeamName = team.FootballTeam.Name, GamePlayed = team.GamePlayed, BttsYes = teamFormDb_allGames?.BTTS_Yes ?? 0, BttsNo = teamFormDb_allGames?.BTTS_No ?? 0, BttsYesHome = teamFormDb_homeGames?.BTTS_Yes ?? 0, BttsNoHome = teamFormDb_homeGames?.BTTS_No ?? 0, BttsYesAway = teamFormDb_awayGames?.BTTS_Yes ?? 0, BttsNoAway = teamFormDb_awayGames?.BTTS_No ?? 0 }; teamTotals.Add(teamTotal); } var totalTable = new BttsTableDto { LeagueSeasonId = seasonId, StartDate = season.StartDate, EndDate = season.EndDate, IsFinished = season.EndDate < DateTime.Now, Teams = teamTotals.OrderBy(t => t.BttsYes).ToList() }; return(Response.AsJson(totalTable)); }; }
public static void CheckSeasonStreaks(PredictionData data, Game game) { var leagueRepo = new LeagueDataRepository(); var analysisRepo = new AnalysisDataRepository(); var seasonStats = analysisRepo.GetSeasonStats(game.SeasonRound.LeagueSeasonId); var allSeasonGames = leagueRepo.GetAllSeasonGames(game.SeasonRound.LeagueSeasonId).Where(g => g.Result != 0).OrderByDescending(g => g.Date).ToList(); //var homeTeamId = game.HomeTeamId; //var awayTeamId = game.AwayTeamId; int lastHomeTeamWinGamesAgo = -1, lastAwayTeamWinGamesAgo = -1, lastDrawGamesAgo = -1; int lastBttsYes = -1, lastBttsNo = -1; int lastTotalOver = -1, lastTotalUnder = -1; for (int i = 0; i < allSeasonGames.Count; i++) { if (lastHomeTeamWinGamesAgo > -1 && lastAwayTeamWinGamesAgo > -1 && lastDrawGamesAgo > -1 && lastTotalOver > -1 && lastTotalUnder > -1 && lastBttsYes > -1 && lastBttsNo > -1) { break; } var tempGame = allSeasonGames[i]; 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; } 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; } } var averageHomeTeamWinFreq = 1 / seasonStats.HomeWinsPercentage; var averageAwayTeamWinFreq = 1 / seasonStats.AwayWinsPercentage; var averageDrawsFreq = 1 / seasonStats.DrawsPercentage; var averageBttsYesFreq = (double)seasonStats.GamePlayed / seasonStats.BTTS_Yes; var averageBttsNoFreq = (double)seasonStats.GamePlayed / seasonStats.BTTS_No; var averageTotalOverFreq = (double)seasonStats.GamePlayed / seasonStats.GamesOver2_5; var averageTotalUnderFreq = (double)seasonStats.GamePlayed / seasonStats.GamesUnder2_5; var minFreq = averageHomeTeamWinFreq; if (averageAwayTeamWinFreq < minFreq) { minFreq = averageAwayTeamWinFreq; } if (averageDrawsFreq < minFreq) { minFreq = averageDrawsFreq; } if (minFreq == averageHomeTeamWinFreq) { data.CalculatedProbabilities.HomeWinProbability *= SeasonStatsCoef; } if (minFreq == averageDrawsFreq) { data.CalculatedProbabilities.DrawProbability *= SeasonStatsCoef; } if (minFreq == averageAwayTeamWinFreq) { data.CalculatedProbabilities.AwayWinProbability *= SeasonStatsCoef; } minFreq = averageBttsYesFreq; if (averageBttsNoFreq < minFreq) { minFreq = averageBttsNoFreq; } if (minFreq == averageBttsNoFreq) { data.CalculatedProbabilities.BttsNoProbability *= SeasonStatsCoef; } if (minFreq == averageBttsYesFreq) { data.CalculatedProbabilities.BttsYesProbability *= SeasonStatsCoef; } minFreq = averageTotalOverFreq; if (averageTotalUnderFreq < minFreq) { minFreq = averageTotalUnderFreq; } if (minFreq == averageTotalOverFreq) { data.CalculatedProbabilities.TotalOverProbability *= SeasonStatsCoef; } if (minFreq == averageTotalUnderFreq) { data.CalculatedProbabilities.TotalUnderProbability *= SeasonStatsCoef; } //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; data.CalculatedProbabilities.HomeWinProbability *= GetCoefByDifference(diffHomeTeamWins); data.CalculatedProbabilities.AwayWinProbability *= GetCoefByDifference(diffAwayTeamWins); data.CalculatedProbabilities.DrawProbability *= GetCoefByDifference(diffDraws); data.CalculatedProbabilities.BttsYesProbability *= GetCoefByDifference(diffBttsYes); data.CalculatedProbabilities.BttsNoProbability *= GetCoefByDifference(diffBttsNo); data.CalculatedProbabilities.TotalOverProbability *= GetCoefByDifference(diffTotalOver); data.CalculatedProbabilities.TotalUnderProbability *= GetCoefByDifference(diffTotalUnder); }