public async Task <IActionResult> Edit(int id, [Bind("Id,Name,BattleScore,SportsmanshipScore,Army,Active,EmailAddress,Notes,Paid")] Player player)
        {
            if (id != player.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(player);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PlayerExists(player.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(player));
        }
        public async Task <IActionResult> ResultsEdit(int id, [Bind("Id,RoundNo,PlayerOneId,PlayerTwoId,PlayerOneBattleScore,PlayerTwoBattleScore,PlayerOneSportsmanshipScore,PlayerTwoSportsmanshipScore,Table")] RoundMatchups roundMatchup)
        {
            if (id != roundMatchup.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(roundMatchup);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoundMatchupsExists(roundMatchup.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Results)));
            }
            return(View(roundMatchup));
        }
Пример #3
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,PlayerOneId,PlayerTwoId,PlayerThreeId,PlayerFourId,CurrentRound")] AdminEditRoundMatchupsViewModel roundMatchup)
        {
            if (id != roundMatchup.Id)
            {
                return(NotFound());
            }
            PairRoundMatchup updatedPairRoundMatchup = null;
            RoundMatchup     updatedRoundMatchup     = null;

            if (_context.RoundMatchups.Find(id) is PairRoundMatchup)
            {
                updatedPairRoundMatchup             = _context.RoundMatchups.Find(id) as PairRoundMatchup;
                updatedPairRoundMatchup.PlayerOne   = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerOneId);
                updatedPairRoundMatchup.PlayerTwo   = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerTwoId);
                updatedPairRoundMatchup.PlayerThree = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerThreeId);
                updatedPairRoundMatchup.PlayerFour  = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerFourId);
            }
            else
            {
                updatedRoundMatchup           = _context.RoundMatchups.Find(id);
                updatedRoundMatchup.PlayerOne = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerOneId);
                updatedRoundMatchup.PlayerTwo = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerTwoId);
            }



            if (ModelState.IsValid)
            {
                try
                {
                    if (updatedPairRoundMatchup != null)
                    {
                        _context.Update(updatedPairRoundMatchup);
                    }
                    else
                    {
                        _context.Update(updatedRoundMatchup);
                    }

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoundMatchupActions.RoundMatchupsExists(roundMatchup.Id, _context))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(roundMatchup));
        }
Пример #4
0
        public async Task <IActionResult> RoundMatchupEdit(int id, [Bind("Id, RoundNo, PlayerOneId, PlayerTwoId, PlayerOneBattleScore, PlayerTwoBattleScore, PlayerOneSportsmanshipScore, PlayerTwoSportsmanshipScore, Table")] RoundMatchupEditViewModel roundMatchupvm)
        {
            if (id != roundMatchupvm.Id)
            {
                return(NotFound());
            }

            var playerOne = await _context.Players.SingleOrDefaultAsync(p => p.Id == roundMatchupvm.PlayerOneId);

            var playerTwo = await _context.Players.SingleOrDefaultAsync(p => p.Id == roundMatchupvm.PlayerTwoId);

            var roundMatchup = new RoundMatchup()
            {
                Id                          = roundMatchupvm.Id,
                RoundNo                     = roundMatchupvm.RoundNo,
                PlayerOne                   = playerOne,
                PlayerOneBattleScore        = roundMatchupvm.PlayerOneBattleScore,
                PlayerOneSportsmanshipScore = roundMatchupvm.PlayerOneSportsmanshipScore,
                PlayerTwo                   = playerTwo,
                PlayerTwoBattleScore        = roundMatchupvm.PlayerTwoBattleScore,
                PlayerTwoSportsmanshipScore = roundMatchupvm.PlayerTwoSportsmanshipScore,
                Table                       = roundMatchupvm.Table
            };

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(roundMatchup);
                    await _context.SaveChangesAsync();

                    PlayerActions.SetPlayerScores((int)roundMatchupvm.PlayerOneId, _context);
                    PlayerActions.SetPlayerScores((int)roundMatchupvm.PlayerTwoId, _context);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoundMatchupActions.RoundMatchupsExists(roundMatchup.Id, _context))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(roundMatchup));
        }
