Exemplo n.º 1
0
 public IActionResult UpdatePlayer(int id, Player player)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     //Integer PlayerData.NumberWins = value.NumberWins;
     player.Id = id;
     using (PingPongDb db = new PingPongDb())
     {
         db.Players.Update(player);
         db.SaveChanges();
         return(Ok());
     }
 }
Exemplo n.º 2
0
 public IActionResult AddNewPlayer(Player player)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     player.clean();
     try {
         using (PingPongDb db = new PingPongDb())
         {
             db.Players.Add(player);
             db.SaveChanges();
             return(Ok(db.Players.Where(x => x.Name == player.Name).FirstOrDefault()));
         }
     } catch (Exception e) {
         throw new HttpRequestException(string.Format("Error: Failed to add player.", e));
     }
 }
Exemplo n.º 3
0
        public IActionResult Post([FromBody] Game gameRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //Sanitize the data for searching/adding new records.
            gameRequest.PlayerOne.clean();
            gameRequest.PlayerTwo.clean();

            //Saving the two players locally for updating their win/loss record.
            var playerOne = gameRequest.PlayerOne;
            var playerTwo = gameRequest.PlayerTwo;

            using (PingPongDb db = new PingPongDb())
            {
                //See if playerOne exists in the database yet.
                Player p1 = db.Players.FirstOrDefault(x => x.Name == gameRequest.PlayerOne.Name);
                //if they do. Remove the player from the game object as to not re-add them to the database.
                //            But set the Forign Key to that players Id.
                //            Update the local version of playerOne as to have accurate win/loss record.
                if (p1 != null)
                {
                    gameRequest.PlayerOne   = null;
                    gameRequest.PlayerOneId = p1.Id;
                    playerOne = p1;
                }

                //Repeat for playerTwo.
                Player p2 = db.Players.FirstOrDefault(x => x.Name == gameRequest.PlayerTwo.Name);
                if (p2 != null)
                {
                    gameRequest.PlayerTwo   = null;
                    gameRequest.PlayerTwoId = p2.Id;
                    playerTwo = p2;
                }

                //Determine Game winner based on Score.
                //Add logic for if games scores are correct, i.e. someone reached 21(or 11), The winner won by 2 points, etc.
                //Update the win/loss record of each player, via the local version of that player.
                if (gameRequest.PlayerOneScore > gameRequest.PlayerTwoScore)
                {
                    gameRequest.Winner = gameRequest.PlayerOneId;
                    playerOne.numberWins++;
                    playerTwo.numberLosses++;
                }
                else
                {
                    gameRequest.Winner = gameRequest.PlayerTwoId;
                    playerTwo.numberWins++;
                    playerOne.numberLosses++;
                }
                gameRequest.complete = true;
                try{
                    //Add a record into the Games table.
                    //This .Add function will add new rows to the player table if the objects are not null.
                    //      Object == null | Id != null < Update
                    //      Object != null | Id == null < Add
                    db.Games.Add(gameRequest);
                    db.SaveChanges();
                }catch (Exception e) {
                    throw new HttpRequestException("Error: Failed to add game record.", e);
                }
            }
            //Update the Database again with the Players new win/loss records.
            playersController.UpdatePlayer(playerOne.Id, playerOne);
            playersController.UpdatePlayer(playerTwo.Id, playerTwo);

            return(Ok());
        }