72 lines
2.2 KiB
C#
72 lines
2.2 KiB
C#
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<JoinAllocation> 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<Allocation> result = new();
|
|
yield return RelayService.Instance
|
|
.CreateAllocationAsync(MAX_CONNECTIONS)
|
|
.Await(result);
|
|
|
|
allocation = result.Value;
|
|
}
|
|
|
|
string joinCode;
|
|
{
|
|
Result<string> 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();
|
|
}
|
|
}
|
|
}
|