Example #1
0
        public JsonResult SetStatusOfChallenge(int challengeId, bool status, string token)
        {
            if (!base.ValidateToken(token))
            {
                return(Json(new
                {
                    errMsg = "Invalid token detected - status of challenge not set.",
                    invalidToken = true
                }));
            }

            // Sets the status of the challenge
            var updatedChallenge = _challengeRepo.SetStatus(status, challengeId);

            // Check to make sure we actually did update the challenge.
            if (updatedChallenge == null)
            {
                // If we did not update the challenge, send an error message
                return(Json(new
                {
                    errMsg = "Challenge status not set - please try again.",
                    invalidToken = false
                }));
            }

            // Check to see if the challenge was accepted, make a new game
            if (updatedChallenge.Accepted)
            {
                db_Game game = new db_Game()
                {
                    Player_1_Id = updatedChallenge.Player_1,
                    Player_2_Id = updatedChallenge.Player_2,
                    Complete    = false,
                    Turn        = updatedChallenge.Player_1
                };

                // Make the game
                var gameCreated = _gameRepo.CreateNewGame(game);

                // Check to make sure the game was created
                if (gameCreated == null)
                {
                    // Return an error if the game was not made
                    return(Json(new
                    {
                        errMsg = "Game could not be created. You may already have an active game with this user.",
                        err = "Null returned from creating a game",
                        invalidToken = false
                    }));
                }
            }

            // Return the normal updated challenge if the game was not accepted
            return(Json(updatedChallenge));
        }
        public JsonResult CreateGame(int player1Id, int player2Id, string token)
        {
            // Ensure that we have a valid token before retreiving / modifying data
            if (!ValidateToken(token))
            {
                return(Json(new
                {
                    errMsg = "Invalid token detected. Please log in again.",
                    err = "Invalid token detected in CreateGame",
                    invalidToken = true
                }));
            }

            // Create a new game object to send to the DB with data provided from params
            db_Game game = new db_Game()
            {
                Player_1_Id = player1Id,
                Player_2_Id = player2Id,
                Complete    = false,
                Turn        = player1Id
            };

            // Create the new game in the DB without the boards
            var createdGame = _gameRepo.CreateNewGame(game);

            if (createdGame == null)
            {
                return(Json(new
                {
                    errMsg = "Error creating new game. Please try again.",
                    err = "Null returned from _gameRepo.CreateNewGame",
                    invalidToken = false
                }));
            }
            return(Json(createdGame));
        }
Example #3
0
        /// <summary>
        /// Creates a new game object in the database.
        /// Will also create all of the boards.
        /// </summary>
        /// <param name="gameInfo"></param>
        /// <returns>bool</returns>
        public db_Game CreateNewGame(db_Game gameInfo)
        {
            Debug.WriteLine("===========================");
            try
            {
                // Ensure there is not already an active game between the two players
                if (GetActiveGameByPlayers(gameInfo.Player_1_Id, gameInfo.Player_2_Id) == null)
                {
                    // Create the new game
                    _context.MySqlDb.Query <db_Game>("INSERT INTO game (" +
                                                     "player_1_id, " +
                                                     "player_2_id, " +
                                                     "complete, " +
                                                     "turn" +
                                                     ") VALUES (" +
                                                     gameInfo.Player_1_Id + ", " +
                                                     gameInfo.Player_2_Id + "," +
                                                     gameInfo.Complete + "," +
                                                     gameInfo.Turn + ");",
                                                     commandType: CommandType.Text);

                    // Retrieve the game created
                    var gameCreated = _context.MySqlDb.Query <db_Game>("SELECT * FROM game;",
                                                                       commandType: CommandType.Text).LastOrDefault();

                    // Save the game's ID
                    var gameCreatedId = gameCreated.Game_Id;

                    // Create and set the first board
                    _context.MySqlDb.Query <db_Board>("INSERT INTO board (game_id) VALUES (" + gameCreatedId + ");", commandType: CommandType.Text);

                    var board1   = _context.MySqlDb.Query <db_Board>("SELECT * FROM board WHERE game_id = " + gameCreatedId + ";", commandType: CommandType.Text).LastOrDefault();
                    var board1Id = board1.Board_Id;

                    _context.MySqlDb.Query <db_Game>("UPDATE game SET player_1_board_id = " + board1Id + " WHERE game_id = " + gameCreatedId + ";", commandType: CommandType.Text);

                    // Create and set the second board
                    _context.MySqlDb.Query <db_Board>("INSERT INTO board (game_id) VALUES (" + gameCreatedId + ");", commandType: CommandType.Text);

                    var board2   = _context.MySqlDb.Query <db_Board>("SELECT * FROM board WHERE game_id = " + gameCreatedId + ";", commandType: CommandType.Text).LastOrDefault();
                    var board2Id = board2.Board_Id;

                    _context.MySqlDb.Query <db_Game>("UPDATE game SET player_2_board_id = " + board2Id + " WHERE game_id = " + gameCreatedId + ";", commandType: CommandType.Text);

                    // Re-retrieve the created game that has been updated
                    var updatedCreatedGame = _context.MySqlDb.Query <db_Game>("SELECT * FROM game WHERE game_id = " + gameCreatedId + ";",
                                                                              commandType: CommandType.Text).FirstOrDefault();

                    return(updatedCreatedGame);
                }

                return(null);
            }
            catch (MySqlException mysqlex)
            {
                Debug.WriteLine("MYSQL EXCEPTION IN CreateNewGame");
                Debug.WriteLine(mysqlex.InnerException);
                return(null);
            }
            catch (InvalidOperationException ioe)
            {
                Debug.WriteLine("INVALID OPERATION EXCEPTION IN CreateNewGame");
                Debug.WriteLine(ioe.InnerException);
                return(null);
            }
            catch (Exception e)
            {
                Debug.WriteLine("EXCEPTION IN CreateNewGame");
                Debug.WriteLine(e.InnerException);
                return(null);
            }
        }