using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Grpc.Core; public class MainMenuController : MonoBehaviour { [Header("Prefabs")] public GameObject popupPrefab; [Header("Other")] public GameObject mainMenu; public GameObject serverMenu; public GameObject lobbyMenu; private InputField username; private InputField ip; private InputField port; private Text serverName; private GameObject serverScroll; private Text lobbyCode; private GameObject lobbyScroll; private List usersInLobby = new List(); private long discordLobbyId = -1; void Start() { for(int i = 5; i < 0; i++){ Debug.Log(i); } // Simulate Lobbies in a Server //for (int i = 0; i < 2; i++) { // var lobby = Instantiate(serverMenu.transform.Find("BaseElement"), serverMenu.transform.Find("Content")); // lobby.GetComponentInChildren().text = "LBY" + Random.Range(0, 10) + Random.Range(0, 10) + Random.Range(0, 10); // lobby.gameObject.SetActive(true); //} mainMenu.SetActive(true); serverMenu.SetActive(false); lobbyMenu.SetActive(false); username = mainMenu.FindRecursive("Username")?.GetComponent(); ip = mainMenu.FindRecursive("IPAddress")?.GetComponent(); port = mainMenu.FindRecursive("Port")?.GetComponent(); serverName = serverMenu.FindRecursive("ServerName")?.GetComponent(); serverScroll = serverMenu.FindRecursive("Content"); lobbyCode = lobbyMenu.FindRecursive("LobbyCode")?.GetComponent(); lobbyScroll = lobbyMenu.FindRecursive("Content"); Popup.OnPopupClosed += OnPopupClosed; } public void OnPopupClosed(object arg, GameObject popup) { if(popup.name == "JoinALobby") { if (arg.GetType() == typeof(GameObject)) { GameObject go = (GameObject)arg; ConnectLobby(go.GetComponent().text); } } } void Update() {} public void OnApplicationQuit() { var conn = Client.GetConnection(); if(conn != null) { conn.LeaveLobby(); } Client.CloseConnection(); } public void ConnectServer() { try { var conn = Client.GetConnection(); if (conn == null) { if (ip.text != "" && port.text != "") Client.Connect(username.text, ip.text.Trim() + ":" + port.text.Trim()); else Client.Connect(username.text); conn = Client.GetConnection(); } if (conn != null) { mainMenu.SetActive(false); serverMenu.SetActive(true); lobbyMenu.SetActive(false); ReloadMenu(); } } catch(RpcException e) { if(e.StatusCode == StatusCode.Unavailable) { Popup.Open(PopupType.Alert, mainMenu.transform, "ConnectionUnavailableError", "Error", "Server unavailable.\nPlease retry later or contact the server admin.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nPOOTIS"); } } } public void LeaveServer() { Client.CloseConnection(); mainMenu.SetActive(true); serverMenu.SetActive(false); lobbyMenu.SetActive(false); ReloadMenu(); } public void JoinLobbyButtonAction() { Popup.Open(PopupType.Prompt, serverMenu.transform, "JoinALobby", "Join a Lobby", "Lobby Code"); } public void ConnectLobby(string code) { var conn = Client.GetConnection(); if (conn != null) { var lobby = conn.GetLobby(); if (lobby == null) { conn.JoinLobby(code); mainMenu.SetActive(false); serverMenu.SetActive(false); lobbyMenu.SetActive(true); lobbyCode.text = "Code: " + code; ReloadMenu(); } } } public void ConnectLobby(Text codeText) { ConnectLobby(codeText.text); } public void ExitGame() { Application.Quit(); } public void CreateLobby() { var conn = Client.GetConnection(); if (conn != null) { var lobby = conn.GetLobby(); if (lobby == null) { lobby = conn.CreateLobby(true); mainMenu.SetActive(false); serverMenu.SetActive(false); lobbyMenu.SetActive(true); lobbyCode.text = "Code: " + lobby; ReloadMenu(); } } } public void LeaveLobby() { var conn = Client.GetConnection(); if (conn != null) { var lobby = conn.GetLobby(); if (lobby != null) { conn.LeaveLobby(); mainMenu.SetActive(false); serverMenu.SetActive(true); lobbyMenu.SetActive(false); ReloadMenu(); } } } public void UserButtonClicked() { Popup.Open(PopupType.Alert, lobbyMenu.transform, "UsersInLobbyAlert", "Users", string.Join("\n", usersInLobby)); } public void DiscordInviteClicked() { Popup.Open(PopupType.List, lobbyMenu.transform, "DiscordInviteUserList", delegate (dynamic arg) { return new dynamic[] { (Discord.Relationship)arg }; }, "Invite a Friend", Resources.Load("ListElements/DiscordUser"), DiscordUtils.GetFriends().ToArray()); } public void ReloadMenu() { if(mainMenu.activeSelf) { DiscordUtils.SetPresence(0, 0, false, "", ""); if (discordLobbyId != -1) discordLobbyId = -1; } if (serverMenu.activeSelf) { for (int i = serverScroll.transform.childCount-1; i >= 1 ; i--) { DestroyImmediate(serverScroll.transform.GetChild(i).gameObject); } var conn = Client.GetConnection(); if(conn != null) { foreach(string lobby in conn.GetPublicLobbies()) { var lobbyGO = Instantiate(serverScroll.transform.GetChild(0), serverScroll.transform); lobbyGO.GetComponentInChildren().text = lobby; lobbyGO.gameObject.SetActive(true); } // conn.Close(); } serverName.text = Client.GetConnection().Name(); serverMenu.GetComponentInChildren().Reload(); if (discordLobbyId == -1) DiscordUtils.CreateLobby(10, (lobby) => { discordLobbyId = lobby.Id; //DiscordUtils.GetLobbyMetadataKeys(discordLobbyId); }); else DiscordUtils.UpdateLobby(discordLobbyId, 10); Debug.Log(discordLobbyId); } if (lobbyMenu.activeSelf) { for (int i = lobbyScroll.transform.childCount-1; i >= 1; i--) { DestroyImmediate(lobbyScroll.transform.GetChild(i).gameObject); } var conn = Client.GetConnection(); if(conn != null) { var games = conn.GetGames(); foreach(Game.Game game in games) { var gameGO = Instantiate(lobbyScroll.transform.GetChild(0), lobbyScroll.transform); gameGO.GetComponentsInChildren()[0].text = game.Name; gameGO.GetComponentsInChildren()[1].text = game.Version; gameGO.GetComponentsInChildren()[2].text = string.Join(", ", game.Authors); gameGO.gameObject.SetActive(true); } var users = conn.GetUsersInSameLobby(); lobbyMenu.FindRecursive("Usercount").GetComponent().text = "x " + users.Count; usersInLobby = users; // conn.Close(); Debug.Log(discordLobbyId); if (discordLobbyId == -1) DiscordUtils.CreateLobby(10, (lobby) => { discordLobbyId = lobby.Id; }); else DiscordUtils.UpdateLobby(discordLobbyId, 10); } lobbyMenu.GetComponentInChildren().Reload(); } } }