public ActionResult Create([Bind(Include = "Id,Players,SponsorLogoUrl,Deadline,Sport,TournamentName,Date,Location,ParticipantsLimit")] Tournament tournament)
        {
            tournament.Players   = 0;
            tournament.Organizer = User.Identity.Name;
            if (tournament.Deadline == null)
            {
                tournament.Deadline = tournament.Date;
            }
            if (tournament.Deadline > tournament.Date)
            {
                ViewBag.errorMessage = "Deadline must be placed before start of the tournament.";
                return(View(tournament));
            }
            if (tournament.Date < DateTime.Now)
            {
                ViewBag.errorMessage = "You can't upload past tournaments.";
                return(View(tournament));
            }
            ModelState.Clear();
            TryValidateModel(tournament);
            if (ModelState.IsValid)
            {
                db.Tournament.Add(tournament);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(tournament));
        }
        public void Database()
        {
            List <RoundsModel> rounds = _context.RoundsModel.ToList();

            foreach (RoundsModel round in rounds)
            {
                _context.Remove(round);
            }

            List <RoundMatchup> roundmatchups = _context.RoundMatchups.ToList();

            foreach (RoundMatchup roundmatchup in roundmatchups)
            {
                _context.Remove(roundmatchup);
            }

            _context.SaveChanges();
            foreach (Player player in _context.Players.ToList())
            {
                player.BattleScore        = 0;
                player.SportsmanshipScore = 0;
                player.ArmyScore          = 0;
                _context.SaveChanges();
            }
            //PlayerActions.SetAllPlayerScores(_context);
        }
 public void Create(Tournament DataModel)
 {
     using (_coneTournamentDbContext)
     {
         var tournament = new TournamentModel();
         tournament.Name = DataModel.Name;
         _coneTournamentDbContext.Tournament.Add(tournament);
         _coneTournamentDbContext.SaveChanges();
     }
 }
        public void Database()
        {
            List <RoundMatchups> roundmatchups = _context.RoundMatchups.ToList();

            foreach (RoundMatchups roundmatchup in roundmatchups)
            {
                _context.Remove(roundmatchup);
            }
            _context.SaveChanges();
            SetAllPlayerBattleScores();
        }
        public ActionResult Create([Bind(Include = "Id,name,location,discipline,organisator,requestLimit,time,deadline,playersCount")] Tournament tournament)
        {
            if (ModelState.IsValid)
            {
                db.Tournaments.Add(tournament);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(tournament));
        }
        public void SetAllPlayerBattleScores()
        {
            Dictionary <Player, int> playerBattleScores = new Dictionary <Player, int>();
            List <Player>            players            = _context.Players.ToList();

            foreach (Player player in players)
            {
                playerBattleScores.Add(player, 0);
            }
            List <RoundMatchup> roundMatchups = _context.RoundMatchups.ToList();

            foreach (RoundMatchup roundMatchup in roundMatchups)
            {
                playerBattleScores[roundMatchup.PlayerOne] += roundMatchup.PlayerOneBattleScore;
                playerBattleScores[roundMatchup.PlayerTwo] += roundMatchup.PlayerTwoBattleScore;
            }
            foreach (KeyValuePair <Player, int> playerBattleScore in playerBattleScores)
            {
                playerBattleScore.Key.BattleScore = playerBattleScore.Value;
                _context.Update(playerBattleScore.Key);
            }
            if (ModelState.IsValid)
            {
                _context.SaveChanges();
            }
        }
        //Reset the matchups
        public ActionResult ResetRoundMatchups()
        {
            int     roundNo          = RoundMatchupActions.GetLastRoundNo(_context);
            Boolean pairRoundMatchup = false;

            foreach (RoundMatchup roundMatchup in _context.RoundMatchups.Include(r => r.PlayerTwo).Where(r => r.RoundNo == roundNo).ToList())
            {
                if (roundMatchup is PairRoundMatchup && roundMatchup.PlayerTwo != null)
                {
                    pairRoundMatchup = true;
                }

                _context.Remove(roundMatchup);
            }
            _context.SaveChanges();
            if (pairRoundMatchup == true)
            {
                if (RoundMatchupActions.GenerateNextPairRound(_context) == false)
                {
                    TempData["Errors"] = "There are no unique matchups left, manual selection will be required (Random Matchups have been allocated where opponents from last round are teammates)";
                }
            }
            else
            {
                if (RoundMatchupActions.GenerateNextRound(_context) == false)
                {
                    TempData["Errors"] = "There are no unique matchups left, manual selection will be required (Matchups are generated based on most evenly skilled players according to BattleScore, with no regard for previous opponents)";
                }
            }
            return(RedirectToAction(nameof(Index), "Admin"));
        }
