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); }
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); }
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); }