breaking: NetworkServer.NoExternalConnections => HasExternalConnections to avoid double negative checks as usual

This commit is contained in:
vis2k 2022-02-05 00:47:24 +08:00
parent 7488aac1d6
commit bf2350030b
2 changed files with 25 additions and 11 deletions

View File

@ -250,10 +250,24 @@ internal static void RemoveLocalConnection()
}
/// <summary>True if we have no external connections (host is allowed)</summary>
public static bool NoExternalConnections()
// DEPRECATED 2022-02-05
[Obsolete("Use !HasExternalConnections() instead of NoExternalConnections() to avoid double negatives.")]
public static bool NoExternalConnections() => !HasExternalConnections();
/// <summary>True if we have external connections (that are not host)</summary>
public static bool HasExternalConnections()
{
return connections.Count == 0 ||
(connections.Count == 1 && localConnection != null);
// any connections?
if (connections.Count > 0)
{
// only host connection?
if (connections.Count == 1 && localConnection != null)
return false;
// otherwise we have real external connections
return true;
}
return false;
}
// send ////////////////////////////////////////////////////////////////

View File

@ -1204,37 +1204,37 @@ public void SendToReady_CalledWhileNotActive_ShouldGiveWarning()
}
[Test]
public void NoExternalConnections_WithNoConnection()
public void HasExternalConnections_WithNoConnection()
{
Assert.That(NetworkServer.connections.Count, Is.EqualTo(0));
Assert.That(NetworkServer.NoExternalConnections(), Is.True);
Assert.That(NetworkServer.HasExternalConnections(), Is.False);
}
[Test]
public void NoExternalConnections_WithConnections()
public void HasExternalConnections_WithConnections()
{
NetworkServer.connections.Add(1, null);
NetworkServer.connections.Add(2, null);
Assert.That(NetworkServer.NoExternalConnections(), Is.False);
Assert.That(NetworkServer.HasExternalConnections(), Is.True);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(2));
}
[Test]
public void NoExternalConnections_WithHostOnly()
public void HasExternalConnections_WithHostOnly()
{
CreateLocalConnectionPair(out LocalConnectionToClient connectionToClient, out _);
NetworkServer.SetLocalConnection(connectionToClient);
NetworkServer.connections.Add(0, connectionToClient);
Assert.That(NetworkServer.NoExternalConnections(), Is.True);
Assert.That(NetworkServer.HasExternalConnections(), Is.False);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(1));
NetworkServer.RemoveLocalConnection();
}
[Test]
public void NoExternalConnections_WithHostAndConnection()
public void HasExternalConnections_WithHostAndConnection()
{
CreateLocalConnectionPair(out LocalConnectionToClient connectionToClient, out _);
@ -1242,7 +1242,7 @@ public void NoExternalConnections_WithHostAndConnection()
NetworkServer.connections.Add(0, connectionToClient);
NetworkServer.connections.Add(1, null);
Assert.That(NetworkServer.NoExternalConnections(), Is.False);
Assert.That(NetworkServer.HasExternalConnections(), Is.True);
Assert.That(NetworkServer.connections.Count, Is.EqualTo(2));
NetworkServer.RemoveLocalConnection();