Ejemplo n.º 1
0
        private void Init()
        {
            ConfigurationRegistry.InitializeRegistry();

            #region Server Callbacks

            #region Terminal Header

            //Terminal Header format
            _server.SetTerminalHeader(() =>
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write($"[BFB-Server|{DateTime.Now:h:mm:ss tt}|T:{Process.GetCurrentProcess().Threads.Count}] ");
                Console.ResetColor();
            });

            #endregion

            #region Handle Client Connection

            _server.OnClientConnect = socket =>
            {
                _server.PrintMessage($"Client {socket.ClientId} Connected");
            };

            #endregion

            #region Handle Client Authentication

            _server.OnClientAuthentication = m =>
            {
                _server.PrintMessage($"Client {m.ClientId} Authenticated.");
                return(true);
            };

            #endregion

            #region OnClientPrepare

            _server.OnClientPrepare = socket =>
            {
                socket.Emit("prepare", _simulation.World.GetInitWorldData());
            };

            #endregion

            #region Handle Client Ready

            _server.OnClientReady = socket =>
            {
                SimulationEntity player = SimulationEntity.SimulationEntityFactory("Human", socket: socket);
                _simulation.AddEntity(player);
                _simulation.ConnectedClients += 1;

                _server.PrintMessage($"Client {socket.ClientId} Ready and added to Simulation");
            };

            #endregion

            #region Handle Client Disconnect

            _server.OnClientDisconnect = id =>
            {
                _simulation.RemoveEntity(id, EntityRemovalReason.Disconnect);
                _simulation.ConnectedClients -= 1;
                _server.PrintMessage($"Client {id} Disconnected");
            };

            #endregion

            #region OnServerStart/Generate World

            //probably should just be triggered to generate the first time the simulation starts
            _server.OnServerStart = () =>
            {
                Console.WriteLine("Generating World...");
                Console.Write("[");
                _cursorPositionStart = Console.CursorLeft;
                Console.CursorLeft   = _cursorPositionStart + 101;
                Console.Write("]");
                Console.CursorLeft      = _cursorPositionStart;
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.CursorVisible   = false;
                _simulation.GenerateWorld();
                Console.ResetColor();
                Console.CursorVisible = true;
                Console.WriteLine();
                _server.PrintMessage();
            };

            #endregion

            #endregion

            #region Simulation Callbacks

            #region OnSimulationStart

            _simulation.OnSimulationStart = () => _server.PrintMessage("Simulation exiting Hibernation");

            #endregion

            #region OnSimulationStop

            _simulation.OnSimulationStop = () => _server.PrintMessage("Simulation entering Hibernation");

            #endregion

            #region OnSimulationTick

            _simulation.OnSimulationTick = () =>
            {
                _server.Emit("HeartBeat");
            };

            #endregion

            #region OnWorldGenerationProgress

            _simulation.OnWorldGenerationProgress = progress =>
            {
                Console.Write("=");
                int previous = Console.CursorLeft;
                Console.CursorLeft = _cursorPositionStart + 50;
                Console.Write(progress);
                Console.CursorLeft = previous;
            };

            #endregion

            #region OnEntityAdd

            _simulation.OnEntityAdd = (entityKey, isPlayer) =>
            {
                if (isPlayer)
                {
                    _server.GetClient(entityKey).On("UISelect", (m) =>
                    {
                        SimulationEntity player = SimulationEntity.SimulationEntityFactory(m.Message, socket: _server.GetClient(m.ClientId));
                        player.Position.X       = 100;
                        player.Position.Y       = 100;
                        _simulation.AddEntity(player);
                    });
                }
            };

            #endregion

            #region OnEntityRemove

            _simulation.OnEntityRemove = (entityKey, isPlayer) =>
            {
                _server.Emit("/player/disconnect", new DataMessage {
                    Message = entityKey
                });
            };

            #endregion

            #region OnEntityUpdates

            _simulation.OnEntityUpdates = (entityKey, updates) =>
            {
                _server.GetClient(entityKey)?.Emit("/players/updates", updates);
            };

            #endregion

            #region OnInventoryUpdates

            _simulation.OnInventoryUpdate = (entityKey, updates) =>
            {
                _server.GetClient(entityKey)?.Emit("/players/inventoryUpdates", updates);
            };

            #endregion

            #region OnChunkUpdates

            _simulation.OnChunkUpdates = (entityKey, updates) =>
            {
                _server.GetClient(entityKey)?.Emit("/players/chunkUpdates", updates);
            };

            #endregion

            #region OnSimulationOverload

            _simulation.OnSimulationOverLoad = ticksBehind => _server.PrintMessage($"SERVER IS OVERLOADED. ({ticksBehind}).");

            #endregion

            #endregion

            #region Terminal Prep

            Console.Clear();
            Console.Title = "BFB Server";
            _server.PrintMessage();

            #endregion
        }