Ejemplo n.º 1
0
        public override void ProcessPacket(Guid sourcePlayerId, Packet packet, IProcessorContext context)
        {
            ServerProcessorContext    processorContext          = (ServerProcessorContext)context;
            AuthenticateRequestPacket authenticateRequestPacket = (AuthenticateRequestPacket)packet;
            ServerSettings            serverSettings            = processorContext.Server.Settings;
            PlayerManager             playerManager             = processorContext.Server.PlayerManager;
            SimulationManager         simulationManager         = processorContext.Server.SimulationManager;

            AuthenticatePacket authenticateResponsePacket;

            if (playerManager.PlayerExists(sourcePlayerId))
            {
                return; // Player already authenticated
            }
            // Will fail if the requested username contains disallowed characters/does not meet length requirements/etc.
            if (!playerManager.IsUsernameAllowed(authenticateRequestPacket.Username))
            {
                authenticateResponsePacket = new AuthenticatePacket(false, AuthenticationErrorReason.IllegalUsername, null, null, null);
                Console.WriteLine($"Player {sourcePlayerId} attempted joining with an illegal username");
            }
            // Will fail if a player with the same nickname is already connected.
            else if (playerManager.IsUsernameTaken(authenticateRequestPacket.Username))
            {
                authenticateResponsePacket = new AuthenticatePacket(false, AuthenticationErrorReason.UsernameTaken, null, null, null);
                Console.WriteLine($"Player {sourcePlayerId} attempted joining with an already taken username: {authenticateRequestPacket.Username}");
            }
            // Will fail if the authenticating client provided an incorrect server password.
            else if (serverSettings.PasswordProtected && serverSettings.Password != authenticateRequestPacket.Password)
            {
                authenticateResponsePacket = new AuthenticatePacket(false, AuthenticationErrorReason.IncorrectPassword, null, null, null);
                Console.WriteLine($"Player {sourcePlayerId} attempted joining with an incorrect password");
            }
            // Approve authentication request and let other players know that a player joined.
            else
            {
                Player   client          = playerManager.CreatePlayer(sourcePlayerId, authenticateRequestPacket.Username, state: PlayerState.ConnectedMainMenu);
                Player[] players         = playerManager.GetPlayers().ToArray();
                Player?  simulationOwner = simulationManager.GetSimulationOwner();
                authenticateResponsePacket = new AuthenticatePacket(true, null, client, players, simulationOwner);
                Console.WriteLine($"Player {sourcePlayerId} successfully authenticated!");
            }

            processorContext.Server.SendPacketToPlayer(authenticateResponsePacket, sourcePlayerId);
        }
        public void OnSessionDataReceived(SessionData session)
        {
            Debug.Log("Received session data: " + session.ServerName + ", PasswordProtected: "
                      + session.PasswordProtected + ", Player count: " + session.PlayerCount);

            AuthenticateRequestPacket authenticateRequestPacket;

            if (session.PasswordProtected)
            {
                authenticateRequestPacket = new AuthenticateRequestPacket(connectionOptions.Username, connectionOptions.Password);
            }
            else
            {
                authenticateRequestPacket = new AuthenticateRequestPacket(connectionOptions.Username, null);
            }

            Debug.Log("Sending authenticate request");
            SendPacket(authenticateRequestPacket);
        }