Example #1
0
        public CompetitionSchedule CreateSchedule(List <Team> teams, Season season, MatchDateManager matchDateManager)
        {
            var competitionSchedule = new CompetitionSchedule();

            // Create a cup season competition.
            SeasonCompetition cupSeasonCompetition = new SeasonCompetition
            {
                Competition = _competition,
                Season      = season
            };

            competitionSchedule.SeasonCompetitions.Add(cupSeasonCompetition);

            var cupSchedule = new KnockoutTournamentManager().GetSchedule(teams);

            int numberOfRounds = DetermineNumberOfRounds(teams.Count);

            var firstScheduleItem = cupSchedule.First();
            var matchDate         = matchDateManager.GetNextMatchDate(CompetitionType.NationalCup, firstScheduleItem.Key);

            // Create the first round and its matches.
            int roundIndex = 0;
            var firstRound = RoundFactory.CreateRound(GetCupRoundName(numberOfRounds, roundIndex), cupSeasonCompetition, matchDate, roundIndex, _competition);

            foreach (var match in firstScheduleItem.Value)
            {
                match.Season        = season;
                match.Round         = firstRound;
                match.Date          = matchDate;
                match.DrawPermitted = false;
                match.CompetitionId = _competition.Id;
                competitionSchedule.Matches.Add(match);
            }

            competitionSchedule.Rounds.Add(firstRound);

            // Create remaining rounds for the tournament, these rounds do not have matches yet.
            // The date on which the matches on these rounds will be played are stored in the round.
            int numberOfRoundsLeft = numberOfRounds - 1;

            if (numberOfRoundsLeft > 0)
            {
                for (int i = 0; i < numberOfRoundsLeft; i++)
                {
                    roundIndex++;

                    matchDate = matchDateManager.GetNextMatchDate(CompetitionType.NationalCup, roundIndex);
                    var round = RoundFactory.CreateRound(GetCupRoundName(numberOfRounds, roundIndex), cupSeasonCompetition, matchDate, roundIndex, _competition);
                    competitionSchedule.Rounds.Add(round);
                }
            }

            // Add the teams to the cup of this season.
            foreach (var team in teams)
            {
                var seasonCompetitionTeam = new SeasonCompetitionTeam
                {
                    SeasonCompetition = cupSeasonCompetition,
                    Team = team
                };
                competitionSchedule.SeasonCompetitionTeams.Add(seasonCompetitionTeam);
            }

            return(competitionSchedule);
        }