Example #1
0
 internal static void PersistFixture(Fixture fixture)
 {
     if(fixture.Id == 0) {
         fixture.Id = _fixtures.Count + 1;
         _fixtures.Add(fixture.Id, fixture);
     } else {
         _fixtures[fixture.Id] = fixture;
     }
 }
Example #2
0
        public static void Initialize()
        {
            if(_fixtures.Count > 0) return;

            var fixture = new Fixture(1, "Demo Fixture");
            fixture.Mode = MatchMode.LeagueOneWay;
            fixture.AddTeam("Team A");
            fixture.AddTeam("Team C");
            fixture.AddTeam("Team D");
            fixture.AddTeam("Team B");

            fixture.BuildMatches();

            _fixtures.Add(1, fixture);
        }
Example #3
0
 /// <summary>
 /// Create or Updates the fixture passed in
 /// </summary>
 /// <param name="fixture">the affected fixture</param>
 /// <returns></returns>
 public IHttpActionResult PostFixture(Fixture fixture)
 {
     Database.PersistFixture(fixture);
     return Ok();
 }
Example #4
0
 private void CheckValidations(Fixture fixture)
 {
     if (fixture.Mode == MatchMode.Tournament)
     {
         if (fixture.Teams.Count < 4)
         {
             throw new Exception("Insuficient Numbers of Teams");
         } else if (fixture.Teams.Count < fixture.GroupSize)
         {
             throw new Exception("Insuficient Numbers of Teams according GroupSize");
         }
     }
 }
Example #5
0
 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--;
                 }
             }
         }
     }
 }
Example #6
0
        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
        }