Esempio n. 1
0
        /// <summary>
        ///   Expects operation <see cref = "EnterWorld" /> and creates a new <see cref = "MmoActor" /> with a new <see cref = "MmoItem" /> as avatar and a new <see cref = "MmoClientInterestArea" />.
        ///   The <see cref = "MmoActor" /> becomes the new <see cref = "Peer.CurrentOperationHandler">operation handler</see>.
        ///   If another <see cref = "MmoActor" /> with the same name exists he is disconnected.
        ///   An <see cref = "OperationResponse" /> with error code <see cref = "ReturnCode.Ok" /> is published on success.
        /// </summary>
        /// <param name = "peer">
        ///   The client peer.
        /// </param>
        /// <param name = "request">
        ///   The request.
        /// </param>
        /// <param name = "sendParameters">
        ///   The send Parameters.
        /// </param>
        /// <returns>
        ///   Null or an <see cref = "OperationResponse" /> with error code <see cref = "ReturnCode.WorldNotFound" />.
        /// </returns>
        public OperationResponse OperationEnterWorld(PeerBase peer, OperationRequest request, SendParameters sendParameters)
        {
            var operation = new EnterWorld(peer.Protocol, request);

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

            MmoWorld world;

            if (MmoWorldCache.Instance.TryGet(operation.WorldName, out world) == false)
            {
                return(operation.GetOperationResponse((int)ReturnCode.WorldNotFound, "WorldNotFound"));
            }

            var interestArea = new MmoClientInterestArea(peer, operation.InterestAreaId, world)
            {
                ViewDistanceEnter = operation.ViewDistanceEnter.ToVector(), ViewDistanceExit = operation.ViewDistanceExit.ToVector()
            };

            var actor  = new MmoActor(peer, world, interestArea);
            var avatar = new MmoItem(world, operation.Position, operation.Rotation, operation.Properties, actor, operation.Username, (byte)ItemType.Avatar);

            while (world.ItemCache.AddItem(avatar) == false)
            {
                Item otherAvatarItem;
                if (world.ItemCache.TryGetItem(avatar.Type, avatar.Id, out otherAvatarItem))
                {
                    avatar.Dispose();
                    actor.Dispose();
                    interestArea.Dispose();

                    ((Peer)((MmoItem)otherAvatarItem).Owner.Peer).DisconnectByOtherPeer(this, request, sendParameters);

                    // request continued later, no response here
                    return(null);
                }
            }

            // init avatar
            actor.AddItem(avatar);
            actor.Avatar = avatar;

            ((Peer)peer).SetCurrentOperationHandler(actor);

            // set return values
            var responseObject = new EnterWorldResponse
            {
                BottomRightCorner = world.Area.Max.ToFloatArray(2),
                TopLeftCorner     = world.Area.Min.ToFloatArray(2),
                TileDimensions    = world.TileDimensions.ToFloatArray(2),
                WorldName         = world.Name
            };

            // send response; use item channel to ensure that this event arrives before any move or subscribe events
            var response = new OperationResponse(request.OperationCode, responseObject);

            sendParameters.ChannelId = Settings.ItemEventChannel;
            peer.SendOperationResponse(response, sendParameters);

            lock (interestArea.SyncRoot)
            {
                interestArea.AttachToItem(avatar);
                interestArea.UpdateInterestManagement();
            }

            avatar.Spawn(operation.Position);
            world.Radar.AddItem(avatar, operation.Position);

            // response already sent
            return(null);
        }
