Esempio n. 1
0
        public IActionResult JoinGame([FromBody] GameToFindModel gameToFindModel)
        {
            var  game       = gameDataAccess.GetGame(gameToFindModel.GameID);
            var  user       = userGetter.GetUserFromClaims(HttpContext);
            User gamePlayer = userDataAccess.GetUser(user.UserID);

            if (gamePlayer.ID == game.PlayerBlackID || gamePlayer.ID == game.PlayerWhiteID)
            {
                return(Ok("You have already joined the game"));
            }
            if (game.PlayerWhiteID != null && game.PlayerBlackID != null)
            {
                return(BadRequest("Both seats in the game are taken"));
            }
            //if(game.PlayerWhiteID == null)
            //{
            //    gameDataAccess.ChangePlayers(game.ID, gamePlayer, game.PlayerBlack);
            //}else if(game.PlayerBlackID == null)
            //{
            //    gameDataAccess.ChangePlayers(game.ID, game.PlayerWhite, gamePlayer);
            //}

            GamePresentationModel gameModel = ViewModelMapper.MapGameToPresentation(game);

            return(Ok(gameModel));
        }
Esempio n. 2
0
        public IActionResult QuitGame([FromBody] GameToFindModel gameToFindModel)
        {
            var  game       = gameDataAccess.GetGame(gameToFindModel.GameID);
            var  user       = userGetter.GetUserFromClaims(HttpContext);
            User gamePlayer = userDataAccess.GetUser(user.UserID);

            if (game.PlayerWhiteID != gamePlayer.ID && game.PlayerBlackID != gamePlayer.ID)
            {
                return(BadRequest(new { message = "Can't quit a game u are not in" }));
            }
            if (game.PlayerWhiteID == gamePlayer.ID)
            {
                gameDataAccess.ChangePlayers(game.ID, null, game.PlayerBlack);
            }
            else if (game.PlayerBlackID == gamePlayer.ID)
            {
                gameDataAccess.ChangePlayers(game.ID, game.PlayerWhite, null);
            }

            GamePresentationModel gameModel = ViewModelMapper.MapGameToPresentation(game);

            return(Ok(gameModel));
        }