Skip to content

Commit 59ea34c

Browse files
committed
Moved removal responsibility of player from lobby to host
1 parent 4bd7922 commit 59ea34c

File tree

4 files changed

+69
-16
lines changed

4 files changed

+69
-16
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,6 @@ private void OnDisconnectOrTimeout(ulong clientID)
153153
// not a host (in which case we know this is about us) or that the clientID is the same as ours if we are the host.
154154
if (!NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsHost && NetworkManager.Singleton.LocalClientId == clientID)
155155
{
156-
// Leave the current lobby.
157-
//TODO: When the SessionManager will use the PlayerId from AuthenticationService instead of our current custom GUID, this could be replaced by the host kicking the disconnecting client from the lobby
158-
m_LobbyServiceFacade.ForceLeaveLobbyAttempt();
159156
//On a client disconnect we want to take them back to the main menu.
160157
//We have to check here in SceneManager if our active scene is the main menu, as if it is, it means we timed out rather than a raw disconnect;
161158
if (SceneManager.GetActiveScene().name != "MainMenu")

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
using System.Collections.Generic;
33
using Unity.Collections;
44
using Unity.Multiplayer.Samples.BossRoom.Client;
5+
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
6+
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
57
using Unity.Multiplayer.Samples.Utilities;
68
using UnityEngine;
79
using Unity.Netcode;
8-
using UnityEngine.SceneManagement;
910

1011
namespace Unity.Multiplayer.Samples.BossRoom.Server
1112
{
@@ -17,21 +18,31 @@ public class ServerGameNetPortal : MonoBehaviour
1718
[SerializeField]
1819
NetworkObject m_GameState;
1920

20-
private GameNetPortal m_Portal;
21+
GameNetPortal m_Portal;
2122

2223
// used in ApprovalCheck. This is intended as a bit of light protection against DOS attacks that rely on sending silly big buffers of garbage.
23-
private const int k_MaxConnectPayload = 1024;
24+
const int k_MaxConnectPayload = 1024;
2425

2526
/// <summary>
2627
/// Keeps a list of what clients are in what scenes.
2728
/// </summary>
28-
private Dictionary<ulong, int> m_ClientSceneMap = new Dictionary<ulong, int>();
29+
Dictionary<ulong, int> m_ClientSceneMap = new Dictionary<ulong, int>();
2930

3031
/// <summary>
3132
/// The active server scene index.
3233
/// </summary>
3334
public int ServerScene { get { return UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex; } }
3435

36+
private LocalLobby m_LocalLobby;
37+
private LobbyServiceFacade m_LobbyServiceFacade;
38+
39+
[Inject]
40+
private void InjectDependencies(LocalLobby localLobby, LobbyServiceFacade lobbyServiceFacade)
41+
{
42+
m_LocalLobby = localLobby;
43+
m_LobbyServiceFacade = lobbyServiceFacade;
44+
}
45+
3546
void Start()
3647
{
3748
m_Portal = GetComponent<GameNetPortal>();
@@ -82,7 +93,7 @@ public void OnNetworkReady()
8293
/// Handles the case where NetworkManager has told us a client has disconnected. This includes ourselves, if we're the host,
8394
/// and the server is stopped."
8495
/// </summary>
85-
private void OnClientDisconnect(ulong clientId)
96+
void OnClientDisconnect(ulong clientId)
8697
{
8798
m_ClientSceneMap.Remove(clientId);
8899

@@ -91,6 +102,15 @@ private void OnClientDisconnect(ulong clientId)
91102
//the ServerGameNetPortal may be initialized again, which will cause its OnNetworkSpawn to be called again.
92103
//Consequently we need to unregister anything we registered, when the NetworkManager is shutting down.
93104
m_Portal.NetManager.OnClientDisconnectCallback -= OnClientDisconnect;
105+
m_LobbyServiceFacade.DeleteLobbyAsync(m_LocalLobby.LobbyID, null, null);
106+
}
107+
else
108+
{
109+
var playerId = SessionManager<SessionPlayerData>.Instance.GetPlayerId(clientId);
110+
if (playerId != null)
111+
{
112+
m_LobbyServiceFacade.RemovePlayerFromLobbyAsync(playerId, m_LocalLobby.LobbyID, null, null);
113+
}
94114
}
95115
}
96116

@@ -121,7 +141,7 @@ IEnumerator WaitToShutdown()
121141
m_Portal.NetManager.Shutdown();
122142
}
123143

124-
private void Clear()
144+
void Clear()
125145
{
126146
//resets all our runtime state.
127147
m_ClientSceneMap.Clear();
@@ -151,7 +171,7 @@ public bool AreAllClientsInServerScene()
151171
/// <param name="connectionData">binary data passed into StartClient. In our case this is the client's GUID, which is a unique identifier for their install of the game that persists across app restarts. </param>
152172
/// <param name="clientId">This is the clientId that Netcode assigned us on login. It does not persist across multiple logins from the same client. </param>
153173
/// <param name="connectionApprovedCallback">The delegate we must invoke to signal that the connection was approved or not. </param>
154-
private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate connectionApprovedCallback)
174+
void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate connectionApprovedCallback)
155175
{
156176
if (connectionData.Length > k_MaxConnectPayload)
157177
{
@@ -226,7 +246,7 @@ private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager
226246
}
227247
}
228248

