public StandingTableAnalysis GetStandingTable(LeagueSeason season) { if (Db.Database.Connection.State == System.Data.ConnectionState.Closed) { Db.Database.Connection.Open(); } var standingTable = (from n in Db.StandingTableAnalysis where n.LeagueSeasonId == season.LeagueSeasonId select n).FirstOrDefault(); if (standingTable == null) { standingTable = new StandingTableAnalysis { AnalysisDone = false, Created = DateTime.Now, LastUpdate = DateTime.Now, LeagueSeasonId = season.LeagueSeasonId }; Db.StandingTableAnalysis.Add(standingTable); Db.SaveChanges(); var repo = new LogRepository(); repo.WriteLog(Severity.Information, "Insert to StandingTableAnalysis table new record", nameof(SystemDataRepository), "localhost", "[LeagueSeasonId = " + standingTable.LeagueSeasonId + "]", ""); } Db.Database.Connection.Close(); return(standingTable); }
//update properties of StandingTable in DB public void UpdateStandingTable(StandingTableAnalysis standingTable) { if (Db.Database.Connection.State == System.Data.ConnectionState.Closed) { Db.Database.Connection.Open(); } var standingTableDb = (from n in Db.StandingTableAnalysis where n.LeagueSeasonId == standingTable.LeagueSeasonId select n).FirstOrDefault(); if (standingTableDb != null) { standingTableDb.Copy(standingTable); var repo = new LogRepository(); repo.WriteLog(Database.SystemData.Severity.Information, "Update record in StandingTableAnalysis table", nameof(SystemDataRepository), "localhost", "[LeagueSeasonId = " + standingTable.LeagueSeasonId + "]", ""); } Db.SaveChanges(); Db.Database.Connection.Close(); }
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); } }