예제 #1
0
        public static void HandleAddCharacter(Int32 id, DataBuffer buffer)
        {
            var legal  = new Regex("^[a-zA-Z0-9]*$");
            var name   = buffer.ReadString().Trim();
            var classn = buffer.ReadInt32();
            var gender = buffer.ReadByte();

            // Make sure this player exists.
            if (!Data.Players.ContainsKey(id))
            {
                return;
            }

            // Make sure the name is at least X characters long.
            if (name.Length < Data.Settings.MinUsernameChar)
            {
                Send.AlertMessage(id, String.Format("Character name must be at least {0} characters in length.", Data.Settings.MinUsernameChar));
                return;
            }

            // Make sure they only entered regular characters.
            if (!legal.IsMatch(name))
            {
                Send.AlertMessage(id, "Invalid name, only letters and numbers are allowed in names.");
                return;
            }

            // Check if the name is already in use or not.
            if (Data.Characters.Contains(name))
            {
                Send.AlertMessage(id, "A character with this name already exists!");
                return;
            }

            // Check if there's a slot left to use!
            var slot = -1;

            for (var i = 0; slot < Data.Players[id].Characters.Length; i++)
            {
                if (Data.Players[id].Characters[i].Name.Length < 1)
                {
                    slot = i;
                    break;
                }
            }
            if (slot < 0)
            {
                Send.AlertMessage(id, "Unable to create character.");
                return;
            }

            // We've come this far, we can add this character!
            Data.Characters.Add(name);
            Data.Players[id].Characters[slot].Name      = name;
            Data.Players[id].Characters[slot].Gender    = (Byte)gender;
            Data.Players[id].Characters[slot].Class     = classn;
            Data.Players[id].Characters[slot].Sprite    = gender == (Int32)Enumerations.Gender.Male ? Data.Classes[classn].MaleSprite : Data.Classes[classn].FemaleSprite;
            Data.Players[id].Characters[slot].Level     = 1;
            Data.Players[id].Characters[slot].Map       = Data.Settings.StartMap;
            Data.Players[id].Characters[slot].X         = Data.Settings.StartX;
            Data.Players[id].Characters[slot].Y         = Data.Settings.StartY;
            Data.Players[id].Characters[slot].Direction = (Int32)Enumerations.Direction.Down;
            for (var i = 0; i < (Int32)Enumerations.Stats.Stat_Count; i++)
            {
                Data.Players[id].Characters[slot].Statistic[i] = Data.Classes[classn].Statistic[i];
            }

            // Save our data!
            Data.SaveCharacterList();
            Data.SavePlayer(id);

            // Notify our user!
            Logger.Write(String.Format("ID: {0} has created a character named '{1}'.", id, name));
            HandleData.HandleUseCharacter(id, slot);
        }
예제 #2
0
 internal static void HandleRequestMap(Int32 id, DataBuffer buffer)
 {
     Send.MapData(id, Data.Players[id].Characters[Data.TempPlayers[id].CurrentCharacter].Map);
 }
예제 #3
0
 internal static void HandleRequestNewCharacter(Int32 id, DataBuffer buffer)
 {
     Send.NewCharacterData(id);
 }
예제 #4
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);
            }
        }