CapersProject/Assets/02.Scripts/Ui/Tycoon/RankRow.cs
2024-12-19 19:20:44 +09:00

131 lines
4.2 KiB
C#

using System.Collections.Generic;
using System.Collections;
using System.Globalization;
using System.Threading.Tasks;
using UnityEngine.Networking;
using Firebase.Database;
using Firebase.Extensions;
using TMPro;
using UnityEngine;
public class RankRow : MonoBehaviour
{
private DatabaseReference m_Reference;
[SerializeField]
private TextMeshPro rankText;
[SerializeField]
private TextMeshPro nameText;
[SerializeField]
private TextMeshPro goldText;
[SerializeField]
private TextMeshPro timeText;
[SerializeField]
private TextMeshPro triesText;
void Start()
{
m_Reference = FirebaseDatabase.DefaultInstance.RootReference;
}
public void WriteUserData(string nickname)
{
string currentLanguage = CultureInfo.CurrentCulture.Name; // 예: "en-US"
string ipAddress = GetPublicIP(); //공인 IP 주소
if (string.IsNullOrEmpty(nickname) || string.IsNullOrEmpty(ipAddress)) //비어있는 필수 객체 확인하기.
{
Debug.LogError("User ID or Username cannot be null or empty.");
return;
}
var userData = new Dictionary<string, object>
{
{ "Nickname", nickname },
{ "Language", currentLanguage },
{ "IP", ipAddress }
};
m_Reference.Child("users").Child(nickname).SetValueAsync(userData).ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
{
//Debug.LogError("Error writing data: " + task.Exception);
}
else if (task.IsCompleted)
{
//Debug.Log($"Successfully wrote data for User ID: {userId}, Username: {username}");
}
});
}
public string GetPublicIP()
{
using (UnityWebRequest webRequest = UnityWebRequest.Get("https://api.ipify.org?format=text"))
{
var operation = webRequest.SendWebRequest();
while (!operation.isDone)
{
// Wait until the operation is done
}
if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError("Error getting public IP: " + webRequest.error);
return null;
}
else
{
return webRequest.downloadHandler.text;
}
}
}
public void GetAllUsersData()
{
// "users" 노드에 있는 전체 데이터를 가져옴
m_Reference.Child("users").GetValueAsync().ContinueWithOnMainThread(task =>
{
if (task.IsFaulted)
{
// 에러 처리
Debug.LogError("Failed to get data: " + task.Exception);
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
if (snapshot.Exists)
{
Debug.Log("All Users Data:");
// users 하위 노드의 데이터를 순회
foreach (DataSnapshot userSnapshot in snapshot.Children)
{
string userId = userSnapshot.Key; // 예: 유저의 고유 키
string nickname = userSnapshot.Child("Nickname").Value?.ToString();
//string round = userSnapshot.Child("Round").Value?.ToString();
//string gold = userSnapshot.Child("Gold").Value?.ToString();
//string time = userSnapshot.Child("Time").Value?.ToString();
//string tries = userSnapshot.Child("Tries").Value?.ToString();
string language = userSnapshot.Child("Language").Value?.ToString();
string ipAddress = userSnapshot.Child("IP").Value?.ToString();
Debug.Log($"User ID: {userId}, Nickname: {nickname}, Language: {language}, IP: {ipAddress}");
}
}
else
{
Debug.Log("No users data found.");
}
}
});
}
}