2024-12-19 10:20:44 +00:00
|
|
|
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;
|
2024-12-17 10:44:39 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class RankRow : MonoBehaviour
|
|
|
|
{
|
2024-12-19 10:20:44 +00:00
|
|
|
private DatabaseReference m_Reference;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private TextMeshPro rankText;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private TextMeshPro nameText;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private TextMeshPro goldText;
|
2024-12-17 10:44:39 +00:00
|
|
|
|
2024-12-19 10:20:44 +00:00
|
|
|
[SerializeField]
|
|
|
|
private TextMeshPro timeText;
|
2024-12-17 10:44:39 +00:00
|
|
|
|
2024-12-19 10:20:44 +00:00
|
|
|
[SerializeField]
|
|
|
|
private TextMeshPro triesText;
|
2024-12-17 10:44:39 +00:00
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
2024-12-19 10:20:44 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-12-17 10:44:39 +00:00
|
|
|
|
2024-12-19 10:20:44 +00:00
|
|
|
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}");
|
|
|
|
}
|
|
|
|
});
|
2024-12-17 10:44:39 +00:00
|
|
|
}
|
2024-12-19 10:20:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-12-17 10:44:39 +00:00
|
|
|
|
2024-12-19 10:20:44 +00:00
|
|
|
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()
|
2024-12-17 10:44:39 +00:00
|
|
|
{
|
2024-12-19 10:20:44 +00:00
|
|
|
// "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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-12-17 10:44:39 +00:00
|
|
|
}
|
2024-12-19 10:20:44 +00:00
|
|
|
|
2024-12-17 10:44:39 +00:00
|
|
|
}
|