Example #1
0
 public IEnumerable<Match> GetByRound(Round round)
 {
     using (var matchRepository = new RepositoryFactory().CreateMatchRepository())
      {
     return matchRepository.GetByRound(round.Id);
      }
 }
Example #2
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 superCupRound = new Round
            {
               Id = IdGenerator.GetId(),
               Name = "Super Cup Final",
               SeasonCompetition = superCupSeasonCompetition,
               Order = roundNr,
               CompetitionName = "Super Cup",
               CompetitionType = CompetitionType.NationalSuperCup
            };
            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 = matchDateManager.GetNextMatchDate(CompetitionType.NationalSuperCup, roundNr);
            match.DrawPermitted = false;

            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;
        }
Example #3
0
        public List<Match> DrawNextRound(Round previousRound)
        {
            var matchesToUpdate = new List<Match>();

             // Determine whether for the given round all matches have been played.
             using (var matchRepository = new RepositoryFactory().CreateMatchRepository())
             using (var roundRepository = new RepositoryFactory().CreateRoundRepository())
             {
            var matchesCurrentRound = matchRepository.GetByRound(previousRound.Id).ToList();
            bool allMatchesPlayed = matchesCurrentRound.All(match => match.MatchStatus == MatchStatus.Ended);

            if (allMatchesPlayed)
            {
               // Determine the next round and verify the matches have no home and away teams defined yet.
               var nextRound = roundRepository.GetNextRound(previousRound);
               if (nextRound != null)
               {
                  var matchesNextRound = matchRepository.GetByRound(nextRound.Id).ToList();
                  bool roundMustBeDrawn = matchesNextRound.All(match => match.MatchStatus == MatchStatus.TeamsUndefined);

                  if (roundMustBeDrawn)
                  {
                     var winners = matchesCurrentRound.Select(match => match.GetWinner()).ToList();
                     new KnockoutTournamentManager().DrawNextRound(winners, matchesNextRound);

                     foreach (var match in matchesNextRound)
                     {
                        matchesToUpdate.Add(match);
                     }
                  }
               }
            }
             }

             return matchesToUpdate;
        }
Example #4
0
        public CompetitionSchedule CreateSchedule(List<Team> teams, Season season, MatchDateManager matchDateManager)
        {
            //Draws the first round.
             //Gets the schedule for all rounds, where only the first round has a HomeTeam and AwayTeam.
             //Determines match dates with the DateMatchManager.
             //Creates SeasonCompetition, Rounds etc.

             var competitionSchedule = new CompetitionSchedule();

             // Create a cup season competition.
             SeasonCompetition cupSeasonCompetition;

             using (var competitionRepository = new RepositoryFactory().CreateCompetitionRepository())
             {
            var cup = competitionRepository.GetNationalCup();
            cupSeasonCompetition = new SeasonCompetition
            {
               Competition = cup,
               Season = season
            };
             }

             competitionSchedule.SeasonCompetitions.Add(cupSeasonCompetition);

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

             foreach (var round in cupSchedule)
             {
            var cupRound = new Round
            {
               Id = IdGenerator.GetId(),
               Name = GetCupRoundName(cupSchedule.Count, round.Key),
               SeasonCompetition = cupSeasonCompetition,
               Order = round.Key,
               CompetitionName = "Cup",
               CompetitionType = CompetitionType.NationalCup
            };
            competitionSchedule.Rounds.Add(cupRound);

            foreach (var match in round.Value)
            {
               match.Season = season;
               match.Round = cupRound;
               match.Date = matchDateManager.GetNextMatchDate(CompetitionType.NationalCup, round.Key);
               match.DrawPermitted = false;

               competitionSchedule.Matches.Add(match);
            }
             }

             // 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;
        }
Example #5
0
        private void CreateLeague(CompetitionSchedule competitionSchedule, Competition league, List<Team> teams, Season season, MatchDateManager matchDateManager)
        {
            // Create a competition for the League and save it to the database.
             var leagueSeasonCompetition = new SeasonCompetition
             {
            Competition = league,
            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 = league;
             }

             // Create a match schedule.
             var roundRobinTournamentManager = new RoundRobinTournamentManager();
             var matchSchedule = roundRobinTournamentManager.GetSchedule(teams);
             foreach (var round in matchSchedule)
             {
            var leagueRound = new Round
            {
               Id = IdGenerator.GetId(),
               Name = $"{league.Name} round {round.Key + 1}",
               SeasonCompetition = leagueSeasonCompetition,
               Order = round.Key,
               CompetitionName = league.Name,
               CompetitionType = CompetitionType.League
            };

            competitionSchedule.Rounds.Add(leagueRound);

            foreach (var match in round.Value)
            {
               match.Season = season;
               match.Round = leagueRound;
               match.Date = matchDateManager.GetNextMatchDate(CompetitionType.League, round.Key);
               competitionSchedule.Matches.Add(match);
            }
             }

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

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

             competitionSchedule.LeagueTables.Add(leagueTable);
        }