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.
 
 
 
 
 

272 lines
9.6 KiB

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<string> usersInLobby = new List<string>();
private long discordLobbyId = -1;
private bool ready = false;
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>().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<InputField>();
ip = mainMenu.FindRecursive("IPAddress")?.GetComponent<InputField>();
port = mainMenu.FindRecursive("Port")?.GetComponent<InputField>();
serverName = serverMenu.FindRecursive("ServerName")?.GetComponent<Text>();
serverScroll = serverMenu.FindRecursive("Content");
lobbyCode = lobbyMenu.FindRecursive("LobbyCode")?.GetComponent<Text>();
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<InputField>().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 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) {
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() {
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) {
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>().text = lobby;
lobbyGO.gameObject.SetActive(true);
}
// conn.Close();
}
serverName.text = Client.GetConnection().Name();
serverMenu.GetComponentInChildren<Scrollable>().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) {
Debug.Log(conn.GetLobby());
var games = conn.GetGames();
foreach(ConnectionService.Game game in games) {
var gameGO = Instantiate(lobbyScroll.transform.GetChild(0), lobbyScroll.transform);
gameGO.GetComponentsInChildren<Text>()[0].text = game.Name;
gameGO.GetComponentsInChildren<Text>()[1].text = game.Version;
gameGO.GetComponentsInChildren<Text>()[2].text = string.Join(", ", game.Authors);
gameGO.GetComponentsInChildren<Text>()[3].text = conn.GetLobbyVoteCount(game.Id) + " votes";
gameGO.GetComponentsInChildren<Text>()[4].text = ""+game.Id;
gameGO.gameObject.SetActive(true);
}
var selfVote = conn.GetSelfVote(username.text);
if (selfVote != null)
ready = selfVote.Ready;
//Debug.Log(selfVote);
lobbyMenu.transform.Find("ReadyButton").GetComponent<Image>().color = ready ? new Color(0, 255, 0) : new Color(255, 255, 255);
var users = conn.GetUsersInSameLobby();
lobbyMenu.FindRecursive("Usercount").GetComponent<Text>().text = "x " + users.Count;
usersInLobby = users;
// conn.Close();
if (reloadDiscord) {
Debug.Log(discordLobbyId);
if (discordLobbyId == -1)
DiscordUtils.CreateLobby(10, (lobby) => {
discordLobbyId = lobby.Id;
});
else
DiscordUtils.UpdateLobby(discordLobbyId, 10);
}
}
lobbyMenu.GetComponentInChildren<Scrollable>().Reload();
}
}
}