using System.Collections; using System.Collections.Generic; using Photon.Pun; using Photon.Realtime; using TMPro; using UnityEngine; using UnityEngine.Networking; using UnityEngine.SceneManagement; using UnityEngine.UI; public class MenuManager : MonoBehaviourPunCallbacks { public GameObject connectingPanel; public TMP_Text ownerUsernameTxt; public Image ownerProfiPicImage; public TMP_Text joinerUsernameTxt; public Image joinerProfilePicImage; void ConnectPhoton(){ connectingPanel.SetActive(true); PhotonNetwork.AuthValues = new AuthenticationValues(); PhotonNetwork.AuthValues.UserId = GameData.user_id; PhotonNetwork.ConnectUsingSettings(); } float timeoutTimer =25; void Update(){ if(timeoutTimer > 0){ timeoutTimer -= Time.deltaTime; if(timeoutTimer <= 0){ SceneManager.LoadScene(0); } } } void Start() { if(PhotonNetwork.IsConnected){ connectingPanel.SetActive(false); }else{ } string url = Application.absoluteURL; if (!string.IsNullOrEmpty(url)) { // Extract query parameters string betId = GetQueryParam(url, "betId"); string owner = GetQueryParam(url, "owner"); string joiner = GetQueryParam(url, "joiner"); string address = GetQueryParam(url, "address"); string user_id = GetQueryParam(url, "uid"); string pubkey = GetQueryParam(url, "pubkey"); int wager = int.Parse(GetQueryParam(url, "wager")); GameData.id = betId; GameData.ownerId= owner; GameData.joinerId = joiner; GameData.address = address; GameData.user_id = user_id; GameData.pubkey = pubkey; GameData.wager = wager; ConnectPhoton(); StartCoroutine(CoroutineUpdateGameData()); }else{ string betId = "tetris"; string owner = "did:privy:cm8tsahek00mdb857e0a7igd3" + Random.Range(1,100); string joiner = "did:privy:cm8u2diob01cu14c92y4yq5pf"; string address = "3NEY7LD9gL7cgwZ6Lw6dNjmnthdkqtStYmaQUCTiDRFk"; string pubkey = "cocD4r4yNpHxPq7CzUebxEMyLki3X4d2Y3HcTX5ptUc"; string user_id = owner; GameData.id = betId; GameData.ownerId= owner; GameData.joinerId = joiner; GameData.address = address; GameData.user_id = user_id; GameData.pubkey = pubkey; ConnectPhoton(); StartCoroutine(CoroutineUpdateGameData()); } } IEnumerator CoroutineUpdateGameData() { // Request user profile data for both owner and joiner UnityWebRequest ownerDataReq = UnityWebRequest.Get($"{Constants.API_URL}get_user_by_id.php?id={GameData.ownerId}"); UnityWebRequest joinerDataReq = UnityWebRequest.Get($"{Constants.API_URL}get_user_by_id.php?id={GameData.joinerId}"); // Wait for both requests to finish yield return ownerDataReq.SendWebRequest(); yield return joinerDataReq.SendWebRequest(); // Handle errors if any if (ownerDataReq.result != UnityWebRequest.Result.Success || joinerDataReq.result != UnityWebRequest.Result.Success) { Debug.LogError($"Error fetching data: {ownerDataReq.error}, {joinerDataReq.error}"); yield break; } // Parse the response data into UserProfile objects try{ UserProfile ownerProfile = JsonUtility.FromJson(ownerDataReq.downloadHandler.text); UserProfile joinerProfile = JsonUtility.FromJson(joinerDataReq.downloadHandler.text); // Update username text ownerUsernameTxt.text = ownerProfile.username; joinerUsernameTxt.text = joinerProfile.username; // Load the profile images using UnityWebRequest StartCoroutine(LoadProfileImage(ownerProfile.x_profile_url, ownerProfiPicImage)); StartCoroutine(LoadProfileImage(joinerProfile.x_profile_url, joinerProfilePicImage)); }catch{} yield return new WaitForSeconds(2f); if(connectingPanel.activeSelf){ yield return new WaitForSeconds(2f); } PhotonNetwork.JoinOrCreateRoom(GameData.address, new Photon.Realtime.RoomOptions(){MaxPlayers=2}, Photon.Realtime.TypedLobby.Default); } // Helper method to load an image from a URL and set it to an Image component IEnumerator LoadProfileImage(string url, Image imageComponent) { UnityWebRequest imageRequest = UnityWebRequestTexture.GetTexture(url); yield return imageRequest.SendWebRequest(); if (imageRequest.result == UnityWebRequest.Result.Success) { Texture2D texture = ((DownloadHandlerTexture)imageRequest.downloadHandler).texture; Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); imageComponent.sprite = sprite; } else { Debug.LogError($"Error loading image from {url}: {imageRequest.error}"); } } public override void OnConnectedToMaster() { base.OnConnectedToMaster(); connectingPanel.SetActive(false); } public void Play(){ PhotonNetwork.JoinRandomOrCreateRoom(new ExitGames.Client.Photon.Hashtable(), 2); connectingPanel.SetActive(true); } public override void OnJoinedRoom() { base.OnJoinedRoom(); SceneManager.LoadScene("Game"); } string GetQueryParam(string url, string key) { if (!url.Contains("?")) return null; string[] queries = url.Split('?')[1].Split('&'); foreach (string query in queries) { string[] pair = query.Split('='); if (pair.Length == 2 && pair[0] == key) return pair[1]; } return null; } }