/// <summary>
        /// Insert game using gameStorageModel
        /// </summary>
        /// <param name="gameStorage"></param>
        public int Insert(GameStorageModel gameStorage)
        {
            int newIdNumber = -1;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string     quey      = "INSERT INTO GameStorage(UserId, DateTime, GameBoard) VALUES(@userId, @dateTime, @gameBoard)";
                SqlCommand myCommand = new SqlCommand(quey, connection);
                myCommand.Parameters.AddWithValue("@userId", gameStorage.userId);
                myCommand.Parameters.AddWithValue("@dateTime", gameStorage.dateTime);
                myCommand.Parameters.AddWithValue("@gameBoard", gameStorage.gameBoard);
                try
                {
                    connection.Open();

                    //check if there was a row affected
                    if (myCommand.ExecuteNonQuery() > 0)
                    {
                        newIdNumber = 1;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                return(newIdNumber);
            }
        }
        /*
         * this method saves a game
         */
        public bool Save(GameStorageModel storageModel)
        {
            // assume the game will not be saved
            bool success = false;

            // databse connection string
            String connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MinesweeperDatabase;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";

            // connect to databse in business service to support ACID transactions
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                // instantiate DAO
                GameDAO dao = new GameDAO(connection);

                // open databse connection
                connection.Open();

                // pass control to DAO to delete user previous save
                dao.Delete(storageModel.User);

                // pass control to DAO to save the current game and catch return value
                success = dao.Create(storageModel);

                // close database connection
                connection.Close();
            }

            // return result of save attempt
            return(success);
        }
        /// <summary>
        /// Select gamestorage by id
        /// </summary>
        /// <param name="id"></param>
        public GameStorageModel GetGameById(int id)
        {
            GameStorageModel gameStorageModel = null;
            string           sqlStatement     = "SELECT * FROM GameStorage WHERE Id = @id";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(sqlStatement, connection);
                command.Parameters.AddWithValue("@id", id);
                try
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        gameStorageModel = new GameStorageModel((int)reader[0], (int)reader[1], (string)reader[2], (string)reader[3]);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                };
            }
            return(gameStorageModel);
        }
        /*
         * this method creates a new game save
         * @param storageModel the game to be saved
         * @return bool true if the save operation was successful false otherwise
         */
        public bool Create(GameStorageModel storageModel)
        {
            // the SQL query with prepared statement parameters
            String query = "INSERT INTO dbo.GAME (STATE, ACCOUNT) VALUES (@state, @account)";

            // set a SqlCommand object using the SQL query and database connection
            // this object supposts prepared statements
            using (SqlCommand command = new SqlCommand(query, Connection))
            {
                // configure prepared statement parameters
                command.Parameters.Add("@state", SqlDbType.Text).Value       = storageModel.GameState;
                command.Parameters.Add("@account", SqlDbType.NVarChar).Value = storageModel.User;

                // execute query and catch row count
                int rowsAffected = command.ExecuteNonQuery();

                // if rows were affected
                if (rowsAffected > 0)
                {
                    // save operation is successful
                    return(true);
                }

                // otherwise
                else
                {
                    // save operation fails
                    return(false);
                }
            }
        }
Example #5
0
        public ActionResult HandleLoad()
        {
            try
            {
                // instantiate business service
                GameService service = new GameService();

                // pass control to service and catch return value
                GameStorageModel businessLayerResponseModel = service.Load(Bundle.User);

                // deserialize game state and save to bundle
                Bundle = JsonConvert.DeserializeObject <GameBundle>(businessLayerResponseModel.GameState);

                // configure timer
                Bundle.StartTime = DateTime.Now - Bundle.Timer;

                // log successful load
                Logger.Info("Game loaded successfully!");

                // return board view with bundle
                return(View("gameBoard", Bundle));
            }

            catch (Exception e)
            {
                Logger.Error("Failed to load game: " + e.Message);
                return(View("exception"));
            }
        }
Example #6
0
        public ActionResult HandleSave()
        {
            try
            {
                // instantiate business service
                GameService service = new GameService();

                // instantiate model
                GameStorageModel storageModel = new GameStorageModel(-1, JsonConvert.SerializeObject(Bundle), Bundle.User);

                // pass control to service and catch return value
                bool success = service.Save(storageModel);

                Logger.Info("Game saved successfully!");

                // return view and boolean flag
                return(View("save", success));
            }

            catch (Exception e)
            {
                Logger.Error("Failed to save game: " + e.Message);
                return(View("exception"));
            }
        }
        public IActionResult SaveGameClick()
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            var gameJson            = js.Serialize(gameBoard);
            int userId = (int)HttpContext.Session.GetInt32("userId");
            GameStorageModel gameStorageModel = new GameStorageModel(userId, new DateTime().ToString(), gameJson);

            mineSweeperService.insertGameRecord(gameStorageModel);
            return(View("Index", gameBoard.buttons));
        }
Example #8
0
        public ActionResult <GameStorageModel> showSavedGames(int Id)
        {
            GameStorageModel gsm = mds.GetGameById(Id);

            return(gsm);
        }
Example #9
0
 public int insertGameRecord(GameStorageModel gameStorageModel)
 {
     return(mineSweeperDataService.Insert(gameStorageModel));
 }