예제 #1
0
        public static void HandleUseCharacter(Int32 id, Int32 slot)
        {
            // Make sure the user exists.
            if (!Data.Players.ContainsKey(id))
            {
                return;
            }

            // If the user isn't already in-game, send them all the appropriate data!
            if (!Data.TempPlayers[id].InGame)
            {
                // Player is using this character!
                Data.TempPlayers[id].CurrentCharacter = slot;

                // Send our client the go-ahead!
                Send.LoginOK(id);

                // Send our clients their game data!
                // This includes everyone on the same map as our lovely friend.
                Send.PlayerData(id, id);
                for (var i = 0; i < Data.Players.Count; i++)
                {
                    var key = Data.Players.ElementAt(i).Key;
                    if (Data.TempPlayers[key].InGame && Data.Players[key].Characters[Data.TempPlayers[key].CurrentCharacter].Map == Data.Players[id].Characters[Data.TempPlayers[id].CurrentCharacter].Map)
                    {
                        Send.PlayerData(key, id);
                        Send.PlayerData(id, key);
                        Send.PlayerMoving(id, key);
                    }
                }

                // Now send our friendly friend our map!
                Send.LoadMap(id, Data.Players[id].Characters[Data.TempPlayers[id].CurrentCharacter].Map);
            }
        }
예제 #2
0
        public static void HandleNewAccount(Int32 id, DataBuffer buffer)
        {
            // Handles a user's request to register an account.
            var username = buffer.ReadString().Trim();
            var password = buffer.ReadString().Trim();
            var legal    = new Regex("^[a-zA-Z0-9]*$");

            // Check if the user isn't sending too long/short data.
            if (username.Trim().Length < Data.Settings.MinUsernameChar || username.Trim().Length > Data.Settings.MaxUsernameChar || password.Trim().Length < Data.Settings.MinPasswordChar || password.Trim().Length > Data.Settings.MaxPasswordChar)
            {
                Send.AlertMessage(id, String.Format("Your username must be between {0} and {1} characters long. Your password must be between {2} and {3} characters long.", Data.Settings.MinUsernameChar, Data.Settings.MaxUsernameChar, Data.Settings.MinPasswordChar, Data.Settings.MaxPasswordChar));
                return;
            }

            // Check if the user isn't sending any unallowed characters.
            if (!legal.IsMatch(username))
            {
                Send.AlertMessage(id, "Invalid username, only letters and numbers are allowed in names.");
                return;
            }

            // Check if this account already exists.
            if (File.Exists(String.Format("{0}data files\\accounts\\{1}.xml", Data.AppPath, username.ToLower())))
            {
                Send.AlertMessage(id, "Sorry, that account name is already taken!");
                return;
            }

            // Now we can finaly start creating the account!
            Data.Players[id].Username = username;
            Data.Players[id].SetPassword(password);     // Always use this over setting the password directly! This hashes and salts it.

            // Save the player!
            Data.SavePlayer(id);
            Logger.Write(String.Format("ID: {0} has created a new account named '{1}'.", id, username));

            // Send them our OK!
            Send.LoginOK(id);

            // Send our player the data required to create a new character!
            Send.NewCharacterData(id);
        }
예제 #3
0
        public static void HandleLogin(Int32 id, DataBuffer buffer)
        {
            // Handles a user's request to login.
            var username = buffer.ReadString().Trim();
            var password = buffer.ReadString().Trim();

            // Check if the user isn't sending too long/short data.
            if (username.Trim().Length < Data.Settings.MinUsernameChar || username.Trim().Length > Data.Settings.MaxUsernameChar || password.Trim().Length < Data.Settings.MinPasswordChar || password.Trim().Length > Data.Settings.MaxPasswordChar)
            {
                Send.AlertMessage(id, String.Format("Your username must be between {0} and {1} characters long. Your password must be between {2} and {3} characters long.", Data.Settings.MinUsernameChar, Data.Settings.MaxUsernameChar, Data.Settings.MinPasswordChar, Data.Settings.MaxPasswordChar));
                return;
            }

            // Check if this account exists.
            if (!File.Exists(String.Format("{0}data files\\accounts\\{1}.xml", Data.AppPath, username.ToLower())))
            {
                Send.AlertMessage(id, "Invalid Username/Password!");
                return;
            }

            // Load our player!
            Data.LoadPlayer(id, username.ToLower());

            // Compare their passwords!
            if (!Data.Players[id].ComparePassword(password))
            {
                Send.AlertMessage(id, "Invalid Username/Password!");
                Data.Players[id] = new Extensions.Database.Player();
                return;
            }

            // Send our OK.
            Logger.Write(String.Format("ID: {0} has logged in as {1}", id, Data.Players[id].Username));
            Send.LoginOK(id);

            // Disconnect anyone else logged into this account.
            var oldclient = 0;

            for (var i = 0; i < Data.Players.Count; i++)
            {
                var key = Data.Players.ElementAt(i).Key;
                if (Data.Players[key].Username.ToLower().Equals(username.ToLower()) && key != id)
                {
                    oldclient = key;
                }
            }
            if (oldclient != 0)
            {
                Send.AlertMessage(oldclient, "Someone else has logged onto your account!");
                Logger.Write(String.Format("ID: {0} conflicts with a login at ID: {1}, Disconnecting ID: {1}", id, oldclient));
                Data.SavePlayer(oldclient);
                // NOTE: the user is still logged on until they get this message, we're saving their data on purpose.
                // Their data will now be saved twice, but we can safely load it.
                Data.LoadPlayer(id, username.ToLower());
                // Now remove the old player from the world by force.
                Program.Server.DisconnectClient(oldclient);
            }

            // Check if they have at least one character.
            var haschars = false;

            foreach (var chr in Data.Players[id].Characters)
            {
                if (chr.Name.Length > 0)
                {
                    haschars = true;
                }
            }

            // If we have characters, show character select.
            // Otherwise force the user to make a new character.
            if (haschars)
            {
                Send.SelectCharacterData(id);
            }
            else
            {
                Send.NewCharacterData(id);
            }
        }