Ejemplo n.º 1
0
        public static void CreateCupRoundGames(List <TeamModel> roundParticipants, RoundModel round)
        {
            int numberOfGames          = roundParticipants.Count / 2;
            List <TeamModel> homeTeams = new List <TeamModel>(roundParticipants);
            List <TeamModel> awayTeams = new List <TeamModel>();

            if (homeTeams.Contains(homeTeams.Find(team => team.TeamName == "Dummy Team")))
            {
                //  Transfer all dummy teams to awayList
                foreach (TeamModel team in roundParticipants)
                {
                    if (team.TeamName == "Dummy Team")
                    {
                        awayTeams.Add(team);
                        homeTeams.Remove(team);
                    }
                }



                //  Separate teams in 2 lists
                while (awayTeams.Count != homeTeams.Count)
                {
                    awayTeams.Add(homeTeams[0]);
                    homeTeams.Remove(homeTeams[0]);
                }



                //  Pair teams from 2 lists into game
                for (int gameNumber = 1; gameNumber <= numberOfGames; gameNumber++)
                {
                    GameModel game = new GameModel(round.TournamentId, round.Id, true);
                    SqlDataHandler.CreateGame(game);

                    game.Competitors.Add
                        (new GameParticipantModel {
                        TournamentId = round.TournamentId, RoundId = round.Id, GameId = game.Id, TeamName = homeTeams[0].TeamName
                    });
                    game.Competitors.Add
                        (new GameParticipantModel {
                        TournamentId = round.TournamentId, RoundId = round.Id, GameId = game.Id, TeamName = awayTeams[0].TeamName
                    });

                    homeTeams.RemoveAt(0);
                    awayTeams.RemoveAt(0);

                    foreach (GameParticipantModel team in game.Competitors)
                    {
                        SqlDataHandler.CreateGameParticipant(team);
                    }
                }
            }
            else
            {
                //  Separate teams in 2 lists
                while (awayTeams.Count != homeTeams.Count)
                {
                    awayTeams.Add(homeTeams[0]);
                    homeTeams.Remove(homeTeams[0]);
                }



                //  Pair teams from 2 lists into game
                for (int gameNumber = 1; gameNumber <= numberOfGames; gameNumber++)
                {
                    GameModel game = new GameModel(round.TournamentId, round.Id, true);
                    SqlDataHandler.CreateGame(game);

                    game.Competitors.Add
                        (new GameParticipantModel {
                        TournamentId = round.TournamentId, RoundId = round.Id, GameId = game.Id, TeamName = homeTeams[0].TeamName
                    });
                    game.Competitors.Add
                        (new GameParticipantModel {
                        TournamentId = round.TournamentId, RoundId = round.Id, GameId = game.Id, TeamName = awayTeams[0].TeamName
                    });

                    homeTeams.RemoveAt(0);
                    awayTeams.RemoveAt(0);

                    foreach (GameParticipantModel team in game.Competitors)
                    {
                        SqlDataHandler.CreateGameParticipant(team);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static void CreateRounds(TournamentModel tournament)
        {
            List <TeamModel> participants = tournament.ParticipatingTeams;

            participants.Shuffle();

            if (tournament.IsLeague)
            {
                //  SOLUTION FOUND ON https://stackoverflow.com/questions/1293058/round-robin-tournament-algorithm-in-c-sharp

                int numberOfRounds = GetNumberOfRounds(tournament);
                int halfOfTeams    = tournament.ParticipatingTeams.Count / 2;

                List <TeamModel> shortenedTeamList = new List <TeamModel>();
                shortenedTeamList.AddRange(participants);
                shortenedTeamList.RemoveAt(0);

                int shortenedCount = shortenedTeamList.Count;



                //  Create rounds
                for (int roundNumber = 1; roundNumber <= numberOfRounds; roundNumber++)
                {
                    //  Create round
                    RoundModel round = new RoundModel(tournament.Id, roundNumber);
                    SqlDataHandler.CreateRound(tournament, round);
                    tournament.Rounds.Add(round);

                    int teamIndexer = roundNumber % shortenedCount;



                    //  Create 1st game in the round
                    GameModel game = new GameModel(tournament.Id, round.Id, true);
                    SqlDataHandler.CreateGame(game);
                    round.Games.Add(game);



                    //  Create 1st game participants
                    GameParticipantModel homeTeam = new GameParticipantModel
                    {
                        TournamentId = tournament.Id, RoundId = round.Id, GameId = game.Id, TeamName = shortenedTeamList[teamIndexer].TeamName
                    };
                    GameParticipantModel awayTeam = new GameParticipantModel
                    {
                        TournamentId = tournament.Id, RoundId = round.Id, GameId = game.Id, TeamName = participants[0].TeamName
                    };

                    game.Competitors.Add(homeTeam);
                    game.Competitors.Add(awayTeam);

                    foreach (GameParticipantModel participant in game.Competitors)
                    {
                        SqlDataHandler.CreateGameParticipant(participant);
                    }



                    //  Create other games of the round
                    for (int index = 1; index < halfOfTeams; index++)
                    {
                        int homeIndex = (roundNumber + index) % shortenedCount;
                        int awayIndex = (roundNumber + shortenedCount - index) % shortenedCount;



                        //  Create the next game
                        GameModel nextGame = new GameModel(tournament.Id, round.Id, true);
                        SqlDataHandler.CreateGame(nextGame);
                        round.Games.Add(nextGame);



                        //  Create next game participants
                        GameParticipantModel nextHomeTeam = new GameParticipantModel
                        {
                            TournamentId = tournament.Id, RoundId = round.Id, GameId = nextGame.Id, TeamName = shortenedTeamList[homeIndex].TeamName
                        };
                        GameParticipantModel nextAwayTeam = new GameParticipantModel
                        {
                            TournamentId = tournament.Id, RoundId = round.Id, GameId = nextGame.Id, TeamName = shortenedTeamList[awayIndex].TeamName
                        };

                        nextGame.Competitors.Add(nextHomeTeam);
                        nextGame.Competitors.Add(nextAwayTeam);

                        foreach (GameParticipantModel participant in nextGame.Competitors)
                        {
                            SqlDataHandler.CreateGameParticipant(participant);
                        }
                    }
                }



                if (tournament.HomeAndAway)
                {
                    int numberToAdd = 1;

                    //  Create the duplicate rounds and duplicate of every game in the round with teams on opposite places
                    foreach (RoundModel createdRound in tournament.Rounds)
                    {
                        //  Create the 1st next duplicate round
                        RoundModel nextRound = new RoundModel(tournament.Id, tournament.Rounds.Count + numberToAdd);
                        SqlDataHandler.CreateRound(tournament, nextRound);
                        numberToAdd += 1;



                        foreach (GameModel createdGame in createdRound.Games)
                        {
                            //  Create duplicate game
                            GameModel nextGame = new GameModel(tournament.Id, nextRound.Id, true);
                            SqlDataHandler.CreateGame(nextGame);



                            //  Create game participants
                            GameParticipantModel homeTeam = new GameParticipantModel
                            {
                                TournamentId = tournament.Id, RoundId = nextRound.Id, GameId = nextGame.Id, TeamName = createdGame.Competitors[1].TeamName
                            };
                            GameParticipantModel awayTeam = new GameParticipantModel
                            {
                                TournamentId = tournament.Id, RoundId = nextRound.Id, GameId = nextGame.Id, TeamName = createdGame.Competitors[0].TeamName
                            };

                            nextGame.Competitors.Add(homeTeam);
                            nextGame.Competitors.Add(awayTeam);

                            foreach (GameParticipantModel participant in nextGame.Competitors)
                            {
                                SqlDataHandler.CreateGameParticipant(participant);
                            }
                        }
                    }
                }
            }
            else
            {
                int numberOfRounds = GetNumberOfRounds(tournament);

                for (int roundNumber = 1; roundNumber <= numberOfRounds; roundNumber++)
                {
                    RoundModel round = new RoundModel(tournament.Id, roundNumber);
                    SqlDataHandler.CreateRound(tournament, round);

                    if (round.RoundNumber == 1)
                    {
                        CreateCupRoundGames(participants, round);
                    }

                    //  Every next round's games will be created subsequently after all previous round's results are stored
                    //  Its only needed to get previous round winners and pass them as parameter to the CreateCupRoundGames() method
                }
            }
        }