private void ServerComponent_MessageGameInformation(object sender, ProtocolMessageEventArgs <GameInformationMessage> e) { GameInformations = e.Message; }
private void ConnectionMessageLogin(object sender, ProtocolMessageEventArgs <LoginMessage> e) { var connection = (ClientConnection)sender; // check client version if (e.Message.Version != ServerConnection.ProtocolVersion) { logger.Error("Protocol version mismatch Client: {0} Server: {1}", e.Message.Version, ServerConnection.ProtocolVersion); var error = new ErrorMessage { ErrorCode = ErrorCodes.VersionMismatch, Data = ServerConnection.ProtocolVersion, Message = string.Format("Wrong client version ({0}) expected {1}", e.Message.Version, ServerConnection.ProtocolVersion) }; connection.Send(error); connection.Disconnect(); return; } if (string.IsNullOrEmpty(e.Message.Login) || string.IsNullOrEmpty(e.Message.Password)) { logger.Error("Invalid login or password"); var error = new ErrorMessage { ErrorCode = ErrorCodes.LoginPasswordIncorrect, Message = "Invalid login or password" }; connection.Send(error); connection.Disconnect(); return; } // checking login and password LoginData loginData; var login = e.Message.Login.ToLower(); if (_server.UsersStorage.Login(login, e.Message.Password, out loginData)) { TimeSpan banTimeLeft; if (_server.UsersStorage.IsBanned(login, out banTimeLeft)) { var error = new ErrorMessage { ErrorCode = ErrorCodes.LoginPasswordIncorrect, Message = "You are banned. Time left: " + banTimeLeft }; connection.Send(error); logger.Error("User banned {0} ({1})", e.Message.Login, connection.Id); connection.Disconnect(); return; } var oldConnection = _server.ConnectionManager.Find(c => c.UserId == loginData.UserId); if (oldConnection != null) { logger.Info("Disconnecting previous instance"); oldConnection.Send(new ErrorMessage { ErrorCode = ErrorCodes.AnotherInstanceLogged, Message = "Another instance of you connected. You will be disconnected." }); oldConnection.Disconnect(); } connection.Authorized = true; connection.UserId = loginData.UserId; connection.UserRole = loginData.Role; connection.Login = login; connection.DisplayName = e.Message.DisplayName; ServerPlayerCharacterEntity playerEntity; #region Getting player entity if (loginData.State == null) { logger.Info("No state. Creating new entity and the state for " + e.Message.DisplayName); // create new message playerEntity = GetNewPlayerEntity(connection, DynamicIdHelper.GetNextUniqueId()); var state = new UserState { EntityId = playerEntity.DynamicEntity.DynamicId }; _server.UsersStorage.SetData(login, state.Save()); } else { var state = UserState.Load(loginData.State); // load new player entity playerEntity = new ServerPlayerCharacterEntity(connection, new PlayerCharacter(), _server); var bytes = _server.EntityStorage.LoadEntityBytes(state.EntityId); if (bytes == null) { logger.Warn("{0} entity is absent, creating new one... Id={1}", e.Message.DisplayName, state.EntityId); playerEntity = GetNewPlayerEntity(connection, state.EntityId); } else { using (var ms = new MemoryStream(bytes)) { playerEntity.DynamicEntity = Serializer.Deserialize <PlayerCharacter>(ms); } } } playerEntity.PlayerCharacter.IsReadOnly = loginData.Role == UserRole.Guest; playerEntity.PlayerCharacter.CanFly = loginData.Role == UserRole.Administrator; //Check playerEntity.DynamicEntity Initialisation var player = playerEntity.DynamicEntity as PlayerCharacter; if (player != null) { if (player.Health.MaxValue == 0 || player.Health.EntityOwnerId == 0) { player.Health.MaxValue = 100; player.Health.CurrentValue = 100; player.HealthState = DynamicEntityHealthState.Normal; player.DisplacementMode = EntityDisplacementModes.Walking; logger.Info("PlayerCharacter was missing Health initialization + health state. EntityId : {0}", player.DynamicId); } if (player.Stamina.MaxValue == 0 || player.Stamina.EntityOwnerId == 0) { player.Stamina.MaxValue = 100; player.Stamina.CurrentValue = 100; logger.Info("PlayerCharacter was missing Stamina initialization. EntityId : {0}", player.DynamicId); } if (player.Oxygen.MaxValue == 0 || player.Oxygen.EntityOwnerId == 0) { player.Oxygen.MaxValue = 100; player.Oxygen.CurrentValue = 100; logger.Info("PlayerCharacter was missing Oxygen initialization. EntityId : {0}", player.DynamicId); } } #endregion _server.EntityFactory.PrepareEntity(playerEntity.DynamicEntity); connection.ServerEntity = playerEntity; connection.Send(new LoginResultMessage { Logged = true }); logger.Info("{1} {0} logged as {3} EntityId = {2} ", e.Message.Login, connection.Id, connection.ServerEntity.DynamicEntity.DynamicId, e.Message.DisplayName); var gameInfo = new GameInformationMessage { ChunkSize = AbstractChunk.ChunkSize, MaxViewRange = 32, WorldParameter = _server.LandscapeManager.WorldGenerator.WorldParameters, GlobalState = _server.GlobalStateManager.GlobalState, AreaSize = MapArea.AreaSize }; connection.Send(gameInfo); connection.Send(new EntityInMessage { Entity = playerEntity.DynamicEntity, Link = playerEntity.DynamicEntity.GetLink() }); connection.Send(new DateTimeMessage { DateTime = _server.Clock.Now, TimeFactor = _server.Clock.TimeFactor }); OnPlayerAuthorized(new ConnectionEventArgs { Connection = connection }); } else { var error = new ErrorMessage { ErrorCode = ErrorCodes.LoginPasswordIncorrect, Message = "Wrong login/password combination" }; logger.Error("Incorrect login information {0} ({1})", e.Message.Login, connection.Id); connection.Send(error, new LoginResultMessage { Logged = false }); connection.Disconnect(); } }
private void _server_MessagePing(object sender, ProtocolMessageEventArgs <PingMessage> e) { AddMessage(string.Format("<Pong> {0} ms", Console.PingTimer.ElapsedMilliseconds)); Console.PingTimer.Stop(); }
//Synchronize hour with server private void ServerConnection_MessageDateTime(object sender, ProtocolMessageEventArgs <DateTimeMessage> e) { AssignTimeAndFactor(e.Message.TimeFactor, e.Message.DateTime); logger.Info("Received Server date time for syncing : {0}, local time was : {1}", e.Message.DateTime, ClockTime.ToString()); }
void _server_MessageEntityEquipment(object sender, ProtocolMessageEventArgs <EntityEquipmentMessage> e) { _dynamicEntityManager.UpdateEntity((ICharacterEntity)e.Message.Entity); }
void _server_MessageUseFeedback(object sender, ProtocolMessageEventArgs <UseFeedbackMessage> e) { _syncManager.RegisterFeedback(e.Message); }
private void ConnectionMessageEntityLock(object sender, ProtocolMessageEventArgs <EntityLockMessage> e) { var connection = (ClientConnection)sender; bool success = false; if (e.Message.Lock) { // locking if (e.Message.EntityLink.IsStatic) { #region Lock static entity var staticEntity = e.Message.EntityLink.ResolveStatic(_server.LandscapeManager); if (staticEntity == null) { connection.Send(new EntityLockResultMessage { EntityLink = e.Message.EntityLink, LockResult = LockResult.NoSuchEntity }); return; } lock (_lockedStaticEntities) { if (_lockedStaticEntities.ContainsKey(e.Message.EntityLink)) { connection.Send(new EntityLockResultMessage { EntityLink = e.Message.EntityLink, LockResult = LockResult.FailAlreadyLocked }); return; } _lockedStaticEntities.Add(e.Message.EntityLink, connection.ServerEntity.DynamicEntity.DynamicId); } connection.ServerEntity.LockedEntity = staticEntity; connection.Send(new EntityLockResultMessage { EntityLink = e.Message.EntityLink, LockResult = LockResult.SuccessLocked }); success = true; #endregion } else { #region Lock dynamic entity lock (_lockedDynamicEntities) { if (_lockedDynamicEntities.ContainsKey(e.Message.EntityLink.DynamicEntityId)) { connection.Send(new EntityLockResultMessage { EntityLink = e.Message.EntityLink, LockResult = LockResult.FailAlreadyLocked }); return; } var dynEntity = _server.AreaManager.Find(e.Message.EntityLink.DynamicEntityId); if (dynEntity != null) { _lockedDynamicEntities.Add(e.Message.EntityLink.DynamicEntityId, connection.ServerEntity.DynamicEntity.DynamicId); var lockEntity = (IEntity)dynEntity.DynamicEntity; connection.ServerEntity.LockedEntity = lockEntity; connection.Send(new EntityLockResultMessage { EntityLink = e.Message.EntityLink, LockResult = LockResult.SuccessLocked }); success = true; } else { connection.Send(new EntityLockResultMessage { EntityLink = e.Message.EntityLink, LockResult = LockResult.NoSuchEntity }); } } #endregion } } else { // unlocking if (e.Message.EntityLink.IsStatic) { #region unlock static var staticEntity = e.Message.EntityLink.ResolveStatic(_server.LandscapeManager); if (staticEntity == null) { return; } lock (_lockedStaticEntities) { uint lockOwner; if (_lockedStaticEntities.TryGetValue(e.Message.EntityLink, out lockOwner)) { if (lockOwner == connection.ServerEntity.DynamicEntity.DynamicId) { _lockedStaticEntities.Remove(e.Message.EntityLink); connection.ServerEntity.LockedEntity = null; success = true; } } } #endregion } else { #region unlock dynamic lock (_lockedDynamicEntities) { uint lockOwner; if (_lockedDynamicEntities.TryGetValue(e.Message.EntityLink.DynamicEntityId, out lockOwner)) { if (lockOwner == connection.ServerEntity.DynamicEntity.DynamicId) { _lockedDynamicEntities.Remove(e.Message.EntityLink.DynamicEntityId); connection.ServerEntity.LockedEntity = null; success = true; } } } #endregion } } // retranslate success locks if (success) { OnEntityLockChanged(e); } }
private void ConnectionMessageEntityEquipment(object sender, ProtocolMessageEventArgs <EntityEquipmentMessage> e) { var connection = (ClientConnection)sender; connection.ServerEntity.Equip(e.Message); }