Exemplo n.º 8
0
        public void UpdateScoreForPlayer(int userId, int score, int roundId)
        {
            var roundUserPoints = _context.RoundUserPoints
                                  .Where((points) => points.UserId == userId)
                                  .First(points => points.RoundId == roundId);

            roundUserPoints.Score = score;
            _context.SaveChanges();
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder <TournamentDbContext>();

            optionsBuilder.UseSqlServer("Server=./MSSQLSERVER2017; Database=Tournament_Local; Trusted_Connection=True");

            using (var ctx = new TournamentDbContext(optionsBuilder.Options))
            {
                ctx.SaveChanges();
            }
        }
Exemplo n.º 10
0
        public static void SetPlayerScores(int id, TournamentDbContext _context)
        {
            Player player = _context.Players.SingleOrDefault(p => p.Id == id);

            int[] playerScores             = PlayerActions.GetPlayerScores(id, _context);
            int   playerBattleScore        = playerScores[0];
            int   playerSportsmanshipScore = playerScores[1];

            player.BattleScore        = playerBattleScore;
            player.SportsmanshipScore = playerSportsmanshipScore;
            _context.SaveChanges();
        }
Exemplo n.º 11
0
 public void Create(TournamentEvent DataModel)
 {
     using (_coneTournamentDbContext)
     {
         var tournament = _coneTournamentDbContext.Tournament.Find(DataModel.TournamentId);
         var eventModel = new EventModel();
         eventModel.Name             = DataModel.Name;
         eventModel.AutoClose        = DataModel.AutoClose;
         eventModel.EventDateTime    = DataModel.EventDateTime;
         eventModel.EventEndDateTime = DataModel.EventEndDateTime;
         eventModel.EventDetailModel = DataModel.EventDetailModel;
         eventModel.EventNumber      = DataModel.EventNumber;
         eventModel.Tournament       = tournament;
         _coneTournamentDbContext.Event.Add(eventModel);
         _coneTournamentDbContext.SaveChanges();
     }
 }
        public void AddPlayerToTeam(int id, PlayerRequest request)
        {
            var team   = _context.Teams.SingleOrDefault(m => m.IdTeam == id);
            var player =
                _context.Players.SingleOrDefault(
                    m => m.FirstName == request.firstName && m.LastName == request.lastName);

            if (player == null)
            {
                throw new PlayerNotExistsException($"Gracz o imieniu: {request.firstName} i nazwisku: {request.lastName} nie istnieje!");
            }

            DateTime now = DateTime.Today;
            int      age = now.Year - player.DateOfBirth.Year;

            if (now < player.DateOfBirth.AddYears(age))
            {
                age--;
            }

            if (age > team.MaxAge)
            {
                throw new WrongAgeException("Zawodnik jest za stary na tę druzynę!");
            }

            if (_context.PlayerTeams.Any(m => m.IdPlayer == player.IdPlayer && m.IdTeam == team.IdTeam))
            {
                throw new PlayerAlreadyExistException("Taki zawodnik juz istneije!");
            }

            _context.PlayerTeams.Add(new PlayerTeam()
            {
                IdPlayer   = player.IdPlayer,
                Comment    = request.comment,
                IdTeam     = team.IdTeam,
                NumOnShirt = request.numOnShirt,
            });
            _context.SaveChanges();
        }
