/// <summary> /// Spawns an item. /// </summary> /// <param name = "item"> /// The mmo item. /// </param> /// <param name = "operation"> /// The operation. /// </param> /// <param name = "interestArea"> /// The interest Area. /// </param> /// <returns> /// error code ok /// </returns> private OperationResponse ItemOperationSpawn(MmoItem item, SpawnItem operation, InterestArea interestArea) { // this should always return Ok MethodReturnValue result = this.CheckAccess(item); if (result) { item.Rotation = operation.Rotation; item.Spawn(operation.Position); ((MmoWorld)this.World).Radar.AddItem(item, operation.Position); if (interestArea != null) { lock (interestArea.SyncRoot) { interestArea.SubscribeItem(item); } } } operation.OnComplete(); return operation.GetOperationResponse(result); }
/// <summary> /// The Chat Operation /// </summary> /// <param name="item">The mmo item</param> /// <param name="operation">The operation</param> /// <returns>error code OK</returns> private OperationResponse ItemOperationChat(MmoItem item, Chat operation, SendParameters sendParameters) { MethodReturnValue result = this.CheckAccess(item); if (result) { var eventInstance = new ItemChat { ItemId = item.Id, ItemType = item.Type, Group = operation.Group, Message = operation.Message }; EventData eventData = new EventData((byte)EventCode.ItemChat, eventInstance); sendParameters.ChannelId = Settings.ItemEventChannel; var message = new ItemEventMessage(item, eventData, sendParameters); item.EventChannel.Publish(message); operation.OnComplete(); return null; } return operation.GetOperationResponse(result); }
/// <summary> /// The move operation. /// </summary> /// <param name = "item"> /// The mmo item. /// </param> /// <param name = "operation"> /// The operation. /// </param> /// <param name = "sendParameters"> /// The send Parameters. /// </param> /// <returns> /// error code ok /// </returns> private OperationResponse ItemOperationMove(MmoItem item, Move operation, SendParameters sendParameters) { // should always be OK MethodReturnValue result = this.CheckAccess(item); if (result) { // save previous for event float[] oldPosition = item.Coordinate; float[] oldRotation = item.Rotation; // move item.Rotation = operation.Rotation; item.Move(operation.Position); // send event var eventInstance = new ItemMoved { ItemId = item.Id, ItemType = item.Type, 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); }
/// <summary> /// The Animate Operation /// </summary> /// <param name="item">The mmo item</param> /// <param name="operation">The operation</param> /// <returns>error code OK</returns> private OperationResponse ItemOperationAnimate(MmoItem item, Animate operation, string animation, byte action, byte animationWrap, float animationSpeed, int animationLayer, SendParameters sendParameters) { MethodReturnValue result = this.CheckAccess(item); if (result) { item.Animation = animation; item.AnimationWrap = animationWrap; item.AnimationSpeed = animationSpeed; var eventInstance = new ItemAnimate { ItemId = item.Id, ItemType = item.Type, Animation = animation, AnimationAction = action, AnimationWrap = animationWrap, AnimationSpeed = animationSpeed, AnimationLayer = animationLayer }; EventData eventData = new EventData((byte)EventCode.ItemAnimate, eventInstance); sendParameters.ChannelId = Settings.ItemEventChannel; var message = new ItemEventMessage(item, eventData, sendParameters); item.EventChannel.Publish(message); operation.OnComplete(); return null; } return operation.GetOperationResponse(result); }
/// <summary> /// The Game item move operation. /// </summary> /// <param name = "item"> /// The mmo item. /// </param> /// <param name = "operation"> /// The operation. /// </param> /// <param name = "sendParameters"> /// The send Parameters. /// </param> /// <returns> /// error code ok /// </returns> private OperationResponse GameItemOperationMove(MmoItem item, GameItemMove operation, SendParameters sendParameters) { // should always be OK MethodReturnValue result = this.CheckAccess(item); if (result) { // send event var eventInstance = new GameItemMoved { ItemId = operation.ItemId, GameItemId = operation.GameItemId, Position = operation.Position, Rotation = operation.Rotation }; var eventData = new EventData((byte)EventCode.GameItemMoved, 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); }
/// <summary> /// Handles operation <see cref = "SpawnItem" />: Creates a new <see cref = "Item" /> and optionally subscribes an <see cref = "InterestArea" /> to it. /// </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" />, <see cref = "ReturnCode.ItemAlreadyExists" /> or <see cref = "ReturnCode.InterestAreaNotFound" />. /// </returns> public OperationResponse OperationSpawnItem(PeerBase peer, OperationRequest request) { var operation = new SpawnItem(peer.Protocol, request); if (!operation.IsValid) { return new OperationResponse(request.OperationCode) { ReturnCode = (int)ReturnCode.InvalidOperationParameter, DebugMessage = operation.GetErrorMessage() }; } operation.OnStart(); InterestArea interestArea; if (operation.InterestAreaId.HasValue) { if (this.TryGetInterestArea(operation.InterestAreaId.Value, out interestArea) == false) { return operation.GetOperationResponse((int)ReturnCode.InterestAreaNotFound, "InterestAreaNotFound"); } } else { interestArea = null; } IWorld world = this.World; var item = new MmoItem(world, operation.Position, operation.Rotation, operation.Properties, this, operation.ItemId, operation.ItemType); if (world.ItemCache.AddItem(item)) { this.AddItem(item); return this.ItemOperationSpawn(item, operation, interestArea); } item.Dispose(); return operation.GetOperationResponse((int)ReturnCode.ItemAlreadyExists, "ItemAlreadyExists"); }
/// <summary> /// Adds the <paramref name = "item" /> to the owned items. /// </summary> /// <param name = "item"> /// The mmo item. /// </param> /// <exception cref = "ArgumentException"> /// The item <see cref = "MmoItem.Owner" /> must be this actor. /// </exception> public void AddItem(MmoItem item) { if (item.Owner != this) { throw new ArgumentException("foreign owner forbidden"); } base.AddItem(item); }
/// <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; } } #region PopBloop avatar.AvatarName = operation.AvatarName; log.InfoFormat("View Distance {0}, {1}", operation.ViewDistanceEnter[0], operation.ViewDistanceEnter[1]); DbManager.Instance.RecordPlayerStats(avatar.Id, operation.WorldName); DbManager.Instance.RecordRoomStats(avatar.Id, operation.WorldName); DbManager.Instance.SetConcurrent(avatar.Id, operation.WorldName, false); #endregion // 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); PhotonCounter.SessionCount.Increment(); // response already sent return null; }