Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using IngameDebugConsole;
- using System.Collections.Generic;
- using Unity.Services.Authentication;
- using Unity.Services.Core;
- using Unity.Services.Lobbies;
- using Unity.Services.Lobbies.Models;
- using UnityEngine.UI;
- using Unity.Services.Relay;
- using Unity.Services.Relay.Models;
- using Unity.Netcode;
- using Unity.Netcode.Transports.UTP;
- using Unity.Networking.Transport.Relay;
- using TMPro;
- using UnityEngine;
- public class TestLobby : MonoBehaviour
- {
- // Start is called before the first frame update
- private Lobby hostLobby;
- private Lobby joinedLobby;
- private float timer;
- private string playerName;
- private float timer2;
- public TMP_InputField input;
- public TMP_InputField nameInput;
- public TextMeshProUGUI codeText;
- public bool createdLobby = false;
- public string players;
- public TextMeshProUGUI playersText;
- public float textTimer = 0.0f;
- public Transform inputTransform;
- public Transform createTransform;
- public Transform codeTransform;
- public TestRelay testRelay;
- public Transform canvas;
- private async void Start()
- {
- await UnityServices.InitializeAsync();
- AuthenticationService.Instance.SignedIn += () =>
- {
- Debug.Log("Signed in" + AuthenticationService.Instance.PlayerId);
- };
- //playerName = "User" + UnityEngine.Random.Range(0, 100);
- Debug.Log(playerName);
- await AuthenticationService.Instance.SignInAnonymouslyAsync();
- }
- public void Update()
- {
- playerName = nameInput.text;
- if (Input.GetKeyDown(KeyCode.Y))
- {
- CreateLobby();
- }
- if (Input.GetKeyDown(KeyCode.U))
- {
- ListLobbies();
- }
- if (Input.GetKeyDown(KeyCode.I))
- {
- PrintPlayers(joinedLobby);
- }
- codeText.SetText(hostLobby.LobbyCode);
- HandleLobbyHeartbeat();
- HandleLobbyPollForUpdates();
- playersText.SetText("Players: " + players);
- textTimer += 1.0f * Time.deltaTime;
- if(textTimer >= 0.5f)
- {
- PrintPlayers();
- UpdatePlayerName(playerName);
- textTimer = 0.0f;
- }
- if(joinedLobby == null)
- {
- }
- Debug.Log(playerName);
- }
- private async void HandleLobbyHeartbeat()
- {
- if (hostLobby != null)
- {
- timer -= 1.0f * Time.deltaTime;
- if(timer < 0.0f)
- {
- float timerMax = 15.0f;
- timer = timerMax;
- await LobbyService.Instance.SendHeartbeatPingAsync(hostLobby.Id);
- }
- }
- }
- private async void HandleLobbyPollForUpdates()
- {
- if (joinedLobby != null)
- {
- timer2 -= 1.0f * Time.deltaTime;
- if (timer2 < 0.0f)
- {
- float timerMax2 = 1.1f;
- timer2 = timerMax2;
- Lobby lobby = await LobbyService.Instance.GetLobbyAsync(joinedLobby.Id);
- joinedLobby = lobby;
- }
- }
- if(joinedLobby.Data["StartGame"].Value != "0")
- {
- if (!IsLobbyHost())
- {
- testRelay.JoinRelay(joinedLobby.Data["GameStart"].Value);
- }
- }
- joinedLobby = null;
- }
- private async void CreateLobby()
- {
- try
- {
- string lobbyName = "lobby";
- int maxPlayers = 4;
- CreateLobbyOptions createLobbyOptions = new CreateLobbyOptions
- {
- IsPrivate = false,
- Player = GetPlayer(),
- Data = new Dictionary<string, DataObject>
- {
- {"GameMode", new DataObject(DataObject.VisibilityOptions.Public, "Arena") },
- {"StartGame", new DataObject(DataObject.VisibilityOptions.Member, "0") }
- }
- };
- Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, createLobbyOptions);
- hostLobby = lobby;
- joinedLobby = hostLobby;
- Debug.Log("Lobby Created" + lobby.Name + ", " + lobby.MaxPlayers + ", " + lobby.Id + ", " + lobby.LobbyCode);
- createTransform.transform.gameObject.SetActive(false);
- inputTransform.transform.gameObject.SetActive(false);
- codeTransform.transform.gameObject.SetActive(false);
- PrintPlayers(hostLobby);
- } catch (LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- private async void ListLobbies()
- {
- try
- {
- QueryLobbiesOptions queryLobbiesOptions = 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 queryResponse = await Lobbies.Instance.QueryLobbiesAsync(queryLobbiesOptions);
- Debug.Log("Lobbies Found: " + queryResponse.Results.Count);
- foreach(Lobby lobby in queryResponse.Results)
- {
- Debug.Log(lobby.Name + " " + lobby.MaxPlayers + ", " + lobby.Data["GameMode"].Value);
- }
- }
- catch(LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- private async void JoinLobbyByCode(string lobbyCode)
- {
- try
- {
- JoinLobbyByCodeOptions joinLobbyByCodeOptions = new JoinLobbyByCodeOptions
- {
- Player = GetPlayer()
- };
- Lobby lobby = await Lobbies.Instance.JoinLobbyByCodeAsync(lobbyCode, joinLobbyByCodeOptions);
- joinedLobby = lobby;
- PrintPlayers(joinedLobby);
- Debug.Log("Joined Lobby: " + lobbyCode);
- createTransform.transform.gameObject.SetActive(false);
- inputTransform.transform.gameObject.SetActive(false);
- codeTransform.transform.gameObject.SetActive(false);
- }
- catch (LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- private async void QuickJoin()
- {
- try
- {
- await LobbyService.Instance.QuickJoinLobbyAsync();
- }
- catch(LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- private Player GetPlayer(){
- return new Player {
- Data = new Dictionary<string, PlayerDataObject> {
- { "PlayerName", new PlayerDataObject(PlayerDataObject.VisibilityOptions.Public, playerName) }
- }
- };
- }
- private void PrintPlayers()
- {
- PrintPlayers(joinedLobby);
- }
- private void PrintPlayers(Lobby lobby)
- {
- //Debug.Log("Players in Lobby " + lobby.Name + ", " + lobby.Data["GameMode"].Value);
- players = "";
- foreach(Player player in lobby.Players)
- {
- players += player.Data["PlayerName"].Value + ", ";
- }
- //Debug.Log(players);
- playersText.SetText("Players: " + players);
- }
- private async void UpdateLobbyGamemode(string gamemode)
- {
- try
- {
- hostLobby = await Lobbies.Instance.UpdateLobbyAsync(hostLobby.Id, new UpdateLobbyOptions
- {
- Data = new Dictionary<string, DataObject>{
- { "GameMode", new DataObject(DataObject.VisibilityOptions.Public, gamemode ) }
- }
- });
- joinedLobby = hostLobby;
- PrintPlayers(hostLobby);
- }
- catch (LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- private async void UpdatePlayerName(string newPlayerName)
- {
- try
- {
- playerName = newPlayerName;
- await LobbyService.Instance.UpdatePlayerAsync(joinedLobby.Id, AuthenticationService.Instance.PlayerId, new UpdatePlayerOptions
- {
- Data = new Dictionary<string, PlayerDataObject>{
- { "PlayerName", new PlayerDataObject(PlayerDataObject.VisibilityOptions.Public, playerName) }
- }
- });
- }
- catch (LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- private async void LeaveLobby()
- {
- try
- {
- await LobbyService.Instance.RemovePlayerAsync(joinedLobby.Id, AuthenticationService.Instance.PlayerId);
- }
- catch (LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- private async void KickPlayer()
- {
- try
- {
- await LobbyService.Instance.RemovePlayerAsync(joinedLobby.Id, joinedLobby.Players[1].Id);
- }
- catch (LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- private async void MigrateLobbyHost()
- {
- try
- {
- hostLobby = await Lobbies.Instance.UpdateLobbyAsync(hostLobby.Id, new UpdateLobbyOptions
- {
- HostId = joinedLobby.Players[1].Id
- });
- joinedLobby = hostLobby;
- PrintPlayers(hostLobby);
- }
- catch (LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- private async void DeleteLobby()
- {
- try
- {
- await LobbyService.Instance.DeleteLobbyAsync(joinedLobby.Id);
- }
- catch (LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- public bool IsLobbyHost()
- {
- return joinedLobby != null && joinedLobby.HostId == AuthenticationService.Instance.PlayerId;
- }
- public async void StartGame()
- {
- try
- {
- canvas.transform.gameObject.SetActive(false);
- string relayCode = await testRelay.CreateRelay();
- Lobby lobby = await Lobbies.Instance.UpdateLobbyAsync(joinedLobby.Id, new UpdateLobbyOptions
- {
- Data = new Dictionary<string, DataObject>
- {
- { "StartGame", new DataObject(DataObject.VisibilityOptions.Member, relayCode) }
- }
- });
- joinedLobby = lobby;
- }
- catch (LobbyServiceException e)
- {
- Debug.Log(e);
- }
- }
- public void ClickSetName()
- {
- playerName = nameInput.text;
- UpdatePlayerName(playerName);
- Debug.Log(playerName);
- }
- public void ClickJoin()
- {
- JoinLobbyByCode(input.text);
- }
- public void ClickCreateLobby()
- {
- CreateLobby();
- }
- public void ClickStartGame()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement