public void BoilOffThreeTeamsIsOneRound()
        {
            IPairingsGenerator bopg = new BoilOffPairingsGenerator();

            List<TournamentTeam> teams = new List<TournamentTeam>(CreateTeams(3));
            List<TournamentRound> rounds = new List<TournamentRound>();

            try
            {
                bopg.LoadState(teams, rounds);
                TournamentRound round = bopg.CreateNextRound(null);
                foreach (var pairing in round.Pairings)
                {
                    foreach (var teamScore in pairing.TeamScores)
                    {
                        teamScore.Score = new HighestPointsScore(r.Next(20));
                    }
                }
                rounds.Add(round);

                bopg.LoadState(teams, rounds);
                round = bopg.CreateNextRound(null);
                Assert.AreEqual(null, round);
            }
            catch (ArgumentNullException)
            {
            }
        }
        public void BoilOffManyTeams()
        {
            IPairingsGenerator bopg = new BoilOffPairingsGenerator();

            List<TournamentTeam> teams = new List<TournamentTeam>(CreateTeams(9));
            List<TournamentRound> rounds = new List<TournamentRound>();

            try
            {
                bopg.LoadState(teams, rounds);
                DisplayTournamentRound(bopg.CreateNextRound(null));
            }
            catch (ArgumentNullException)
            {
            }
        }
        public void BoilOffHandlesManyCompetitorsWell()
        {
            IPairingsGenerator pg = new BoilOffPairingsGenerator();

            List<TournamentTeam> teams = new List<TournamentTeam>(CreateTeams(50));
            List<TournamentRound> rounds = new List<TournamentRound>();

            try
            {
                RunTournament(pg, teams, rounds, true, null);

                DisplayTournamentRounds(rounds);
                DisplayTournamentRankings(pg.GenerateRankings());
            }
            catch (InvalidTournamentStateException)
            {
                Assert.Fail();
            }
        }
        private static Tournament DisplayTournament(GameTournament tourny)
        {
            try
            {
                var memId = RDN.Library.Classes.Account.User.GetMemberId();
                Tournament to = new Tournament();
                to.TournamentClass = (TournamentClassEnum)tourny.TournamentClass;
                to.TournamentType = (TournamentTypeEnum)tourny.TournamentTypeEnum;
                to.EndDate = tourny.EndDate;
                if (tourny.SelectedShop != null)
                    to.SelectedShop = tourny.SelectedShop.MerchantId.ToString();
                to.EndDateDisplay = tourny.EndDate.ToShortDateString();
                to.StartDateDisplay = tourny.StartDate.ToShortDateString();
                to.StartDate = tourny.StartDate;
                to.Id = tourny.TournamentId;
                to.IsPublished = tourny.IsPublished;
                to.AreBracketsPublished = tourny.AreBracketsPublished;
                to.PrivateKey = tourny.PrivateTournamentId;
                to.Name = tourny.TournamentName;
                to.PassCodeForGames = tourny.TournamentPasscode;
                to.EmbedVideoString = tourny.EmbedVideoString;
                to.TournamentWebsite = tourny.TournamentWebsite;

                if (tourny.OwnerOfTournament != null)
                {
                    MemberDisplayBasic m = new MemberDisplayBasic();
                    m.DerbyName = tourny.OwnerOfTournament.DerbyName;
                    m.MemberId = tourny.OwnerOfTournament.MemberId;
                    m.UserId = tourny.OwnerOfTournament.AspNetUserId;
                    to.Owners.Add(m);
                }

                for (int i = 0; i < tourny.OwnersOfTournament.Count; i++)
                {
                    try
                    {
                        if (to.Owners.Where(x => x.MemberId == tourny.OwnersOfTournament[i].Owner.MemberId).FirstOrDefault() == null)
                        {
                            MemberDisplayBasic m = new MemberDisplayBasic();
                            m.DerbyName = tourny.OwnersOfTournament[i].Owner.DerbyName;
                            m.MemberId = tourny.OwnersOfTournament[i].Owner.MemberId;
                            m.UserId = tourny.OwnersOfTournament[i].Owner.AspNetUserId;
                            to.Owners.Add(m);
                        }
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                }


                if (tourny.Logo != null)
                    to.Photos.Add(new PhotoItem(tourny.Logo.ImageUrl, tourny.Logo.ImageUrlThumb, true, tourny.TournamentName));
                #region contact
                if (tourny.ContactCard != null)
                {
                    var add = tourny.ContactCard.Addresses.FirstOrDefault();
                    if (add != null)
                    {
                        to.Address = add.Address1;
                        to.Address2 = add.Address2;
                        to.City = add.CityRaw;
                        if (add.Country != null)
                        {
                            to.Country = add.Country.Code;
                            to.CountryId = add.Country.CountryId;
                        }
                        to.State = add.StateRaw;
                        to.ZipCode = add.Zip;
                    }
                }

                #endregion
                #region paywall
                if (tourny.Paywall != null)
                {
                    to.Paywall.DailyPrice = tourny.Paywall.DailyPrice;
                    to.Paywall.DescriptionOfPaywall = tourny.Paywall.DescriptionOfPaywall;
                    to.Paywall.EndDate = tourny.Paywall.EndDate;
                    if (tourny.Paywall.EndDate.HasValue)
                        to.Paywall.EndDateDisplay = tourny.Paywall.EndDate.Value.ToShortDateString();
                    to.Paywall.IsEnabled = tourny.Paywall.IsEnabled;
                    to.Paywall.IsRemoved = tourny.Paywall.IsRemoved;
                    to.Paywall.PaywallId = tourny.Paywall.PaywallId;
                    to.Paywall.StartDate = tourny.Paywall.StartDate;
                    if (tourny.Paywall.StartDate.HasValue)
                        to.Paywall.StartDateDisplay = tourny.Paywall.StartDate.Value.ToShortDateString();
                    to.Paywall.TimespanPrice = tourny.Paywall.TimespanPrice;
                    to.PaywallId = tourny.Paywall.PaywallId;
                    to.Paywall.AcceptPaypal = tourny.Paywall.Merchant.AcceptPaymentsViaPaypal;
                    to.Paywall.AcceptStripe = tourny.Paywall.Merchant.AcceptPaymentsViaStripe;
                    if (memId != new Guid())
                    {
                        var isPaid = tourny.Paywall.PaywallInvoices.Where(x => x.MemberPaidId == memId).OrderByDescending(x => x.Created).FirstOrDefault();
                        if (isPaid != null)
                        {
                            if (isPaid.ValidUntil > DateTime.UtcNow && (isPaid.Invoice.InvoiceStatus == (byte)InvoiceStatus.Payment_Successful || isPaid.Invoice.InvoiceStatus == (byte)InvoiceStatus.Pending_Payment_From_Paypal))
                            {
                                to.Paywall.IsPaid = true;
                            }
                        }
                        else
                            to.Paywall.IsPaid = false;
                    }
                    to.Paywall.MerchantId = tourny.Paywall.Merchant.MerchantId;
                }

                #endregion
                #region Games
                List<Game> games = new List<Game>();
                foreach (var game in tourny.Games)
                {
                    try
                    {
                        Game g = new Game();
                        g.GameId = game.GameId;
                        g.GameName = game.GameName;
                        g.GameDate = game.GameDate;
                        var teams = game.GameTeams.OrderByDescending(x => x.Created).Take(2);
                        var team1 = teams.OrderByDescending(x => x.TeamName).FirstOrDefault();
                        var team2 = teams.OrderBy(x => x.TeamName).FirstOrDefault();
                        g.Team1Name = team1.TeamName;
                        g.Team1LinkId = team1.TeamIdLink;
                        g.Team1Id = team1.TeamId;
                        g.Team2Name = team2.TeamName;
                        g.Team2LinkId = team2.TeamIdLink;
                        g.Team2Id = team2.TeamId;
                        g.Team1ScoreTotal = team1.GameScores.Select(x => x.Point).Sum();
                        g.Team2ScoreTotal = team2.GameScores.Select(x => x.Point).Sum();

                        foreach (var b in team1.GameMembers)
                        {
                            TeamMembersViewModel a = new TeamMembersViewModel();
                            a.SkaterId = b.GameMemberId;
                            a.SkaterLinkId = b.MemberLinkId;
                            a.SkaterName = b.MemberName;
                            a.SkaterNumber = b.MemberNumber;
                            if (to.AllSkaters.Where(x => x.SkaterLinkId == a.SkaterLinkId).FirstOrDefault() == null)
                                to.AllSkaters.Add(a);
                        }
                        foreach (var b in team2.GameMembers)
                        {
                            TeamMembersViewModel a = new TeamMembersViewModel();
                            a.SkaterId = b.GameMemberId;
                            a.SkaterLinkId = b.MemberLinkId;
                            a.SkaterName = b.MemberName;
                            a.SkaterNumber = b.MemberNumber;
                            if (to.AllSkaters.Where(x => x.SkaterLinkId == a.SkaterLinkId).FirstOrDefault() == null)
                                to.AllSkaters.Add(a);

                        }

                        foreach (var assist in game.GameMemberAssists)
                        {
                            AssistViewModel a = new AssistViewModel();
                            a.AssistId = assist.GameAssistId;
                            a.PlayerWhoAssisted = new TeamMembersViewModel();
                            a.PlayerWhoAssisted.SkaterId = assist.MemberWhoAssisted.GameMemberId;
                            a.PlayerWhoAssisted.SkaterName = assist.MemberWhoAssisted.MemberName;
                            a.PlayerWhoAssisted.SkaterLinkId = assist.MemberWhoAssisted.MemberLinkId;
                            to.AssistLeaders.Add(a);
                        }
                        foreach (var pen in game.GameMemberPenalties)
                        {
                            PenaltyViewModel a = new PenaltyViewModel();
                            a.PenaltyId = pen.GamePenaltyId;
                            a.PenaltyAgainstMember = new TeamMembersViewModel();
                            a.PenaltyAgainstMember.SkaterId = pen.MemberWhoPenaltied.GameMemberId;
                            a.PenaltyAgainstMember.SkaterName = pen.MemberWhoPenaltied.MemberName;
                            a.PenaltyAgainstMember.SkaterLinkId = pen.MemberWhoPenaltied.MemberLinkId;
                            a.PenaltyType = (PenaltiesEnum)Enum.Parse(typeof(PenaltiesEnum), pen.PenaltyType.ToString());
                            to.PenaltyLeaders.Add(a);
                        }
                        foreach (var blocks in game.GameMemberBlocks)
                        {
                            BlockViewModel a = new BlockViewModel();
                            a.BlockId = blocks.GameBlockId;
                            a.PlayerWhoBlocked = new TeamMembersViewModel();
                            a.PlayerWhoBlocked.SkaterId = blocks.MemberWhoBlocked.GameMemberId;
                            a.PlayerWhoBlocked.SkaterName = blocks.MemberWhoBlocked.MemberName;
                            a.PlayerWhoBlocked.SkaterLinkId = blocks.MemberWhoBlocked.MemberLinkId;
                            to.BlockLeaders.Add(a);
                        }
                        games.Add(g);
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                }

                to.Games = games;

                #endregion

                var ts = tourny.Teams.Where(x => x.IsRemoved == false);
                foreach (var team in ts)
                {
                    try
                    {
                        TeamViewModel t = new TeamViewModel();
                        t.PoolNumber = team.PoolNumber;
                        t.LeagueName = team.LeageName;
                        //t.TeamLinkId = team.TeamIdLink;
                        t.TeamName = team.TeamName;
                        t.TeamId = team.TeamId;
                        t.SeedRating = team.SeedRating;
                        to.TeamsForTournament.Add(t);
                        TournamentApi.TournamentTeam tt = new TournamentApi.TournamentTeam(team.TeamId, team.SeedRating, team.PoolNumber);
                        to.TeamsForTournamentApi.Add(tt);
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                }

                #region seededbracketsForPerformance
                try
                {
                    to.TouramentTypeForSeedingEnum = (TournamentTypeEnum)tourny.TournamentTypeEnumForSeedingRound;
                    if (to.TouramentTypeForSeedingEnum != TournamentTypeEnum.None)
                    {

                        TournamentApi.IPairingsGenerator pg = null;

                        switch (to.TouramentTypeForSeedingEnum)
                        {
                            case TournamentTypeEnum.Boil_Off:
                                pg = new BoilOffPairingsGenerator();
                                break;
                            case TournamentTypeEnum.Round_Robin:
                                pg = new RoundRobinPairingsGenerator();
                                break;
                            case TournamentTypeEnum.Double_Elimination:
                                pg = new EliminationTournament(2);
                                break;
                            case TournamentTypeEnum.Single_Elimination:
                                pg = new EliminationTournament(1);
                                break;
                            case TournamentTypeEnum.Round_Robin_Pool_Play:
                                pg = new RoundRobinPairingsGenerator(true);
                                break;
                        }
                        if (pg is TournamentApi.ITournamentVisualizer)
                        {
                            to.VisualizedBracketsSeeded = (TournamentApi.ITournamentVisualizer)pg;
                        }

                        foreach (var round in tourny.SeedingRounds)
                        {
                            try
                            {
                                TournamentRoundsClass r = new TournamentRoundsClass();
                                List<TournamentApi.TournamentPairing> ppList = new List<TournamentApi.TournamentPairing>();
                                r.RoundNumber = round.RoundNumber;
                                foreach (var pair in round.Pairings)
                                {
                                    List<TournamentApi.TournamentTeamScore> ttList = new List<TournamentApi.TournamentTeamScore>();
                                    TournamentPairingClass p = new TournamentPairingClass();
                                    p.Id = pair.PairingId;
                                    p.GroupId = pair.GroupId;
                                    p.TimeToStart = pair.StartTime.GetValueOrDefault();
                                    if (pair.StartTime.HasValue)
                                        p.TimeToStartDisplay = p.TimeToStart.ToString("HH:mm");
                                    p.TrackId = pair.TrackNumber;
                                    foreach (var team in pair.Teams)
                                    {
                                        TeamViewModel t = new TeamViewModel();
                                        t.TeamId = team.Team.TeamId;
                                        t.TeamName = team.Team.TeamName;
                                        t.TeamLinkId = team.TeamIdInGame;
                                        if (t.TeamLinkId != new Guid() && p.GameId == new Guid())
                                        {
                                            var g = to.Games.Where(x => x.Team1Id == t.TeamLinkId || x.Team2Id == t.TeamLinkId).FirstOrDefault();
                                            if (g != null)
                                                p.GameId = g.GameId;
                                        }
                                        t.Score = team.Score;
                                        p.Teams.Add(t);

                                        TournamentApi.TournamentTeamScore tt = new TournamentApi.TournamentTeamScore(new TournamentApi.TournamentTeam(t.TeamId, team.Team.SeedRating), new TournamentApi.HighestPointsScore(team.Score));
                                        ttList.Add(tt);

                                    }
                                    TournamentApi.TournamentPairing pp = new TournamentApi.TournamentPairing(ttList);
                                    pp.GroupId = p.GroupId;
                                    ppList.Add(pp);
                                    r.Pairings.Add(p);
                                }

                                TournamentApi.TournamentRound rr = new TournamentApi.TournamentRound(ppList);
                                to.TournamentRoundsApiForSeeding.Add(rr);
                                to.TournamentRoundsForSeedingGameplay.Add(r);
                            }
                            catch (Exception exception)
                            {
                                ErrorDatabaseManager.AddException(exception, exception.GetType());
                            }
                        }

                        if (pg != null)
                        {
                            pg.LoadState(to.TeamsForTournamentApi, to.TournamentRoundsApiForSeeding);
                            try
                            {
                                var nextRound = pg.CreateNextRound(null);

                                to.HasSeedingFinishForTournament = (nextRound == null) && (tourny.SeedingRounds.Count() > 1);

                                if (to.TournamentRoundsApiForSeeding.Any())
                                {
                                    var rankings = pg.GenerateRankings();

                                    to.RankingsForSeededRounds = new List<TournamentApi.TournamentRanking>(from rk in rankings
                                                                                                           select new TournamentApi.TournamentRanking
                                                                                                           {
                                                                                                               TeamName = to.TeamsForTournament.Where(tm => tm.TeamId == rk.team.TeamId).Single().TeamName,
                                                                                                               rank = rk.rank,
                                                                                                               Loses = rk.Loses,
                                                                                                               PointSpread = rk.PointSpread,
                                                                                                               scoreDescription = rk.scoreDescription,
                                                                                                               team = rk.team,
                                                                                                               TotalPoints = rk.TotalPoints,
                                                                                                               Wins = rk.Wins
                                                                                                           });
                                }

                            }
                            catch (Exception exception)
                            {
                                ErrorDatabaseManager.AddException(exception, exception.GetType());
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    ErrorDatabaseManager.AddException(exception, exception.GetType());
                }

                #endregion


                #region brackets
                try
                {
                    to.TournamentType = (TournamentTypeEnum)tourny.TournamentTypeEnum;
                    if (to.TournamentType != TournamentTypeEnum.None)
                    {
                        if (to.TouramentTypeForSeedingEnum != TournamentTypeEnum.None && to.HasSeedingFinishForTournament)
                        {
                            foreach (var ranking in to.RankingsForSeededRounds)
                            {
                                var team = to.TeamsForTournamentApi.Where(x => x.TeamId == ranking.team.TeamId).FirstOrDefault();
                                team.Rating = (int)ranking.rank;
                            }
                        }


                        TournamentApi.IPairingsGenerator pg = null;

                        switch (to.TournamentType)
                        {
                            case TournamentTypeEnum.Boil_Off:
                                pg = new BoilOffPairingsGenerator();
                                break;
                            case TournamentTypeEnum.Round_Robin:
                                pg = new RoundRobinPairingsGenerator();
                                break;
                            case TournamentTypeEnum.Round_Robin_Pool_Play:
                                pg = new RoundRobinPairingsGenerator(true);
                                break;
                            case TournamentTypeEnum.Double_Elimination:
                                pg = new EliminationTournament(2);
                                break;
                            case TournamentTypeEnum.Single_Elimination:
                                pg = new EliminationTournament(1);
                                break;
                        }
                        try
                        {
                            if (pg is TournamentApi.ITournamentVisualizer)
                            {
                                to.VisualizedBrackets = (TournamentApi.ITournamentVisualizer)pg;
                            }
                        }
                        catch //(TournamentApi.InvalidTournamentStateException ex)
                        {
                            //Debug.WriteLine(ex);
                        }

                        foreach (var round in tourny.Rounds)
                        {
                            try
                            {
                                TournamentRoundsClass r = new TournamentRoundsClass();
                                List<TournamentApi.TournamentPairing> ppList = new List<TournamentApi.TournamentPairing>();
                                r.RoundNumber = round.RoundNumber;
                                foreach (var pair in round.Pairings)
                                {
                                    List<TournamentApi.TournamentTeamScore> ttList = new List<TournamentApi.TournamentTeamScore>();
                                    TournamentPairingClass p = new TournamentPairingClass();
                                    p.Id = pair.PairingId;
                                    p.GroupId = pair.GroupId;
                                    p.TimeToStart = pair.StartTime.GetValueOrDefault();
                                    if (pair.StartTime.HasValue)
                                        p.TimeToStartDisplay = p.TimeToStart.ToString("HH:mm");
                                    p.TrackId = pair.TrackNumber;
                                    foreach (var team in pair.Teams)
                                    {
                                        TeamViewModel t = new TeamViewModel();
                                        t.TeamId = team.Team.TeamId;
                                        t.TeamName = team.Team.TeamName;
                                        t.TeamLinkId = team.TeamIdInGame;
                                        if (t.TeamLinkId != new Guid() && p.GameId == new Guid())
                                        {
                                            var g = to.Games.Where(x => x.Team1Id == t.TeamLinkId || x.Team2Id == t.TeamLinkId).FirstOrDefault();
                                            if (g != null)
                                                p.GameId = g.GameId;
                                        }
                                        t.Score = team.Score;
                                        p.Teams.Add(t);

                                        TournamentApi.TournamentTeamScore tt = new TournamentApi.TournamentTeamScore(new TournamentApi.TournamentTeam(t.TeamId, team.Team.SeedRating), new TournamentApi.HighestPointsScore(team.Score));
                                        ttList.Add(tt);

                                    }
                                    TournamentApi.TournamentPairing pp = new TournamentApi.TournamentPairing(ttList);
                                    pp.GroupId = p.GroupId;
                                    ppList.Add(pp);
                                    r.Pairings.Add(p);
                                }

                                TournamentApi.TournamentRound rr = new TournamentApi.TournamentRound(ppList);
                                to.TournamentRoundsApi.Add(rr);
                                to.TournamentRounds.Add(r);
                            }
                            catch (Exception exception)
                            {
                                ErrorDatabaseManager.AddException(exception, exception.GetType());
                            }
                        }

                        if (pg != null)
                        {
                            pg.LoadState(to.TeamsForTournamentApi, to.TournamentRoundsApi);
                            try
                            {
                                var nextRound = pg.CreateNextRound(null);

                                to.IsTournamentFinished = (nextRound == null) && (tourny.Rounds.Count() > 1);

                                if (to.TournamentRoundsApi.Any())
                                {
                                    var rankings = pg.GenerateRankings();

                                    to.Rankings = new List<TournamentApi.TournamentRanking>(from rk in rankings
                                                                                            select new TournamentApi.TournamentRanking
                                                                                         {
                                                                                             TeamName = to.TeamsForTournament.Where(tm => tm.TeamId == rk.team.TeamId).Single().TeamName,
                                                                                             rank = rk.rank,
                                                                                             Loses = rk.Loses,
                                                                                             PointSpread = rk.PointSpread,
                                                                                             scoreDescription = rk.scoreDescription,
                                                                                             team = rk.team,
                                                                                             TotalPoints = rk.TotalPoints,
                                                                                             Wins = rk.Wins
                                                                                         });
                                }

                            }
                            catch (TournamentApi.InvalidTournamentStateException)
                            {
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    ErrorDatabaseManager.AddException(exception, exception.GetType());
                }

                #endregion
                return to;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tournamentId"></param>
        /// <param name="isPerformanceRound">if the round is for seeding, otherwise its for the actual tournament.</param>
        /// <returns></returns>
        public static bool StartNextRound(Guid tournamentId, bool isPerformanceRound)
        {
            try
            {
                var dc = new ManagementContext();
                var tourny = GetTournament(tournamentId);
                TournamentApi.IPairingsGenerator pg = null;
                TournamentTypeEnum tempType = tourny.TournamentType;
                if (isPerformanceRound)
                    tempType = tourny.TouramentTypeForSeedingEnum;
                switch (tempType)
                {
                    case TournamentTypeEnum.Boil_Off:
                        pg = new BoilOffPairingsGenerator();
                        break;
                    case TournamentTypeEnum.Round_Robin:
                        pg = new RoundRobinPairingsGenerator();
                        break;
                    case TournamentTypeEnum.Round_Robin_Pool_Play:
                        pg = new RoundRobinPairingsGenerator(true);
                        break;
                    case TournamentTypeEnum.Double_Elimination:
                        pg = new EliminationTournament(2);
                        break;
                    case TournamentTypeEnum.Single_Elimination:
                        pg = new EliminationTournament(1);
                        break;
                }
                if (isPerformanceRound)
                    pg.LoadState(tourny.TeamsForTournamentApi, tourny.TournamentRoundsApiForSeeding);
                else
                    pg.LoadState(tourny.TeamsForTournamentApi, tourny.TournamentRoundsApi);

                var tempRound = pg.CreateNextRound(null);

                bool tournamentFinished = (tempRound == null) && (tourny.TournamentRoundsApi.Count > 1);
                if (tournamentFinished)
                    return true;
                var tournament = dc.GameTournaments.Where(x => x.TournamentId == tourny.Id).FirstOrDefault();
                TournamentRound newRound = new TournamentRound();
                if (isPerformanceRound)
                    newRound.RoundNumber = tourny.TournamentRoundsApiForSeeding.Count + 1;
                else
                    newRound.RoundNumber = tourny.TournamentRoundsApi.Count + 1;
                newRound.Tournament = tournament;
                var teams = tournament.Teams.ToList();
                foreach (var pairing in tempRound.Pairings)
                {
                    var newPairing = new TournamentPairing
                    {
                        Round = newRound,
                        GroupId = pairing.GroupId
                    };

                    for (int i = 0; i < pairing.TeamScores.Count; i++)
                    {
                        var newTeamPairing = new TournamentPairingTeam
                        {
                            Team = teams.Where(t => t.TeamId == pairing.TeamScores[i].Team.TeamId).FirstOrDefault(),
                            Pairing = newPairing,
                            Score = 0
                        };
                        newPairing.Teams.Add(newTeamPairing);
                        //dc.TournamentTeams.Add(newTeamPairing);
                    }
                    newRound.Pairings.Add(newPairing);
                }
                //}

                if (isPerformanceRound)
                    tournament.SeedingRounds.Add(newRound);
                else
                    tournament.Rounds.Add(newRound);

                int c = dc.SaveChanges();
                return c > 0;


            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }
 public void BoilOffThrowsExceptionOnNullParameters()
 {
     IPairingsGenerator bopg = new BoilOffPairingsGenerator();
     IPairingsGeneratorThrowsExceptionOnNullParameters(bopg);
 }
        public void BoilOffThrowsExceptionOnUnfinishedRound()
        {
            IPairingsGenerator bopg = new BoilOffPairingsGenerator();
            List<TournamentTeam> teams = new List<TournamentTeam>(CreateTeams(9));
            List<TournamentRound> rounds = new List<TournamentRound>();
            bopg.LoadState(teams, rounds);
            var round = bopg.CreateNextRound(null);
            rounds.Add(round);

            round.Pairings[0].TeamScores[0].Score = new HighestPointsScore(1.0);

            bopg.LoadState(teams, rounds);

            try
            {
                round = bopg.CreateNextRound(null);
                Assert.Fail();
            }
            catch (InvalidTournamentStateException)
            {
                return;
            }
        }