private void CreateNextStepBrackets(Group parentGroup, Group childGroup) { var parentBracketGroups = parentGroup.PlayoffBrackets.GroupBy(b => new { b.Stage, b.MinPos, b.MaxPos }); List <PlayoffBracket> nextBrackets = new List <PlayoffBracket>(); foreach (var brackets in parentBracketGroups) { int minPos = brackets.Key.MinPos; int maxPos = brackets.Key.MaxPos; int middelPos = ((brackets.Key.MinPos - brackets.Key.MaxPos) / 2) + brackets.Key.MaxPos; int stage = brackets.Key.Stage + 1; int startIndex = 0; int endIndex = brackets.Count() - 1; for (int i = 0; i < brackets.Count() / 2; i++) { var parent1 = brackets.ElementAt(startIndex); startIndex++; var parent2 = brackets.ElementAt(endIndex); endIndex--; PlayoffBracket winnerBracket = new PlayoffBracket(); winnerBracket.Team1Id = parent1.WinnerId; winnerBracket.Team2Id = parent2.WinnerId; winnerBracket.Stage = stage; winnerBracket.MaxPos = maxPos; winnerBracket.MinPos = middelPos; winnerBracket.Type = (int)PlayoffBracketType.Winner; winnerBracket.ParentBracket1Id = parent1.Id; winnerBracket.ParentBracket2Id = parent2.Id; winnerBracket.GroupId = childGroup.GroupId; nextBrackets.Add(winnerBracket); if (childGroup.TypeId == 2) { PlayoffBracket loserBracket = new PlayoffBracket(); loserBracket.Team1Id = parent1.LoserId; loserBracket.Team2Id = parent2.LoserId; loserBracket.Stage = brackets.Key.Stage + 1; loserBracket.MaxPos = middelPos + 1; loserBracket.MinPos = minPos; loserBracket.Type = (int)PlayoffBracketType.Loseer; loserBracket.ParentBracket1Id = parent1.Id; loserBracket.ParentBracket2Id = parent2.Id; loserBracket.GroupId = childGroup.GroupId; nextBrackets.Add(loserBracket); } } } var list = nextBrackets.OrderBy(b => b.Type).ToList(); bracketsRepo.SaveBrackets(list); }
private int CreateBracketsForEarlyStagePlayoffGroup(Group group) { group.IsAdvanced = true; List <int> groupTeamsIds = group.GroupsTeams.Where(gt => gt.Team.IsArchive == false).OrderBy(gt => gt.Pos).Select(gt => gt.TeamId).ToList(); List <int> ligitTeamCounts = new List <int> { 2, 4, 8, 16, 32, 64 }; int numberOfTeams = groupTeamsIds.Count; int ligitNumber = 0; foreach (var num in ligitTeamCounts) { if (numberOfTeams <= num) { ligitNumber = num; break; } } for (int i = numberOfTeams; i < ligitNumber; i++) { groupTeamsIds.Add(0); } List <Tuple <int, int> > combinations = CreatePlayoffCombinations(groupTeamsIds); List <PlayoffBracket> brackets = new List <PlayoffBracket>(); foreach (var comb in combinations) { PlayoffBracket b = new PlayoffBracket { Team1Id = comb.Item1, Team2Id = comb.Item2, MaxPos = 1, MinPos = combinations.Count * 2, Stage = 1, Type = (int)PlayoffBracketType.Root }; if (comb.Item2 == 0) { b.Team2Id = null; b.WinnerId = comb.Item1; } brackets.Add(b); } group.PlayoffBrackets = brackets; groupesRepo.Save(); return(ligitNumber); }
private void FindTeamsRecursively(PlayoffBracket bracket, List <Team> list, int index) { if (bracket.Type == (int)PlayoffBracketType.Root) { if (bracket.Team1Id != null) { list.Add(bracket.FirstTeam); } if (bracket.Team2Id != null) { list.Add(bracket.SecondTeam); } } else { PlayoffBracket parent; if (index == 1) { parent = bracket.Parent1; } else { parent = bracket.Parent2; } if (parent.Team1Id != null) { list.Add(parent.FirstTeam); } else { FindTeamsRecursively(parent, list, 1); } if (parent.Team2Id != null) { list.Add(parent.SecondTeam); } else { FindTeamsRecursively(parent, list, 2); } } }
internal void GameEndedEvent(GamesCycle gc) { PlayoffBracket bracket = gc.PlayoffBracket; if (bracket != null && bracket.GamesCycles.All(g => g.GameStatus == GameStatus.Ended)) { var goldenGame = bracket.GamesCycles.FirstOrDefault(g => g.GameSets.Any(s => s.IsGoldenSet == true)); if (goldenGame != null) { gameRepo.ResetGame(goldenGame, true); var goldenSet = goldenGame.GameSets.FirstOrDefault(s => s.IsGoldenSet == true); if (goldenSet.HomeTeamScore > goldenSet.GuestTeamScore) { bracket.WinnerId = goldenGame.HomeTeamId; bracket.LoserId = goldenGame.GuestTeamId; } else { bracket.WinnerId = goldenGame.GuestTeamId; bracket.LoserId = goldenGame.HomeTeamId; } } else { int t1score = bracket.GamesCycles.Where(g => g.HomeTeamId == bracket.FirstTeam?.TeamId && g.HomeTeamScore > g.GuestTeamScore) .Concat(bracket.GamesCycles.Where(g => g.GuestTeamId == bracket.FirstTeam?.TeamId && g.HomeTeamScore < g.GuestTeamScore)).Count(); int t2score = bracket.GamesCycles.Where(g => g.HomeTeamId == bracket.SecondTeam?.TeamId && g.HomeTeamScore > g.GuestTeamScore) .Concat(bracket.GamesCycles.Where(g => g.GuestTeamId == bracket.SecondTeam?.TeamId && g.HomeTeamScore < g.GuestTeamScore)).Count(); if (t1score > t2score) { bracket.WinnerId = bracket.FirstTeam.TeamId; bracket.LoserId = bracket.SecondTeam.TeamId; } else if (t1score < t2score) { bracket.WinnerId = bracket.SecondTeam.TeamId; bracket.LoserId = bracket.FirstTeam.TeamId; } } foreach (var child in bracket.ChildrenSide1) { if (child.Type == (int)PlayoffBracketType.Winner) { child.Team1Id = bracket.WinnerId; } else if (child.Type == (int)PlayoffBracketType.Loseer) { child.Team1Id = bracket.LoserId; } for (int i = 0; i < child.GamesCycles.Count; i++) { GamesCycle game = child.GamesCycles.ElementAt(i); gameRepo.ResetGame(game, true); if (i % 2 == 0) { game.HomeTeamId = child.Team1Id; game.GuestTeamId = child.Team2Id; } else { game.HomeTeamId = child.Team2Id; game.GuestTeamId = child.Team1Id; } } } foreach (var child in bracket.ChildrenSide2) { if (child.Type == (int)PlayoffBracketType.Winner) { child.Team2Id = bracket.WinnerId; } else if (child.Type == (int)PlayoffBracketType.Loseer) { child.Team2Id = bracket.LoserId; } for (int i = 0; i < child.GamesCycles.Count; i++) { GamesCycle game = child.GamesCycles.ElementAt(i); gameRepo.ResetGame(game, true); if (i % 2 == 0) { game.HomeTeamId = child.Team1Id; game.GuestTeamId = child.Team2Id; } else { game.HomeTeamId = child.Team2Id; game.GuestTeamId = child.Team1Id; } } } } Save(); }