Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, [Bind("id,BracketName,CreatedAt,TotalRounds")] Bracket bracket)
        {
            if (id != bracket.id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(bracket);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BracketExists(bracket.id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Edit(int id, int?bracketID, [Bind("id,TeamName,BracketID")] Team team)
        {
            if (id != team.id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(team);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TeamExists(team.id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Details", "Brackets", new { id = team.BracketID }));
            }
            return(View(team));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(int id, [Bind("id,BracketID,RoundNumber")] Round round)
        {
            if (id != round.id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(round);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoundExists(round.id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BracketID"] = new SelectList(_context.Brackets, "id", "BracketName", round.BracketID);
            return(View(round));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Edit(int id, [Bind("BracketID,Name,RoundFormat,CreatedAt,Active,TeamTotal")] BracketTable bracketTable)
        {
            if (id != bracketTable.BracketID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(bracketTable);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BracketTableExists(bracketTable.BracketID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(bracketTable));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Edit(int id, [Bind("playerID,playerName")] Player player)
        {
            if (id != player.playerID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(player);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PlayerExists(player.playerID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(player));
        }
Ejemplo n.º 6
0
        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));
        }