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)ErrorCode.InvalidOperationParameter, DebugMessage = operation.GetErrorMessage() }; } MmoWorld world = MmoWorld.Instance; 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 { }; // 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); avatar.Spawn(operation.Position); // response already sent return null; }
public OperationResponse OperationEnterWorld(PeerBase peer, OperationRequest request, SendParameters sendParameters) { try { 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(application).TryGet(operation.WorldName, out world) == false) { return(operation.GetOperationResponse((int)ReturnCode.WorldNotFound, "WorldNotFound", new System.Collections.Hashtable())); } var interestArea = new MmoClientInterestArea(peer, operation.InterestAreaId, world) { ViewDistanceEnter = operation.ViewDistanceEnter.ToVector(true), ViewDistanceExit = operation.ViewDistanceExit.ToVector(true) }; PlayerCharacter character = new PlayerCharacter(); character.SetCharacterId(operation.CharacterId); character.SetModel(operation.Model); character.SetName(operation.Name); character.SetRace((Race)operation.Race); character.SetWorkshop((Workshop)operation.Workshop); character.Login = operation.Login; Type[] components = new Type[] { typeof(MmoActor), typeof(AIState), typeof(PlayerCharacterObject), typeof(RaceableObject), typeof(MmoMessageComponent), typeof(PlayerShip), typeof(ShipWeapon), typeof(PlayerSkills), typeof(PlayerBonuses), typeof(PlayerLoaderObject), typeof(ShipBasedDamagableObject), typeof(PlayerTarget), typeof(ShipEnergyBlock), typeof(PlayerShipMovable), typeof(PassiveBonusesComponent), typeof(PlayerTimedEffects), typeof(PetManager), typeof(PlayerEventSubscriber), typeof(ContractManager), typeof(AchievmentComponent), typeof(QuestManager) //typeof(QuestManager), //typeof(DialogManager) }; Dictionary <byte, object> tags = new Dictionary <byte, object> { { (byte)PlayerTags.GameRefId, operation.GameRefId }, { (byte)PlayerTags.CharacterId, character.CharacterId }, { (byte)PlayerTags.Race, character.Race }, { (byte)PlayerTags.Workshop, character.Workshop }, { (byte)PlayerTags.Name, (character.Name != null) ? character.Name : string.Empty }, { (byte)PlayerTags.Model, character.Model }, { (byte)PlayerTags.Login, operation.Login } }; //var actor = new MmoActor(peer, world, interestArea, operation.GameRefId, character, application); log.InfoFormat("before creating player item = {0}", operation.GameRefId); var avatar = new MmoItem(peer, interestArea, world, application, operation.Position, operation.Rotation, operation.Properties, operation.GameRefId, tags, 1f, 0, components); avatar.GetComponent <MmoActor>().SetApplication(application); log.InfoFormat("init player item at position = {0}", operation.Position.ToVector3()); log.InfoFormat("Before Start() player"); avatar.GetComponent <DamagableObject>().SetIgnoreDamageAtStart(true); avatar.GetComponent <DamagableObject>().SetIgnoreDamageInterval(30); avatar.Update(0); log.InfoFormat("Before Load() player"); log.InfoFormat("calling load on avatar [dy]"); avatar.GetComponent <PlayerLoaderObject>().Load(); while (world.ItemCache.AddItem(avatar) == false) { Item otherAvatarItem; if (world.ItemCache.TryGetItem(avatar.Type, avatar.Id, out otherAvatarItem)) { avatar.Dispose(); avatar.GetComponent <MmoActor>().Dispose(); interestArea.Dispose(); ((Peer)((MmoItem)otherAvatarItem).Owner.Peer).DisconnectByOtherPeer(this, request, sendParameters); return(null); } } log.Info("After adding player to world"); //actor.AddItem(avatar); //actor.Avatar = avatar; //actor.Initialize(); //actor.Target.ResetTarget(); ((Peer)peer).SetCurrentOperationHandler(avatar.GetComponent <MmoActor>()); log.Info("Set player item operation handler to Actor"); var responseObject = new EnterWorldResponse { BottomRightCorner = world.Area.Max.ToFloatArray(3), TopLeftCorner = world.Area.Min.ToFloatArray(3), TileDimensions = world.TileDimensions.ToFloatArray(3), WorldName = world.Name, Content = world.GetInfo(), ItemId = avatar.Id, components = avatar.componentIds, size = avatar.size, subZone = avatar.subZone //startAtStation = operation.EnterAtStation }; log.InfoFormat("Components = {0} sended to player back", avatar.componentIds.Length); var response = new OperationResponse(request.OperationCode, responseObject); sendParameters.ChannelId = Settings.ItemEventChannel; peer.SendOperationResponse(response, sendParameters); log.Info("World entered response sended back"); lock (interestArea.SyncRoot) { interestArea.AttachToItem(avatar); interestArea.UpdateInterestManagement(); } avatar.Spawn(operation.Position); log.Info("Avatar Spawn fire...."); return(null); } catch (Exception ex) { CL.Out(LogFilter.PLAYER, ex.Message); CL.Out(LogFilter.PLAYER, ex.StackTrace); } return(null); }
/// <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); }