コード例 #1
0
        public async Task <IActionResult> AdminEdit(int id, [Bind("Id,PlayerOneId,PlayerTwoId,TableNo")] AdminEditRoundMatchupsViewModel roundMatchup)
        {
            if (id != roundMatchup.Id)
            {
                return(NotFound());
            }

            RoundMatchups 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);
            updatedRoundMatchup.Table     = roundMatchup.TableNo;

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(updatedRoundMatchup);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoundMatchupsExists(roundMatchup.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Admin)));
            }
            return(View(roundMatchup));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,RoundNo,PlayerOneBattleScore,PlayerTwoBattleScore,PlayerOneSportsmanshipPoints,PlayerTwoSportsmanshipPoints,Table")] RoundMatchups roundMatchups)
        {
            if (id != roundMatchups.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(roundMatchups);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoundMatchupsExists(roundMatchups.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(roundMatchups));
        }
コード例 #3
0
        public async Task <int> GetCurrentRound()
        {
            int           currentRound = 1;
            RoundMatchups lastRound    = _context.RoundMatchups.LastOrDefault();

            if (lastRound != null)
            {
                currentRound = lastRound.RoundNo;
            }
            return(currentRound);
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("id,roundNo,battlePoints,sportsmanshipPoints,table")] RoundMatchups roundMatchups)
        {
            if (ModelState.IsValid)
            {
                _context.Add(roundMatchups);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(roundMatchups));
        }
コード例 #5
0
        public void GenerateRoundMatchupsAlgorithm()
        {
            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 = getAllOpponents(players[i]);
                            var hasPlayed = false;
                            foreach (Player opponent in opponents)
                            {
                                if (players[s] == opponent)
                                {
                                    hasPlayed = true;
                                }
                            }
                            Debug.WriteLine(hasPlayed);
                            //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 currentRound = 1;

            if (_context.RoundMatchups.LastOrDefault() != null)
            {
                currentRound = _context.RoundMatchups.Last().RoundNo + 1;
            }
            foreach (Player player in players)
            {
                if (players.IndexOf(player) < players.IndexOf(player.CurrentOpponent))
                {
                    RoundMatchups roundMatchup = new RoundMatchups
                    {
                        RoundNo   = currentRound,
                        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);
            }
            if (ModelState.IsValid)
            {
                _context.SaveChanges();
            }
        }