From 5ce32ab5a4d67dcb20bdaae8da3f9452076ebad6 Mon Sep 17 00:00:00 2001 From: VuNgocHaiC7 Date: Mon, 11 May 2026 17:50:25 +0700 Subject: [PATCH] fix logout when disconnect --- .../Scripts/Network/UnityGameSession.cs | 26 ++++++++++- .../Scripts/Tech3CSDK/Tech3CSDKWrapper.cs | 46 ++++++++++++------- .../Scripts/UI/Login/BtnBackToSelectRole.cs | 12 ++++- .../Scripts/UI/Login/LoginScreenUI.cs | 34 +++++++++++--- 4 files changed, 92 insertions(+), 26 deletions(-) diff --git a/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs b/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs index 03dc90ee87..7703cb0417 100644 --- a/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs +++ b/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs @@ -38,6 +38,8 @@ namespace BrewMonster.Network private string _username = ""; private string _password = ""; private bool _isIntentionalDisconnect = false; + + private bool _wasUnexpectedlyDisconnected = false; // When true, prevent all outgoing protocols (background ticks, tasks, etc.) from sending. private bool _suppressOutgoing = false; @@ -267,7 +269,10 @@ namespace BrewMonster.Network if (entryTarget == LogoutFlowState.LoginEntryTarget.LoginUI) { // Account logout only; half logout keeps SDK credentials so LoginScene can reopen role select. - Tech3CSDKWrapper.Instance.Logout(); + if (Tech3CSDKWrapper.Instance.EnsureInitialized()) + { + Tech3CSDKWrapper.Instance.Logout(); + } } } @@ -357,6 +362,14 @@ namespace BrewMonster.Network SceneManager.UnloadSceneAsync(currentScene); } + public bool ConsumeUnexpectedDisconnect() + { + bool value = _wasUnexpectedlyDisconnected; + _wasUnexpectedlyDisconnected = false; + return value; + } + + private Task WaitForDisconnectAsync(int timeoutMs) { if (_gameSession == null || !_gameSession.IsConnected) @@ -803,6 +816,8 @@ namespace BrewMonster.Network return; } + _wasUnexpectedlyDisconnected = true; + // Show disconnect message box CECUIManager.Instance?.ShowMessageBoxYes("Disconnected", "Connection to the server has been lost.", null, () => @@ -813,6 +828,15 @@ namespace BrewMonster.Network } + public bool TryConsumeUnexpectedDisconnect() + { + if(!_wasUnexpectedlyDisconnected) + return false; + + _wasUnexpectedlyDisconnected = false; + return true; + } + public static void c2s_CmdGoto(float x, float y, float z) { Instance._gameSession.c2s_CmdGoto(x, y, z); diff --git a/Assets/PerfectWorld/Scripts/Tech3CSDK/Tech3CSDKWrapper.cs b/Assets/PerfectWorld/Scripts/Tech3CSDK/Tech3CSDKWrapper.cs index 348554316c..e22deb65aa 100644 --- a/Assets/PerfectWorld/Scripts/Tech3CSDK/Tech3CSDKWrapper.cs +++ b/Assets/PerfectWorld/Scripts/Tech3CSDK/Tech3CSDKWrapper.cs @@ -37,28 +37,40 @@ namespace BrewMonster.Scripts SetupCallbacks(); // Auto-initialize SDK - if (!string.IsNullOrEmpty(clientId) && !string.IsNullOrEmpty(clientSecret)) + EnsureInitialized(); + + BMLogger.Log("[SDK] Tech3C SDK Started"); + } + + public bool EnsureInitialized() + { + if (Tech3CSDK.Instance.IsInitialized) { - BMLogger.Log($"Initializing Tech3C SDK...\n Client ID: {clientId}"); - Tech3CSDK.Instance.Initialize(clientId, clientSecret); - - if (Tech3CSDK.Instance.IsInitialized) - { - BMLogger.Log("[SDK] SDK Initialized Successfully!"); - } - else - { - BMLogger.Log("[SDK] Failed to initialize SDK"); - } + isInitialized = true; + return true; + } + + if(string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret)) + { + BMLogger.LogError("[SDK] Client ID and/or Client Secret not set. Please configure in Inspector."); + isInitialized = false; + return false; + } + + BMLogger.Log($"Initializing Tech3C SDK...\n Client ID: {clientId}"); + Tech3CSDK.Instance.Initialize(clientId, clientSecret); + + if (Tech3CSDK.Instance.IsInitialized) + { + BMLogger.Log("[SDK] SDK Initialized Successfully!"); } else { - BMLogger.Log("[SDK] Client ID and/or Client Secret not set. Please configure in Inspector."); + BMLogger.Log("[SDK] Failed to initialize SDK"); } - - BMLogger.Log("[SDK] Tech3C SDK Started"); - isInitialized = true; + isInitialized = Tech3CSDK.Instance.IsInitialized; + return isInitialized; } private void SetupCallbacks() @@ -205,4 +217,4 @@ namespace BrewMonster.Scripts #endregion } -} \ No newline at end of file +} diff --git a/Assets/PerfectWorld/Scripts/UI/Login/BtnBackToSelectRole.cs b/Assets/PerfectWorld/Scripts/UI/Login/BtnBackToSelectRole.cs index 5d5abc9835..32f6cc51cd 100644 --- a/Assets/PerfectWorld/Scripts/UI/Login/BtnBackToSelectRole.cs +++ b/Assets/PerfectWorld/Scripts/UI/Login/BtnBackToSelectRole.cs @@ -47,10 +47,20 @@ namespace BrewMonster.UI void OnClickYes() { CECGameRun.Instance.GetPendingLogOut().AppendForSaveConfig(new CECPendingLogoutHalf()); - if (!UnityGameSession.Instance.GameSession.IsConnectedInternet) + + if (UnityGameSession.Instance != null && UnityGameSession.Instance.TryConsumeUnexpectedDisconnect()) + { + EC_Game.GetGameRun().SetLogoutFlag(1); + UnityGameSession.LogoutAccount(); + return; + } + + if (UnityGameSession.Instance == null || !UnityGameSession.Instance.GameSession.IsConnected) { //force log out half EC_Game.GetGameRun().SetLogoutFlag(1); + UnityGameSession.LogoutAccount(); + return; } } diff --git a/Assets/PerfectWorld/Scripts/UI/Login/LoginScreenUI.cs b/Assets/PerfectWorld/Scripts/UI/Login/LoginScreenUI.cs index ecfa569367..302d8ef2e1 100644 --- a/Assets/PerfectWorld/Scripts/UI/Login/LoginScreenUI.cs +++ b/Assets/PerfectWorld/Scripts/UI/Login/LoginScreenUI.cs @@ -138,16 +138,37 @@ namespace BrewMonster.UI _loginInProgress = true; if (_loginButton != null) _loginButton.interactable = false; + bool sdkReady = Tech3CSDKWrapper.Instance.EnsureInitialized(); + // If username or password is empty, use Tech3C SDK login UI. if (string.IsNullOrEmpty(_usernameInputField.text) || string.IsNullOrEmpty(_passwordInputField.text)) { - // Use Tech3C SDK login UI. - bool started = Tech3CSDKWrapper.Instance.Login(); - if (!started) + if (sdkReady) { - // Fallback: manual username/password login (useful in dev if SDK not configured). - BMLogger.LogWarning("[LoginScreenUI] Tech3CSDKWrapper.Login() failed, fallback to manual login."); - await BeginGameLoginAsync(_usernameInputField.text, _passwordInputField.text); + // Use Tech3C SDK login UI. + bool started = Tech3CSDKWrapper.Instance.Login(); + if (!started) + { + // Fallback: manual username/password login (useful in dev if SDK not configured). + BMLogger.LogWarning("[LoginScreenUI] Tech3CSDKWrapper.Login() failed, fallback to manual login."); + await BeginGameLoginAsync(_usernameInputField.text, _passwordInputField.text); + } + } + else + { + string savedUsername = PlayerPrefs.GetString("username", ""); + string savedPassword = PlayerPrefs.GetString("password", ""); + if (!string.IsNullOrEmpty(savedUsername) && !string.IsNullOrEmpty(savedPassword)) + { + BMLogger.LogWarning("[LoginScreenUI] Tech3C SDK not ready, using saved username/password for login."); + await BeginGameLoginAsync(savedUsername, savedPassword); + } + else + { + BMLogger.LogError("[LoginScreenUI] Tech3C SDK not ready and no saved username/password."); + _loginInProgress = false; + if (_loginButton != null) _loginButton.interactable = true; + } } } else @@ -156,7 +177,6 @@ namespace BrewMonster.UI BMLogger.LogError("[LoginScreenUI] Username/password empty."); await BeginGameLoginAsync(_usernameInputField.text, _passwordInputField.text); } - } private async Task BeginGameLoginAsync(string username, string password)