fix logout when disconnect

This commit is contained in:
VuNgocHaiC7
2026-05-11 17:50:25 +07:00
parent 6cebb9079d
commit 5ce32ab5a4
4 changed files with 92 additions and 26 deletions
@@ -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);
@@ -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
}
}
}
@@ -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;
}
}
@@ -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)