Skip to content

Commit a5395d8

Browse files
committed
using ReconnectMessage to display popups when reconnecting and to remove said popup when failing to reconnect
1 parent e9a7b49 commit a5395d8

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

Assets/BossRoom/Scripts/Client/UI/ConnectionStatusMessageUIManager.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ namespace Unity.Multiplayer.Samples.BossRoom.Visual
99
/// </summary>
1010
public class ConnectionStatusMessageUIManager : MonoBehaviour
1111
{
12-
IDisposable m_Subscriptions;
12+
DisposableGroup m_Subscriptions;
1313

14-
PopupPanel m_CurrentPopup;
14+
PopupPanel m_CurrentReconnectPopup;
1515

1616
[Inject]
17-
void InjectDependencies(ISubscriber<ConnectStatus> connectStatusSub)
17+
void InjectDependencies(ISubscriber<ConnectStatus> connectStatusSub, ISubscriber<ReconnectMessage> reconnectMessageSub)
1818
{
19-
m_Subscriptions = connectStatusSub.Subscribe(OnConnectStatus);
19+
m_Subscriptions = new DisposableGroup();
20+
m_Subscriptions.Add(connectStatusSub.Subscribe(OnConnectStatus));
21+
m_Subscriptions.Add(reconnectMessageSub.Subscribe(OnReconnectMessage));
2022
}
2123

2224
void Awake()
@@ -31,10 +33,6 @@ void OnDestroy()
3133

3234
void OnConnectStatus(ConnectStatus status)
3335
{
34-
if (m_CurrentPopup != null)
35-
{
36-
m_CurrentPopup.Hide();
37-
}
3836
switch (status)
3937
{
4038
case ConnectStatus.Undefined:
@@ -44,6 +42,11 @@ void OnConnectStatus(ConnectStatus status)
4442
PopupManager.ShowPopupPanel("Connection Failed", "The Host is full and cannot accept any additional connections.");
4543
break;
4644
case ConnectStatus.Success:
45+
if (m_CurrentReconnectPopup != null)
46+
{
47+
m_CurrentReconnectPopup.Hide();
48+
m_CurrentReconnectPopup = null;
49+
}
4750
break;
4851
case ConnectStatus.LoggedInAgain:
4952
PopupManager.ShowPopupPanel("Connection Failed", "You have logged in elsewhere using the same account.");
@@ -58,12 +61,29 @@ void OnConnectStatus(ConnectStatus status)
5861
PopupManager.ShowPopupPanel("Disconnected From Host", "The host has ended the game session.");
5962
break;
6063
case ConnectStatus.Reconnecting:
61-
m_CurrentPopup = PopupManager.ShowPopupPanel("Connection lost", "Attempting to reconnect...", closeableByUser: false);
6264
break;
6365
default:
6466
Debug.LogWarning($"New ConnectStatus {status} has been added, but no connect message defined for it.");
6567
break;
6668
}
6769
}
70+
71+
void OnReconnectMessage(ReconnectMessage message)
72+
{
73+
if (message.CurrentAttempt == message.MaxAttempt)
74+
{
75+
m_CurrentReconnectPopup.Hide();
76+
m_CurrentReconnectPopup = null;
77+
}
78+
else if (m_CurrentReconnectPopup != null)
79+
{
80+
m_CurrentReconnectPopup.SetupPopupPanel("Connection lost", $"Attempting to reconnect...\nAttempt {message.CurrentAttempt+1}/{message.MaxAttempt}", closeableByUser: false);
81+
}
82+
else
83+
{
84+
m_CurrentReconnectPopup = PopupManager.ShowPopupPanel("Connection lost", $"Attempting to reconnect...\nAttempt {message.CurrentAttempt+1}/{message.MaxAttempt}", closeableByUser: false);
85+
}
86+
87+
}
6888
}
6989
}

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ClientGameNetPortal.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ private IEnumerator TryToReconnect(string lobbyCode)
201201

202202
yield return new WaitWhile(() => NetworkManager.Singleton.ShutdownInProgress); // wait until NetworkManager completes shutting down
203203
Debug.Log($"Reconnecting attempt {nbTries + 1}/{k_NbReconnectAttempts}...");
204+
m_ReconnectMessagePub.Publish(new ReconnectMessage(nbTries, k_NbReconnectAttempts));
204205
if (!string.IsNullOrEmpty(lobbyCode))
205206
{
206207
var leavingLobby = m_LobbyServiceFacade.EndTracking();
@@ -234,12 +235,13 @@ private IEnumerator TryToReconnect(string lobbyCode)
234235
NetworkManager.Singleton.Shutdown();
235236
}
236237

237-
SceneManager.LoadScene("MainMenu");
238+
SceneLoaderWrapper.Instance.LoadScene("MainMenu");
238239
if (!DisconnectReason.HasTransitionReason)
239240
{
240241
DisconnectReason.SetDisconnectReason(ConnectStatus.GenericDisconnect);
241242
}
242243
m_TryToReconnectCoroutine = null;
244+
m_ReconnectMessagePub.Publish(new ReconnectMessage(k_NbReconnectAttempts, k_NbReconnectAttempts));
243245
m_ConnectStatusPub.Publish(DisconnectReason.Reason);
244246
}
245247

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/GameNetPortal.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public struct ReconnectMessage
3030
{
3131
public int CurrentAttempt;
3232
public int MaxAttempt;
33+
34+
public ReconnectMessage(int currentAttempt, int maxAttempt)
35+
{
36+
CurrentAttempt = currentAttempt;
37+
MaxAttempt = maxAttempt;
38+
}
3339
}
3440

3541
public enum OnlineMode

0 commit comments

Comments
 (0)