コード例 #1
0
        /// <summary>
        /// Handles operation AddInterestArea: Creates a new InterestArea and optionally attaches it to an existing Item.
        /// </summary>
        public OperationResponse OperationAddInterestArea(PeerBase peer, OperationRequest request, SendParameters sendParameters)
        {
            var operation = new AddInterestArea(peer.Protocol, request);

            if (!operation.IsValid)
            {
                return(new OperationResponse(request.OperationCode)
                {
                    ReturnCode = (int)ReturnCode.InvalidOperationParameter, DebugMessage = operation.GetErrorMessage()
                });
            }

            operation.OnStart();
            InterestArea interestArea;

            if (this.TryGetInterestArea(operation.InterestAreaId, out interestArea))
            {
                return(operation.GetOperationResponse((int)ReturnCode.InterestAreaAlreadyExists, "InterestAreaAlreadyExists"));
            }

            interestArea = new ClientInterestArea(this.Peer, operation.InterestAreaId, this.World);
            this.AddInterestArea(interestArea);

            // attach interestArea to item
            if (string.IsNullOrEmpty(operation.ItemId) == false)
            {
                Item item;

                bool actorItem = this.TryGetItem(operation.ItemId, out item);
                if (actorItem)
                {
                    // we are already in the item thread, invoke directly
                    return(ItemOperationAddInterestArea(item, operation, interestArea));
                }
                else
                {
                    if (this.World.ItemCache.TryGetItem(operation.ItemId, out item) == false)
                    {
                        return(operation.GetOperationResponse((int)ReturnCode.ItemNotFound, "ItemNotFound"));
                    }
                    else
                    {
                        // second parameter (peer) allows us to send an error event to the client (in case of an error)
                        item.Fiber.Enqueue(() => this.ExecItemOperation(() => ItemOperationAddInterestArea(item, operation, interestArea), sendParameters));
                        // send response later
                        return(null);
                    }
                }
            }
            else
            {
                // free floating interestArea
                lock (interestArea.SyncRoot)
                {
                    interestArea.Position          = operation.Position;
                    interestArea.ViewDistanceEnter = operation.ViewDistanceEnter;
                    interestArea.ViewDistanceExit  = operation.ViewDistanceExit;
                    interestArea.UpdateInterestManagement();
                }

                return(operation.GetOperationResponse(MethodReturnValue.Ok));
            }
        }
コード例 #2
0
        /// <summary>
        ///   Handles operation <see cref = "AddInterestArea" />: Creates a new <see cref = "InterestArea" /> and optionally attaches it to an existing <see cref = "Item" />.
        /// </summary>
        /// <param name = "peer">
        ///   The client peer.
        /// </param>
        /// <param name = "request">
        ///   The request.
        /// </param>
        /// <param name = "sendParameters">
        ///   The send Parameters.
        /// </param>
        /// <returns>
        ///   An <see cref = "OperationResponse" /> with error code <see cref = "ReturnCode.Ok" /> or <see cref = "ReturnCode.InterestAreaAlreadyExists" />.
        ///   If the <see cref = "InterestArea" /> is supposed to be attached to an <see cref = "Item" /> error code <see cref = "ReturnCode.ItemNotFound" /> could be returned.
        /// </returns>
        /// <remarks>
        ///   The <see cref = "InterestArea" /> is created even if error code <see cref = "ReturnCode.ItemNotFound" /> is returned.
        /// </remarks>
        public override OperationResponse Handle(MmoActor actor, OperationRequest request, SendParameters sendParameters)
        {
            var operation = new AddInterestArea(actor.Peer.Protocol, request);

            if (!operation.IsValid)
            {
                return(new OperationResponse(request.OperationCode)
                {
                    ReturnCode = (int)ReturnCode.InvalidOperationParameter,
                    DebugMessage = operation.GetErrorMessage()
                });
            }

            operation.OnStart();
            InterestArea interestArea;

            if (actor.TryGetInterestArea(operation.InterestAreaId, out interestArea))
            {
                return(operation.GetOperationResponse((int)ReturnCode.InterestAreaAlreadyExists, "InterestAreaAlreadyExists"));
            }

            interestArea = new MmoClientInterestArea(actor.Peer, operation.InterestAreaId, actor.World);
            actor.AddInterestArea(interestArea);

            // attach interestArea to item
            Item item;

            if (operation.ItemType.HasValue && string.IsNullOrEmpty(operation.ItemId) == false)
            {
                IWorld world = actor.World;

                bool actorItem = actor.TryGetItem(operation.ItemType.Value, operation.ItemId, out item);
                if (actorItem == false)
                {
                    if (world.ItemCache.TryGetItem(operation.ItemType.Value, operation.ItemId, out item) == false)
                    {
                        return(operation.GetOperationResponse((int)ReturnCode.ItemNotFound, "ItemNotFound"));
                    }
                }

                if (actorItem)
                {
                    // we are already in the item thread, invoke directly
                    return(ItemOperationAddInterestArea(item, operation, interestArea));
                }

                // second parameter (peer) allows us to send an error event to the client (in case of an error)
                item.Fiber.Enqueue(() => actor.ExecItemOperation(() => ItemOperationAddInterestArea(item, operation, interestArea), sendParameters));

                // send response later
                return(null);
            }

            // free floating interestArea
            if (operation.Position != null)
            {
                lock (interestArea.SyncRoot) {
                    interestArea.Position          = operation.Position.ToVector(true);
                    interestArea.ViewDistanceEnter = operation.ViewDistanceEnter.ToVector(true);
                    interestArea.ViewDistanceExit  = operation.ViewDistanceExit.ToVector(true);
                    interestArea.UpdateInterestManagement();
                }
            }

            return(operation.GetOperationResponse(MethodReturnValue.Ok));
        }