Ejemplo n.º 1
0
        public void AddEntity(ServerDynamicEntity entity)
        {
            // maybe we need to generate new unique entity id
            if (entity.DynamicEntity.DynamicId == 0)
            {
                entity.DynamicEntity.DynamicId = DynamicIdHelper.GetNextUniqueId();
            }

            lock (_dynamicEntities)
            {
                if (_dynamicEntities.ContainsKey(entity.DynamicEntity.DynamicId))
                {
                    throw new InvalidOperationException("Such entity is already in manager");
                }

                _dynamicEntities.Add(entity.DynamicEntity.DynamicId, entity);
            }

            MapArea entityArea = null;

            // listen all 9 areas and add at center area
            for (int x = -1; x < 2; x++)
            {
                for (int z = -1; z < 2; z++)
                {
                    var area = GetArea(new Vector3D(entity.DynamicEntity.Position.X + x * MapArea.AreaSize.X, 0,
                                                    entity.DynamicEntity.Position.Z + z * MapArea.AreaSize.Y));
                    entity.AddArea(area);
                    if (x == 0 && z == 0)
                    {
                        area.AddEntity(entity);
                        entityArea = area;
                    }
                    area.OnEntityInViewRange(entity);
                }
            }
            entity.CurrentArea = entityArea;

            OnEntityAdded(new AreaEntityEventArgs {
                Entity = entity
            });
        }
Ejemplo n.º 2
0
        public ServerNpc AddNpc(CharacterEntity charEntity)
        {
            charEntity.EntityFactory = _server.EntityFactory;

            var npc = new ServerNpc(_server, charEntity);
            var id  = charEntity.DynamicId;

            if (id == 0)
            {
                id = DynamicIdHelper.GetNextUniqueId();
                npc.DynamicEntity.DynamicId = id;
            }
            _server.AreaManager.AddEntity(npc);
            _npcs.Add(id, npc);

            charEntity.HealthStateChanged += charEntity_HealthStateChanged;
            charEntity.NeedSave           += charEntity_NeedSave;

            return(npc);
        }
Ejemplo n.º 3
0
        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();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create new instance of the Server class
        /// </summary>
        public ServerCore(
            XmlSettingsManager <ServerSettings> settingsManager,
            WorldGenerator worldGenerator,
            IUsersStorage usersStorage,
            IChunksStorage chunksStorage,
            IEntityStorage entityStorage,
            ICustomStorage customStorage,
            EntityFactory entityFactory,
            WorldParameters wp
            )
        {
            // dependency injection
            SettingsManager = settingsManager;
            UsersStorage    = usersStorage;
            EntityStorage   = entityStorage;
            CustomStorage   = customStorage;
            EntityFactory   = entityFactory;
            WorldParameters = wp;

            if (SettingsManager.Settings == null)
            {
                SettingsManager.Load();
            }

            var settings = SettingsManager.Settings;

            ConnectionManager = new ConnectionManager(SettingsManager.Settings.ServerPort);

            Scheduler = new ScheduleManager();

            UtopiaTime startTime = CustomStorage.GetVariable <UtopiaTime>("GameTimeElapsed");

            Clock = new Clock(this, startTime, TimeSpan.FromMinutes(20));

            LandscapeManager = new ServerLandscapeManager(
                this,
                chunksStorage,
                worldGenerator,
                EntityFactory,
                settings.ChunkLiveTimeMinutes,
                settings.CleanUpInterval,
                settings.SaveInterval,
                settings.ChunksCountLimit,
                wp);

            EntityManager = new EntityManager(this);

            AreaManager = new AreaManager(this);

            DynamicIdHelper.SetMaxExistsId(EntityStorage.GetMaximumId());

            Services = new ServiceManager(this);

            PerformanceManager = new PerformanceManager(AreaManager);

            CommandsManager = new CommandsManager(this);

            ChatManager = new ChatManager(this);

            GlobalStateManager = new GlobalStateManager(this);

            LoginManager = new LoginManager(this, EntityFactory);

            EntitySpawningManager = new EntitySpawningManager(this, worldGenerator.EntitySpawningControler);

            EntityGrowingManager = new Managers.EntityGrowingManager(this);
        }