Example #1
0
        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;
            }
        }