Ejemplo n.º 1
0
        public static Player CreateFromDataBase()
        {
            try {
                // this is our connection to the data base
                using (NpgsqlConnection connection = new NpgsqlConnection(_connectionString)) {
                    connection.Open();
                    Player player;

                    // create a sql command object that uses the connection to our data base the sql command object is where we
                    // create our sql statement
                    using (NpgsqlCommand savedGameCommand = new NpgsqlCommand("SELECT * FROM SavedGame LIMIT 1", connection))
                        using (NpgsqlDataReader reader = savedGameCommand.ExecuteReader()) {
                            if (!reader.HasRows)
                            {
                                // there is no data in the savegame table so return null no saved player data
                                return(null);
                            }

                            // get the row/record fomr the data reader
                            reader.Read();
                            // get the column value for the row/record
                            int currentHitPoints  = (int)reader["CurrentHitPoints"];
                            int maximumHitPoints  = (int)reader["MaximumHitPoints"];
                            int gold              = (int)reader["Gold"];
                            int experiencePoints  = (int)reader["ExperiencePoints"];
                            int currentLocationID = (int)reader["CurrentLocationID"];
                            // create the player object with the saved game values
                            player = Player.CreatePlayerFromDatabase(
                                currentHitPoints, maximumHitPoints, gold, experiencePoints, currentLocationID);
                        }

                    // read the rows / records from the quest table and add them to the player
                    using (NpgsqlCommand questCommand = new NpgsqlCommand("SELECT * FROM Quest", connection))
                        using (NpgsqlDataReader reader = questCommand.ExecuteReader()) {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    int  questID     = (int)reader["QuestID"];
                                    bool isCompleted = (bool)reader["IsCompleted"];
                                    //build the PlayerQuest item for this row
                                    PlayerQuest playerQuest =
                                        new PlayerQuest(World.QuestByID(questID));
                                    playerQuest.IsCompleted = isCompleted;
                                    // add the PlayerQuest to the players property
                                    var playerQuestHas = player.Quests.FirstOrDefault(x => x.Details.ID == playerQuest.Details.ID);
                                    if (playerQuestHas != null)
                                    {
                                        playerQuestHas.IsCompleted = isCompleted;
                                    }
                                    else
                                    {
                                        player.Quests.Add(playerQuest);
                                    }
                                }
                            }
                        }

                    //read the rows/ records from the inventory table and add them to the player
                    using (NpgsqlCommand inventoryCommand = new NpgsqlCommand("SELECT * FROM Inventory", connection))
                        using (NpgsqlDataReader reader = inventoryCommand.ExecuteReader()) {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    int inventoryItemID = (int)reader["InventoryItemID"];
                                    int quantity        = (int)reader["Quantity"];

                                    // add the item to the players inventory
                                    player.AddItemToInventory(World.ItemByID(inventoryItemID), quantity);
                                }
                            }
                        }

                    //now that the player has been build from the database return it
                    return(player);
                }
            } catch (Exception ex) {
                Console.WriteLine(ex);
                //ignore errors if there is an error this function will return a null player
            }

            return(null);
        }