229-
private IEnumerator WaitToDisconnect(ulong clientId)
249+
IEnumerator WaitToDisconnect(ulong clientId)
230250
{
231251
yield return new WaitForSeconds(0.5f);
232252
m_Portal.NetManager.DisconnectClient(clientId);
@@ -269,7 +289,7 @@ public void SendServerToClientConnectResult(ulong clientID, ConnectStatus status
269289
/// <summary>
270290
/// Called after the server is created- This is primarily meant for the host server to clean up or handle/set state as its starting up
271291
/// </summary>
272-
private void ServerStartedHandler()
292+
void ServerStartedHandler()
273293
{
274294
// server spawns game state
275295
var gameState = Instantiate(m_GameState);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Infrastructure;
55
using Unity.Services.Authentication;
66
using Unity.Services.Lobbies.Models;
7+
using UnityEngine;
78

89
namespace Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies
910
{
@@ -237,6 +238,30 @@ public void LeaveLobbyAsync(string lobbyId, Action onSuccess, Action onFailure)
237238
m_LobbyApiInterface.LeaveLobbyAsync(uasId, lobbyId, onSuccess, onFailure);
238239
}
239240

241+
public void RemovePlayerFromLobbyAsync(string uasId, string lobbyId, Action onSuccess, Action onFailure)
242+
{
243+
if (m_LocalUser.IsHost)
244+
{
245+
m_LobbyApiInterface.LeaveLobbyAsync(uasId, lobbyId, onSuccess, onFailure);
246+
}
247+
else
248+
{
249+
Debug.LogError("Only the host can remove other players from the lobby.");
250+
}
251+
}
252+
253+
public void DeleteLobbyAsync(string lobbyId, Action onSuccess, Action onFailure)
254+
{
255+
if (m_LocalUser.IsHost)
256+
{
257+
m_LobbyApiInterface.DeleteLobbyAsync(lobbyId, onSuccess, onFailure);
258+
}
259+
else
260+
{
261+
Debug.LogError("Only the host can delete a lobby.");
262+
}
263+
}
264+
240265
/// <summary>
241266
/// Attempt to push a set of key-value pairs associated with the local player which will overwrite any existing data for these keys.
242267
/// </summary>

Packages/com.unity.multiplayer.samples.coop/Utilities/Net/SessionManager.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,29 @@ public bool SetupConnectingPlayerSessionData(ulong clientId, string clientGUID,
179179
return success;
180180
}
181181

182+
public string GetPlayerId(ulong clientId)
183+
{
184+
if (m_ClientIDToGuid.TryGetValue(clientId, out string playerId))
185+
{
186+
return playerId;
187+
}
188+
189+
Debug.LogError($"No client guid found mapped to the given client ID: {clientId}");
190+
return null;
191+
}
192+
182193
/// <summary>
183194
///
184195
/// </summary>
185196
/// <param name="clientId"> id of the client whose data is requested</param>
186197
/// <returns>Player data struct matching the given ID</returns>
187198
public T? GetPlayerData(ulong clientId)
188199
{
189-
//First see if we have a guid matching the clientID given.
190-
191-
if (m_ClientIDToGuid.TryGetValue(clientId, out string clientGUID))
200+
//First see if we have a playerId matching the clientID given.
201+
var playerId = GetPlayerId(clientId);
202+
if (playerId != null)
192203
{
193-
return GetPlayerData(clientGUID);
204+
return GetPlayerData(playerId);
194205
}
195206

196207
Debug.LogError($"No client guid found mapped to the given client ID: {clientId}");

0 commit comments

Comments
 (0)