예제 #1
0
        /**
         * Disconnect the local client from the server
         */
        public void Disconnect(bool sendDisconnect = true)
        {
            if (_netClient.IsConnected)
            {
                if (sendDisconnect)
                {
                    // First send the server that we are disconnecting
                    Logger.Get().Info(this, "Sending PlayerDisconnect packet");
                    _netClient.UpdateManager.SetPlayerDisconnect();
                }

                // Then actually disconnect
                _netClient.Disconnect();

                // Let the player manager know we disconnected
                _playerManager.OnDisconnect();

                // Check whether the game is in the pause menu and reset timescale to 0 in that case
                if (UIManager.instance.uiState.Equals(UIState.PAUSED))
                {
                    PauseManager.SetTimeScale(0);
                }

                Ui.UiManager.InfoBox.AddMessage("You are disconnected from the server");
            }
            else
            {
                Logger.Get().Warn(this, "Could not disconnect client, it was not connected");
            }

            // We are disconnected, so we stopped updating heart beats
            MonoBehaviourUtil.Instance.OnUpdateEvent -= CheckHeartBeat;

            _heartBeatReceiveStopwatch.Stop();
        }
예제 #2
0
        /// <summary>
        /// Callback method for when the net client establishes a connection with a server.
        /// </summary>
        /// <param name="loginResponse">The login response received from the server.</param>
        private void OnClientConnect(LoginResponse loginResponse)
        {
            // First relay the addon order from the login response to the addon manager
            _addonManager.UpdateNetworkedAddonOrder(loginResponse.AddonOrder);

            // We should only be able to connect during a gameplay scene,
            // which is when the player is spawned already, so we can add the username
            _playerManager.AddNameToPlayer(HeroController.instance.gameObject, _username,
                                           _playerManager.LocalPlayerTeam);

            Logger.Get().Info(this, "Client is connected, sending Hello packet");

            // If we are in a non-gameplay scene, we transmit that we are not active yet
            var currentSceneName = SceneUtil.GetCurrentSceneName();

            if (SceneUtil.IsNonGameplayScene(currentSceneName))
            {
                Logger.Get().Error(this,
                                   $"Client connected during a non-gameplay scene named {currentSceneName}, this should never happen!");
                return;
            }

            var transform = HeroController.instance.transform;
            var position  = transform.position;

            Logger.Get().Info(this, "Sending Hello packet");

            _netClient.UpdateManager.SetHelloServerData(
                _username,
                SceneUtil.GetCurrentSceneName(),
                new Vector2(position.x, position.y),
                transform.localScale.x > 0,
                (ushort)AnimationManager.GetCurrentAnimationClip()
                );

            // Since we are probably in the pause menu when we connect, set the timescale so the game
            // is running while paused
            PauseManager.SetTimeScale(1.0f);

            UiManager.InternalChatBox.AddMessage("You are connected to the server");

            try {
                ConnectEvent?.Invoke();
            } catch (Exception e) {
                Logger.Get().Warn(this,
                                  $"Exception thrown while invoking Connect event, {e.GetType()}, {e.Message}, {e.StackTrace}");
            }
        }
예제 #3
0
        private void OnClientConnect()
        {
            // We should only be able to connect during a gameplay scene,
            // which is when the player is spawned already, so we can add the username
            ThreadUtil.RunActionOnMainThread(() => {
                _playerManager.AddNameToPlayer(HeroController.instance.gameObject, _username,
                                               _playerManager.LocalPlayerTeam);
            });

            Logger.Get().Info(this, "Client is connected, sending Hello packet");

            // If we are in a non-gameplay scene, we transmit that we are not active yet
            var currentSceneName = SceneUtil.GetCurrentSceneName();

            if (SceneUtil.IsNonGameplayScene(currentSceneName))
            {
                Logger.Get().Error(this,
                                   $"Client connected during a non-gameplay scene named {currentSceneName}, this should never happen!");
                return;
            }

            var transform = HeroController.instance.transform;
            var position  = transform.position;

            Logger.Get().Info(this, "Sending Hello packet");

            _netClient.UpdateManager.SetHelloServerData(
                _username,
                SceneUtil.GetCurrentSceneName(),
                new Math.Vector2(position.x, position.y),
                transform.localScale.x > 0,
                (ushort)_animationManager.GetCurrentAnimationClip()
                );

            // Since we are probably in the pause menu when we connect, set the timescale so the game
            // is running while paused
            PauseManager.SetTimeScale(1.0f);

            // We have established a TCP connection so we should receive heart beats now
            _heartBeatReceiveStopwatch.Reset();
            _heartBeatReceiveStopwatch.Start();

            MonoBehaviourUtil.Instance.OnUpdateEvent += CheckHeartBeat;

            Ui.UiManager.InfoBox.AddMessage("You are connected to the server");
        }
예제 #4
0
        /// <summary>
        /// Disconnect the local client from the server.
        /// </summary>
        /// <param name="sendDisconnect">Whether to tell the server we are disconnecting.</param>
        public void Disconnect(bool sendDisconnect = true)
        {
            if (_netClient.IsConnected)
            {
                if (sendDisconnect)
                {
                    // First send the server that we are disconnecting
                    Logger.Get().Info(this, "Sending PlayerDisconnect packet");
                    _netClient.UpdateManager.SetPlayerDisconnect();
                }

                // Then actually disconnect
                _netClient.Disconnect();

                // Let the player manager know we disconnected
                _playerManager.OnDisconnect();

                // Clear the player data dictionary
                _playerData.Clear();

                _uiManager.OnClientDisconnect();

                _addonManager.ClearNetworkedAddonIds();

                // Check whether the game is in the pause menu and reset timescale to 0 in that case
                if (UIManager.instance.uiState.Equals(UIState.PAUSED))
                {
                    PauseManager.SetTimeScale(0);
                }

                try {
                    DisconnectEvent?.Invoke();
                } catch (Exception e) {
                    Logger.Get().Warn(this,
                                      $"Exception thrown while invoking Disconnect event, {e.GetType()}, {e.Message}, {e.StackTrace}");
                }
            }
            else
            {
                Logger.Get().Warn(this, "Could not disconnect client, it was not connected");
            }
        }