Ejemplo n.º 2
0
        public static Player CreateFromDatabase()
        {
            try
            {
                // This is the connection to the database
                using (SqlConnection connection = new SqlConnection(_connectionString))
                {
                    // Open the connection to make it possible to perform SQL commands
                    connection.Open();

                    Player player;

                    // Create a SQL command object that uses the connection to the database.
                    // The SqlCommand object is where we create our SQL statement
                    using (SqlCommand savedGameCommand = connection.CreateCommand())
                    {
                        savedGameCommand.CommandType = CommandType.Text;

                        // This SQL statement reads the first rows in the SavedGame table.
                        // For this program, we should only ever have one row, but this will
                        // ensure we only get one record in our SQL query results.
                        savedGameCommand.CommandText = "SELECT TOP 1 * FROM SavedGame";

                        // Use ExecuteReader when you expect the query to return a row, or multiple rows.
                        SqlDataReader reader = savedGameCommand.ExecuteReader();

                        // Check if the query did not return a row/record of data
                        if (!reader.HasRows)
                        {
                            // There is no data in the SavedGame table, so return null (no saved player data)
                            return(null);
                        }

                        // Get the row/record from the data reader
                        reader.Read();

                        // Get the column values for the row/record
                        int currentHitPoints  = (int)reader["CurrentHitPoints"];
                        int maximumHitPoints  = (int)reader["MaximumHitPoints"];
                        int gold              = (int)reader["Gold"];
                        int experiencePoints  = (int)reader["ExperiencePoints"];
                        int currentLocationID = (int)reader["CurrentLocationID"];

                        // Create the Player object, with the saved game values
                        player = Player.CreatePlayerFromDatabase(currentHitPoints, maximumHitPoints, gold, experiencePoints, currentLocationID);
                    }

                    // Read the rows/records from the Quest table and add them to the player's quest list
                    using (SqlCommand questCommand = connection.CreateCommand())
                    {
                        questCommand.CommandType = CommandType.Text;
                        questCommand.CommandText = "SELECT * FROM Quest";

                        SqlDataReader reader = questCommand.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                int  questID     = (int)reader["QuestID"];
                                bool isCompleted = (bool)reader["IsCompleted"];

                                // Build the PlayerQuest item for this row
                                PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(questID));
                                playerQuest.IsCompleted = isCompleted;

                                // Add the PlayerQuest to the player's property
                                player.Quests.Add(playerQuest);
                            }
                        }
                    }

                    // Read the rows/records from the Inventory table and add them to the player's inventory
                    using (SqlCommand inventoryCommand = connection.CreateCommand())
                    {
                        inventoryCommand.CommandType = CommandType.Text;
                        inventoryCommand.CommandText = "SELECT * FROM Inventory";

                        SqlDataReader reader = inventoryCommand.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                int inventoryItemID = (int)reader["InventoryItemID"];
                                int quantity        = (int)reader["Quantity"];

                                // Add the item to the player's inventory
                                player.AddItemToInventory(World.ItemByID(inventoryItemID), quantity);
                            }
                        }
                    }

                    // Now that the player has been built from the database, return it.
                    return(player);
                }
            }
            catch (Exception ex)
            {
                // Ignore errors. If there is an error, this function will return a "null" player.
            }

            return(null);
        }
Ejemplo n.º 3
0
        public static Player CreateFromDatabase()
        {
            try
            {
                using (NpgsqlConnection connection = new NpgsqlConnection(_connectionString))
                {
                    connection.Open();

                    Player player;

                    using (NpgsqlCommand savedGameCommand = connection.CreateCommand())
                    {
                        savedGameCommand.CommandType = CommandType.Text;
                        // reads the first rows in the SavedGame table.
                        savedGameCommand.CommandText = "SELECT TOP 1 * FROM SavedGame";

                        // Use when you expect the query to return a row, or rows
                        NpgsqlDataReader reader = savedGameCommand.ExecuteReader();

                        // Check if the query did not return a row/record of data
                        if (!reader.HasRows)
                        {
                            return(null);
                        }

                        reader.Read();

                        // Get the column values for the row/record
                        int currentHitPoints  = (int)reader["CurrentHitPoints"];
                        int maximumHitPoints  = (int)reader["MaximumHitPoints"];
                        int gold              = (int)reader["Gold"];
                        int experiencePoints  = (int)reader["ExperiencePoints"];
                        int currentLocationID = (int)reader["CurrentLocationID"];

                        // Create the Player object, with the saved game values
                        player = Player.CreatePlayerFromDatabase(currentHitPoints, maximumHitPoints, gold,
                                                                 experiencePoints, currentLocationID);
                    }

                    // Read the rows/records from the Quest table, and add them to the player
                    using (NpgsqlCommand questCommand = connection.CreateCommand())
                    {
                        questCommand.CommandType = CommandType.Text;
                        questCommand.CommandText = "SELECT * FROM Quest";

                        NpgsqlDataReader reader = questCommand.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                int  questID     = (int)reader["QuestID"];
                                bool isCompleted = (bool)reader["IsCompleted"];

                                // Build the PlayerQuest item, for this row
                                PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(questID))
                                {
                                    IsCompleted = isCompleted
                                };

                                player.Quests.Add(playerQuest);
                            }
                        }
                    }

                    using (NpgsqlCommand inventoryCommand = connection.CreateCommand())
                    {
                        inventoryCommand.CommandType = CommandType.Text;
                        inventoryCommand.CommandText = "SELECT * FROM Inventory";

                        NpgsqlDataReader reader = inventoryCommand.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                int inventoryItemID = (int)reader["InventoryItemID"];
                                int quantity        = (int)reader["Quantity"];

                                // Add the item to the player's inventory
                                player.AddItemToInventory(World.ItemByID(inventoryItemID), quantity);
                            }
                        }
                    }

                    return(player);
                }
            }
            catch (Exception ex)
            {
                //returns null player...hopefully
            }

            return(null);
        }