Пример #5
0
        public async Task <IActionResult> Edit(long Id, [Bind("Id, TeamId, Name, Country, AreEliminated")] Team team)
        {
            // todo work out what's happening with Id
            if (Id != team.Id)
            {
                return(NotFound());
            }

            // and this isn't the answer (except it is)
            //team.Id = Id;

            if (ModelState.IsValid)
            {
                try
                {
                    _dbContext.Update(team);
                    await _dbContext.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TeamExists(team.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            PopulateCountryList(team.Country);
            return(View(team));
        }
Пример #6
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,BattleScoreRatio,SportsmanshipScoreRatio,ArmyScoreRatio")] Tournament tournament)
        {
            if (id != tournament.Id)
            {
                return(NotFound());
            }

            if (tournament.BattleScoreRatio + tournament.SportsmanshipScoreRatio + tournament.ArmyScoreRatio != 1)
            {
                TempData["Errors"] = "The score ratios must add up to a total of 1";
                return(View(tournament));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(tournament);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TournamentExists(tournament.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index", "Players"));
            }
            return(View(tournament));
        }
        /**
         *
         * Round Generation
         *
         **/
        public static void GenerateNextRound(TournamentDbContext _context)
        {
            List <Player> players = _context.Players.OrderByDescending(p => p.BattleScore).ToList();
            //List<int> AllocatedTables = new List<int>(GetnoOfTables());
            int secondaryIndex = 0;
            int i = 0;

            while (i < players.Count)
            {
                //Skip this player if they are already allocated an opponent
                if (players[i].CurrentOpponent == null)
                {
                    if (secondaryIndex == 0)
                    {
                        secondaryIndex = i + 1;
                    }
                    int s = 0;

                    for (s = secondaryIndex; s < players.Count; s++)
                    {
                        if (players[s].CurrentOpponent == null)
                        {
                            //Check if higher player has ever played lower player
                            var opponents = PlayerActions.GetAllOpponents(players[i], _context);
                            var hasPlayed = false;
                            foreach (Player opponent in opponents)
                            {
                                if (players[s] == opponent)
                                {
                                    hasPlayed = true;
                                }
                            }
                            //If they have not played allocate them as opponents
                            if (hasPlayed == false)
                            {
                                players[i].CurrentOpponent = players[s];
                                players[s].CurrentOpponent = players[i];
                                secondaryIndex             = 0;
                                break;
                            }

                            /**
                             * Following block is to deallocate the next lowest ranked allocated pair
                             **/
                            if (players.Where(p => p.CurrentOpponent == null).LastOrDefault() == players[s] && (players[i].CurrentOpponent == null))
                            //if (s == (players.Count - 1) && (players[i].CurrentOpponent == null))
                            {
                                if (i - 1 >= 0)
                                {
                                    //Set the lowestAllocatedPair to the highest ranked player
                                    Player lowestAllocatedPair = players[0];
                                    //Iterate from the second highest ranked player all the way to the player ranked one higher than the player currently being matched
                                    for (int playerIndex = 1; playerIndex < i; playerIndex++)
                                    {
                                        //Assign the current player to the player being examined in the current iteration of the loop
                                        Player currentPlayer = players[playerIndex];

                                        //Check that the current player has an opponent (if not, skip to the next iteration of the loop)
                                        if (currentPlayer.CurrentOpponent != null)
                                        {
                                            //Proceed if the current player's opponent has a higher rank than the current player
                                            if (players.IndexOf(currentPlayer.CurrentOpponent) < players.IndexOf(currentPlayer))
                                            {
                                                if (players.IndexOf(currentPlayer.CurrentOpponent) > players.IndexOf(lowestAllocatedPair))
                                                {
                                                    //Set lowestAllocatedPair to the current player's opponent
                                                    //(The highest ranked member of the new lowest ranked allocated pair)
                                                    lowestAllocatedPair = currentPlayer.CurrentOpponent;
                                                }
                                            }
                                            //Proceed if the current player has a higher rank than their opponent
                                            else if (players.IndexOf(currentPlayer) < players.IndexOf(currentPlayer.CurrentOpponent))
                                            {
                                                //Proceed if the current player has a lower rank than the previous value of lowestAllocatedPair
                                                if (players.IndexOf(currentPlayer) > players.IndexOf(lowestAllocatedPair))
                                                {
                                                    //Set lowestAllocatedPair to the current player
                                                    //(The highest ranked member of the new lowest ranked allocated pair)
                                                    lowestAllocatedPair = currentPlayer;
                                                }
                                            }
                                        }
                                    }
                                    //Set the new player to be allocated to the highest member of the lowestAllocatedPair
                                    i = players.IndexOf(lowestAllocatedPair) - 1;
                                    //Set the starting player that will be tested for allocation suitability to one rank lower than
                                    //the opponent of the highest member of the allocated pair
                                    secondaryIndex = players.IndexOf(lowestAllocatedPair.CurrentOpponent) + 1;

                                    //Deallocate the lowest allocated pair as each other's opponent
                                    lowestAllocatedPair.CurrentOpponent.CurrentOpponent = null;
                                    lowestAllocatedPair.CurrentOpponent = null;
                                }
                            }
                        }
                    }
                }
                i++;
            }

            int newRound = GetLastRoundNo(_context) + 1;

            foreach (Player player in players)
            {
                if (players.IndexOf(player) < players.IndexOf(player.CurrentOpponent))
                {
                    RoundMatchup roundMatchup = new RoundMatchup
                    {
                        RoundNo   = newRound,
                        PlayerOne = player,
                        PlayerTwo = player.CurrentOpponent
                    };

                    //allocates table for matchup
                    // roundMatchup.Table = AllocateTable(GetTables(player), AllocatedTables);

                    _context.Add(roundMatchup);
                }
            }

            foreach (Player player in players)
            {
                player.CurrentOpponent = null;
                _context.Update(player);
            }

            _context.SaveChanges();
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,PlayerOneId,PlayerTwoId,PlayerThreeId,PlayerFourId,CurrentRound")] AdminEditRoundMatchupsViewModel roundMatchup)
        {
            if (id != roundMatchup.Id)
            {
                return(NotFound());
            }


            //Check if a player is versing themself or on a team with themself in a standard round
            if (roundMatchup.PlayerThreeId == 0.5 && roundMatchup.PlayerFourId == 0.5)
            {
                if (roundMatchup.PlayerOneId == roundMatchup.PlayerTwoId)
                {
                    TempData["Errors"]   = "A player cannot verse themself or be on a team with themself";
                    roundMatchup.Players = _context.Players.ToList();
                    return(View(roundMatchup));
                }
            }
            //Check if a player is versing themself or on a team with themself in a pair round
            else if (roundMatchup.PlayerOneId == roundMatchup.PlayerTwoId

                     || roundMatchup.PlayerOneId == roundMatchup.PlayerThreeId ||
                     roundMatchup.PlayerOneId == roundMatchup.PlayerFourId ||
                     roundMatchup.PlayerTwoId == roundMatchup.PlayerThreeId ||
                     roundMatchup.PlayerTwoId == roundMatchup.PlayerFourId ||
                     roundMatchup.PlayerThreeId == roundMatchup.PlayerFourId)
            {
                TempData["Errors"] = "A player cannot verse themself or be on a team with themself";

                roundMatchup.Players = _context.Players.ToList();
                return(View(roundMatchup));
            }
            PairRoundMatchup updatedPairRoundMatchup = null;
            RoundMatchup     updatedRoundMatchup     = null;

            if (_context.RoundMatchups.Find(id) is PairRoundMatchup)
            {
                updatedPairRoundMatchup             = _context.RoundMatchups.Include(p => p.PlayerOne).Include(p => p.PlayerTwo).SingleOrDefault(r => r.Id == id) as PairRoundMatchup;
                updatedPairRoundMatchup.PlayerOne   = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerOneId);
                updatedPairRoundMatchup.PlayerTwo   = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerTwoId);
                updatedPairRoundMatchup.PlayerThree = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerThreeId);
                updatedPairRoundMatchup.PlayerFour  = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerFourId);
                updatedPairRoundMatchup.Table       = roundMatchup.TableNo;
            }
            else
            {
                updatedRoundMatchup           = _context.RoundMatchups.Include(p => p.PlayerOne).Include(p => p.PlayerTwo).SingleOrDefault(r => r.Id == id);
                updatedRoundMatchup.PlayerOne = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerOneId);
                updatedRoundMatchup.PlayerTwo = _context.Players.FirstOrDefault(p => p.Id == roundMatchup.PlayerTwoId);

                updatedRoundMatchup.Table = roundMatchup.TableNo;
            }


            if (ModelState.IsValid)
            {
                try
                {
                    if (updatedPairRoundMatchup != null)
                    {
                        _context.Update(updatedPairRoundMatchup);
                    }
                    else
                    {
                        _context.Update(updatedRoundMatchup);
                    }

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoundMatchupActions.RoundMatchupsExists(roundMatchup.Id, _context))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            roundMatchup.Players = _context.Players.ToList();
            return(View(roundMatchup));
        }