Ejemplo n.º 1
0
        private SeasonAndCompetitionSchedules CreateSeasonAndCompetitionSchedules(NewSeasonInfo newSeasonInfo)
        {
            var matchDateManager = new MatchDateManager(newSeasonInfo.StartYear);

            matchDateManager.Initialize();

            // The season officially ends a day after the last match so there is a possibility to add events between the last match and the end of the season.
            var endSeasonDateTime = matchDateManager.GetAllMatchDates().OrderBy(d => d).Last().AddDays(1);

            var season = new Season
            {
                StartYear    = newSeasonInfo.StartYear,
                SeasonStatus = SeasonStatus.Started,
                EndDateTime  = endSeasonDateTime
            };

            var seasonAndCompetitionSchedules = new SeasonAndCompetitionSchedules {
                Season = season
            };

            // Create leagues and schedule.
            var leagueManager = new LeagueManager();

            seasonAndCompetitionSchedules.LeaguesSchedule = leagueManager.CreateSchedules(newSeasonInfo.TeamsLeague1, newSeasonInfo.TeamsLeague2, newSeasonInfo.TeamsLeague3, newSeasonInfo.TeamsLeague4, season, matchDateManager);

            // Create a national cup tournament.
            var nationalCupManager = new NationalCupManager(_repositoryFactory);

            seasonAndCompetitionSchedules.NationalCupSchedule = nationalCupManager.CreateSchedule(newSeasonInfo.AllTeams.ToList(), season, matchDateManager);

            // Create pre-season friendlies.
            var preSeasonFriendlyManager = new PreSeasonFriendlyManager();

            seasonAndCompetitionSchedules.PreSeasonFriendliesSchedule = preSeasonFriendlyManager.CreatePreSeasonSchedule(newSeasonInfo.AllTeams.ToList(), season, matchDateManager);

            // Create friendlies during the season.
            // Determine on which dates these friendlies can be played. For now, this is only during the national cup tournament, except the first round and the final.
            var cupDates = seasonAndCompetitionSchedules.NationalCupSchedule.Rounds.Select(r => r.MatchDate).Skip(1).Take(seasonAndCompetitionSchedules.NationalCupSchedule.Rounds.Count - 2).ToList();
            var friendlyRoundsManager = new DuringSeasonFriendlyRoundsManager();

            seasonAndCompetitionSchedules.DuringSeasonFriendliesSchedule = friendlyRoundsManager.CreateDuringSeasonFriendlyRounds(seasonAndCompetitionSchedules.PreSeasonFriendliesSchedule.SeasonCompetitions.First(), cupDates, Constants.HowManyPreSeasonFriendlies + 1);

            // Create Super Cup.
            var nationalSuperCupManager = new NationalSuperCupManager();
            // The home team is always the national champion and the away team is either the cup winner or the league 1 runner up if the champion also won the cup.
            var homeTeam = newSeasonInfo.PreviousSeasonStatistics.NationalChampion;
            var awayTeam = newSeasonInfo.PreviousSeasonStatistics.NationalChampion.Equals(newSeasonInfo.PreviousSeasonStatistics.CupWinner) ? newSeasonInfo.PreviousSeasonStatistics.NationalChampionRunnerUp : newSeasonInfo.PreviousSeasonStatistics.CupWinner;

            seasonAndCompetitionSchedules.NationalSuperCupSchedule = nationalSuperCupManager.CreateSchedule(homeTeam, awayTeam, season, matchDateManager);

            // In the meantime 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);
        }
        public void CreatePreSeasonSchedule_WhenDuplicateTeams_ThrowsException()
        {
            // Arrange
            var teams = GetTeams(1).ToList();

            teams.Add(teams[0]);

            // Act
            var friendlyManager = new PreSeasonFriendlyManager();

            friendlyManager.CreatePreSeasonSchedule(teams, new Season(), GetMatchDateManager());
        }
        public void CreatePreSeasonSchedule_CreatesRoundsAndMatches()
        {
            // Arrange
            const int numberOfTeams          = 16;
            const int expectedNumberOfRounds = 4;

            var friendlyManager = new PreSeasonFriendlyManager();

            var teams  = GetTeams(numberOfTeams).ToList();
            var season = new Season();
            MatchDateManager matchDateManager = GetMatchDateManager();

            // Act
            var schedule = friendlyManager.CreatePreSeasonSchedule(teams, season, matchDateManager);

            // Assert

            // Check the expected number of rounds.
            Assert.AreEqual(expectedNumberOfRounds, schedule.Rounds.Count);

            // There are 8 matches per round.
            var numberOfMatchesPerRound = schedule.Matches.GroupBy(m => m.RoundId).Select(c => c.Count());

            Assert.IsTrue(numberOfMatchesPerRound.All(x => x == 8));

            // Per round, check all matches have different teams.
            foreach (var round in schedule.Rounds)
            {
                var matches          = schedule.Matches.Where(m => m.RoundId == round.Id);
                var teamsInThisRound = matches.Select(m => m.HomeTeamId).ToList();
                teamsInThisRound.AddRange(matches.Select(m => m.AwayTeamId));
                Assert.IsTrue(teamsInThisRound.Distinct().Count() == teamsInThisRound.Count(), "Per round all matches must have different teams");
            }

            // Check every team has different opponents.
            foreach (var team in teams)
            {
                var opponents = schedule.Matches.Where(m => m.HomeTeam.Id == team.Id).Select(m => m.AwayTeam).ToList();
                opponents.AddRange(schedule.Matches.Where(m => m.AwayTeam.Id == team.Id).Select(m => m.HomeTeam));
                Assert.IsFalse(opponents.GroupBy(t => t.Id).Where(g => g.Count() > 1).Any(), "Every team must have different opponents");
            }
        }