Skip to content

Commit 85c3bc8

Browse files
committed
Created manager to handle connection status messages
1 parent b51461d commit 85c3bc8

File tree

6 files changed

+68
-95
lines changed

6 files changed

+68
-95
lines changed

Assets/BossRoom/Scenes/Startup.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:7f94d9ef30f99dd13a417106572a3d3dda71b434c4f9a41b7b54a717971bb6a8
3-
size 33791
2+
oid sha256:7b2c33d4681d53ec62b32b7ff2c81123b4457d8dcd8d0b6d643097e74ae36856
3+
size 35006
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using Unity.Multiplayer.Samples.BossRoom;
5+
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
6+
using Unity.Multiplayer.Samples.BossRoom.Visual;
7+
using UnityEngine;
8+
9+
public class ConnectionStatusMessageUIManager : MonoBehaviour
10+
{
11+
12+
IDisposable m_Subscriptions;
13+
14+
[Inject]
15+
void InjectDependencies(ISubscriber<ConnectStatus> connectStatusSub)
16+
{
17+
m_Subscriptions = connectStatusSub.Subscribe(OnConnectStatus);
18+
}
19+
20+
void Awake()
21+
{
22+
DontDestroyOnLoad(gameObject);
23+
}
24+
25+
void OnDestroy()
26+
{
27+
m_Subscriptions?.Dispose();
28+
}
29+
30+
void OnConnectStatus(ConnectStatus status)
31+
{
32+
switch (status)
33+
{
34+
case ConnectStatus.Undefined:
35+
case ConnectStatus.UserRequestedDisconnect:
36+
break;
37+
case ConnectStatus.ServerFull:
38+
PopupPanel.ShowPopupPanel("Connection Failed", "The Host is full and cannot accept any additional connections.");
39+
break;
40+
case ConnectStatus.Success:
41+
break;
42+
case ConnectStatus.LoggedInAgain:
43+
PopupPanel.ShowPopupPanel("Connection Failed", "You have logged in elsewhere using the same account.");
44+
break;
45+
case ConnectStatus.GenericDisconnect:
46+
PopupPanel.ShowPopupPanel("Disconnected From Host", "The connection to the host was lost");
47+
break;
48+
default:
49+
Debug.LogWarning($"New ConnectStatus {status} has been added, but no connect message defined for it.");
50+
break;
51+
}
52+
}
53+
}

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

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/BossRoom/Scripts/Client/UI/Lobby/LobbyUIMediator.cs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,13 @@ ClientGameNetPortal clientGameNetPortal
4646
RegenerateName();
4747

4848
m_ClientNetPortal.NetworkTimedOut += OnNetworkTimeout;
49-
m_ClientNetPortal.ConnectFinished += OnConnectFinished;
50-
51-
//any disconnect reason set? Show it to the user here.
52-
/*ConnectStatusToMessage(m_ClientNetPortal.DisconnectReason.Reason, false);
53-
m_ClientNetPortal.DisconnectReason.Clear();*/
5449
}
5550

5651
void OnDestroy()
5752
{
5853
if (m_ClientNetPortal != null)
5954
{
6055
m_ClientNetPortal.NetworkTimedOut -= OnNetworkTimeout;
61-
m_ClientNetPortal.ConnectFinished -= OnConnectFinished;
6256
}
6357
}
6458

@@ -218,15 +212,6 @@ void UnblockUIAfterLoadingIsComplete()
218212
}
219213
}
220214

