Skip to content

Commit

Permalink
feat: INetworkPlayer now has a IsHost property
Browse files Browse the repository at this point in the history
BREAKING CHANGE: NetworkPlayer constructor now requires isHost parameter
  • Loading branch information
James-Frowen committed Jul 19, 2023
1 parent 485965f commit 5d7e5b7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Assets/Mirage/Runtime/INetworkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public interface INetworkPlayer : IMessageSender, IVisibilityTracker, IObjectOwn
PlayerAuthentication Authentication { get; }
void SetAuthentication(PlayerAuthentication authentication, bool allowReplace = false);
bool IsAuthenticated { get; }
/// <summary>
/// True if this Player is the local player on the server or client
/// </summary>
bool IsHost { get; }

void Disconnect();
void MarkAsDisconnected();
Expand Down
4 changes: 2 additions & 2 deletions Assets/Mirage/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void Connect(string address = null, ushort? port = null)
Application.runInBackground = RunInBackground;

// setup all the handlers
Player = new NetworkPlayer(connection);
Player = new NetworkPlayer(connection, false);
dataHandler.SetConnection(connection, Player);

RegisterMessageHandlers();
Expand Down Expand Up @@ -230,7 +230,7 @@ internal void ConnectHost(NetworkServer server, IDataHandler serverDataHandler)

// set up client before connecting to server, server could invoke handlers
IsLocalClient = true;
Player = new NetworkPlayer(clientConn);
Player = new NetworkPlayer(clientConn, true);
dataHandler.SetConnection(clientConn, Player);

// invoke started event after everything is set up, but before peer has connected
Expand Down
6 changes: 4 additions & 2 deletions Assets/Mirage/Runtime/NetworkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public sealed class NetworkPlayer : INetworkPlayer
/// </remarks>
private readonly IConnection _connection;

public bool IsHost { get; }

/// <summary>
/// Has this player been marked as disconnected
/// <para>Messages sent to disconnected players will be ignored</para>
Expand Down Expand Up @@ -155,12 +157,12 @@ public NetworkIdentity Identity
/// Creates a new NetworkConnection with the specified address and connectionId
/// </summary>
/// <param name="networkConnectionId"></param>
public NetworkPlayer(IConnection connection)
public NetworkPlayer(IConnection connection, bool isHost)
{
_connection = connection ?? throw new ArgumentNullException(nameof(connection));
IsHost = isHost;
}


/// <summary>
/// This sends a network message to the connection.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void UpdateSent()

private void Peer_OnConnected(IConnection conn)
{
var player = new NetworkPlayer(conn);
var player = new NetworkPlayer(conn, false);
if (logger.LogEnabled()) logger.Log($"Server new player {player}");

// add connection
Expand Down Expand Up @@ -416,7 +416,7 @@ internal void AddLocalConnection(NetworkClient client, IConnection connection)
throw new InvalidOperationException("Local client connection already exists");
}

var player = new NetworkPlayer(connection);
var player = new NetworkPlayer(connection, true);
LocalPlayer = player;
LocalClient = client;

Expand Down

0 comments on commit 5d7e5b7

Please sign in to comment.