Skip to content

Commit

Permalink
refactor: moving ready methods to NetworkSceneManager
Browse files Browse the repository at this point in the history
BREAKING CHANGE: moving SetAllClientsNotReady and SetClientNotReady from ServerObjectManager to NetworkSceneManager
  • Loading branch information
James-Frowen committed Sep 8, 2021
1 parent 77413da commit 5dade34
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 70 deletions.
35 changes: 35 additions & 0 deletions Assets/Mirage/Runtime/NetworkSceneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ public void SetSceneIsReady()
// Let server prepare for scene change
logger.Log("[NetworkSceneManager] - OnServerChangeScene");

SetAllClientsNotReady();
OnServerStartedSceneChange?.Invoke(scenePath, sceneOperation);

if (players == null)
Expand Down Expand Up @@ -378,6 +379,7 @@ private void ServerSceneUnLoading(Scene scene, IEnumerable<INetworkPlayer> playe
// Let server prepare for scene change
if (logger.LogEnabled()) logger.Log("[NetworkSceneManager] - OnServerChangeScene");

SetAllClientsNotReady();
OnServerStartedSceneChange?.Invoke(scene.path, SceneOperation.UnloadAdditive);

// if not host
Expand Down Expand Up @@ -495,6 +497,39 @@ protected internal virtual void OnServerPlayerDisconnected(INetworkPlayer discon
}
}

/// <summary>
/// Marks all connected clients as no longer ready.
/// <para>
/// All clients will no longer be sent state synchronization updates.
/// The player's clients can call ClientManager.Ready() again to re-enter the ready state.
/// This is useful when switching scenes.
/// </para>
/// </summary>
public void SetAllClientsNotReady()
{
foreach (INetworkPlayer player in Server.Players)
{
SetClientNotReady(player);
}
}

/// <summary>
/// Sets the client of the connection to be not-ready.
/// <para>Clients that are not ready do not receive spawned objects or state synchronization updates. They client can be made ready again by calling SetClientReady().</para>
/// </summary>
/// <param name="player">The connection of the client to make not ready.</param>
public void SetClientNotReady(INetworkPlayer player)
{
if (player.SceneIsReady)
{
if (logger.LogEnabled()) logger.Log("PlayerNotReady " + player);
player.SceneIsReady = false;
player.RemoveAllVisibleObjects();

player.Send(new SceneNotReadyMessage());
}
}

#endregion

#region Scene Operations
Expand Down
34 changes: 0 additions & 34 deletions Assets/Mirage/Runtime/ServerObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void Start()

if (NetworkSceneManager != null)
{
NetworkSceneManager.OnServerStartedSceneChange.AddListener(OnStartedSceneChange);
NetworkSceneManager.OnServerFinishedSceneChange.AddListener(OnFinishedSceneChange);
}
}
Expand Down Expand Up @@ -91,11 +90,6 @@ void OnServerStopped()
nextNetworkId = 1;
}

void OnStartedSceneChange(string scenePath, SceneOperation sceneOperation)
{
SetAllClientsNotReady();
}

void OnFinishedSceneChange(string scenePath, SceneOperation sceneOperation)
{
SpawnOrActivate();
Expand Down Expand Up @@ -674,34 +668,6 @@ public void SpawnVisibleObjects(INetworkPlayer player)
SpawnObserversForConnection(player);
}

/// <summary>
/// Marks all connected clients as no longer ready.
/// <para>All clients will no longer be sent state synchronization updates. The player's clients can call ClientManager.Ready() again to re-enter the ready state. This is useful when switching scenes.</para>
/// </summary>
public void SetAllClientsNotReady()
{
foreach (INetworkPlayer player in Server.Players)
{
SetClientNotReady(player);
}
}

/// <summary>
/// Sets the client of the connection to be not-ready.
/// <para>Clients that are not ready do not receive spawned objects or state synchronization updates. They client can be made ready again by calling SetClientReady().</para>
/// </summary>
/// <param name="player">The connection of the client to make not ready.</param>
public void SetClientNotReady(INetworkPlayer player)
{
if (player.SceneIsReady)
{
if (logger.LogEnabled()) logger.Log("PlayerNotReady " + player);
player.SceneIsReady = false;
player.RemoveAllVisibleObjects();

player.Send(new SceneNotReadyMessage());
}
}

/// <summary>
/// default ready handler.
Expand Down
40 changes: 40 additions & 0 deletions Assets/Tests/Runtime/Host/PlayerSceneReadyHostTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using NUnit.Framework;
using static Mirage.Tests.LocalConnections;

namespace Mirage.Tests.Runtime.Host
{
public class PlayerSceneReadyHostTests : HostSetup<MockComponent>
{
[Test]
public void SetClientReadyAndNotReadyTest()
{
(_, NetworkPlayer connection) = PipedConnections(ClientMessageHandler, ServerMessageHandler);
Assert.That(connection.SceneIsReady, Is.False);

serverObjectManager.SpawnVisibleObjects(connection);
Assert.That(connection.SceneIsReady, Is.True);

sceneManager.SetClientNotReady(connection);
Assert.That(connection.SceneIsReady, Is.False);
}

[Test]
public void SetAllClientsNotReadyTest()
{
// add first ready client
(_, NetworkPlayer first) = PipedConnections(ClientMessageHandler, ServerMessageHandler);
first.SceneIsReady = true;
server.Players.Add(first);

// add second ready client
(_, NetworkPlayer second) = PipedConnections(ClientMessageHandler, ServerMessageHandler);
second.SceneIsReady = true;
server.Players.Add(second);

// set all not ready
sceneManager.SetAllClientsNotReady();
Assert.That(first.SceneIsReady, Is.False);
Assert.That(second.SceneIsReady, Is.False);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Runtime/Host/PlayerSceneReadyHostTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 0 additions & 36 deletions Assets/Tests/Runtime/Host/ServerObjectManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,13 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using static Mirage.Tests.LocalConnections;
using Object = UnityEngine.Object;

namespace Mirage.Tests.Runtime.Host
{

[TestFixture]
public class ServerObjectManagerHostTest : HostSetup<MockComponent>
{
[Test]
public void SetClientReadyAndNotReadyTest()
{
(_, NetworkPlayer connection) = PipedConnections(ClientMessageHandler, ServerMessageHandler);
Assert.That(connection.SceneIsReady, Is.False);

serverObjectManager.SpawnVisibleObjects(connection);
Assert.That(connection.SceneIsReady, Is.True);

serverObjectManager.SetClientNotReady(connection);
Assert.That(connection.SceneIsReady, Is.False);
}

[Test]
public void SetAllClientsNotReadyTest()
{
// add first ready client
(_, NetworkPlayer first) = PipedConnections(ClientMessageHandler, ServerMessageHandler);
first.SceneIsReady = true;
server.Players.Add(first);

// add second ready client
(_, NetworkPlayer second) = PipedConnections(ClientMessageHandler, ServerMessageHandler);
second.SceneIsReady = true;
server.Players.Add(second);

// set all not ready
serverObjectManager.SetAllClientsNotReady();
Assert.That(first.SceneIsReady, Is.False);
Assert.That(second.SceneIsReady, Is.False);
}



[Test]
public void HideForConnection()
{
Expand Down

0 comments on commit 5dade34

Please sign in to comment.