private OperationResponse ItemOperationDestroy(Item item, DestroyItem operation)
        {
            MethodReturnValue result = this.CheckAccess(item);

            if (result.IsOk)
            {
                item.Destroy();
                item.Dispose();
                this.RemoveItem(item);

                item.World.ItemCache.RemoveItem(item.Id);
                var eventInstance = new ItemDestroyed {
                    ItemId = item.Id
                };
                var eventData = new EventData((byte)EventCode.ItemDestroyed, eventInstance);
                this.Peer.SendEvent(eventData, new SendParameters {
                    ChannelId = Settings.ItemEventChannel
                });

                // no response, event is sufficient
                operation.OnComplete();
                return(null);
            }

            return(operation.GetOperationResponse(result));
        }
        private OperationResponse ItemOperationSetProperties(Item item, SetProperties operation, SendParameters sendParameters)
        {
            MethodReturnValue result = this.CheckAccess(item);

            if (result.IsOk)
            {
                item.SetProperties(operation.PropertiesSet, operation.PropertiesUnset);
                var eventInstance = new ItemPropertiesSet
                {
                    ItemId             = item.Id,
                    PropertiesRevision = item.PropertiesRevision,
                    PropertiesSet      = operation.PropertiesSet,
                    PropertiesUnset    = operation.PropertiesUnset
                };

                var eventData = new EventData((byte)EventCode.ItemPropertiesSet, eventInstance);
                sendParameters.ChannelId = Settings.ItemEventChannel;
                var message = new ItemEventMessage(item, eventData, sendParameters);
                item.EventChannel.Publish(message);

                // no response sent
                operation.OnComplete();
                return(null);
            }

            return(operation.GetOperationResponse(result));
        }
        private MethodReturnValue CheckAccess(Item item)
        {
            if (item.Disposed)
            {
                return(MethodReturnValue.New((int)ReturnCode.ItemNotFound, "ItemNotFound"));
            }

            if (((Item)item).GrantWriteAccess(this))
            {
                return(MethodReturnValue.Ok);
            }

            return(MethodReturnValue.New((int)ReturnCode.ItemAccessDenied, "ItemAccessDenied"));
        }
        private OperationResponse ItemOperationSpawn(Item item, SpawnItem operation)
        {
            // this should always return Ok
            MethodReturnValue result = this.CheckAccess(item);

            if (result.IsOk)
            {
                item.Rotation = operation.Rotation;
                item.Spawn(operation.Position);
                ((World)this.World).Radar.AddItem(item, operation.Position);
            }

            operation.OnComplete();
            return(operation.GetOperationResponse(result));
        }
Example #5
0
        /// <summary>
        ///   Expects operation <see cref = "CreateWorld" /> and adds a new <see cref = "MmoWorld" /> to the <see cref = "MmoWorldCache" />.
        /// </summary>
        /// <param name = "peer">
        ///   The client peer.
        /// </param>
        /// <param name = "request">
        ///   The request.
        /// </param>
        /// <returns>
        ///   An <see cref = "OperationResponse" /> with error code <see cref = "ReturnCode.Ok" /> or <see cref = "ReturnCode.WorldAlreadyExists" />.
        /// </returns>
        public OperationResponse OperationCreateWorld(PeerBase peer, OperationRequest request)
        {
            var operation = new CreateWorld(peer.Protocol, request);

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

            MmoWorld          world;
            MethodReturnValue result = MmoWorldCache.Instance.TryCreate(
                operation.WorldName, operation.TopLeftCorner.ToVector(), operation.BottomRightCorner.ToVector(), operation.TileDimensions.ToVector(), out world)
                                           ? MethodReturnValue.Ok
                                           : MethodReturnValue.Fail((int)ReturnCode.WorldAlreadyExists, "WorldAlreadyExists");

            return(operation.GetOperationResponse(result));
        }
        private OperationResponse ItemOperationMove(Item item, Move operation, SendParameters sendParameters)
        {
            // should always be OK
            MethodReturnValue result = this.CheckAccess(item);

            if (result.IsOk)
            {
                // save previous for event
                Vector oldPosition = item.Position;
                Vector oldRotation = item.Rotation;

                // move
                item.Rotation = operation.Rotation;
                item.Move(operation.Position);

                // send event
                var eventInstance = new ItemMoved
                {
                    ItemId      = item.Id,
                    OldPosition = oldPosition,
                    Position    = operation.Position,
                    Rotation    = operation.Rotation,
                    OldRotation = oldRotation
                };

                var eventData = new EventData((byte)EventCode.ItemMoved, eventInstance);
                sendParameters.ChannelId = Settings.ItemEventChannel;
                var message = new ItemEventMessage(item, eventData, sendParameters);
                item.EventChannel.Publish(message);

                // no response sent
                operation.OnComplete();
                return(null);
            }

            return(operation.GetOperationResponse(result));
        }
 public OperationResponse GetOperationResponse(MethodReturnValue returnValue)
 {
     return this.GetOperationResponse(returnValue.Error, returnValue.Debug);
 }