221-
/// <summary>
222-
/// Callback when the server sends us back a connection finished event.
223-
/// </summary>
224-
/// <param name="status"></param>
225-
void OnConnectFinished(ConnectStatus status)
226-
{
227-
ConnectStatusToMessage(status, true);
228-
}
229-
230215
/// <summary>
231216
/// Invoked when the client sent a connection request to the server and didn't hear back at all.
232217
/// This should create a UI letting the player know that something went wrong and to try again
@@ -235,40 +220,5 @@ void OnNetworkTimeout()
235220
{
236221
UnblockUIAfterLoadingIsComplete();
237222
}
238-
239-
/// <summary>
240-
/// Takes a ConnectStatus and shows an appropriate message to the user. This can be called on: (1) successful connect,
241-
/// (2) failed connect, (3) disconnect.
242-
/// </summary>
243-
/// <param name="connecting">pass true if this is being called in response to a connect finishing.</param>
244-
void ConnectStatusToMessage(ConnectStatus status, bool connecting)
245-
{
246-
switch (status)
247-
{
248-
case ConnectStatus.Undefined:
249-
case ConnectStatus.UserRequestedDisconnect:
250-
break;
251-
case ConnectStatus.ServerFull:
252-
PopupPanel.ShowPopupPanel("Connection Failed", "The Host is full and cannot accept any additional connections.");
253-
break;
254-
case ConnectStatus.Success:
255-
if (connecting)
256-
{
257-
PopupPanel.ShowPopupPanel("Success!", "Joining Now...");
258-
}
259-
break;
260-
case ConnectStatus.LoggedInAgain:
261-
PopupPanel.ShowPopupPanel("Connection Failed", "You have logged in elsewhere using the same account.");
262-
break;
263-
case ConnectStatus.GenericDisconnect:
264-
var title = connecting ? "Connection Failed" : "Disconnected From Host";
265-
var text = connecting ? "Something went wrong" : "The connection to the host was lost";
266-
PopupPanel.ShowPopupPanel(title, text);
267-
break;
268-
default:
269-
Debug.LogWarning($"New ConnectStatus {status} has been added, but no connect message defined for it.");
270-
break;
271-
}
272-
}
273223
}
274224
}

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

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public class PopupPanel : MonoBehaviour
2323
/// </summary>
2424
Action m_ConfirmFunction;
2525

26-
IDisposable m_Subscriptions;
27-
2826
static PopupPanel s_Instance;
2927

3028
void Awake()
@@ -36,41 +34,6 @@ void Awake()
3634
void OnDestroy()
3735
{
3836
s_Instance = null;
39-
m_Subscriptions?.Dispose();
40-
}
41-
42-
[Inject]
43-
void InjectDependencies(
44-
ISubscriber<UnityServiceErrorMessage> unityServiceErrorMessageSub,
45-
ISubscriber<ConnectStatus> connectStatusSub)
46-
{
47-
m_Subscriptions = connectStatusSub.Subscribe(OnConnectStatus);
48-
}
49-
50-
void OnConnectStatus(ConnectStatus status)
51-
{
52-
switch (status)
53-
{
54-
case ConnectStatus.Undefined:
55-
case ConnectStatus.UserRequestedDisconnect:
56-
break;
57-
case ConnectStatus.ServerFull:
58-
SetupNotifierDisplay("Connection Failed", "The Host is full and cannot accept any additional connections.");
59-
break;
60-
case ConnectStatus.Success:
61-
break;
62-
case ConnectStatus.LoggedInAgain:
63-
SetupNotifierDisplay("Connection Failed", "You have logged in elsewhere using the same account.");
64-
break;
65-
case ConnectStatus.GenericDisconnect:
66-
var title = false ? "Connection Failed" : "Disconnected From Host";
67-
var text = false ? "Something went wrong" : "The connection to the host was lost";
68-
SetupNotifierDisplay(title, text);
69-
break;
70-
default:
71-
Debug.LogWarning($"New ConnectStatus {status} has been added, but no connect message defined for it.");
72-
break;
73-
}
7437
}
7538

7639
public void OnConfirmClick()
@@ -100,15 +63,15 @@ public static void ShowPopupPanel(string titleText, string mainText, Action conf
10063
{
10164
if (s_Instance != null)
10265
{
103-
s_Instance.SetupNotifierDisplay(titleText, mainText, confirmFunction);
66+
s_Instance.SetupPopupPanel(titleText, mainText, confirmFunction);
10467
}
10568
else
10669
{
10770
Debug.LogError($"No PopupPanel instance found. Cannot display message: {titleText}: {mainText}");
10871
}
10972
}
11073

111-
void SetupNotifierDisplay(string titleText, string mainText, Action confirmFunction = null)
74+
void SetupPopupPanel(string titleText, string mainText, Action confirmFunction = null)
11275
{
11376
ResetState();
11477

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public class ClientGameNetPortal : MonoBehaviour
2828
/// </summary>
2929
private const int k_TimeoutDuration = 10;
3030

31-
public event Action<ConnectStatus> ConnectFinished;
32-
3331
/// <summary>
3432
/// This event fires when the client sent out a request to start the client, but failed to hear back after an allotted amount of
3533
/// time from the host.
@@ -109,8 +107,6 @@ public void OnConnectFinished(ConnectStatus status)
109107
//this indicates a game level failure, rather than a network failure. See note in ServerGameNetPortal.
110108
DisconnectReason.SetDisconnectReason(status);
111109
}
112-
113-
ConnectFinished?.Invoke(status);
114110
}
115111

116112
private void OnDisconnectReasonReceived(ConnectStatus status)

0 commit comments

Comments
 (0)