Exemple #1
0
 public IActionResult GetAllPlayers()
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     using (PingPongDb db = new PingPongDb())
     {
         var players = db.Players.ToList();
         return(Ok(players));
     }
 }
Exemple #2
0
 public IActionResult Get()
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     using (PingPongDb db = new PingPongDb())
     {
         var games = db.Games.ToList();
         return(Ok(games));
     }
 }
Exemple #3
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());
     }
 }
Exemple #4
0
 public IActionResult Get(int id)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     using (PingPongDb db = new PingPongDb())
     {
         var game = db.Games.FirstOrDefault(p => p.Id == id);
         if (game == null)
         {
             return(NotFound());
         }
         return(Ok(game));
     }
 }
Exemple #5
0
 public IActionResult FindPlayerById(int id)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     using (PingPongDb db = new PingPongDb())
     {
         var player = db.Players.FirstOrDefault(p => p.Id == id);
         if (player == null)
         {
             return(NotFound());
         }
         return(Ok(player));
     }
 }
Exemple #6
0
 public IActionResult GetLeaderBoard(int top)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     try {
         using (PingPongDb db = new PingPongDb())
         {
             var players = db.Players.OrderByDescending(x => x.numberWins).Take(top).ToArray();
             return(Ok(players));
         }
     }catch (Exception e) {
         throw new HttpRequestException(string.Format("Error: Failed to get Leaderboard.", e));
     }
 }
Exemple #7
0
 public IActionResult FindPlayerByName([FromQueryAttribute] String name)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     name = name.ToLower();
     using (PingPongDb db = new PingPongDb())
     {
         var player = db.Players.FirstOrDefault(p => p.Name == name);
         if (player == null)
         {
             return(NotFound());
         }
         return(Ok(player));
     }
 }
Exemple #8
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));
     }
 }
Exemple #9
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());
        }