Skip to content

Commit

Permalink
Fix reconnections. This allows the main scene to be reloaded without
Browse files Browse the repository at this point in the history
causing errors.

Disconnected connection should be removed from active connections

Small fix that has to do with the active connections list being modified
during enumeration

Small fix

Another fix

Small simplification, should be working now

Small improvement to how OnDestroy works

Small fix

Compilation fix

:shrug-emoji:

Updated tests

NPM ignore file

Fix reconnection issues

Small compilation fix

Small fix

Fixes for reconnections

Reformat

Self review

Restored API - no API breaks here

Fix C# SDK Tests

Super small polish

Updated readme + publish/generate scripts

Move unity-tests to unity-tests~

Resolving conflicts

Fixing conflicts
  • Loading branch information
John Detter authored and lcodes committed Oct 30, 2024
1 parent 2a62738 commit ed0fcdf
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 69 deletions.
68 changes: 39 additions & 29 deletions src/SpacetimeDBNetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,48 @@

namespace SpacetimeDB
{
// This class is only used in Unity projects.
// Attach this to a gameobject in your scene to use SpacetimeDB.
public class SpacetimeDBNetworkManager : MonoBehaviour
{
private static bool _alreadyInitialized;
// This class is only used in Unity projects.
// Attach this to a GameObject in your scene to use SpacetimeDB.
public class SpacetimeDBNetworkManager : MonoBehaviour
{
private static SpacetimeDBNetworkManager? _instance;

public void Awake()
{
// Ensure that users don't create several SpacetimeDBNetworkManager instances.
// We're using a global (static) list of active connections and we don't want several instances to walk over it several times.
if (_alreadyInitialized)
{
throw new InvalidOperationException("SpacetimeDBNetworkManager is a singleton and should only be attached once.");
}
else
{
_alreadyInitialized = true;
}
}
public void Awake()
{
// Ensure that users don't create several SpacetimeDBNetworkManager instances.
// We're using a global (static) list of active connections and we don't want several instances to walk over it several times.
if (_instance != null)
{
throw new InvalidOperationException("SpacetimeDBNetworkManager is a singleton and should only be attached once.");
}
else
{
_instance = this;
}
}

internal static HashSet<IDbConnection> ActiveConnections = new();
internal static HashSet<IDbConnection> ActiveConnections = new();

private void ForEachConnection(Action<IDbConnection> action)
{
foreach (var conn in ActiveConnections)
{
action(conn);
}
}
private List<IDbConnection> cache = new List<IDbConnection>();

private void Update() => ForEachConnection(conn => conn.FrameTick());
private void OnDestroy() => ForEachConnection(conn => conn.Disconnect());
}
private void ForEachConnection(Action<IDbConnection> action)
{
// TODO(jdetter): We're doing this for now because we can't break the API during a minor release but
// in the future we should just change ActiveConnections to be a list so that we can reverse-iterate
// through it.
cache.Clear();
cache.AddRange(ActiveConnections);

// It's common to call disconnect from Update, which will then modify the ActiveConnections collection,
// therefore we must reverse-iterate the list of connections.
for (var x = cache.Count - 1; x >= 0; x--)
{
action(cache[x]);
}
}

private void Update() => ForEachConnection(conn => conn.FrameTick());
private void OnDestroy() => ForEachConnection(conn => conn.Disconnect());
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class PlayModeExampleTest
{
// [UnityTest] - This won't work until we have reconnections
[UnityTest]
public IEnumerator SimpleConnectionTest()
{
PlayerPrefs.DeleteAll();
Expand Down Expand Up @@ -138,7 +138,7 @@ public IEnumerator CreatePlayerAndTestDecay()
Debug.Assert(massEnd < massStart, "Mass should have decayed");
}

// [UnityTest] - This won't work until we have reconnections
[UnityTest]
public IEnumerator OneOffTest1()
{
var connected = false;
Expand Down Expand Up @@ -183,7 +183,7 @@ public IEnumerator OneOffTest1()
Debug.Log($"id: {players[0].PlayerId} Username: {players[0].Name}");
}

// [UnityTest] - This won't work until we have reconnections
[UnityTest]
public IEnumerator OneOffTest2()
{
var connected = false;
Expand Down Expand Up @@ -227,7 +227,7 @@ public IEnumerator OneOffTest2()
Debug.Log($"id: {players[0].PlayerId} Username: {players[0].Name}");
}

//[UnityTest]
[UnityTest]
public IEnumerator ReconnectionViaReloadingScene()
{
var connected = false;
Expand Down
3 changes: 1 addition & 2 deletions unity-tests~/client/Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class GameManager : MonoBehaviour

public static GameManager instance;
public static Camera localCamera;
public static Dictionary<uint, PlayerController> playerIdToPlayerController =
public Dictionary<uint, PlayerController> playerIdToPlayerController =
new Dictionary<uint, PlayerController>();

public static Identity? localIdentity;
Expand All @@ -48,7 +48,6 @@ private void Start()
{
instance = this;
Application.targetFrameRate = 60;
PlayerPrefs.DeleteAll();

// Now that we’ve registered all our callbacks, lets connect to spacetimedb
conn = DbConnection.Builder().OnConnect((_conn, identity, token) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void UpdateRowEnabled(int count)

private void Update()
{
var players = GameManager.playerIdToPlayerController.Values.Select(
var players = GameManager.instance.playerIdToPlayerController.Values.Select(
a => (a, a.TotalMass())).OrderByDescending(a => a.Item2).Take(10);

var index = 0;
Expand Down
Loading

0 comments on commit ed0fcdf

Please sign in to comment.