public void GenerateNextRounds(int teamCount, int round, int tournamentId, int?passedTeamCount) { try { if (teamCount == 1) { SaveEntitiesToDb(); return; } if (!MathManager.isPowerOfTwo(Teams.Count) && round == 2) { //In this case, teamCount parameter represents passed team count at 1 st round var teamListOn2ndRound = Teams.Where(I => I != null).ToList(); int firstRoundGameCount = gamesToBePlayed.Count; int counter = 0; for (int i = 0; i < firstRoundGameCount; i++) { PlayedGamesEntity game = new PlayedGamesEntity(); game.RoundMatchId = counter; game.HomeTeamId = GetPassedTeamId(gamesToBePlayed[i]) ?? null; game.AwayTeamId = GetPassedTeamId(gamesToBePlayed[i + 1]) ?? null; game.RoundNumber = round; gamesToBePlayed.Add(game); counter++; i++; } } else { for (int i = 0; i < teamCount / 2; i++) { PlayedGamesEntity game = new PlayedGamesEntity { RoundMatchId = i, RoundNumber = round }; gamesToBePlayed.Add(game); } } TournamentBracketRoundEntity _round = new TournamentBracketRoundEntity { RoundNumber = round, TournamentId = tournamentId }; rounds.Add(_round); GenerateNextRounds(teamCount / 2, round + 1, tournamentId, null); } catch (Exception e) { throw new Exception(e.Message); } }
public void GenerateFixture() { try { //Select first N Teams for first round if (!MathManager.isPowerOfTwo(Teams.Count)) { int playerCount = Teams.Count; int smallestPowerOf2 = MathManager.FindSmallestPowerOf2AsLargeAsParam(playerCount); int firstRoundPlayerCount = (2 * playerCount) - smallestPowerOf2; GenerateFirstNonPowerOf2Round(firstRoundPlayerCount); return; } for (int i = 0; i < Teams.Count / 2; i++) { PlayedGamesEntity game = new PlayedGamesEntity { RoundMatchId = i, HomeTeamId = Teams[i].Id, AwayTeamId = Teams[Teams.Count - (i + 1)].Id, RoundNumber = 1 }; gamesToBePlayed.Add(game); } TournamentBracketRoundEntity round = new TournamentBracketRoundEntity { RoundNumber = 1, TournamentId = TournamentId }; rounds.Add(round); GenerateNextRounds(Teams.Count / 2, 2, TournamentId, null); } catch (Exception e) { throw new Exception(e.Message); } }