private void OnIncomingStatusChange(NetPeer peer, NetIncomingMessage msg)
        {
            if (client.ConnectionStatus == NetConnectionStatus.Connected)
            {
                Debug.Log("Client connected!");
                SessionDataRequestPacket sessionDataRequestPacket = new SessionDataRequestPacket();
                SendPacket(sessionDataRequestPacket);
                Debug.Log("Sending session data request");
            }
            if (client.ConnectionStatus == NetConnectionStatus.Disconnected && GameManager.getInstance().getGameState() is GameStateGame)
            {
                Debug.Log("Lost connection with the game server!");
                void OnExitConfirm(object parameter)
                {
                    GameManager.getInstance().setGameStateTitle();
                    Disconnect();
                }

                GuiDefinitions.Callback callback = new GuiDefinitions.Callback(OnExitConfirm);
                if (!MessageBoxOk.Show(callback, "Disconnected from server", "Lost connection with the game server."))
                {
                    OnExitConfirm(null); // Failed to show message box
                }
            }
        }
Esempio n. 2
0
        public override void ProcessPacket(Guid sourcePlayerId, Packet packet, IProcessorContext context)
        {
            ClientProcessorContext  processorContext        = (ClientProcessorContext)context;
            DisconnectRequestPacket disconnectRequestPacket = (DisconnectRequestPacket)packet;

            void OnExitConfirm(object parameter)
            {
                GameManager.getInstance().setGameStateTitle();
                processorContext.Client.Disconnect();
            }

            GuiDefinitions.Callback callback = new GuiDefinitions.Callback(OnExitConfirm);

            switch (disconnectRequestPacket.Reason)
            {
            case DisconnectReason.DisconnectRequestResponse:
                Debug.Log("Graceful disconnect response received, disconnecting.");
                OnExitConfirm(null);
                break;

            case DisconnectReason.KickedOut:
                if (!MessageBoxOk.Show(callback, "Disconnected from server", "You have been kicked out of the game."))
                {
                    OnExitConfirm(null);     // Failed to show window
                }
                break;

            case DisconnectReason.ServerClosing:
                if (!MessageBoxOk.Show(callback, "Disconnected from server", "Server is shutting down."))
                {
                    OnExitConfirm(null);     // Failed to show window
                }
                break;

            default:
                if (!MessageBoxOk.Show(callback, "Disconnected from server", "Unknown reason."))
                {
                    OnExitConfirm(null);     // Failed to show window
                }
                break;
            }
        }
        public override void ProcessPacket(Guid sourcePlayerId, Packet packet, IProcessorContext context)
        {
            ClientProcessorContext processorContext   = (ClientProcessorContext)context;
            AuthenticatePacket     authenticatePacket = (AuthenticatePacket)packet;
            PlayerManager          playerManager      = processorContext.Client.PlayerManager;

            if (!authenticatePacket.AuthenticationSuccessful)
            {
                switch (authenticatePacket.ErrorReason.Value)
                {
                case AuthenticationErrorReason.UsernameTaken:
                    MessageBoxOk.Show(null, "Failed to join game", "Failed to connect to the server: Username is already taken");
                    break;

                case AuthenticationErrorReason.IncorrectPassword:
                    MessageBoxOk.Show(null, "Failed to join game", "Failed to connect to the server: Incorrect password");
                    break;

                case AuthenticationErrorReason.IllegalUsername:
                    MessageBoxOk.Show(null, "Failed to join game", "Failed to connect to the server: Username contains disallowed characters");
                    break;
                }

                processorContext.Client.RequestDisconnect();
                return;
            }

            processorContext.Client.LocalPlayer = authenticatePacket.LocalPlayer.Value;
            processorContext.Client.SimulationManager.OnSimulationOwnerUpdated(authenticatePacket.SimulationOwner);
            foreach (Player player in authenticatePacket.Players)
            {
                playerManager.OnPlayerAdded(player); // Sync players
            }
            Debug.Log("Sending world data request");
            WorldDataRequestPacket worldDataRequestPacket = new WorldDataRequestPacket();

            processorContext.Client.SendPacket(worldDataRequestPacket);
        }