public Season CreateFirstSeason(Game game, List<Team> teams, IUnitOfWorkRepository repository) { // First check the number of teams can be evenly divided into the number of leagues. bool teamsOk = teams.Count % Constants.HowManyLeagues == 0; if (!teamsOk) { throw new Exception($"The number of teams must be divided by {Constants.HowManyLeagues}"); } var newSeasonInfo = new NewSeasonInfo { Game = game, SeasonNumber = 0 }; // Divide all teams between the four leagues based on the team rating. teams.Sort((team1, team2) => team2.Rating.CompareTo(team1.Rating)); int countTeamsPerLeague = teams.Count / Constants.HowManyLeagues; newSeasonInfo.TeamsLeague1 = teams.Take(countTeamsPerLeague).ToList(); newSeasonInfo.TeamsLeague2 = teams.Skip(countTeamsPerLeague).Take(countTeamsPerLeague).ToList(); newSeasonInfo.TeamsLeague3 = teams.Skip(countTeamsPerLeague * 2).Take(countTeamsPerLeague).ToList(); newSeasonInfo.TeamsLeague4 = teams.Skip(countTeamsPerLeague * 3).ToList(); // The teams have been sorted on rating, so given them an initial league table position. AssignInitialLeagueTablePosition(teams); // In the first season there are no champion and cup winner yet, so pick the two best teams. newSeasonInfo.NationalChampion = teams[0]; newSeasonInfo.NationalCupWinner = teams[1]; // Now all teams have been placed in the right leagues, so create match schedules for all competitions. var seasonAndCompetitionSchedules = CreateSeasonAndCompetitionSchedules(newSeasonInfo); // Insert the season and all competition schedules. var season = InsertSeasonAndCompetitionSchedule(repository, seasonAndCompetitionSchedules); return season; }
public void CreateNextSeason(string previousSeasonId, IUnitOfWorkRepository repository) { Season previousSeason; using (var seasonRepository = _repositoryFactory.CreateSeasonRepository()) { previousSeason = seasonRepository.GetOne(previousSeasonId); } if (previousSeason.SeasonStatus != SeasonStatus.Ended) { throw new ConflictException("Season must be ended before a new one can be created"); } var newSeasonInfo = new NewSeasonInfo { Game = previousSeason.Game, SeasonNumber = previousSeason.GameOrder + 1 }; // Determine which teams promote and relegate. IEnumerable<Team> allTeamsSortedOnLeagueAndPosition; using (var teamRepository = _repositoryFactory.CreateTeamRepository()) { allTeamsSortedOnLeagueAndPosition = teamRepository.GetTeamsByGame(previousSeason.GameId).OrderBy(x => x.CurrentLeagueCompetition.Order).ThenBy(x => x.CurrentLeaguePosition); } var teamsGroupedPerLeague = allTeamsSortedOnLeagueAndPosition.GroupBy(t => t.CurrentLeagueCompetitionId).Select(grp => grp.ToList()).ToList(); var newLeagues = _leagueManager.PromoteAndRelegateTeams(teamsGroupedPerLeague); newSeasonInfo.TeamsLeague1 = newLeagues[0]; newSeasonInfo.TeamsLeague2 = newLeagues[1]; newSeasonInfo.TeamsLeague3 = newLeagues[2]; newSeasonInfo.TeamsLeague4 = newLeagues[3]; // Determine champion of the highest league. newSeasonInfo.NationalChampion = teamsGroupedPerLeague[0][0]; // Determine cup winner. using (var matchRepository = new RepositoryFactory().CreateMatchRepository()) { var cupFinal = matchRepository.GetNationalCupFinal(previousSeason.Id); newSeasonInfo.NationalCupWinner = cupFinal.GetWinner(); } // Now all teams have been placed in the right leagues, so create match schedules for all competitions. var seasonAndCompetitionSchedules = CreateSeasonAndCompetitionSchedules(newSeasonInfo); // Insert the season and all competition schedules. InsertSeasonAndCompetitionSchedule(repository, seasonAndCompetitionSchedules); }
private SeasonAndCompetitionSchedules CreateSeasonAndCompetitionSchedules(NewSeasonInfo newSeasonInfo) { var seasonAndCompetitionSchedules = new SeasonAndCompetitionSchedules(); // Create a new season. string seasonName = $"Season {newSeasonInfo.SeasonNumber + 1}"; var season = new Season { Name = seasonName, GameOrder = newSeasonInfo.SeasonNumber, Game = newSeasonInfo.Game, SeasonStatus = SeasonStatus.Started }; seasonAndCompetitionSchedules.Season = season; var competitionManagerFactory = new CompetitionManagerFactory(); var matchDateManager = new MatchDateManager(newSeasonInfo.SeasonNumber); matchDateManager.Initialize(); // Create leagues and schedule. var leagueManager = competitionManagerFactory.CreateLeagueManager(); seasonAndCompetitionSchedules.LeaguesSchedule = leagueManager.CreateSchedules(newSeasonInfo.TeamsLeague1, newSeasonInfo.TeamsLeague2, newSeasonInfo.TeamsLeague3, newSeasonInfo.TeamsLeague4, season, matchDateManager); // Create a national cup tournament. var nationalCupManager = competitionManagerFactory.CreateNationalCupManager(); seasonAndCompetitionSchedules.NationalCupSchedule = nationalCupManager.CreateSchedule(newSeasonInfo.AllTeams.ToList(), season, matchDateManager); // Create friendlies and save it to the database. var friendlyManager = competitionManagerFactory.CreateFriendlyManager(); seasonAndCompetitionSchedules.PreSeasonFriendliesSchedule = friendlyManager.CreatePreSeasonSchedule( newSeasonInfo.TeamsLeague1, newSeasonInfo.TeamsLeague2, newSeasonInfo.TeamsLeague3, newSeasonInfo.TeamsLeague4, 4, season, matchDateManager); // Create Super Cup and save it to the database. var nationalSuperCupManager = competitionManagerFactory.CreateNationalSuperCupManager(); seasonAndCompetitionSchedules.NationalSuperCupSchedule = nationalSuperCupManager.CreateSchedule(newSeasonInfo.NationalChampion, newSeasonInfo.NationalCupWinner, season, matchDateManager); // In the mean time data of the teams has changed, so add them to the SeasonAndCompetitionSchedules object so they can be updated in the database. seasonAndCompetitionSchedules.Teams = newSeasonInfo.AllTeams; return seasonAndCompetitionSchedules; }