public async Task <IActionResult> Create([Bind("id,BracketName,CreatedAt,TotalRounds")] Bracket bracket) { if (ModelState.IsValid) { _context.Add(bracket); await _context.SaveChangesAsync(); /* * For loop for TotalRounds * 2 * Then Create into Team * id, TeamName=Null, BracketID * string query = "SELECT * FROM Department WHERE DepartmentID = @p0"; * var TempQueryData = _context.Teams.FromSql("SET IDENTITY_INSERT [dbo].[Team] ON INSERT INTO[dbo].[Team]([id], [TeamName], [BracketID]) VALUES('" + i + "', ' ', '" + tempID + "') SET IDENTITY_INSERT[dbo].[Team] OFF").ToList(); * Debug.WriteLine("HI: " + i); */ /* 3 rounds * 2^3 = 8 teams * 1v8, 2v7, 3v6, 4v5 * 1/8 v. 2/7, 3/6 v. 4/5 * 1/8/2/7 v. 3/6/4/5 * */ int roundtotal = Convert.ToInt32(bracket.TotalRounds); int teamlength = (int)Math.Pow(2, roundtotal); Debug.WriteLine("Team Total: " + teamlength); int firstroundgames = teamlength / 2; int gametotal = teamlength - 1; int remaininggames = gametotal - firstroundgames; string tempID = bracket.id.ToString(); List <Team> teamList = new List <Team>(); for (int i = 0; i < teamlength; i++) { var initial = new Team() { TeamName = " ", BracketID = bracket.id, }; teamList.Add(initial); _context.Teams.Add(initial); } _context.SaveChanges(); /* Initial Games */ List <int> topHalf = new List <int>(); List <int> tempbottomHalf = new List <int>(); for (int i = 0; i < teamlength; i++) { if (i < (bracket.TotalRounds / 2)) { topHalf.Add(teamList[i].id); } else { tempbottomHalf.Add(teamList[i].id); } } List <int> bottomHalf = new List <int>(); for (int i = 0; i < bottomHalf.Count(); i++) { bottomHalf.Add(tempbottomHalf[bottomHalf.Count() - i - 1]); } //Create Rounds //Bracket, RoundNumber List <Round> roundList = new List <Round>(); List <Match> matchList = new List <Match>(); for (int i = 0; i < roundtotal; i++) { var initial = new Round() { RoundNumber = i + 1, BracketID = bracket.id, }; roundList.Add(initial); _context.Rounds.Add(initial); // First Round Matches for (int x = 0; x < firstroundgames; x++) { try { var matchInitial = new Match() { //MatchNumber, TEAMAID, TEAMBID MatchNumber = x, TeamAID = topHalf[x], TeamBID = bottomHalf[x], }; matchList.Add(matchInitial); _context.Matches.Add(matchInitial); } catch (ArgumentOutOfRangeException ex) { Debug.WriteLine("Out of Range Detected"); } } } _context.SaveChanges(); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(CreatePlayer), new { id = bracket.id })); } return(View(bracket)); }
public async Task <IActionResult> Create([Bind("id,BracketName,CreatedAt,TotalRounds")] Bracket bracket) { if (ModelState.IsValid) { _context.Add(bracket); await _context.SaveChangesAsync(); /* * For loop for TotalRounds * 2 * Then Create into Team * id, TeamName=Null, BracketID * string query = "SELECT * FROM Department WHERE DepartmentID = @p0"; * var TempQueryData = _context.Teams.FromSql("SET IDENTITY_INSERT [dbo].[Team] ON INSERT INTO[dbo].[Team]([id], [TeamName], [BracketID]) VALUES('" + i + "', ' ', '" + tempID + "') SET IDENTITY_INSERT[dbo].[Team] OFF").ToList(); * Debug.WriteLine("HI: " + i); */ /* 3 rounds * 2^3 = 8 teams * 1v8, 2v7, 3v6, 4v5 * 1/8 v. 2/7, 3/6 v. 4/5 * 1/8/2/7 v. 3/6/4/5 * */ int roundtotal = Convert.ToInt32(bracket.TotalRounds); int teamlength = (int)Math.Pow(2, roundtotal); Debug.WriteLine("Team Total: " + teamlength); int gametotal = teamlength - 1; string tempID = bracket.id.ToString(); List <Team> teamList = new List <Team>(); for (int i = 0; i < teamlength; i++) { var initial = new Team() { TeamName = " ", BracketID = bracket.id, }; teamList.Add(initial); _context.Teams.Add(initial); } _context.SaveChanges(); /* Initial Games */ List <Match> matchList = new List <Match>(); List <int> topHalf = new List <int>(); List <int> bottomHalf = new List <int>(); List <int> TempbottomHalf = new List <int>(); for (int i = 0; i < teamList.Count(); i++) { if (((i % 2) == 0) == true) { topHalf.Add(teamList[i].id); } else if (((i % 2) == 0) == false) { TempbottomHalf.Add(teamList[i].id); } else { Debug.WriteLine("Exception Found"); } } for (int i = TempbottomHalf.Count - 1; i >= 0; i--) { bottomHalf.Add(TempbottomHalf[i]); } for (int x = 0; x < topHalf.Count(); x++) { if (topHalf[x] < bottomHalf[x]) { var matchInitial = new Match() { BracketID = bracket.id, TeamAID = topHalf[x], TeamBID = bottomHalf[x], RoundNumber = 1, MatchNumber = matchList.Count() + 1, }; matchList.Add(matchInitial); _context.Matches.Add(matchInitial); _context.SaveChanges(); } else { var matchInitial = new Match() { BracketID = bracket.id, TeamBID = topHalf[x], TeamAID = bottomHalf[x], RoundNumber = 1, MatchNumber = matchList.Count() + 1, }; matchList.Add(matchInitial); _context.Matches.Add(matchInitial); _context.SaveChanges(); } } _context.SaveChanges(); // Non-First Round Games /* * for (int i = 1; i < roundtotal - 1; i++) * { * * // 3 Rounds * // i = 1 * // 3 - 1 = 2 * // 2 - 0 = 2 * // 2^2 = 4 * * int compute = roundtotal - i; * int val = (int)Math.Pow(2, compute); * for (int x = 0; x < val; x++) * { * var matchInitial = new Match() * { * BracketID = bracket.id, * RoundNumber = i + 2, * MatchNumber = matchList.Count() + 1, * }; * matchList.Add(matchInitial); * _context.Matches.Add(matchInitial); * _context.SaveChanges(); * } * } */ await _context.SaveChangesAsync(); return(RedirectToAction(nameof(CreatePlayer), new { id = bracket.id })); } return(View(bracket)); }
public async Task <IActionResult> Edit(int id, [Bind("MatchID,MatchNumber,TeamAID,TeamBID,TeamAScore,TeamBScore,WinnerID,BracketID,RoundNumber")] Match match) { if (id != match.MatchID) { return(NotFound()); } if (ModelState.IsValid) { try { if (match.TeamAScore > match.TeamBScore) { match.WinnerID = match.TeamAID; } else if (match.TeamAScore < match.TeamBScore) { match.WinnerID = match.TeamBID; } _context.Update(match); await _context.SaveChangesAsync(); //string query = "SELECT COUNT([MatchNumber]) FROM [dbo].[Match] Where [RoundNumber] = {0} and [WinnerID] is NOT NULL"; var matchesRemaining = _context.Matches .Where(m => m.RoundNumber == match.RoundNumber && m.WinnerID == null) .Count(); //select count(*) from Match where RoundNumber = 1 and WinnerID = Null if (matchesRemaining == 0) { List <int> winners = new List <int>(); var bID = _context.Matches .Select(m => m.BracketID) .First(); var matchNumb = _context.Matches .Select(m => m.MatchNumber) .Last(); var RoundNumb = _context.Matches .Select(m => m.RoundNumber) .Last(); var RoundNumbHold = RoundNumb; RoundNumb = RoundNumb + 1; matchNumb = matchNumb + 1; var winnersCircle = _context.Matches.Select(m => new { BracketID = m.BracketID, WinnerID = m.WinnerID, RoundNumber = m.RoundNumber, }) .Where(m => m.BracketID == bID) .Where(m => m.RoundNumber == RoundNumbHold); Debug.WriteLine("Winners' Circle!"); foreach (var item in winnersCircle) { winners.Add(Convert.ToInt32(item.WinnerID)); Debug.WriteLine("ID" + item); } if (winners.Count() > 2) { List <Match> matchList = new List <Match>(); winners.Sort(); int WinnerLen = winners.Count(); List <int> topHalf = winners.Take(WinnerLen / 2).ToList(); List <int> bottomHalf = winners.Skip(WinnerLen / 2).ToList(); foreach (var item in topHalf) { Debug.WriteLine("Top Half" + item); } foreach (var item in bottomHalf) { Debug.WriteLine("Bottom Half" + item); } for (int x = 0; x < topHalf.Count(); x++) { if (topHalf[x] < bottomHalf[x]) { var matchInitial = new Match() { BracketID = bID, TeamAID = topHalf[x], TeamBID = bottomHalf[x], RoundNumber = RoundNumb, MatchNumber = matchNumb + x, }; matchList.Add(matchInitial); _context.Matches.Add(matchInitial); _context.SaveChanges(); } else { var matchInitial = new Match() { BracketID = bID, TeamBID = topHalf[x], TeamAID = bottomHalf[x], RoundNumber = RoundNumb, MatchNumber = matchNumb + x, }; matchList.Add(matchInitial); _context.Matches.Add(matchInitial); _context.SaveChanges(); } } } else if (winners.Count() > 1) { List <Match> matchList = new List <Match>(); winners.Sort(); int WinnerLen = winners.Count(); var matchInitial = new Match() { BracketID = bID, TeamAID = winners[0], TeamBID = winners[1], RoundNumber = RoundNumb, MatchNumber = matchNumb, }; matchList.Add(matchInitial); _context.Matches.Add(matchInitial); _context.SaveChanges(); } } } catch (DbUpdateConcurrencyException) { if (!MatchExists(match.MatchID)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Details", "Brackets", new { id = match.BracketID })); } ViewData["BracketID"] = new SelectList(_context.Brackets, "id", "BracketName", match.BracketID); ViewData["TeamAID"] = new SelectList(_context.Teams, "id", "TeamName", match.TeamAID); ViewData["TeamBID"] = new SelectList(_context.Teams, "id", "TeamName", match.TeamBID); return(View(match)); }