예제 #1
0
        public void ScheduleNewRound(string tournamentName)
        {
            Tournament  selectedTournament = tournamentRepository.GetTournament(tournamentName);
            List <Team> teams     = null;
            Round       thisRound = null;

            //Get teams for new round
            int numberOfRounds = selectedTournament.GetNumberOfRounds();

            //Get teams for new round
            if (numberOfRounds == 0) //SPECIAL CASE: Initialize first round. Handled by adding all teams from tournament
            {
                teams = selectedTournament.Teams();
            }
            else // We are scheduling round > 0, get winners from this round if it is finished
            {
                thisRound = selectedTournament.GetRound(numberOfRounds);
                if (thisRound.IsRoundFinished())
                {
                    teams = thisRound.WinningTeams;
                    if (thisRound.FreeRider != null)
                    {
                        teams.Add(thisRound.FreeRider);
                    }
                }
                else
                {
                    throw new Exception("Round is not finished");
                }
            }

            if (teams.Count < 2)
            {
                selectedTournament.Status = Tournament.State.FINISHED;
            }
            else
            {
                Round newRound = new Round();

                //Manage freeRiders
                if ((teams.Count % 2) != 0) //Select a freeRider
                {
                    //If prior round exists and it had a freeRider and if was first team the select second team
                    if ((numberOfRounds > 0) && (thisRound.FreeRider != null) && (thisRound.FreeRider.Equals(teams[0])))
                    {
                        newRound.FreeRider = teams[1];
                        teams.Remove(teams[1]);
                    }
                    else //select first team
                    {
                        newRound.FreeRider = teams[0];
                        teams.Remove(teams[0]);
                    }
                }

                List <int> randomIndices = GetRandomIndices(teams.Count);
                int        noOfMatches   = teams.Count / 2;
                for (int i = 0; i < noOfMatches; i++)
                {
                    Match newMatch = new Match();
                    newMatch.FirstOpponent  = teams[randomIndices[2 * i]];
                    newMatch.SecondOpponent = teams[randomIndices[2 * i + 1]];
                    newRound.AddMatch(newMatch);
                }
                selectedTournament.AddRound(newRound);
                ShowRound(tournamentName, newRound, numberOfRounds);
            }
        }