Exemplo 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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public CompetitionSchedule CreateSchedule(Team team1, Team team2, Season season, MatchDateManager matchDateManager)
        {
            var competitionSchedule = new CompetitionSchedule();

            using (var competitionRepository = new RepositoryFactory().CreateCompetitionRepository())
            {
                // Create a super cup season competition and round and save it to the database.
                var superCupCompetition       = competitionRepository.GetNationalSuperCup();
                var superCupSeasonCompetition = new SeasonCompetition
                {
                    Competition = superCupCompetition,
                    Season      = season
                };
                competitionSchedule.SeasonCompetitions.Add(superCupSeasonCompetition);

                const int roundNr = 0;

                var matchDate = matchDateManager.GetNextMatchDate(CompetitionType.NationalSuperCup, roundNr);

                var superCupRound = RoundFactory.CreateRound("Final", superCupSeasonCompetition, matchDate, roundNr, superCupCompetition);
                competitionSchedule.Rounds.Add(superCupRound);

                // Create the super cup match and save it to the database.
                var teams1 = new List <Team> {
                    team1
                };
                var teams2 = new List <Team> {
                    team2
                };
                var singleRoundTournamentManager = new SingleRoundTournamentManager();
                var match = singleRoundTournamentManager.GetMatches(teams1, teams2).Single();

                match.Season        = season;
                match.Round         = superCupRound;
                match.Date          = matchDate;
                match.DrawPermitted = false;
                match.CompetitionId = superCupCompetition.Id;
                competitionSchedule.Matches.Add(match);

                // Add both teams to the super cup competition of this season.
                var seasonCompetitionTeam1 = new SeasonCompetitionTeam
                {
                    Team = team1,
                    SeasonCompetition = superCupSeasonCompetition
                };
                var seasonCompetitionTeam2 = new SeasonCompetitionTeam
                {
                    Team = team2,
                    SeasonCompetition = superCupSeasonCompetition
                };
                competitionSchedule.SeasonCompetitionTeams.Add(seasonCompetitionTeam1);
                competitionSchedule.SeasonCompetitionTeams.Add(seasonCompetitionTeam2);
            }

            return(competitionSchedule);
        }
Exemplo n.º 4
0
        private void CreateLeague(CompetitionSchedule competitionSchedule, Competition leagueCompetition, List <Team> teams, Season season, MatchDateManager matchDateManager)
        {
            // Create a competition for the League and save it to the database.
            var leagueSeasonCompetition = new SeasonCompetition
            {
                Competition = leagueCompetition,
                Season      = season
            };

            competitionSchedule.SeasonCompetitions.Add(leagueSeasonCompetition);

            // Add the teams to the league.
            foreach (var team in teams)
            {
                var seasonCompetitionTeam = new SeasonCompetitionTeam
                {
                    SeasonCompetition = leagueSeasonCompetition,
                    Team = team
                };
                competitionSchedule.SeasonCompetitionTeams.Add(seasonCompetitionTeam);

                // Update current league for the team.
                team.CurrentLeagueCompetition = leagueCompetition;
            }

            // Create a match schedule.
            var roundRobinTournamentManager = new RoundRobinTournamentManager();
            var matchSchedule = roundRobinTournamentManager.GetSchedule(teams);

            foreach (var round in matchSchedule)
            {
                var matchDate   = matchDateManager.GetNextMatchDate(CompetitionType.League, round.Key);
                var leagueRound = RoundFactory.CreateRound($"Round {round.Key + 1}", leagueSeasonCompetition, matchDate, round.Key, leagueCompetition);
                competitionSchedule.Rounds.Add(leagueRound);

                foreach (var match in round.Value)
                {
                    match.Season        = season;
                    match.Round         = leagueRound;
                    match.Date          = matchDate;
                    match.CompetitionId = leagueCompetition.Id;
                    competitionSchedule.Matches.Add(match);
                }
            }

            // Create a league table.
            var leagueTable = new LeagueTable
            {
                CompetitionName   = leagueCompetition.Name,
                SeasonCompetition = leagueSeasonCompetition,
                SeasonId          = leagueSeasonCompetition.SeasonId
            };

            leagueTable.LeagueTablePositions = new List <LeagueTablePosition>();
            int position = 1;

            foreach (var team in teams)
            {
                team.CurrentLeaguePosition = position;
                leagueTable.LeagueTablePositions.Add(new LeagueTablePosition {
                    Team = team, LeagueTable = leagueTable, Position = position
                });
                position++;
            }

            competitionSchedule.LeagueTables.Add(leagueTable);
        }
Exemplo n.º 5
0
        public CompetitionSchedule CreateSchedules(List <Team> teamsLeague1, List <Team> teamsLeague2, List <Team> teamsLeague3, List <Team> teamsLeague4, Season season, MatchDateManager matchDateManager)
        {
            var competitionSchedule = new CompetitionSchedule();

            using (var competitionRepository = new RepositoryFactory().CreateCompetitionRepository())
            {
                // Create the leagues with the teams.
                CreateLeague(competitionSchedule, competitionRepository.GetLeague1(), teamsLeague1, season, matchDateManager);
                CreateLeague(competitionSchedule, competitionRepository.GetLeague2(), teamsLeague2, season, matchDateManager);
                CreateLeague(competitionSchedule, competitionRepository.GetLeague3(), teamsLeague3, season, matchDateManager);
                CreateLeague(competitionSchedule, competitionRepository.GetLeague4(), teamsLeague4, season, matchDateManager);
            }

            return(competitionSchedule);
        }