Skip to content

Commit

Permalink
feat: adding list for AuthenticatedPlayers in NetworkServer
Browse files Browse the repository at this point in the history
Adding extra list for authenticated players so that can be looped over instead of all connections
  • Loading branch information
James-Frowen committed May 6, 2024
1 parent 075ccad commit dc07c38
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,19 @@ public class NetworkServer : MonoBehaviour
public bool IsHost => LocalClient != null && LocalClient.Active;

/// <summary>
/// A list of connections on the server.
/// All players on server (including unauthenticated players)
/// </summary>
public IReadOnlyCollection<INetworkPlayer> AllPlayers => _connections.Values;
[Obsolete("Use AllPlayers or AuthenticatedPlayers instead")]
public IReadOnlyCollection<INetworkPlayer> Players => _connections.Values;

/// <summary>
/// List of players that have Authenticated with server
/// </summary>
public IReadOnlyList<INetworkPlayer> AuthenticatedPlayers => _authenticatedPlayers;

private readonly Dictionary<IConnection, INetworkPlayer> _connections = new Dictionary<IConnection, INetworkPlayer>();
private readonly List<INetworkPlayer> _authenticatedPlayers = new List<INetworkPlayer>();

/// <summary>
/// <para>Checks if the server has been started.</para>
Expand Down Expand Up @@ -186,6 +194,7 @@ public void Stop()

// just clear list, connections will be disconnected when peer is closed
_connections.Clear();
_authenticatedPlayers.Clear();
LocalClient = null;
LocalPlayer = null;

Expand Down Expand Up @@ -229,7 +238,7 @@ public void StartServer(NetworkClient localClient = null)
Application.quitting += Stop;
if (logger.LogEnabled()) logger.Log($"NetworkServer created, Mirage version: {Version.Current}");

logger.Assert(Players.Count == 0, "Player should have been reset since previous session");
logger.Assert(_authenticatedPlayers.Count == 0, "Player should have been reset since previous session");
logger.Assert(_connections.Count == 0, "Connections should have been reset since previous session");

World = new NetworkWorld();
Expand Down Expand Up @@ -413,6 +422,7 @@ private void AuthenticationSuccess(INetworkPlayer player, AuthenticationResult r
player.Send(new AuthSuccessMessage { AuthenticatorName = result.Authenticator?.AuthenticatorName });

// add connection
_authenticatedPlayers.Add(player);
Authenticated?.Invoke(player);
}

Expand All @@ -438,6 +448,7 @@ private void Peer_OnDisconnected(IConnection conn, DisconnectReason reason)
private void RemoveConnection(INetworkPlayer player)
{
_connections.Remove(player.Connection);
_authenticatedPlayers.Remove(player);
}

/// <summary>
Expand Down

0 comments on commit dc07c38

Please sign in to comment.