Skip to content

Commit 152448e

Browse files
feat: better lobby exception handling [MTT-2649] (#585)
* Added switch-case to only show popups for Lobby exceptions pertinent to the users * Adding input sanitization for join code Co-authored-by: Philipp Deschain <[email protected]>
1 parent 297af21 commit 152448e

File tree

5 files changed

+80
-52
lines changed

5 files changed

+80
-52
lines changed

Assets/BossRoom/Prefabs/UI/LobbyUI.prefab

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,8 @@ MonoBehaviour:
659659
m_text: 'Your Name:'
660660
m_isRightToLeft: 0
661661
m_fontAsset: {fileID: 11400000, guid: 9641ce046d2227445b9684161a165f68, type: 2}
662-
m_sharedMaterial: {fileID: -4106257185398102161, guid: 9641ce046d2227445b9684161a165f68, type: 2}
662+
m_sharedMaterial: {fileID: -4106257185398102161, guid: 9641ce046d2227445b9684161a165f68,
663+
type: 2}
663664
m_fontSharedMaterials: []
664665
m_fontMaterial: {fileID: 0}
665666
m_fontMaterials: []
@@ -975,7 +976,11 @@ MonoBehaviour:
975976
m_Calls: []
976977
m_OnValueChanged:
977978
m_PersistentCalls:
978-
m_Calls: []
979+
m_Calls: [{m_Target: {fileID: 3432713757992463987}, m_TargetAssemblyTypeName: 'Unity.Multiplayer.Samples.BossRoom.Visual.LobbyJoiningUI,
980+
Unity.Multiplayer.Samples.BossRoom.Client', m_MethodName: SanitizeJoinCodeInputText,
981+
m_Mode: 1, m_Arguments: {m_ObjectArgument: {fileID: 0}, m_ObjectArgumentAssemblyTypeName: 'UnityEngine.Object,
982+
UnityEngine', m_IntArgument: 0, m_FloatArgument: 0, m_StringArgument: '',
983+
m_BoolArgument: 0}, m_CallState: 2}]
979984
m_CaretColor: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
980985
m_CustomCaretColor: 0
981986
m_SelectionColor: {r: 0.65882355, g: 0.80784315, b: 1, a: 0.7529412}
@@ -1189,7 +1194,8 @@ MonoBehaviour:
11891194
'
11901195
m_isRightToLeft: 0
11911196
m_fontAsset: {fileID: 11400000, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
1192-
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
1197+
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e,
1198+
type: 2}
11931199
m_fontSharedMaterials: []
11941200
m_fontMaterial: {fileID: 0}
11951201
m_fontMaterials: []
@@ -1323,7 +1329,8 @@ MonoBehaviour:
13231329
m_text: Lobby List
13241330
m_isRightToLeft: 0
13251331
m_fontAsset: {fileID: 11400000, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
1326-
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
1332+
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e,
1333+
type: 2}
13271334
m_fontSharedMaterials: []
13281335
m_fontMaterial: {fileID: 0}
13291336
m_fontMaterials: []
@@ -1840,7 +1847,8 @@ MonoBehaviour:
18401847
m_text: Join
18411848
m_isRightToLeft: 0
18421849
m_fontAsset: {fileID: 11400000, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
1843-
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
1850+
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e,
1851+
type: 2}
18441852
m_fontSharedMaterials: []
18451853
m_fontMaterial: {fileID: 0}
18461854
m_fontMaterials: []
@@ -1974,7 +1982,8 @@ MonoBehaviour:
19741982
m_text: Private
19751983
m_isRightToLeft: 0
19761984
m_fontAsset: {fileID: 11400000, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
1977-
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
1985+
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e,
1986+
type: 2}
19781987
m_fontSharedMaterials: []
19791988
m_fontMaterial: {fileID: 0}
19801989
m_fontMaterials: []
@@ -2785,7 +2794,8 @@ MonoBehaviour:
27852794
m_text: Displayed Name
27862795
m_isRightToLeft: 0
27872796
m_fontAsset: {fileID: 11400000, guid: 9641ce046d2227445b9684161a165f68, type: 2}
2788-
m_sharedMaterial: {fileID: -4106257185398102161, guid: 9641ce046d2227445b9684161a165f68, type: 2}
2797+
m_sharedMaterial: {fileID: -4106257185398102161, guid: 9641ce046d2227445b9684161a165f68,
2798+
type: 2}
27892799
m_fontSharedMaterials: []
27902800
m_fontMaterial: {fileID: 0}
27912801
m_fontMaterials: []
@@ -3201,7 +3211,8 @@ MonoBehaviour:
32013211
m_text: no lobbies
32023212
m_isRightToLeft: 0
32033213
m_fontAsset: {fileID: 11400000, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
3204-
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
3214+
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e,
3215+
type: 2}
32053216
m_fontSharedMaterials: []
32063217
m_fontMaterial: {fileID: 0}
32073218
m_fontMaterials: []
@@ -3335,7 +3346,8 @@ MonoBehaviour:
33353346
m_text: 0/4
33363347
m_isRightToLeft: 0
33373348
m_fontAsset: {fileID: 11400000, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
3338-
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
3349+
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e,
3350+
type: 2}
33393351
m_fontSharedMaterials: []
33403352
m_fontMaterial: {fileID: 0}
33413353
m_fontMaterials: []
@@ -3602,7 +3614,8 @@ MonoBehaviour:
36023614
m_text: Relay
36033615
m_isRightToLeft: 0
36043616
m_fontAsset: {fileID: 11400000, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
3605-
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
3617+
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e,
3618+
type: 2}
36063619
m_fontSharedMaterials: []
36073620
m_fontMaterial: {fileID: 0}
36083621
m_fontMaterials: []
@@ -4240,7 +4253,8 @@ MonoBehaviour:
42404253
'
42414254
m_isRightToLeft: 0
42424255
m_fontAsset: {fileID: 11400000, guid: 9641ce046d2227445b9684161a165f68, type: 2}
4243-
m_sharedMaterial: {fileID: -4106257185398102161, guid: 9641ce046d2227445b9684161a165f68, type: 2}
4256+
m_sharedMaterial: {fileID: -4106257185398102161, guid: 9641ce046d2227445b9684161a165f68,
4257+
type: 2}
42444258
m_fontSharedMaterials: []
42454259
m_fontMaterial: {fileID: 0}
42464260
m_fontMaterials: []
@@ -4578,7 +4592,8 @@ MonoBehaviour:
45784592
m_text: Quick Join
45794593
m_isRightToLeft: 0
45804594
m_fontAsset: {fileID: 11400000, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
4581-
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
4595+
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e,
4596+
type: 2}
45824597
m_fontSharedMaterials: []
45834598
m_fontMaterial: {fileID: 0}
45844599
m_fontMaterials: []
@@ -4913,7 +4928,8 @@ MonoBehaviour:
49134928
m_text: Create
49144929
m_isRightToLeft: 0
49154930
m_fontAsset: {fileID: 11400000, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
4916-
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
4931+
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e,
4932+
type: 2}
49174933
m_fontSharedMaterials: []
49184934
m_fontMaterial: {fileID: 0}
49194935
m_fontMaterials: []
@@ -5047,7 +5063,8 @@ MonoBehaviour:
50475063
m_text: Join an existing lobby...
50485064
m_isRightToLeft: 0
50495065
m_fontAsset: {fileID: 11400000, guid: 9641ce046d2227445b9684161a165f68, type: 2}
5050-
m_sharedMaterial: {fileID: -4106257185398102161, guid: 9641ce046d2227445b9684161a165f68, type: 2}
5066+
m_sharedMaterial: {fileID: -4106257185398102161, guid: 9641ce046d2227445b9684161a165f68,
5067+
type: 2}
50515068
m_fontSharedMaterials: []
50525069
m_fontMaterial: {fileID: 0}
50535070
m_fontMaterials: []
@@ -5246,7 +5263,8 @@ MonoBehaviour:
52465263
m_text: Room
52475264
m_isRightToLeft: 0
52485265
m_fontAsset: {fileID: 11400000, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
5249-
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e, type: 2}
5266+
m_sharedMaterial: {fileID: -466885322316925189, guid: 1a8c97d4cbe5134499b26527f8609c7e,
5267+
type: 2}
52505268
m_fontSharedMaterials: []
52515269
m_fontMaterial: {fileID: 0}
52525270
m_fontMaterials: []

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Text.RegularExpressions;
34
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
45
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
56
using UnityEngine;
@@ -57,9 +58,22 @@ void InjectDependenciesAndInitialize(
5758
m_Subscriptions = localLobbiesRefreshedSub.Subscribe(UpdateUI);
5859
}
5960

61+
/// <summary>
62+
/// Added to the InputField component's OnValueChanged callback for the join code text.
63+
/// </summary>
64+
public void SanitizeJoinCodeInputText()
65+
{
66+
m_JoinCodeField.text = SanitizeJoinCode(m_JoinCodeField.text);
67+
}
68+
69+
string SanitizeJoinCode(string dirtyString)
70+
{
71+
return Regex.Replace(dirtyString.ToUpper(), "[^A-Z0-9]", "");
72+
}
73+
6074
public void OnJoinButtonPressed()
6175
{
62-
m_LobbyUIMediator.JoinLobbyWithCodeRequest(m_JoinCodeField.text.ToUpper());
76+
m_LobbyUIMediator.JoinLobbyWithCodeRequest(SanitizeJoinCode(m_JoinCodeField.text));
6377
}
6478

6579
void PeriodicRefresh(float _)

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,38 @@ void ServiceErrorHandler(UnityServiceErrorMessage error)
4949

5050
void HandleLobbyError(UnityServiceErrorMessage error)
5151
{
52-
var errorMessage = error.Message;
53-
54-
switch (((LobbyServiceException)error.OriginalException).Reason)
52+
var exception = error.OriginalException as LobbyServiceException;
53+
if (exception != null)
5554
{
56-
case LobbyExceptionReason.LobbyConflict:
57-
{
58-
errorMessage += "\nSee logs for possible causes and solution.";
59-
Debug.LogError($"Got service error {error.Message} with LobbyConflict. Possible conflict cause: Trying to play with two builds on the " +
60-
$"same machine. Please use command line arg '{ProfileManager.AuthProfileCommandLineArg} someName' to set a different auth profile.\n");
61-
break;
62-
}
63-
case LobbyExceptionReason.LobbyFull:
55+
switch (exception.Reason)
6456
{
65-
PopupManager.ShowPopupPanel("Failed to join lobby", "Lobby is full and can't accept more players");
66-
// Returning out of the function because we replace default popup panel with this.
67-
return;
57+
// If the error is one of the following, the player needs to know about it, so show in a popup message. Otherwise, the log in the console is sufficient.
58+
case LobbyExceptionReason.ValidationError:
59+
PopupManager.ShowPopupPanel("Validation Error", "Validation check failed on Lobby. Is the join code correctly formatted?");
60+
break;
61+
case LobbyExceptionReason.LobbyNotFound:
62+
PopupManager.ShowPopupPanel("Lobby Not Found", "Requested lobby not found. The join code is incorrect or the lobby has ended.");
63+
break;
64+
case LobbyExceptionReason.LobbyConflict:
65+
// LobbyConflict can have multiple causes. Let's add other solutions here if there's other situations that arise for this.
66+
Debug.LogError($"Got service error {error.Message} with LobbyConflict. Possible conflict cause: Trying to play with two builds on the " +
67+
$"same machine. Please use command line arg '{ProfileManager.AuthProfileCommandLineArg} someName' to set a different auth profile.\n");
68+
PopupManager.ShowPopupPanel("Failed to join Lobby", "Failed to join Lobby due to a conflict. See logs for more details.");
69+
break;
70+
case LobbyExceptionReason.NoOpenLobbies:
71+
PopupManager.ShowPopupPanel("Failed to join Lobby", "No accessible lobbies are currently available for quick-join.");
72+
break;
73+
case LobbyExceptionReason.LobbyFull:
74+
PopupManager.ShowPopupPanel("Failed to join Lobby", "Lobby is full and can't accept more players.");
75+
break;
76+
case LobbyExceptionReason.Unauthorized:
77+
PopupManager.ShowPopupPanel("Lobby error", "Received HTTP error 401 Unauthorized from Lobby Service.");
78+
break;
79+
case LobbyExceptionReason.RequestTimeOut:
80+
PopupManager.ShowPopupPanel("Lobby error", "Received HTTP error 408 Request timed out from Lobby Service.");
81+
break;
6882
}
6983
}
70-
71-
PopupManager.ShowPopupPanel("Service error: " + error.Title, errorMessage);
7284
}
7385

7486
void OnDestroy()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void OnClientDisconnect(ulong clientId)
111111
{
112112
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
113113
{
114-
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(playerId, m_LobbyServiceFacade.CurrentUnityLobby.Id, null, null);
114+
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(playerId, m_LobbyServiceFacade.CurrentUnityLobby.Id);
115115
}
116116
SessionManager<SessionPlayerData>.Instance.DisconnectClient(clientId);
117117
}
@@ -209,7 +209,7 @@ void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.Connect
209209
StartCoroutine(WaitToDenyApproval(connectionApprovedCallback));
210210
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
211211
{
212-
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(connectionPayload.playerId, m_LobbyServiceFacade.CurrentUnityLobby.Id, null, null);
212+
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(connectionPayload.playerId, m_LobbyServiceFacade.CurrentUnityLobby.Id);
213213
}
214214
}
215215
}