Ejemplo n.º 4
0
        public static Player CreateFromDatabase()
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(_connectionString))
                {
                    connection.Open();

                    Player player;

                    using (SqlCommand savedGameCommand = connection.CreateCommand())
                    {
                        savedGameCommand.CommandType = CommandType.Text;
                        savedGameCommand.CommandText = "SELECT TOP 1 * FROM SavedGame";

                        SqlDataReader reader = savedGameCommand.ExecuteReader();

                        if (!reader.HasRows)
                        {
                            return(null);
                        }

                        reader.Read();

                        int currentHitPoints  = (int)reader["CurrentHitPoints"];
                        int maximumHitPoints  = (int)reader["MaximumHitPoints"];
                        int gold              = (int)reader["Gold"];
                        int experiencePoints  = (int)reader["ExperiencePoints"];
                        int currentLocationID = (int)reader["CurrentLocationID"];

                        player = Player.CreatePlayerFromDatabase(currentHitPoints, maximumHitPoints, gold,
                                                                 experiencePoints, currentLocationID);
                    }

                    using (SqlCommand questCommand = connection.CreateCommand())
                    {
                        questCommand.CommandType = CommandType.Text;
                        questCommand.CommandText = "SELECT * FROM Quest";

                        SqlDataReader reader = questCommand.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                int  questID     = (int)reader["QuestID"];
                                bool isCompleted = (bool)reader["IsCompleted"];

                                PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(questID));
                                playerQuest.IsCompleted = isCompleted;

                                player.Quests.Add(playerQuest);
                            }
                        }
                    }

                    using (SqlCommand inventoryCommand = connection.CreateCommand())
                    {
                        inventoryCommand.CommandType = CommandType.Text;
                        inventoryCommand.CommandText = "SELECT * FROM Inventory";

                        SqlDataReader reader = inventoryCommand.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                int inventoryItemID = (int)reader["InventoryItemID"];
                                int quantity        = (int)reader["Quantity"];

                                player.AddItemToInventory(World.ItemByID(inventoryItemID), quantity);
                            }
                        }
                    }

                    return(player);
                }
            }
            catch (Exception ex)
            {
                //TODO add some basic error handling
            }

            return(null);
        }
