Skip to content

Commit fa2b4ba

Browse files
committed
simplifying LobbyServiceFacade delete, leaveLobby and reconnect method calls
1 parent 5dc2c6d commit fa2b4ba

File tree

3 files changed

+27
-36
lines changed

3 files changed

+27
-36
lines changed

Assets/Scripts/ConnectionManagement/ConnectionState/ClientConnectingState.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
3-
using Unity.BossRoom.UnityServices.Lobbies;
43
using Unity.Multiplayer.Samples.Utilities;
54
using UnityEngine;
6-
using VContainer;
75

86
namespace Unity.BossRoom.ConnectionManagement
97
{
@@ -13,10 +11,6 @@ namespace Unity.BossRoom.ConnectionManagement
1311
/// </summary>
1412
class ClientConnectingState : OnlineState
1513
{
16-
[Inject]
17-
protected LobbyServiceFacade m_LobbyServiceFacade;
18-
[Inject]
19-
protected LocalLobby m_LocalLobby;
2014
ConnectionMethodBase m_ConnectionMethod;
2115

2216
public ClientConnectingState Configure(ConnectionMethodBase baseConnectionMethod)

Assets/Scripts/ConnectionManagement/ConnectionState/ClientReconnectingState.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using Unity.BossRoom.Infrastructure;
4+
using Unity.BossRoom.UnityServices.Lobbies;
45
using UnityEngine;
56
using VContainer;
67

@@ -15,19 +16,19 @@ namespace Unity.BossRoom.ConnectionManagement
1516
/// </summary>
1617
class ClientReconnectingState : ClientConnectingState
1718
{
19+
[Inject]
20+
LobbyServiceFacade m_LobbyServiceFacade;
1821
[Inject]
1922
IPublisher<ReconnectMessage> m_ReconnectMessagePublisher;
2023

2124
Coroutine m_ReconnectCoroutine;
22-
string m_LobbyCode = "";
2325
int m_NbAttempts;
2426

2527
const float k_TimeBetweenAttempts = 5;
2628

2729
public override void Enter()
2830
{
2931
m_NbAttempts = 0;
30-
m_LobbyCode = m_LobbyServiceFacade.CurrentUnityLobby != null ? m_LobbyServiceFacade.CurrentUnityLobby.LobbyCode : "";
3132
m_ReconnectCoroutine = m_ConnectionManager.StartCoroutine(ReconnectCoroutine());
3233
}
3334

@@ -108,14 +109,14 @@ IEnumerator ReconnectCoroutine()
108109
Debug.Log($"Reconnecting attempt {m_NbAttempts + 1}/{m_ConnectionManager.NbReconnectAttempts}...");
109110
m_ReconnectMessagePublisher.Publish(new ReconnectMessage(m_NbAttempts, m_ConnectionManager.NbReconnectAttempts));
110111
m_NbAttempts++;
111-
if (!string.IsNullOrEmpty(m_LobbyCode)) // Attempting to reconnect to lobby.
112+
if (m_LobbyServiceFacade.CurrentUnityLobby != null) // Attempting to reconnect to lobby.
112113
{
113114
// When using Lobby with Relay, if a user is disconnected from the Relay server, the server will notify
114115
// the Lobby service and mark the user as disconnected, but will not remove them from the lobby. They
115116
// then have some time to attempt to reconnect (defined by the "Disconnect removal time" parameter on
116117
// the dashboard), after which they will be removed from the lobby completely.
117118
// See https://docs.unity.com/lobby/reconnect-to-lobby.html
118-
var reconnectingToLobby = m_LobbyServiceFacade.ReconnectToLobbyAsync(m_LocalLobby?.LobbyID);
119+
var reconnectingToLobby = m_LobbyServiceFacade.ReconnectToLobbyAsync();
119120
yield return new WaitUntil(() => reconnectingToLobby.IsCompleted);
120121

121122
// If succeeded, attempt to connect to Relay

Assets/Scripts/UnityServices/Lobbies/LobbyServiceFacade.cs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,34 +84,31 @@ public void BeginTracking()
8484
}
8585
}
8686

87-
public Task EndTracking()
87+
public async void EndTracking()
8888
{
89-
var task = Task.CompletedTask;
9089
if (CurrentUnityLobby != null)
9190
{
92-
var lobbyId = m_LocalLobby?.LobbyID;
93-
94-
if (!string.IsNullOrEmpty(lobbyId))
91+
if (m_LocalUser.IsHost)
9592
{
96-
if (m_LocalUser.IsHost)
97-
{
98-
task = DeleteLobbyAsync(lobbyId);
99-
}
100-
else
101-
{
102-
task = LeaveLobbyAsync(lobbyId);
103-
}
93+
await DeleteLobbyAsync();
10494
}
95+
else
96+
{
97+
await LeaveLobbyAsync();
98+
}
99+
}
100+
101+
if (m_LobbyEvents != null)
102+
{
103+
await m_LobbyEvents.UnsubscribeAsync();
105104
}
106105

107-
m_LobbyEvents?.UnsubscribeAsync();
108106
if (m_IsTracking)
109107
{
110108
m_IsTracking = false;
111109
m_HeartbeatTime = 0;
112110
m_JoinedLobbyContentHeartbeat.EndTracking();
113111
}
114-
return task;
115112
}
116113

117114
/// <summary>
@@ -225,7 +222,7 @@ void ResetLobby()
225222
// no need to disconnect Netcode, it should already be handled by Netcode's callback to disconnect
226223
}
227224

228-
async void OnLobbyChanges(ILobbyChanges changes)
225+
void OnLobbyChanges(ILobbyChanges changes)
229226
{
230227
if (changes.LobbyDeleted)
231228
{
@@ -250,21 +247,20 @@ async void OnLobbyChanges(ILobbyChanges changes)
250247
}
251248

252249
m_UnityServiceErrorMessagePub.Publish(new UnityServiceErrorMessage("Host left the lobby", "Disconnecting.", UnityServiceErrorMessage.Service.Lobby));
253-
await EndTracking();
254-
250+
EndTracking();
255251
// no need to disconnect Netcode, it should already be handled by Netcode's callback to disconnect
256252
}
257253
}
258254
}
259255

