Пример #1
0
        void RecvUseWorld(IIPSocket conn, BitStream r)
        {
            var useEntityIndex = r.ReadMapEntityIndex();

            // Get the map and user
            User user;
            Map  map;

            if (!TryGetMap(conn, out user, out map))
            {
                return;
            }

            if (user.IsPeerTrading)
            {
                return;
            }

            if (!user.IsAlive)
            {
                const string errmsg = "User `{0}` tried to use world entity while dead.";
                if (log.IsInfoEnabled)
                {
                    log.InfoFormat(errmsg, user);
                }
                return;
            }

            // Grab the DynamicEntity to use
            var useEntity = map.GetDynamicEntity(useEntityIndex);

            if (useEntity == null)
            {
                const string errmsg = "UseEntity received but usedEntityIndex `{0}` is not a valid DynamicEntity.";
                Debug.Fail(string.Format(errmsg, useEntityIndex));
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, useEntityIndex);
                }
                return;
            }

            // Ensure the used DynamicEntity is even usable
            var asUsable = useEntity as IUsableEntity;

            if (asUsable == null)
            {
                const string errmsg =
                    "UseEntity received but useByIndex `{0}` refers to DynamicEntity `{1}` which does " +
                    "not implement IUsableEntity.";
                Debug.Fail(string.Format(errmsg, useEntityIndex, useEntity));
                if (log.IsErrorEnabled)
                {
                    log.WarnFormat(errmsg, useEntityIndex, useEntity);
                }
                return;
            }

            // Use it
            if (asUsable.Use(user))
            {
                // Notify everyone in the map it was used
                if (asUsable.NotifyClientsOfUsage)
                {
                    using (var pw = ServerPacket.UseEntity(useEntity.MapEntityIndex, user.MapEntityIndex))
                    {
                        map.Send(pw, ServerMessageType.Map);
                    }
                }
            }
        }