using System.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System; 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; private bool ready = false; private Lobby.LobbyStatus lobbyStatus; [HideInInspector] public string currentUsername; 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; DontDestroyOnLoad(gameObject); } 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() { var conn = Client.GetConnection(); if(conn != null) { if (conn.IsStarting()) { SceneManager.LoadScene(1); conn.GetGameStatus(); } } } 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(); currentUsername = username.text; } if (conn != null) { mainMenu.SetActive(false); serverMenu.SetActive(true); lobbyMenu.SetActive(false); ReloadMenu(); } } catch (Exception e) { Debug.Log(e.ToString()); 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) { conn.JoinLobby(code); Debug.Log("Lobby joined"); mainMenu.SetActive(false); serverMenu.SetActive(false); lobbyMenu.SetActive(true); lobbyCode.text = "Code: " + code; ReloadMenu(); } } public void ConnectLobby(Text codeText) { ConnectLobby(codeText.text); } public void Vote(uint game) { var conn = Client.GetConnection(); if (conn != null) { conn.Vote(game); ReloadMenu(false); } } public void Vote(Text gameText) { Vote(uint.Parse(gameText.text)); } public void SetReady() { var conn = Client.GetConnection(); if (conn != null) { conn.SetReady(); ReloadMenu(false); } } public void ExitGame() { Application.Quit(); } public void CreateLobby() { var conn = Client.GetConnection(); if (conn != null) { var lobby = conn.GetLobby(); if (lobby == null) { conn.eventManager.AddHandler("ui_create_lobby", (Connection.LobbyCode lobby1) => { Debug.Log("Lobby created"); lobby = Base32.ToString(lobby1.Code); mainMenu.SetActive(false); serverMenu.SetActive(false); lobbyMenu.SetActive(true); lobbyCode.text = "Code: " + lobby; ReloadMenu(); conn.eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCreateLobby, "ui_create_lobby"); }); conn.CreateLobby(true); } } } 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() { Debug.Log(lobbyMenu.transform); 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(bool reloadDiscord = true) { if (mainMenu.activeSelf) { DiscordUtils.SetPresence(0, 0, false, "", ""); if (discordLobbyId != -1) discordLobbyId = -1; } if (serverMenu.activeSelf) { Client.GetConnection().eventManager.AddHandler("ui_list_public_lobbies", (Protocol.LobbyCodes codes) => { for (int i = serverScroll.transform.childCount - 1; i >= 1; i--) { DestroyImmediate(serverScroll.transform.GetChild(i).gameObject); } foreach (string lobby in codes.LobbyCodes_.ToList().Select(code => Base32.ToString(code.Code))) { var lobbyGO = Instantiate(serverScroll.transform.GetChild(0), serverScroll.transform); lobbyGO.GetComponentInChildren().text = lobby; lobbyGO.gameObject.SetActive(true); } Client.GetConnection().eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnPublicLobbies, "ui_list_public_lobbies"); }); Client.GetConnection().GetPublicLobbies(); Client.GetConnection().eventManager.AddHandler("name_handler", (Common.Name name) => { serverName.text = name.Name_; Client.GetConnection().eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnName, "name_handler"); }); Client.GetConnection().QueryName(); // Protocol.ServerClientPacket p = null; // while (p == null) { // Client.GetConnection().conn.q.TryDequeue(out p); // } // serverName.text = p.ReturnName.Name_; // serverName.text = Client.GetConnection().Name(); serverMenu.GetComponentInChildren().Reload(); if (reloadDiscord) { 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) { Client.GetConnection().eventManager.AddHandler("ui_game_lister", (Protocol.Games games) => { foreach (Connection.Game game in games.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.GetComponentsInChildren()[3].text = conn.GetLobbyVoteCount(game.Id) + " votes"; gameGO.GetComponentsInChildren()[4].text = "" + game.Id; gameGO.gameObject.SetActive(true); } Client.GetConnection().eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnGames, "ui_game_lister"); }); conn.GetGames(); var selfVote = conn.GetSelfVote(username.text); if (selfVote != null) ready = selfVote.Ready; //Debug.Log(selfVote); lobbyMenu.transform.Find("ReadyButton").GetComponent().color = ready ? new Color(0, 255, 0) : new Color(255, 255, 255); Client.GetConnection().eventManager.AddHandler("ui_same_users_lobby", (Protocol.Names usernames) => { List users = new List(); foreach (Common.Name username in usernames.Names_) { users.Add(username.Name_); } lobbyMenu.FindRecursive("Usercount").GetComponent().text = "x " + users.Count; usersInLobby = users; Client.GetConnection().eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnUsers, "ui_same_users_lobby"); }); conn.GetUsersInSameLobby(); // conn.Close(); if (reloadDiscord) { Debug.Log(discordLobbyId); if (discordLobbyId == -1) DiscordUtils.CreateLobby(10, (lobby) => { discordLobbyId = lobby.Id; }); else DiscordUtils.UpdateLobby(discordLobbyId, 10); } } lobbyMenu.GetComponentInChildren().Reload(); } } }