Ejemplo n.º 5
0
        public static Player CreateFromDatabase()
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(_connectionString))
                {
                    connection.Open();

                    Player player;

                    // create sql command object
                    using (SqlCommand savedGameCommand = connection.CreateCommand())
                    {
                        savedGameCommand.CommandType = CommandType.Text;
                        savedGameCommand.CommandText = "SELECT TOP 1 * FROM SavedGame";

                        // execute command and check if data is available
                        using (SqlDataReader reader = savedGameCommand.ExecuteReader())
                        {
                            if (!reader.HasRows)
                            {
                                return(null);
                            }

                            // read data from database and create player
                            reader.Read();

                            int currentHitPoints  = (int)reader["CurrentHitPoints"];
                            int maximumHitPoints  = (int)reader["MaximumHitPoints"];
                            int gold              = (int)reader["Gold"];
                            int experiencePoints  = (int)reader["ExperiencePoints"];
                            int currentLocationID = (int)reader["CurrentLocationID"];

                            player = Player.CreatePlayerFromDatabase(currentHitPoints, maximumHitPoints, gold,
                                                                     experiencePoints, currentLocationID);
                        }
                    }

                    // read quest table
                    using (SqlCommand questCommand = connection.CreateCommand())
                    {
                        questCommand.CommandType = CommandType.Text;
                        questCommand.CommandText = "SELECT * FROM Quest";

                        using (SqlDataReader reader = questCommand.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    int  questID     = (int)reader["QuestID"];
                                    bool isCompleted = (bool)reader["IsCompleted"];

                                    PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(questID));
                                    playerQuest.IsCompleted = isCompleted;
                                    player.Quests.Add(playerQuest);
                                }
                            }
                        }
                    }

                    // read inventory
                    using (SqlCommand inventoryCommand = connection.CreateCommand())
                    {
                        inventoryCommand.CommandType = CommandType.Text;
                        inventoryCommand.CommandText = "SELECT * FROM Inventory";

                        using (SqlDataReader reader = inventoryCommand.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    int inventoryItemID = (int)reader["InventoryItemID"];
                                    int quantity        = (int)reader["Quantity"];

                                    player.AddItemToInventory(World.ItemByID(inventoryItemID), quantity);
                                }
                            }
                        }
                    }

                    return(player);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(null);
        }
Ejemplo n.º 6
0
        public static Player CreateFromDatabase()
        {
            try
            {
                //Connect to database
                using (SqlConnection connection = new SqlConnection(_connectionString))
                {
                    //Open connection
                    connection.Open();

                    Player player;

                    using (SqlCommand savedGameCommand = connection.CreateCommand())
                    {
                        savedGameCommand.CommandType = CommandType.Text;

                        savedGameCommand.CommandText = "SELECT TOP 1 * FROM SavedGame";

                        SqlDataReader reader = savedGameCommand.ExecuteReader();

                        //Check if the query did not return a row/record of data
                        if (!reader.HasRows)
                        {
                            //There is no data in SavedGame table. so return null
                            return(null);
                        }
                        //Get record from data reader
                        reader.Read();

                        //Get column values for the record
                        int currentHitPoints  = (int)reader["CurrentHitPoints"];
                        int maximumHitPoints  = (int)reader["MaximumHitPoints"];
                        int gold              = (int)reader["Gold"];
                        int experiencePoints  = (int)reader["ExperiencePoints"];
                        int currentLocationID = (int)reader["CurrentLocationID"];

                        //Create the Player object, with the saved games values
                        player = Player.CreatePlayerFromDatabase(currentHitPoints, maximumHitPoints, gold, experiencePoints, currentLocationID);
                    }

                    //Read records from Quest table, and add them to player
                    using (SqlCommand questCommand = connection.CreateCommand())
                    {
                        questCommand.CommandType = CommandType.Text;
                        questCommand.CommandText = "SELECT * FROM Quest";

                        SqlDataReader reader = questCommand.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                int  questID     = (int)reader["QuestID"];
                                bool isCompleted = (bool)reader["IsCompleted"];

                                //Build the PlayerQuest item, for this row
                                PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(questID));
                                playerQuest.IsCompleted = isCompleted;

                                //Add the PlayerQuest to inventory
                                player.Quests.Add(playerQuest);
                            }
                        }
                    }

                    using (SqlCommand inventoryCommand = connection.CreateCommand())
                    {
                        inventoryCommand.CommandType = CommandType.Text;
                        inventoryCommand.CommandText = "SELECT * FROM Inventory";

                        SqlDataReader reader = inventoryCommand.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                int inventoryItemID = (int)reader["InventoryItemID"];
                                int quantity        = (int)reader["Quantity"];

                                //Add item to inventory
                                player.AddItemToInventory(World.ItemByID(inventoryItemID), quantity);
                            }
                        }
                    }

                    //Now the player has been built from the database, return it.
                    return(player);
                }
            }
            catch (Exception ex)
            {
                //Ignore error. If there is an error, it will return "null" player.
            }

            return(null);
        }