Assets/BossRoom/Scripts/Shared/Net/UnityServices/Lobbies/LobbyServiceFacade.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -282,27 +282,11 @@ public async void LeaveLobbyAsync(string lobbyId)
282282
await m_LobbyApiInterface.RemovePlayerFromLobby(uasId, lobbyId);
283283
}
284284

285-
public async void RemovePlayerFromLobbyAsync(string uasId, string lobbyId, Action onSuccess, Action onFailure)
285+
public async void RemovePlayerFromLobbyAsync(string uasId, string lobbyId)
286286
{
287287
if (m_LocalUser.IsHost)
288288
{
289-
var lobby = await m_LobbyApiInterface.GetLobby(lobbyId);
290-
291-
bool playerFound = false;
292-
foreach (var player in lobby.Players)
293-
{
294-
if (player.Id == uasId)
295-
{
296-
await m_LobbyApiInterface.RemovePlayerFromLobby(uasId, lobbyId);
297-
playerFound = true;
298-
break;
299-
}
300-
}
301-
302-
if (!playerFound)
303-
{
304-
Debug.Log($"Player {uasId} has already left the lobby.");
305-
}
289+
await m_LobbyApiInterface.RemovePlayerFromLobby(uasId, lobbyId);
306290
}
307291
else
308292
{

0 commit comments

Comments
 (0)