LobbyHandler

 avatar
unknown
csharp
a year ago
5.1 kB
6
Indexable
using UnityEngine;
using Unity.Services.Lobbies;
using Unity.Services.Lobbies.Models;
using Unity.Services.Relay.Models;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
using Unity.Networking.Transport.Relay;
using Unity.Services.Relay;
using Unity.Services.Authentication;

public class LobbyHandler : MonoBehaviour
{
    public static LobbyHandler Instance;
    public Lobby CurrentLobby { get; private set; }
    public string LobbyName { get; private set; }
    public string LobbyID { get; private set; }
    public string RoomCode { get; private set; }
    public string RelayCode { get; private set; }
    public bool RoomFound { get; private set; } = false;
    public bool IsPlayerHosting { get; private set; }

    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }

        else
        {
            Destroy(gameObject);
        }
    }

    public async void CreateLobby(string lobbyName = "Lobby")
    {
        try
        {
            int maxPlayers = 2;

            CreateLobbyOptions options = new();
            options.IsPrivate = false;

            Allocation relayAllocation = await RelayService.Instance.CreateAllocationAsync(2);

            string relayJoinCode = await RelayService.Instance.GetJoinCodeAsync(relayAllocation.AllocationId);

            options.Data = new()
            {
                {
                    "RelayCode", new DataObject
                    (visibility: DataObject.VisibilityOptions.Public,
                    value: relayJoinCode)
                }
            };

            Lobby lobby = await Lobbies.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
            NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(new RelayServerData(relayAllocation, "dtls"));
            NetworkManager.Singleton.StartHost();

            string lobbyRoomCode = lobby.LobbyCode;

            //Debug.Log($"Lobby created, ID: {lobby.Id}");
            //Debug.Log($"Room code: {lobbyRoomCode}");
            //Debug.Log($"Relay code: {relayJoinCode}");

            IsPlayerHosting = lobby.HostId == AuthenticationService.Instance.PlayerId ? true : false;
            CurrentLobby = lobby;
            LobbyName = lobbyName;
            LobbyID = lobby.Id;
            RoomCode = lobbyRoomCode;
            RelayCode = relayJoinCode;
        }
        catch (LobbyServiceException e)
        {
            Debug.LogError(e);
        }
    }

    public async void JoinLobby(string roomCode)
    {
        RoomFound = false;

        try
        {
            Debug.Log($"Attempting to join |{roomCode}|");

            Lobby lobby = await Lobbies.Instance.JoinLobbyByCodeAsync(roomCode);

            if (lobby != null)
            {
                RoomFound = true;

                if (lobby.Data.TryGetValue("RelayCode", out DataObject relayDataObject))
                {
                    var joinAllocation = await RelayService.Instance.JoinAllocationAsync(joinCode: relayDataObject.Value);

                    NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(new RelayServerData(joinAllocation, "dtls"));
                    NetworkManager.Singleton.StartClient();

                    LobbyName = lobby.Name;
                    LobbyID = lobby.Id;
                    RoomCode = lobby.LobbyCode;
                    RelayCode = relayDataObject.Value;
                    IsPlayerHosting = lobby.HostId == AuthenticationService.Instance.PlayerId ? true : false;
                    Debug.Log($"Joined lobby {lobby.Id}");
                    Debug.Log($"Relay code: {relayDataObject.Value}");
                }
            }

            else
            {
                Debug.Log($"Lobby |{roomCode}| was not found");
            }
        }

        catch (LobbyServiceException e)
        {
            Debug.LogError($"Error joining lobby: {e.Message}");
        }
    }

    public async void QuickJoin()
    {
        Debug.Log($"QuickPlay...");

        try
        {
            QuickJoinLobbyOptions options = new();

            Lobby lobby = await Lobbies.Instance.QuickJoinLobbyAsync(options);
        }

        catch (LobbyServiceException e)
        {
            Debug.Log("Couldn't find a lobby");
            Debug.LogWarning(e);
        }
    }

    public async void DeleteLobby(string lobbyId)
    {
        try
        {
            await Lobbies.Instance.DeleteLobbyAsync(lobbyId);
            NetworkManager.Singleton.Shutdown();

            await LobbyService.Instance.RemovePlayerAsync(lobbyId, AuthenticationService.Instance.PlayerId);
            Debug.Log($"Deleted lobby {lobbyId}");
            LobbyID = "";
            LobbyName = "";
            RoomCode = "";
        }

        catch (LobbyServiceException e)
        {
            Debug.LogError(e);
        }
    }
}
Editor is loading...
Leave a Comment