using PashaBibko.PenguinChase.GameState; using PashaBibko.PenguinChase.Extensions; using Unity.Services.Relay.Models; using Unity.Services.Relay; using System.Collections; using Unity.Netcode; using UnityEngine; using System; namespace PashaBibko.PenguinChase.Network { public class UnityRelayTransport : INetworkTransport { private const int MAX_CONNECTIONS = 7; public IEnumerator Join(string code, Action callback) { yield return Authenticator.Authenticate(); JoinAllocation allocation; { Result result = new(); yield return RelayService.Instance .JoinAllocationAsync(code) .Await(result); allocation = result.Value; } Network.CurrentTransportComponent.SetRelayServerData(allocation); NetworkManager.Singleton.StartClient(); callback.Invoke(); } public IEnumerator Host(Action callback) { yield return Authenticator.Authenticate(); Debug.Log("Authenticated"); Allocation allocation; { Result result = new(); yield return RelayService.Instance .CreateAllocationAsync(MAX_CONNECTIONS) .Await(result); allocation = result.Value; } string joinCode; { Result result = new(); yield return RelayService.Instance .GetJoinCodeAsync(allocation.AllocationId) .Await(result); joinCode = result.Value; } Network.CurrentTransportComponent.SetHostRelayData(allocation); NetworkManager.Singleton.StartHost(); GameStateSpawner.CreateNetworkGameStateController(); ConnectionManager.CreateNetworkConnectionManager(); Debug.Log($"Started server with code: [{joinCode}]"); callback.Invoke(); } } }