You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

167 lines
4.6 KiB

using System.Collections;
using System.Collections.Generic;
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Lobbies.Models;
using Unity.Services.Lobbies;
using UnityEngine;
using Unity.Netcode.Transports.UTP;
using Unity.Netcode;
using Unity.Services.Relay.Models;
using Unity.Services.Relay;
using Unity.Networking.Transport.Relay;
using System;
public class ConnectionManager : MonoBehaviour
{
public static ConnectionManager Instance { get; private set; }
private void Awake()
{
if (Instance == null) Instance = this;
}
private Lobby hostLobby;
private float heartbeatTimer;
public async void Start()
{
await UnityServices.InitializeAsync();
AuthenticationService.Instance.SignedIn += () =>
{
Debug.Log("signed in " + AuthenticationService.Instance.PlayerId);
};
await AuthenticationService.Instance.SignInAnonymouslyAsync();
GameManager.Instance.Loaded();
}
private void Update()
{
HandleLobbyHeartbeat();
}
private async void HandleLobbyHeartbeat()
{
if (hostLobby != null)
{
heartbeatTimer -= Time.deltaTime;
if (heartbeatTimer < 0f)
{
float heartbeatTimerMax = 15f;
heartbeatTimer = heartbeatTimerMax;
await LobbyService.Instance.SendHeartbeatPingAsync(hostLobby.Id);
}
}
}
public async void JoinLocation(Action onJoin)
{
try
{
QueryLobbiesOptions queryOptions = new QueryLobbiesOptions
{
Filters = new List<QueryFilter>
{
new QueryFilter(QueryFilter.FieldOptions.Name, "LOCATIONSñ", QueryFilter.OpOptions.EQ)
}
};
QueryResponse response = await LobbyService.Instance.QueryLobbiesAsync(queryOptions);
if (response.Results.Count == 0)
{
Debug.LogError("No lobby found");
return;
}
Lobby lobby = response.Results[0];
Lobby joinedLobby = await LobbyService.Instance.JoinLobbyByIdAsync(lobby.Id);
string joinCode = joinedLobby.Data["joinCode"].Value;
JoinAllocation joinAllocation = await RelayService.Instance.JoinAllocationAsync(joinCode);
RelayServerData relayServerData = new RelayServerData(joinAllocation, "dtls");
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartClient();
onJoin?.Invoke();
}
catch (LobbyServiceException ex)
{
Debug.Log(ex);
}
}
public async void CreateLobby(string lobbyName, int maxPlayers, bool isPrivate, Action onCreated)
{
try
{
Allocation allocation = await RelayService.Instance.CreateAllocationAsync(maxPlayers);
string joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
CreateLobbyOptions options = new CreateLobbyOptions
{
IsPrivate = isPrivate,
Data = new Dictionary<string, DataObject>
{
{ "joinCode", new DataObject(DataObject.VisibilityOptions.Member, joinCode) }
}
};
hostLobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
Debug.Log(joinCode);
RelayServerData relayServerData = new RelayServerData(allocation, "dtls");
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartHost();
onCreated?.Invoke();
}
catch (LobbyServiceException ex)
{
Debug.Log(ex);
}
}
private async void ListLobbies()
{
QueryLobbiesOptions options = new QueryLobbiesOptions()
{
Count = 25,
Filters = new List<QueryFilter>()
{
new QueryFilter(QueryFilter.FieldOptions.AvailableSlots, "0", QueryFilter.OpOptions.GT)
},
Order = new List<QueryOrder>()
{
new QueryOrder(false, QueryOrder.FieldOptions.Created)
}
};
QueryResponse lobbies = await Lobbies.Instance.QueryLobbiesAsync();
foreach (var lobbie in lobbies.Results)
{
Debug.Log($"{lobbie.Name} (${lobbie.Id})");
}
}
private void JoinLobby()
{
///
LobbyService.Instance.JoinLobbyByIdAsync("tst");
}
}