Exemple #1
0
        protected override void OnOpen()
        {
            // start with the least permissions to be changed later.
            Permissions = UserPermissionLevel.VIEWER;

            ConnectionOpenedOrClosed(ConnectionAction.OPENED, this);
        }
Exemple #2
0
        public static string PermissionsChangedNotification(UserPermissionLevel level)
        {
            var res = new
            {
                CommandType     = CommandType.PERMISSIONSCHANGEDNOTIFICATION.Value,
                PermissionLevel = level
            };

            return(JsonConvert.SerializeObject(res));
        }
        /// <summary>
        /// Obtains information about a player.
        /// If the information could not be retrieved, false is returned.
        /// </summary>
        /// <param name="id">The ID of the player to obtain information about.</param>
        /// <returns>True if the player information was set successfully, false if not.</returns>
        public bool Load(long id)
        {
            var schema = (from p in GameServer.Instance.DbManager.Players where p.ID == id select p).SingleOrDefault();
            if (schema != null)
            {
                try
                {
                    this.UserName = schema.UserName;
                    this.PasswordHash = schema.Password;
                    this.UserLevel = (UserPermissionLevel)schema.UserLevel;
                    this.DisableReason = (DisableReason)schema.DisableReason;

                    // Do not use the property, it sets the HadLoggedIn property to true and we don't want to do that yet
                    this.loggedIn = schema.LoggedIn;

                    this.LastLogOnAttempt = schema.LastLogOnAttempt;
                    this.LastLogOnSuccess = schema.LastLogOnSuccess;
                    this.LastLogOnAttemptIP = schema.LastLogOnAttemptIP;
                    this.LastLogOnSuccessIP = schema.LastLogOnSuccessIP;
                    this.AttemptedLogOnCount = schema.LogOnAttempts;
                    this.Position = new Vector3(schema.PositionX, schema.PositionY, schema.PositionZ);
                    this.RotationDegrees = new Vector3(schema.RotationX, schema.RotationY, schema.RotationZ);

                    /*ReadOnlyCollection<InstanceItemsSchema> itemSchema = GameServer.Instance.DbManager.Tables.InstanceItemsTable.SelectAllFor(id);
                    if (itemSchema != null)
                    {
                        this.Inventory = new PlayerInventory();

                        // TODO: We must load inventory using our special system
                        this.Inventory = null;
                    }
                    else
                    {
                        return false;
                    }*/
                }
                catch (KeyNotFoundException knf)
                {
                    Console.WriteLine(knf);
                    return false;
                }
                catch (FormatException fe)
                {
                    Console.WriteLine(fe);
                    return false;
                }

                // This we do last just to show that if anything went wrong,
                // we don't have a valid ID so it may be easier to catch errors
                this.Id = id;

                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Logs into a player account. If the player is already signed in, this simply returns true.
        /// </summary>
        /// <param name="userName">The user name of the client player.</param>
        /// <param name="password">The password of the client player.</param>
        /// <returns>Whether the login was successful.</returns>
        public bool ClientLogOn(string userName, string password)
        {
            lock (this)
            {
                bool loadedData = this.Load(userName);

                // Only try to login if the player hasn't already
                if (!this.LoggedIn)
                {
                    // If a player data file was not successfully loaded, destroy the connection
                    if (!loadedData)
                    {
                        PacketSender.Send(AuthenticationResultPacket.FailedInvalidUserName, this);
                        this.Dispose();
                        return false;
                    }

                    // Get the MD5 of the player's password
                    string passwordHash = SimpleCryptography.MD5(password);

                    // If X minutes have passed, reset the attempted login count
                    if ((DateTime.UtcNow.Ticks - this.LastLogOnAttempt) > PlayerHandler.LoginWaitTime)
                    {
                        this.AttemptedLogOnCount = 0;
                    }

                    // Set last login attempt time to now
                    this.LastLogOnAttempt = DateTime.UtcNow.Ticks;
                    this.LastLogOnAttemptIP = NetworkUtilities.GetRemoteIP(this.Connection.Connection.Client);

                    // If there's not been too many wrong logins in a row and player is not banned
                    if (this.AttemptedLogOnCount < PlayerHandler.MaxAttemptsIp && this.UserLevel != UserPermissionLevel.Disabled)
                    {
                        // If the password supplied by the login is equal to the password stored in the database, continue
                        if (passwordHash.ToUpperInvariant() == this.PasswordHash.ToUpperInvariant())
                        {
                            // Set the player logged in to true, player is now logged in!
                            this.LoggedIn = true;

                            // Reset the attempted logins since player is now authenticated
                            if ((DateTime.UtcNow.Ticks - this.LastLogOnAttempt) > PlayerHandler.LoginWaitTime)
                            {
                                this.AttemptedLogOnCount = 0;
                            }

                            // Set the login success time and IP
                            this.LastLogOnSuccess = DateTime.UtcNow.Ticks;
                            this.LastLogOnSuccessIP = NetworkUtilities.GetRemoteIP(this.Connection.Connection.Client);

                            // Tell the client his or her login was successful and tell other users he or she logged in
                            PacketSender.Send(AuthenticationResultPacket.Success, this);

                            InventoryPacket inv = new InventoryPacket(this.Inventory);
                            OrientationPacket ori = new OrientationPacket(this);
                            PacketSender.Send(new IntroductionPacket(inv, ori, this.server.Clients.ToArray()), this);

                            PacketSender.SendToAll(new PlayerLoggedPacket(this, true));
                        }
                        else
                        {
                            // Wrong password, increment login attempts and send message informing the
                            // player that an incorrect password has been supplied, then terminate the connection
                            this.AttemptedLogOnCount++;
                            PacketSender.Send(AuthenticationResultPacket.FailedInvalidPassword, this);
                            this.Dispose();
                            return false;
                        }
                    }
                    else
                    {
                        // Too many wrong logins or player banned... increment attempted logins
                        this.AttemptedLogOnCount++;

                        if (this.AttemptedLogOnCount >= PlayerHandler.MaxAttemptsAll)
                        {
                            // If the player attempted to login far too many times set their user level
                            // to banned (if it wasn't already), send message telling player that their
                            // account has been disabled, and destroy the connection
                            this.UserLevel = UserPermissionLevel.Disabled;
                            this.DisableReason = DisableReason.ExcessLogins;
                            PacketSender.Send(AuthenticationResultPacket.FailedAccountDisabled, this);
                            this.Dispose();
                            return false;
                        }
                        else if (this.UserLevel == UserPermissionLevel.Disabled)
                        {
                            // The player is banned - deny 'em
                            PacketSender.Send(AuthenticationResultPacket.FailedAccountDisabled, this);
                            this.Dispose();
                            return false;
                        }
                        else if (this.AttemptedLogOnCount >= PlayerHandler.MaxAttemptsIp)
                        {
                            // Otherwise, if they only attempted to login a few too many times,
                            // tell them that they have tried to login too many times and will
                            // have to wait a while before trying again
                            PacketSender.Send(AuthenticationResultPacket.FailedTooManyIncorrectLogins, this);
                            this.Dispose();
                            return false;
                        }
                    }
                }
                else
                {
                    // Tell the client he's already logged in
                    PacketSender.Send(AuthenticationResultPacket.AlreadyLoggedIn, this);
                }

                return this.LoggedIn;
            }
        }