Exemplo n.º 13
0
        //Reset the matchups
        public ActionResult ResetRoundMatchups()
        {
            int     roundNo          = RoundMatchupActions.GetLastRoundNo(_context);
            Boolean pairRoundMatchup = false;

            foreach (RoundMatchup roundMatchup in _context.RoundMatchups.Where(r => r.RoundNo == roundNo).ToList())
            {
                if (roundMatchup is PairRoundMatchup)
                {
                    pairRoundMatchup = true;
                }
                _context.Remove(roundMatchup);
            }
            _context.SaveChanges();
            if (pairRoundMatchup == true)
            {
                RoundMatchupActions.GenerateNextPairRound(_context);
            }
            else
            {
                RoundMatchupActions.GenerateNextRound(_context);
            }
            return(RedirectToAction(nameof(Index), "Admin"));
        }
        /**
         *
         * 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();
        }
Exemplo n.º 15
0
        public static Boolean GenerateNextPairRound(TournamentDbContext _context)
        {
            Boolean           error             = false;
            int               lastRound         = GetLastRoundNo(_context);
            var               roundMatchups     = _context.RoundMatchups.Where(r => !(r is PairRoundMatchup)).Include(r => r.PlayerOne).Include(r => r.PlayerTwo).Where(r => r.RoundNo == lastRound).ToList();
            var               pairRoundMatchups = _context.PairRoundMatchups.Include(r => r.PlayerOne).Include(r => r.PlayerTwo).Include(r => r.PlayerThree).Include(r => r.PlayerFour).Where(r => r.RoundNo == lastRound).ToList();
            List <Player>     activePlayers     = _context.Players.Where(p => p.Active && p.Bye == false).ToList();
            List <Player>     activeByePlayers  = new List <Player>();
            List <PlayerPair> playerPairs       = new List <PlayerPair>();

            roundMatchups = roundMatchups.Union(pairRoundMatchups).ToList();

            if (roundMatchups != null)
            {
                Tuple <List <PlayerPair>, List <Player> > playerPairsAndByes = (RoundMatchupActions.GetPlayerPairs(roundMatchups, activePlayers));
                playerPairs      = playerPairsAndByes.Item1;
                activeByePlayers = playerPairsAndByes.Item2;
            }
            else
            {
                error = true;
            }

            //List<int> AllocatedTables = new List<int>(GetnoOfTables());
            int secondaryIndex = 0;
            int i = 0;

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

                    for (s = secondaryIndex; s < playerPairs.Count; s++)
                    {
                        //If there are no more unique matchups, exit
                        if (i == -1)
                        {
                            i     = -2;
                            error = true;
                            break;
                        }
                        if (playerPairs[s].CurrentOpponent == null)
                        {
                            //Check if higher player has ever played lower player
                            var playerOneOpponents = PlayerActions.GetAllOpponents(playerPairs[i].First, _context);
                            var playerTwoOpponents = PlayerActions.GetAllOpponents(playerPairs[i].Second, _context);
                            var opponents          = playerOneOpponents.Union(playerTwoOpponents);
                            var hasPlayed          = false;
                            foreach (Player opponent in opponents)
                            {
                                if (playerPairs[s].First == opponent || playerPairs[s].Second == opponent)
                                {
                                    hasPlayed = true;
                                }
                            }
                            //If they have not played allocate them as opponents
                            if (hasPlayed == false)
                            {
                                playerPairs[i].CurrentOpponent = playerPairs[s];
                                playerPairs[s].CurrentOpponent = playerPairs[i];
                                secondaryIndex = 0;
                                break;
                            }

                            /**
                             * Following block is to deallocate the next lowest ranked allocated pair
                             **/
                            if (playerPairs.Where(p => p.CurrentOpponent == null).LastOrDefault() == playerPairs[s] && (playerPairs[i].CurrentOpponent == null))
                            //if (s == (players.Count - 1) && (players[i].CurrentOpponent == null))
                            {
                                if (i - 1 >= 0)
                                {
                                    //Set the lowestAllocatedPair to the highest ranked pair
                                    PlayerPair lowestAllocatedPair = playerPairs[0];
                                    //Iterate from the second highest ranked pair all the way to the pair 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
                                        PlayerPair currentPair = playerPairs[playerIndex];

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

                                    //If there are no more unique matchups, exit
                                    if (i == -1)
                                    {
                                        i     = -2;
                                        error = true;
                                        break;
                                    }

                                    //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 = playerPairs.IndexOf(lowestAllocatedPair.CurrentOpponent) + 1;

                                    //Deallocate the lowest allocated pair as each other's opponent
                                    lowestAllocatedPair.CurrentOpponent.CurrentOpponent = null;
                                    lowestAllocatedPair.CurrentOpponent = null;
                                }
                                else
                                {
                                    i     = -2;
                                    error = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                i++;
            }
            int newRoundNo = lastRound + 1;

            foreach (PlayerPair playerPair in playerPairs)
            {
                if (playerPairs.IndexOf(playerPair) < playerPairs.IndexOf(playerPair.CurrentOpponent))
                {
                    var roundMatchup = new PairRoundMatchup
                    {
                        RoundNo     = newRoundNo,
                        PlayerOne   = playerPair.First,
                        PlayerTwo   = playerPair.Second,
                        PlayerThree = playerPair.CurrentOpponent.First,
                        PlayerFour  = playerPair.CurrentOpponent.Second
                    };

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

                    _context.Add(roundMatchup);
                }
                //If there are no more unique matchups
                if (error)
                {
                    //RoundMatchup will hold random pair matchups where teammates were opponents last round unless their opponent is no longer active or has a bye
                    if (playerPair.CurrentOpponent == null && playerPairs[playerPairs.IndexOf(playerPair) + 1] != null)
                    {
                        playerPair.CurrentOpponent = playerPairs[playerPairs.IndexOf(playerPair) + 1];
                        playerPair.CurrentOpponent.CurrentOpponent = playerPair;
                        var roundMatchup = new PairRoundMatchup
                        {
                            RoundNo     = newRoundNo,
                            PlayerOne   = playerPair.First,
                            PlayerTwo   = playerPair.Second,
                            PlayerThree = playerPair.CurrentOpponent.First,
                            PlayerFour  = playerPair.CurrentOpponent.Second
                        };
                        _context.Add(roundMatchup);
                    }
                }
            }

            CreateByeRounds(activeByePlayers, newRoundNo, _context);

            _context.SaveChanges();

            if (error)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 16
0
 public void Save()
 {
     _context.SaveChanges();
 }