public bool savePlayer(Player p) { if (p == null) return false; try { string username = p.getLoginDetails().getUsername().ToLower(); /* Character saving code goes here */ XmlTextWriter writer = new XmlTextWriter(misc.getServerPath() + @"\accounts\" + username + ".xml", null); writer.Formatting = Formatting.Indented; writer.WriteStartElement("Player"); writer.WriteStartElement("Login"); writer.WriteElementString("Password", p.getLoginDetails().getPassword()); writer.WriteElementString("Rights", p.getRights().ToString()); writer.WriteElementString("BankPin", p.getBank().getBankPin()); writer.WriteEndElement(); writer.WriteStartElement("Position"); writer.WriteElementString("X", p.getLocation().getX().ToString()); writer.WriteElementString("Y", p.getLocation().getY().ToString()); writer.WriteElementString("Z", p.getLocation().getZ().ToString()); writer.WriteEndElement(); writer.WriteStartElement("Settings"); writer.WriteElementString("RunEnergy", p.getRunEnergy().ToString()); writer.WriteStartElement("PrivacySettings"); writer.WriteElementString("Public", p.getFriends().getPrivacyOption(0).ToString()); writer.WriteElementString("Private", p.getFriends().getPrivacyOption(1).ToString()); writer.WriteElementString("Trade", p.getFriends().getPrivacyOption(2).ToString()); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteStartElement("Friends"); foreach(long friend in p.getFriends().getFriendsList()) writer.WriteElementString("Friend", friend.ToString()); writer.WriteEndElement(); writer.WriteStartElement("Ignores"); foreach (long ignore in p.getFriends().getIgnoresList()) writer.WriteElementString("Ignore", ignore.ToString()); writer.WriteEndElement(); writer.WriteStartElement("Stats"); foreach (Skills.SKILL skill in Enum.GetValues(typeof(Skills.SKILL))) { writer.WriteStartElement(skill.ToString()); //skill name. writer.WriteElementString("CurrentLevel", p.getSkills().getCurLevel(skill).ToString()); writer.WriteElementString("XP", p.getSkills().getXp(skill).ToString()); writer.WriteEndElement(); } writer.WriteEndElement(); Item item; writer.WriteStartElement("EquipmentItems"); foreach (ItemData.EQUIP equip in Enum.GetValues(typeof(ItemData.EQUIP))) { if (equip == ItemData.EQUIP.NOTHING) continue; item = p.getEquipment().getSlot(equip); if (item.getItemId() == -1) continue; //empty slot. writer.WriteStartElement(equip.ToString()); writer.WriteElementString("Id", item.getItemId().ToString()); writer.WriteElementString("Amount", item.getItemAmount().ToString()); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("InventoryItems"); for (int i = 0; i < Inventory.MAX_INVENTORY_SLOTS; i++) { item = p.getInventory().getSlot(i); if (item.getItemId() == -1) continue; //empty slot. writer.WriteStartElement("Item"); writer.WriteElementString("Slot", i.ToString()); writer.WriteElementString("Id", item.getItemId().ToString()); writer.WriteElementString("Amount", item.getItemAmount().ToString()); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("BankItems"); for (int i = 0; i < Bank.MAX_BANK_SLOTS; i++) { item = p.getBank().getSlot(i); if (item.getItemId() == -1) continue; //empty slot. writer.WriteStartElement("Item"); writer.WriteElementString("Slot", i.ToString()); writer.WriteElementString("Id", item.getItemId().ToString()); writer.WriteElementString("Amount", item.getItemAmount().ToString()); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); //Write the XML to file and close the writer writer.Close(); } catch (Exception) { return false; } return true; }
public void execute(Player player, string[] arguments) { if (!player.inCombat()) player.getBank().openBank(); }
public ReturnCode loadPlayer(Connection connection) { if (connection == null) return ReturnCode.COULD_NOT_COMPLETE; LoginDetails loginDetails = connection.getLoginDetails(); if (loginDetails == null || loginDetails.getUsername() == "" || loginDetails.getLongName() == 0) return ReturnCode.INVALID_PASSWORD;//ReturnCode.INVALID_PASSWORD; foreach (char c in loginDetails.getUsername().ToCharArray()) { if (!char.IsLetterOrDigit(c) && !char.IsWhiteSpace(c)) return ReturnCode.INVALID_PASSWORD; } Player createdPlayer = new Player(connection); connection.setPlayer(createdPlayer); //player finally created. createdPlayer.setLoginDetails(loginDetails); if (!File.Exists(misc.getServerPath() + @"\accounts\" + loginDetails.getUsername() + ".xml")) { createdPlayer.setRights(2); //all new users admins atm (change later). createdPlayer.setLocation(new Location(2323, 3174, 0)); return ReturnCode.LOGIN_OK; //new user. } //Yeah reading XML files is a bit h**o. try { int temp; long lTemp; string username = createdPlayer.getLoginDetails().getUsername().ToLower(); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(misc.getServerPath() + @"\accounts\" + username + ".xml"); XmlNode xmlNode = xmlDocument.SelectSingleNode("/Player/Login/Password"); if (xmlNode == null) return ReturnCode.INVALID_PASSWORD; //no password node. if (createdPlayer.getLoginDetails().getPassword() != xmlNode.InnerText) return ReturnCode.INVALID_PASSWORD; XmlNode loginElement = xmlDocument.SelectSingleNode("/Player/Login"); if(loginElement != null && loginElement.HasChildNodes) { XmlNodeList childs = loginElement.ChildNodes; foreach (XmlElement element in childs) { switch (element.Name) { case "Rights": if (!int.TryParse(element.InnerText, out temp)) temp = 0; createdPlayer.setRights(temp); break; case "BankPin": if (element.InnerText == "") continue; createdPlayer.getBank().setBankPin(element.InnerText); break; } } } loginElement = xmlDocument.SelectSingleNode("/Player/Position"); if (loginElement != null && loginElement.HasChildNodes) { XmlNodeList childs = loginElement.ChildNodes; Location location = new Location(); foreach (XmlElement element in childs) { switch (element.Name) { case "X": if (!int.TryParse(element.InnerText, out temp)) { createdPlayer.setLocation(Constants.HOME_SPAWN_LOCATION); break; } location.setX(temp); break; case "Y": if (!int.TryParse(element.InnerText, out temp)) { createdPlayer.setLocation(Constants.HOME_SPAWN_LOCATION); break; } location.setY(temp); break; case "Z": if (!int.TryParse(element.InnerText, out temp)) temp = 0; location.setZ(temp); break; } } createdPlayer.setLocation(location); if (Location.atDuelArena(createdPlayer.getLocation())) DuelSession.teleportDuelArenaHome(createdPlayer); } xmlNode = xmlDocument.SelectSingleNode("/Player/Settings/RunEnergy"); if (xmlNode == null) { temp = 100; } else { if (!int.TryParse(xmlNode.InnerText, out temp)) temp = 100; } createdPlayer.setRunEnergyLoad(temp); loginElement = xmlDocument.SelectSingleNode("/Player/Settings/PrivacySettings"); if (loginElement != null && loginElement.HasChildNodes) { XmlNodeList childs = loginElement.ChildNodes; Friends.STATUS publicStatus = Friends.STATUS.ON, privateStatus = Friends.STATUS.ON, tradeStatus = Friends.STATUS.ON; foreach (XmlElement element in childs) { switch (element.Name) { case "Public": publicStatus = (Friends.STATUS)Enum.Parse(typeof(Friends.STATUS), element.InnerText, true); break; case "Private": privateStatus = (Friends.STATUS)Enum.Parse(typeof(Friends.STATUS), element.InnerText, true); break; case "Trade": tradeStatus = (Friends.STATUS) Enum.Parse(typeof(Friends.STATUS), element.InnerText, true); break; } } createdPlayer.getFriends().setPrivacyOptions(publicStatus, privateStatus, tradeStatus); } loginElement = xmlDocument.SelectSingleNode("/Player/Friends"); if (loginElement != null && loginElement.HasChildNodes) { XmlNodeList childs = loginElement.ChildNodes; //number of Friends foreach (XmlElement element in childs) { if(element.Name == "Friend") { if(long.TryParse(element.InnerText, out lTemp)) createdPlayer.getFriends().getFriendsList().Add(lTemp); } } } loginElement = xmlDocument.SelectSingleNode("/Player/Ignores"); if (loginElement != null && loginElement.HasChildNodes) { XmlNodeList childs = loginElement.ChildNodes; //number of Friends foreach (XmlElement element in childs) { if (element.Name == "Ignore") { if (long.TryParse(element.InnerText, out lTemp)) createdPlayer.getFriends().getIgnoresList().Add(lTemp); } } } loginElement = xmlDocument.SelectSingleNode("/Player/Stats"); if (loginElement != null && loginElement.HasChildNodes) { XmlNode skillNode; foreach (Skills.SKILL skill in Enum.GetValues(typeof(Skills.SKILL))) { skillNode = loginElement.SelectSingleNode("/Player/Stats/" + skill.ToString()); foreach (XmlElement element in skillNode.ChildNodes) { //CurrentLevel/XP switch (element.Name) { case "CurrentLevel": if (!int.TryParse(element.InnerText, out temp)) { if (skill != Skills.SKILL.HITPOINTS) createdPlayer.getSkills().setCurLevel(skill, 1); else createdPlayer.getSkills().setCurLevel(skill, 10); break; } createdPlayer.getSkills().setCurLevel(skill, temp); break; case "XP": if (!int.TryParse(element.InnerText, out temp)) { if (skill != Skills.SKILL.HITPOINTS) createdPlayer.getSkills().setXp(skill, 0); else createdPlayer.getSkills().setXp(skill, 1184); break; } createdPlayer.getSkills().setXp(skill, temp); break; } } } } loginElement = xmlDocument.SelectSingleNode("/Player/EquipmentItems"); if (loginElement != null && loginElement.HasChildNodes) { XmlNode skillNode; foreach (ItemData.EQUIP equip in Enum.GetValues(typeof(ItemData.EQUIP))) { if (equip == ItemData.EQUIP.NOTHING) continue; skillNode = loginElement.SelectSingleNode("/Player/EquipmentItems/" + equip.ToString()); if (skillNode == null) continue; int id = -1, amount = 0; foreach (XmlElement element in skillNode.ChildNodes) { switch (element.Name) { case "Id": if (!int.TryParse(element.InnerText, out id)) id = -1; break; case "Amount": if (!int.TryParse(element.InnerText, out amount)) amount = 0; break; } } if (id != -1) { createdPlayer.getEquipment().getEquipment()[(int)equip].setItemId(id); createdPlayer.getEquipment().getEquipment()[(int)equip].setItemAmount(amount); } } } loginElement = xmlDocument.SelectSingleNode("/Player/InventoryItems"); if (loginElement != null && loginElement.HasChildNodes) { int slot = -1, id = 0, amount = 0; foreach (XmlElement itemElement in loginElement.ChildNodes) //each item. { foreach (XmlElement itemDef in itemElement.ChildNodes) //each item. { switch (itemDef.Name) { case "Slot": if (!int.TryParse(itemDef.InnerText, out slot)) slot = -1; if (slot < 0 || slot > Inventory.MAX_INVENTORY_SLOTS) slot = -1; break; case "Id": if (!int.TryParse(itemDef.InnerText, out id)) slot = -1; break; case "Amount": if (!int.TryParse(itemDef.InnerText, out amount)) slot = -1; break; } } if (slot != -1) { createdPlayer.getInventory().getItems()[slot].setItemId(id); createdPlayer.getInventory().getItems()[slot].setItemAmount(amount); } } } loginElement = xmlDocument.SelectSingleNode("/Player/BankItems"); if (loginElement != null && loginElement.HasChildNodes) { int slot = -1, id = 0, amount = 0; foreach (XmlElement itemElement in loginElement.ChildNodes) //each item. { foreach (XmlElement itemDef in itemElement.ChildNodes) //each item. { switch (itemDef.Name) { case "Slot": if (!int.TryParse(itemDef.InnerText, out slot)) slot = -1; if (slot < 0 || slot > Inventory.MAX_INVENTORY_SLOTS) slot = -1; break; case "Id": if (!int.TryParse(itemDef.InnerText, out id)) slot = -1; break; case "Amount": if (!int.TryParse(itemDef.InnerText, out amount)) slot = -1; break; } } if (slot != -1) { createdPlayer.getBank().getBank()[slot].setItemId(id); createdPlayer.getBank().getBank()[slot].setItemAmount(amount); } } } return ReturnCode.LOGIN_OK; //new user. } catch (Exception e) { misc.WriteError(e.Message); return ReturnCode.COULD_NOT_COMPLETE; } }