public void MarkQuestCompleted(Quest quest) { //find the quest in the player's quest list PlayerQuest playerQuest = Quests.SingleOrDefault( pq => pq.Details.ID == quest.ID); if (playerQuest != null) { playerQuest.IsCompleted = true; } }
public static Player CreatePlayerFromXmlString(string xmlPlayerData) { try { XmlDocument playerData = new XmlDocument(); playerData.LoadXml(xmlPlayerData); int currentHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentHitPoints").InnerText); int maximumHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaximumHitPoints").InnerText); int gold = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Gold").InnerText); int experiencePoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/ExperiencePoints").InnerText); Player player = new Player(currentHitPoints, maximumHitPoints, gold, experiencePoints); int currentLocationID = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentLocation").InnerText); player.CurrentLocation = World.LocationByID(currentLocationID); if (playerData.SelectSingleNode("/Player/Stats/CurrentWeapon") != null) { int currentWeaponID = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentWeapon").InnerText); player.CurrentWeapon = (Weapon)World.ItemByID(currentWeaponID); } foreach (XmlNode node in playerData.SelectNodes("/Player/InventoryItems/InventoryItem")) { int id = Convert.ToInt32(node.Attributes["ID"].Value); int quantity = Convert.ToInt32(node.Attributes["Quantity"].Value); for (int i = 0; i < quantity; i++) { player.AddItemToInventory(World.ItemByID(id)); } } foreach (XmlNode node in playerData.SelectNodes("/Player/PlayerQuests/PlayerQuest")) { int id = Convert.ToInt32(node.Attributes["ID"].Value); bool isCompleted = Convert.ToBoolean(node.Attributes["IsCompleted"].Value); PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(id)); playerQuest.IsCompleted = isCompleted; player.Quests.Add(playerQuest); } return(player); } catch { // If there was an error with the XML data, return a default player object return(Player.CreateDefaultPlayer()); } }
public void MarkQuestCompleted(Quest quest) { // NOTE: If the player does not have 'quest', this will do nothing // Find the quest in the player's quest list PlayerQuest playerQuest = Quests.SingleOrDefault( pq => pq.Details.ID == quest.ID); if (playerQuest != null) { // if we found the quest in player's quest list, complete it playerQuest.IsCompleted = true; } }
public void UpdateQuest(int questID, int state) { PlayerQuest playerQuest = Quests.SingleOrDefault( pq => pq.ID == questID); if (playerQuest == null) { Quests.Add(new PlayerQuest(World.GetQuest(questID), state, false)); } else { playerQuest.State = state; } }
// mark the specified quest as completed and give the player the rewards public void CompleteQuest(int questID) { PlayerQuest playerQuest = Quests.SingleOrDefault( pq => pq.ID == questID); if (playerQuest != null) { foreach (var item in playerQuest.Data.RewardItems) { AddItemToInventory(item.Data, item.Quantity); } AddCombatExperience(playerQuest.Data.RewardXP); Gold += playerQuest.Data.RewardGold; playerQuest.IsComplete = true; } }
public static Player createplayerformxmlString(string xmlPlayerData) { try { XmlDocument playerdata = new XmlDocument(); playerdata.LoadXml(xmlPlayerData); int HP = Convert.ToInt32(playerdata.SelectSingleNode("/Player/Stats/HP").InnerText); int MaxHP = Convert.ToInt32(playerdata.SelectSingleNode("/Player/Stats/MaxHP").InnerText); int gold = Convert.ToInt32(playerdata.SelectSingleNode("/Player/Stats/Gold").InnerText); int exp = Convert.ToInt32(playerdata.SelectSingleNode("/Player/Stats/EXP").InnerText); Player player = new Player(HP, MaxHP, 100, gold, exp, 10, 10, 10, 10, 10, 5); int currentLocationID = Convert.ToInt32(playerdata.SelectSingleNode("/Player/Stats/CurrentLocation").InnerText); player.CurrentLocation = World.LocationByID(currentLocationID); if (playerdata.SelectSingleNode(".Player/Stats/CurrentWeapon") != null) { int currentWeaponID = Convert.ToInt32(playerdata.SelectSingleNode("/Player/Stats/CurrentWeapon").InnerText); player.CurrentWeapon = (Weapon)World.ItemByID(currentWeaponID); } foreach (XmlNode node in playerdata.SelectNodes("/Player/Inventory/Inventory")) { int id = Convert.ToInt32(node.Attributes["ID"].Value); int quantity = Convert.ToInt32(node.Attributes["Quantity"].Value); for (int i = 0; i < quantity; i++) { player.additemtoinventor(World.ItemByID(id)); } } foreach (XmlNode node in playerdata.SelectNodes("/Player/PlayerQuest/PlayerQuest")) { int id = Convert.ToInt32(node.Attributes["ID"].Value); bool iscompleted = Convert.ToBoolean(node.Attributes["IsCompleted"].Value); PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(id)); playerQuest.IsCompleted = iscompleted; player.Quest.Add(playerQuest); } return(player); } catch { return(CreateDefaultPlayer()); } }
public static Player CreatePlayerFromXMLString(string xmlPlayerData) { try { XmlDocument playerData = new XmlDocument(); playerData.LoadXml(xmlPlayerData); int currentHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentHitPoints").InnerText); int maxHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaximumHitPoints").InnerText); int gold = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Gold").InnerText); int expPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/ExpierencePoints").InnerText); Player player = new Player(currentHitPoints, maxHitPoints, gold, expPoints); player.currentLocation = World.locationByID(Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentLocation").InnerText)); if (playerData.SelectSingleNode("/Player/Stats/CurrentWeapon") != null) { int crntWpnID = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentWeapon").InnerText); player.currentWeapon = (Weapon)World.itemByID(crntWpnID); } foreach (XmlNode node in playerData.SelectNodes("/Player/InventoryItems/InventoryItem")) { int id = Convert.ToInt32(node.Attributes["ID"].Value); int quantity = Convert.ToInt32(node.Attributes["Quantity"].Value); for (int i = 0; i < quantity; i++) { player.addItemtoInventory(World.itemByID(id)); } } foreach (XmlNode node in playerData.SelectNodes("/Player/PlayerQuests/PlayerQuest")) { int id = Convert.ToInt32(node.Attributes["ID"].Value); bool isCompleted = Convert.ToBoolean(node.Attributes["IsCompleted"].Value); PlayerQuest playerQuest = new PlayerQuest(World.questByID(id)); playerQuest.isCompleted = isCompleted; player.quests.Add(playerQuest); } return(player); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception souce: {0}", e); Console.WriteLine("Exception souce: {0}", e); return(Player.createDefaultPlayer());; } }
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); }
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); }
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); }
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); }
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); }
public static Player CreatePlayerFromXmlString(string xmlPlayerData) { try { XmlDocument playerData = new XmlDocument(); playerData.LoadXml(xmlPlayerData); int currentHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentHitPoints").InnerText); int maximumHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaximumHitPoints").InnerText); int gold = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Gold").InnerText); int experiencePoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/ExperiencePoints").InnerText); Player player = new Player(currentHitPoints, maximumHitPoints, gold, experiencePoints); int currentLocationID = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentLocation").InnerText); player.CurrentLocation = World.LocationByID(currentLocationID); foreach (XmlNode node in playerData.SelectNodes("/Player/InventoryItems/InventoryItem")) { int id = Convert.ToInt32(node.Attributes["ID"].Value); int quantity = Convert.ToInt32(node.Attributes["Quantity"].Value); for (int i = 0; i < quantity; i++) { player.AddItemToInventory(World.ItemByID(id)); } } foreach (XmlNode node in playerData.SelectNodes("/Player/PlayerQuests/PlayerQuest")) { int id = Convert.ToInt32(node.Attributes["ID"].Value); bool isCompleted = Convert.ToBoolean(node.Attributes["IsCompleted"].Value); PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(id)); playerQuest.IsCompleted = isCompleted; player.Quests.Add(playerQuest); } return player; } catch { // If there was an error with the XML data, return a default player object return Player.CreateDefaultPlayer(); } }
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 HP = (int)reader["HP"]; int MaxHP = (int)reader["MaxHP"]; int gold = (int)reader["Gold"]; int EXP = (int)reader["EXP"]; int CurrentLocationID = (int)reader["CurrentLocationID"]; player = Player.CreatePlayerfromDatabase(HP, MaxHP, gold, EXP, 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.Quest.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.additemtoinventor(World.ItemByID(inventoryItemID), quantity); } } } return(player); } } #pragma warning disable CS0168 // Variable is declared but never used catch (Exception ex) #pragma warning restore CS0168 // Variable is declared but never used { } return(null); }
public static Player LoadPlayerInformationFromXml(string xmlPlayerData) { Player player; try { XmlDocument playerData = new XmlDocument(); playerData.LoadXml(xmlPlayerData); int currentHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentHitPoints").InnerText); int maximumHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaximumHitPoints").InnerText); int gold = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Gold").InnerText); int experiencePoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/ExperiencePoints").InnerText); int level = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Level").InnerText); int strength = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Strength").InnerText); int dexterity = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Dexterity").InnerText); int intelligent = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Intelligent").InnerText); int currentMana = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentMana").InnerText); int maximumMana = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaximumMana").InnerText); Race race = World.RaceByID(Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Race").InnerText)); Armour armourUsed = null; if (Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/ArmourUsed").InnerText) != 0) { armourUsed = (Armour)World.ItemByID(Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/ArmourUsed").InnerText)); } player = new Player(currentHitPoints, maximumHitPoints, gold, experiencePoints, level, strength, dexterity, intelligent, currentMana, maximumMana, race, armourUsed); player.CurrentLocation = World.LocationByID(Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentLocation").InnerText)); if (Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentWeapon").InnerText) != 0) { player.CurrentWeapon = (Weapon)World.ItemByID(Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentWeapon").InnerText)); } else { player.CurrentWeapon = null; } if (Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentPotion").InnerText) != 0) { player.CurrentPotion = (HealingPotion)World.ItemByID(Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentPotion").InnerText)); } else { player.CurrentPotion = null; } foreach (XmlNode node in playerData.SelectNodes("/Player/InventoryItems/InventoryItem")) { player.AddItem(World.ItemByID(Convert.ToInt32(node.Attributes["ID"].Value)), Convert.ToInt32(node.Attributes["Quantity"].Value)); } foreach (XmlNode node in playerData.SelectNodes("/Player/PlayerQuests/PlayerQuest")) { PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(Convert.ToInt32(node.Attributes["ID"].Value))); playerQuest.IsCompleted = Convert.ToBoolean(node.Attributes["IsCompleted"].Value); player.Quests.Add(playerQuest); } } catch { player = new Player(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, World.RaceByID(World.RACE_ID_HUNMAN)); } return(player); }
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); }
public static Player CreatePlayerFromXmlString(string xmlPlayerData) { try { XmlDocument playerData = new XmlDocument(); // playerData.PreserveWhitespace = true; playerData.LoadXml(xmlPlayerData); int currentHitPoints = Convert.ToInt32( playerData.SelectSingleNode("/Player/Stats/CurrentHitPoints").InnerText); int maximumHitPoints = Convert.ToInt32( playerData.SelectSingleNode("/Player/Stats/MaximumHitPoints").InnerText); int gold = Convert.ToInt32( playerData.SelectSingleNode("/Player/Stats/Gold").InnerText); int experiencePoints = Convert.ToInt32( playerData.SelectSingleNode("/Player/Stats/ExperiencePoints").InnerText); Player player = new Player( currentHitPoints, maximumHitPoints, gold, experiencePoints); int currentLocationID = Convert.ToInt32( playerData.SelectSingleNode("/Player/Stats/CurrentLocation").InnerText); player.CurrentLocation = World.LocationByID(currentLocationID); if (playerData.SelectSingleNode("/Player/Stats/CurrentWeaponID") != null) { int currentWeaponID = Convert.ToInt32(playerData.SelectSingleNode( "/Player/Stats/CurrentWeaponID").InnerText); player.CurrentWeapon = (Weapon)World.ItemByID(currentWeaponID); } if (playerData.SelectSingleNode("/Player/Stats/CurrentPotionID") != null) { int currentPotionID = Convert.ToInt32(playerData.SelectSingleNode( "/Player/Stats/CurrentPotionID").InnerText); player.CurrentPotion = (HealingPotion)World.ItemByID(currentPotionID); } foreach (XmlNode node in playerData.SelectNodes( "/Player/InventoryItems/InventoryItem")) { int itemID = Convert.ToInt32(node.Attributes["ID"].Value); int itemQuantity = Convert.ToInt32(node.Attributes["Quantity"].Value); // refactored from tutorial due to overloading AddItemToInventory with quantity player.AddItemToInventory(World.ItemByID(itemID), itemQuantity); } foreach (XmlNode node in playerData.SelectNodes( "/Player/PlayerQuests/PlayerQuest")) { int questID = Convert.ToInt32(node.Attributes["ID"].Value); bool questIsCompleted = Convert.ToBoolean(node.Attributes["IsCompleted"].Value); PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(questID)); playerQuest.IsCompleted = questIsCompleted; player.Quests.Add(playerQuest); } return(player); } catch { // If there was an error with the XML data, return a default player object return(Player.CreateDefaultPlayer()); } }