Fixed relay loading

This commit is contained in:
2026-05-20 11:19:41 +01:00
parent 470f1de835
commit 8d3998945a
15 changed files with 367 additions and 250 deletions

View File

@@ -1,26 +1,46 @@
using Unity.Services.Authentication;
using PashaBibko.PenguinChase.Extensions;
using Unity.Services.Authentication;
using Unity.Services.Core;
using System.Collections;
using UnityEngine;
namespace PashaBibko.PenguinChase.Core.Network
{
public static class Authenticator
{
public static bool IsAuthenticated { get; private set; }
private static bool sIsAuthenticating;
public static IEnumerator Authenticate()
{
// Early return if already authenticated
Debug.Log($"Authenticate called, [Authenticated: {IsAuthenticated}]");
if (IsAuthenticated)
{
// User is already authenticated
yield break;
}
// TODO: Sign in via current platform
yield return UnityServices.InitializeAsync();
yield return AuthenticationService.Instance.SignInAnonymouslyAsync();
// Stops multiple authentication attempts at the same time
if (sIsAuthenticating)
{
yield return new WaitUntil(() => IsAuthenticated || !sIsAuthenticating);
yield break; // User should be logged in from other attempt
}
sIsAuthenticating = true;
Debug.Log("Initializing Unity Services");
yield return UnityServices
.InitializeAsync()
.Await();
Debug.Log("Signing in anonymously");
yield return AuthenticationService.Instance
.SignInAnonymouslyAsync()
.Await();
Debug.Log("User has been authenticated");
sIsAuthenticating = false;
IsAuthenticated = true;
}
}
}
}

View File

@@ -11,7 +11,7 @@ namespace PashaBibko.PenguinChase.Core.Network
[SerializeField] private GameObject PrefabForEachClient;
public static GameObject ClientPrefab => sInstance?.PrefabForEachClient;
private void Start()
private void Awake()
{
// Stops overlapping instances
if (sInstance is not null)

View File

@@ -59,7 +59,7 @@ namespace PashaBibko.PenguinChase.Core.Network
}
}
private void Start()
private void Awake()
{
// Stops overlapping instances
if (sInstance is not null)
@@ -91,9 +91,7 @@ namespace PashaBibko.PenguinChase.Core.Network
// Then actually hosts the lobby
yield return sConnectionManager.Host(() =>
{
SceneManager.UnloadSceneAsync("MainMenu");
NetworkManager.Singleton.SceneManager.LoadScene("GameScene", LoadSceneMode.Additive);
NetworkManager.Singleton.SceneManager.LoadScene("GameScene", LoadSceneMode.Single);
Debug.Log("Hosted");
});
}

View File

@@ -10,7 +10,7 @@ namespace PashaBibko.PenguinChase.Core.Network
{
public class UnityRelayTransport : INetworkTransport
{
private const uint MAX_CONNECTIONS = 7;
private const int MAX_CONNECTIONS = 7;
public IEnumerator Join(string code, Action callback)
{
@@ -34,11 +34,13 @@ namespace PashaBibko.PenguinChase.Core.Network
public IEnumerator Host(Action callback)
{
yield return Authenticator.Authenticate();
Debug.Log("Authenticated");
Allocation allocation;
{
Result<Allocation> result = new();
yield return RelayService.Instance
.CreateAllocationAsync(7)
.CreateAllocationAsync(MAX_CONNECTIONS)
.Await(result);
allocation = result.Value;