private static void GenerateLeagueMatches(ref Fixture fixture, List<Team> teams, bool isTwoWays, Group group = null) { //int maxMatchOrder = fixture.Matches.Count; int maxNumber = CalculateMaxNumberOfLeagueMatches(teams.Count); int bucket = isTwoWays ? maxNumber : maxNumber / 2; Random r = new Random(); while(bucket!=0) { int i = r.Next(teams.Count); int j = r.Next(teams.Count); if(i!=j) { Team teamA = teams[i]; Team teamB = teams[j]; if(isTwoWays) { if(!fixture.AnyHomeMatch(teamA,teamB)) { fixture.AddMatch(teamA, teamB, group); bucket--; } } else { if(!fixture.AnyMatch(teamA, teamB)) { fixture.AddMatch(teamA, teamB, group); bucket--; } } } } }
internal void AddMatch(Team home, Team away, Group group) { int order = Matches.Count; var newMatch = new Match { HomeTeam = home, AwayTeam = away, Group = group, Order = order + 1 }; _matches.Add(newMatch); }
private static void BuildTournamentMatches(ref Fixture fixture) { var groupQuantity = fixture.Teams.Count / fixture.GroupSize; #region Teams Draw // draw phase List<Group> groups = new List<Group>(); List<Team> notDrawTeams = new List<Team>(fixture.Teams); Random ra = new Random(); for (int g = 0; g < groupQuantity; g++) { string groupName = string.Format("Group #{0}", g + 1); Group gr = new Group { Id = g + 1, Name = groupName }; for (int i = 0; i < fixture.GroupSize; i++) { // loop until reach a team that is not drawn yet Team team = fixture.Teams[ra.Next(fixture.Teams.Count)]; while (!notDrawTeams.Contains(team)) { team = fixture.Teams[ra.Next(fixture.Teams.Count)]; } // Now configure the group gr.Teams.Add(team); gr.Scores.Add(team, 0); notDrawTeams.Remove(team); } GenerateLeagueMatches(ref fixture, gr.Teams, false, gr); groups.Add(gr); } fixture._groups = groups; #endregion #region 1st Round Cup Fixture int order = fixture.Matches.Count; int ficticiousId = int.MaxValue - 1; foreach (Group group in fixture.Groups) { // Skip even if (group.Id % 2 == 0) continue; string teamNameA = string.Format("1st Group #{0}", group.Id); string teamNameB = string.Format("2nd Group #{0}", group.Id + 1); var teamA = new Team(ficticiousId, teamNameA); ficticiousId--; var teamB = new Team(ficticiousId, teamNameB); ficticiousId--; var newMatch = new Match { HomeTeam = teamA, AwayTeam = teamB, Order = order + 1, Result = ResultType.Scheduled }; fixture.AddMatch(newMatch); order++; } foreach (Group group in fixture.Groups) { // Skip uneven if (group.Id % 2 == 1) continue; string teamNameA = string.Format("1st Group #{0}", group.Id); string teamNameB = string.Format("2nd Group #{0}", group.Id - 1); var teamA = new Team(ficticiousId, teamNameA); ficticiousId--; var teamB = new Team(ficticiousId, teamNameB); ficticiousId--; var newMatch = new Match { HomeTeam = teamA, AwayTeam = teamB, Order = order + 1, Result = ResultType.Scheduled }; fixture.AddMatch(newMatch); order++; } #endregion #region Eliminatories // Final eliminatories matches var maxGroupNum = fixture.Groups.Count * 2; while (maxGroupNum > 4) { for (int i = 0; i < maxGroupNum; i = i + 2) { int roundMatch1 = order - maxGroupNum - i; int roundMatch2 = order - maxGroupNum - i + 1; var teamA = new Team(ficticiousId, string.Format("Winner Match #{0}", roundMatch1)); ficticiousId--; var teamB = new Team(ficticiousId, string.Format("Winner Match #{0}", roundMatch2)); ficticiousId--; var newMatch = new Match { HomeTeam = teamA, AwayTeam = teamB, Order = order, Result = ResultType.Scheduled }; fixture.AddMatch(newMatch); order++; } maxGroupNum = maxGroupNum / 2; } #endregion #region Final Matches // Firts 4 Teams position matches int finalsMatch1 = order - 1; int finalsMatch2 = order; for (int l = 0; l < 4; l = l + 2) { var teamA = new Team(ficticiousId, string.Format("Semifinal {1} Match #{0}", finalsMatch1, l < 2 ? "Winner" : "Loser")); ficticiousId--; var teamB = new Team(ficticiousId, string.Format("Semifinal {1} Match #{0}", finalsMatch2, l < 2 ? "Winner" : "Loser")); ficticiousId--; order++; var newMatch = new Match { HomeTeam = teamA, AwayTeam = teamB, Order = order, Result = ResultType.Scheduled }; fixture.AddMatch(newMatch); } #endregion }