260256
void OnKickedFromLobby()
261257
{
262-
throw new NotImplementedException();
258+
Debug.Log("Kicked from Lobby");
259+
ResetLobby();
263260
}
264261

265262
void OnLobbyEventConnectionStateChanged(LobbyEventConnectionState lobbyEventConnectionState)
266263
{
267-
268264
Debug.Log($"LobbyEventConnectionState changed to {lobbyEventConnectionState}");
269265
}
270266

@@ -307,11 +303,11 @@ public async Task RetrieveAndPublishLobbyListAsync()
307303
}
308304
}
309305

310-
public async Task<Lobby> ReconnectToLobbyAsync(string lobbyId)
306+
public async Task<Lobby> ReconnectToLobbyAsync()
311307
{
312308
try
313309
{
314-
return await m_LobbyApiInterface.ReconnectToLobby(lobbyId);
310+
return await m_LobbyApiInterface.ReconnectToLobby(m_LocalLobby.LobbyID);
315311
}
316312
catch (LobbyServiceException e)
317313
{
@@ -328,12 +324,12 @@ public async Task<Lobby> ReconnectToLobbyAsync(string lobbyId)
328324
/// <summary>
329325
/// Attempt to leave a lobby
330326
/// </summary>
331-
public async Task LeaveLobbyAsync(string lobbyId)
327+
public async Task LeaveLobbyAsync()
332328
{
333329
string uasId = AuthenticationService.Instance.PlayerId;
334330
try
335331
{
336-
await m_LobbyApiInterface.RemovePlayerFromLobby(uasId, lobbyId);
332+
await m_LobbyApiInterface.RemovePlayerFromLobby(uasId, m_LocalLobby.LobbyID);
337333
ResetLobby();
338334
}
339335
catch (LobbyServiceException e)
@@ -366,13 +362,13 @@ public async void RemovePlayerFromLobbyAsync(string uasId, string lobbyId)
366362
}
367363
}
368364

369-
public async Task DeleteLobbyAsync(string lobbyId)
365+
public async Task DeleteLobbyAsync()
370366
{
371367
if (m_LocalUser.IsHost)
372368
{
373369
try
374370
{
375-
await m_LobbyApiInterface.DeleteLobby(lobbyId);
371+
await m_LobbyApiInterface.DeleteLobby(m_LocalLobby.LobbyID);
376372
ResetLobby();
377373
}
378374
catch (LobbyServiceException e)

0 commit comments

Comments
 (0)