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.
190 lines
6.6 KiB
190 lines
6.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;
|
|
|
|
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 ConnectServer() {
|
|
try {
|
|
var conn = Client.GetConnection();
|
|
if (conn == null) {
|
|
if (ip.text != "" && port.text != "")
|
|
Client.Connect(username.text, ip.text + ":" + port.text);
|
|
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.");
|
|
}
|
|
}
|
|
}
|
|
|
|
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 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 ReloadMenu() {
|
|
if(serverMenu.activeSelf) {
|
|
for (int i = serverScroll.transform.childCount-1; i >= 1 ; i--) {
|
|
DestroyImmediate(serverScroll.transform.GetChild(i).gameObject);
|
|
}
|
|
// Simulate Lobbies in a Server
|
|
var conn = Client.GetConnection();
|
|
if(conn != null) {
|
|
foreach(string lobby in conn.getPublicLobbies()) {
|
|
Debug.Log(lobby);
|
|
}
|
|
// conn.Close();
|
|
}
|
|
for (int i = 0; i < Random.Range(2, 15); i++) {
|
|
var lobby = Instantiate(serverScroll.transform.GetChild(0), serverScroll.transform);
|
|
lobby.GetComponentInChildren<Text>().text = "LBY" + Random.Range(0, 10) + Random.Range(0, 10) + Random.Range(0, 10);
|
|
lobby.gameObject.SetActive(true);
|
|
}
|
|
serverName.text = Client.GetConnection().Name();
|
|
serverMenu.GetComponentInChildren<Scrollable>().Reload();
|
|
}
|
|
if (lobbyMenu.activeSelf) {
|
|
for (int i = lobbyScroll.transform.childCount-1; i >= 1; i--) {
|
|
DestroyImmediate(lobbyScroll.transform.GetChild(i).gameObject);
|
|
}
|
|
// Add Lobby Games to list when function exists
|
|
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<Text>()[0].text = game.Name;
|
|
gameGO.GetComponentsInChildren<Text>()[1].text = game.Version;
|
|
gameGO.GetComponentsInChildren<Text>()[2].text = string.Join(", ", game.Authors);
|
|
gameGO.gameObject.SetActive(true);
|
|
}
|
|
// conn.Close();
|
|
}
|
|
|
|
lobbyMenu.GetComponentInChildren<Scrollable>().Reload();
|
|
}
|
|
}
|
|
}
|
|
|