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()); } }
private void PlayerItems_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 4) { var itemID = PlayerItems.Rows[e.RowIndex].Cells[0].Value; _items itembeingsold = World.ItemByID(Convert.ToInt32(itemID)); if (itembeingsold.Price == World.UNSELLABLE_ITEM_PRICE) { MessageBox.Show("You cannot sell the " + itembeingsold.Name); } else { _currentPlayer.RemoveItemFromInventory(itembeingsold); _currentPlayer.Gold += itembeingsold.Price; } } }
private void TraderItems_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 3) { var itemID = TraderItems.Rows[e.RowIndex].Cells[0].Value; _items itembeingbought = World.ItemByID(Convert.ToInt32(itemID)); if (_currentPlayer.Gold >= itembeingbought.Price) { _currentPlayer.additemtoinventor(itembeingbought); _currentPlayer.Gold -= itembeingbought.Price; } else { MessageBox.Show("You do not have enough gold to buy the " + itembeingbought.Name); } } }
/// <summary> /// Creates a Default Player at the Default Location /// </summary> /// <returns></returns> public static Player CreateDefaultPlayer() { int DEFAULT_HIT_POINTS = 10; int DEFAULT_GOLD = 20; int DEFAULT_EXPERIENCE_POINTS = 0; Player player = new Player( DEFAULT_HIT_POINTS, DEFAULT_HIT_POINTS, DEFAULT_GOLD, DEFAULT_EXPERIENCE_POINTS); player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_RUSTY_SWORD), 1)); // add a club as well to test out the weapon combobox logic player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_CLUB), 1)); player.CurrentLocation = World.LocationByID(World.LOCATION_ID_HOME); return(player); }
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 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/LocationsVisited/LocationVisited")) { int id = Convert.ToInt32(node.Attributes["ID"].Value); player.LocationsVisited.Add(id); } 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(CreateDefaultPlayer()); } }
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); }
private static void PopulateLocations() { // Create each location Location home = new Location(LOCATION_ID_HOME, "Home", "Your house. You really need to clean up the place."); Location townSquare = new Location(LOCATION_ID_TOWN_SQUARE, "Town square", "You see a fountain."); Location alchemistHut = new Location(LOCATION_ID_ALCHEMIST_HUT, "Alchemist's hut", "There are many strange plants on the shelves."); alchemistHut.QuestAvailableHere = QuestByID(QUEST_ID_CLEAR_ALCHEMIST_GARDEN); Location alchemistsGarden = new Location(LOCATION_ID_ALCHEMISTS_GARDEN, "Alchemist's garden", "Many plants are growing here."); alchemistsGarden.MonsterLivingHere = MonsterByID(MONSTER_ID_RAT); Location farmhouse = new Location(LOCATION_ID_FARMHOUSE, "Farmhouse", "There is a small farmhouse, with a farmer in front."); farmhouse.QuestAvailableHere = QuestByID(QUEST_ID_CLEAR_FARMERS_FIELD); Location farmersField = new Location(LOCATION_ID_FARM_FIELD, "Farmer's field", "You see rows of vegetables growing here."); farmersField.MonsterLivingHere = MonsterByID(MONSTER_ID_SNAKE); Location guardPost = new Location(LOCATION_ID_GUARD_POST, "Guard post", "There is a large, tough-looking guard here.", ItemByID(ITEM_ID_ADVENTURER_PASS)); Location bridge = new Location(LOCATION_ID_BRIDGE, "Bridge", "A stone bridge crosses a wide river."); Location spiderField = new Location(LOCATION_ID_SPIDER_FIELD, "Forest", "You see spider webs covering covering the trees in this forest."); spiderField.MonsterLivingHere = MonsterByID(MONSTER_ID_GIANT_SPIDER); List <Item> shop1Items = new List <Item>(); shop1Items.Add(World.ItemByID(ITEM_ID_HEALING_POTION)); shop1Items.Add(World.ItemByID(ITEM_ID_RUSTY_SWORD)); shop1Items.Add(World.ItemByID(ITEM_ID_CLUB)); shop1Items.Add(World.ItemByID(ITEM_ID_DULL_SWORD)); shop1Items.Add(World.ItemByID(ITEM_ID_REGULAR_SWORD)); shop1Items.Add(World.ItemByID(ITEM_ID_BALSA_WOOD_SHIELD)); shop1Items.Add(World.ItemByID(ITEM_ID_FABRIC_HELMET)); shop1Items.Add(World.ItemByID(ITEM_ID_FABRIC_BREASTPLATE)); shop1Items.Add(World.ItemByID(ITEM_ID_FABRIC_PANTS)); Location shop1 = new Shop(LOCATION_ID_SHOP, "Shop", "You see a man behind a dusty counter, a display set in front of him with prices to each item.", shop1Items); // Link the locations together home.LocationToNorth = townSquare; townSquare.LocationToNorth = alchemistHut; townSquare.LocationToSouth = home; townSquare.LocationToEast = guardPost; townSquare.LocationToWest = farmhouse; farmhouse.LocationToEast = townSquare; farmhouse.LocationToWest = farmersField; farmersField.LocationToEast = farmhouse; alchemistHut.LocationToSouth = townSquare; alchemistHut.LocationToNorth = alchemistsGarden; alchemistsGarden.LocationToSouth = alchemistHut; guardPost.LocationToEast = bridge; guardPost.LocationToWest = townSquare; bridge.LocationToWest = guardPost; bridge.LocationToEast = spiderField; bridge.LocationToSouth = shop1; spiderField.LocationToWest = bridge; shop1.LocationToNorth = bridge; // Add the locations to the static list Locations.Add(home); Locations.Add(townSquare); Locations.Add(guardPost); Locations.Add(alchemistHut); Locations.Add(alchemistsGarden); Locations.Add(farmhouse); Locations.Add(farmersField); Locations.Add(bridge); Locations.Add(spiderField); Locations.Add(shop1); }
public static Player CreatePlayerFromXmlString(string PLAYER_DATA_FILE_NAME) { try { XmlDocument playerData = new XmlDocument(); playerData.LoadXml(PLAYER_DATA_FILE_NAME); string playerName = playerData.SelectSingleNode("/Player/Stats/Name").InnerText; // Console.WriteLine("Got name: " + playerName); string PC = playerData.SelectSingleNode("/Player/Stats/Class").InnerText; // Console.WriteLine("Got class: " + PC); string PR = playerData.SelectSingleNode("/Player/Stats/Race").InnerText; //Console.WriteLine("Got race: " + PR); int currentHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentHitPoints").InnerText); // Console.WriteLine("Got current hit points: " + currentHitPoints.ToString()); int maximumHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaximumHitPoints").InnerText); // Console.WriteLine("Got max hit points: " + maximumHitPoints.ToString()); int gold = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Gold").InnerText); // Console.WriteLine("Got gold: " + gold.ToString()); int experiencePoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/ExperiencePoints").InnerText); //Console.WriteLine("Got experience: " + experiencePoints.ToString()); Factions factionString = (Factions)Enum.Parse(typeof(Factions), (playerData.SelectSingleNode("/Player/Stats/Faction").InnerText), true); //Console.WriteLine("Got Faction: " + factionString.ToString()); int alignment = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Alignment").InnerText); //Console.WriteLine("Got Alignment: " + alignment.ToString()); int equiptString = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentWeapon").InnerText); IWeapon equipt = World.WeaponByID(equiptString); // Console.WriteLine("Got equipt weapon: " + equipt.Name.ToString()); Player player = new Player(playerName, PC, PR, gold, currentHitPoints, maximumHitPoints, (Weapon)equipt, false, true, factionString, alignment); 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.Equipt = (Weapon)World.WeaponByID(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); if (id > 100 && id <= 200) { for (int i = 0; i < quantity; i++) { player.AddItemToInventory((Weapon)World.WeaponByID(id)); } } else { for (int i = 0; i < quantity; i++) { player.AddItemToInventory(World.ItemByID(id)); } } } return(player); } catch (Exception ex) { // If there was an error with the XML data, return a default player object Console.WriteLine(ex.ToString()); //return CreateDefaultPlayer(); 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 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 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()); } }
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 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); }