Exemplo n.º 1
0
        public ActionResult <Response <Player> > CreateGame(CreateGameRequest request)
        {
            //Create the player object from the request and validate
            try
            {
                //Create the host player who is joining, will perform data valiation inside Player Class
                Player hostPlayer = new Player(request.nickname, request.imgUrl, request.contact);
                hostPlayer.IsHost = true;

                GameDAL           gameDAL       = new GameDAL();
                Response <Game>   createdGame   = null;
                Response <Player> createdPlayer = null;

                //Generate a game code and create a new game in the database
                //Creating the game inside a while loop because there is a chance that the gamecode could be duplicated
                bool doRun = true;
                while (doRun)
                {
                    Game newGame = null;

                    newGame = new Game(Game.GenerateGameCode(),
                                       request.timeLimit,
                                       request.ammoLimit,
                                       request.startDelay,
                                       request.replenishAmmoDelay,
                                       request.gameMode,
                                       request.isJoinableAtAnytime,
                                       request.Latitude,
                                       request.Longitude,
                                       request.Radius);

                    createdGame = gameDAL.CreateGame(newGame);

                    //If the response is successful the game was successfully created
                    if (createdGame.IsSuccessful())
                    {
                        doRun = false;
                    }

                    //If the response contains an error code of ITEMALREADYEXISTS then the game code is not unique,
                    //Want to run again and generate a new code
                    else if (createdGame.ErrorCode == ErrorCodes.ITEM_ALREADY_EXISTS)
                    {
                        doRun = true;
                    }

                    //If the response contains any other error code we do not want to run again because an un expected
                    //error occurred and we want to return the error response.
                    else
                    {
                        doRun = false;
                    }
                }

                //If the create game failed, return the error message and code from that response
                if (!createdGame.IsSuccessful())
                {
                    return(new Response <Player>(createdGame.ErrorMessage, createdGame.ErrorCode));
                }

                //Call the player data access layer to join the player to that game
                int verificationCode = Player.GenerateVerificationCode();
                createdPlayer = new PlayerDAL().JoinGame(createdGame.Data, hostPlayer, verificationCode);

                //If the response was successful, send the verification code to the player
                string message = "Your CamTag verification code is: " + verificationCode;
                string subject = "CamTag Verification Code";
                if (createdPlayer.IsSuccessful())
                {
                    createdPlayer.Data.ReceiveMessage(message, subject);
                }

                //Otherwise, the host player failed to join the game deleted the created game
                else
                {
                    gameDAL.DeactivateGameAfterHostJoinError(createdGame.Data);
                }

                //Compress the player object so no photo data is sent back to the client to speed up request times
                createdPlayer.Data.Compress(true, true, true);

                return(createdPlayer);
            }
            //Catch any error associated with invalid model data
            catch (InvalidModelException e)
            {
                return(new Response <Player>(e.Msg, e.Code));
            }
            //Catch any unhandled / unexpected server errrors
            catch
            {
                return(StatusCode(500));
            }
        }