From bf2350030b776a1c52bbe41e1baaf19088e4d77c Mon Sep 17 00:00:00 2001 From: vis2k Date: Sat, 5 Feb 2022 00:47:24 +0800 Subject: [PATCH] breaking: NetworkServer.NoExternalConnections => HasExternalConnections to avoid double negative checks as usual --- Assets/Mirror/Runtime/NetworkServer.cs | 20 ++++++++++++++++--- .../Mirror/Tests/Editor/NetworkServerTest.cs | 16 +++++++-------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 6720d0d18..c556984ae 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -250,10 +250,24 @@ internal static void RemoveLocalConnection() } /// True if we have no external connections (host is allowed) - public static bool NoExternalConnections() + // DEPRECATED 2022-02-05 + [Obsolete("Use !HasExternalConnections() instead of NoExternalConnections() to avoid double negatives.")] + public static bool NoExternalConnections() => !HasExternalConnections(); + + /// True if we have external connections (that are not host) + 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 //////////////////////////////////////////////////////////////// diff --git a/Assets/Mirror/Tests/Editor/NetworkServerTest.cs b/Assets/Mirror/Tests/Editor/NetworkServerTest.cs index 9d0438861..2dea47fcc 100644 --- a/Assets/Mirror/Tests/Editor/NetworkServerTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkServerTest.cs @@ -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();