Esempio n. 2
0
        /// <summary>
        ///   Expects operation <see cref = "EnterWorld" /> and creates a new <see cref = "MmoActor" /> with a new <see cref = "MmoItem" /> as avatar and a new <see cref = "MmoClientInterestArea" />. 
        ///   The <see cref = "MmoActor" /> becomes the new <see cref = "Peer.CurrentOperationHandler">operation handler</see>.
        ///   If another <see cref = "MmoActor" /> with the same name exists he is disconnected.
        ///   An <see cref = "OperationResponse" /> with error code <see cref = "ReturnCode.Ok" /> is published on success.
        /// </summary>
        /// <param name = "peer">
        ///   The client peer.
        /// </param>
        /// <param name = "request">
        ///   The request.
        /// </param>
        /// <param name = "sendParameters">
        ///   The send Parameters.
        /// </param>
        /// <returns>
        ///   Null or an <see cref = "OperationResponse" /> with error code <see cref = "ReturnCode.WorldNotFound" />.
        /// </returns>
        public OperationResponse OperationEnterWorld(PeerBase peer, OperationRequest request, SendParameters sendParameters)
        {
            var operation = new EnterWorld(peer.Protocol, request);
            if (!operation.IsValid)
            {
                return new OperationResponse(request.OperationCode) { ReturnCode = (int)ReturnCode.InvalidOperationParameter, DebugMessage = operation.GetErrorMessage() };
            }

            MmoWorld world;
            if (MmoWorldCache.Instance.TryGet(operation.WorldName, out world) == false)
            {
                return operation.GetOperationResponse((int)ReturnCode.WorldNotFound, "WorldNotFound");
            }

            var interestArea = new MmoClientInterestArea(peer, operation.InterestAreaId, world)
                {
                   ViewDistanceEnter = operation.ViewDistanceEnter.ToVector(), ViewDistanceExit = operation.ViewDistanceExit.ToVector() 
                };

            var actor = new MmoActor(peer, world, interestArea);
            var avatar = new MmoItem(world, operation.Position, operation.Rotation, operation.Properties, actor, operation.Username, (byte)ItemType.Avatar);

            while (world.ItemCache.AddItem(avatar) == false)
            {
                Item otherAvatarItem;
                if (world.ItemCache.TryGetItem(avatar.Type, avatar.Id, out otherAvatarItem))
                {
                    avatar.Dispose();
                    actor.Dispose();
                    interestArea.Dispose();

                    ((Peer)((MmoItem)otherAvatarItem).Owner.Peer).DisconnectByOtherPeer(this, request, sendParameters);

                    // request continued later, no response here
                    return null;
                }
            }

            // init avatar
            actor.AddItem(avatar);
            actor.Avatar = avatar;

            ((Peer)peer).SetCurrentOperationHandler(actor);

            // set return values
            var responseObject = new EnterWorldResponse
                {
                    BottomRightCorner = world.Area.Max.ToFloatArray(2), 
                    TopLeftCorner = world.Area.Min.ToFloatArray(2), 
                    TileDimensions = world.TileDimensions.ToFloatArray(2), 
                    WorldName = world.Name
                };

            // send response; use item channel to ensure that this event arrives before any move or subscribe events
            var response = new OperationResponse(request.OperationCode, responseObject);
            sendParameters.ChannelId = Settings.ItemEventChannel;
            peer.SendOperationResponse(response, sendParameters);

            lock (interestArea.SyncRoot)
            {
                interestArea.AttachToItem(avatar);
                interestArea.UpdateInterestManagement();
            }

            avatar.Spawn(operation.Position);
            world.Radar.AddItem(avatar, operation.Position);

            // response already sent
            return null;
        }
Esempio n. 3
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 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 MmoClientInterestArea(this.Peer, operation.InterestAreaId, this.World);
            this.AddInterestArea(interestArea);

            // attach interestArea to item
            Item item;
            if (operation.ItemType.HasValue && string.IsNullOrEmpty(operation.ItemId) == false)
            {
                IWorld world = this.World;

                bool actorItem = this.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(() => this.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();
                    interestArea.ViewDistanceEnter = operation.ViewDistanceEnter.ToVector();
                    interestArea.ViewDistanceExit = operation.ViewDistanceExit.ToVector();
                    interestArea.UpdateInterestManagement();
                }
            }

            return operation.GetOperationResponse(MethodReturnValue.Ok);
        }