2025-01-02 10:44:51 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BlueWater.Tycoons;
|
|
|
|
using BlueWater.Uis;
|
|
|
|
using ExcelDataReader.Log;
|
2024-12-26 20:14:15 +00:00
|
|
|
using Firebase.Database;
|
|
|
|
using Firebase.Extensions;
|
|
|
|
using UnityEngine;
|
2025-01-02 10:44:51 +00:00
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
using UnityEngine.Serialization;
|
2024-12-26 20:14:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace BlueWater
|
|
|
|
{
|
|
|
|
public class BlueWaterRankUserData
|
|
|
|
{
|
2025-01-02 10:44:51 +00:00
|
|
|
public string Id { get; set; } //저장시에만 사용함.
|
|
|
|
public string Name { get; set; }
|
|
|
|
public int Round { get; set; }
|
|
|
|
public int Gold { get; set; }
|
|
|
|
public int Time { get; set; }
|
|
|
|
public int Tries { get; set; }
|
|
|
|
public string NextID { get; set; }
|
2024-12-26 20:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class FirebaseManager : Singleton<FirebaseManager>
|
|
|
|
{
|
|
|
|
private DatabaseReference _reference;
|
2025-01-02 10:44:51 +00:00
|
|
|
public List<BlueWaterRankUserData> UserDatas;
|
|
|
|
private string _lastDatasName;
|
|
|
|
|
2025-01-03 02:44:54 +00:00
|
|
|
[NonSerialized]
|
|
|
|
public TycoonResultUi resultUi;
|
2025-01-02 10:44:51 +00:00
|
|
|
|
|
|
|
public BlueWaterRankUserData thisUser;
|
|
|
|
|
2024-12-26 20:14:15 +00:00
|
|
|
protected override void OnAwake()
|
|
|
|
{
|
|
|
|
_reference = FirebaseDatabase.DefaultInstance.RootReference;
|
2025-01-02 10:44:51 +00:00
|
|
|
UserDatas = new List<BlueWaterRankUserData>();
|
2024-12-26 20:14:15 +00:00
|
|
|
}
|
|
|
|
|
2025-01-02 10:44:51 +00:00
|
|
|
private void OnDestroy()
|
|
|
|
{
|
|
|
|
UserDatas.Clear();
|
|
|
|
}
|
|
|
|
|
2025-01-03 02:44:54 +00:00
|
|
|
public FirebaseManager GetFirebaseManger()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
2025-01-02 10:44:51 +00:00
|
|
|
|
|
|
|
async Task UpdateUserData(Action action = null)
|
2024-12-26 20:14:15 +00:00
|
|
|
{
|
2025-01-02 10:44:51 +00:00
|
|
|
UserDatas.Clear();
|
2025-01-03 02:44:54 +00:00
|
|
|
UserDatas = new List<BlueWaterRankUserData>();
|
|
|
|
|
2025-01-02 10:44:51 +00:00
|
|
|
var snapshot = await FirebaseDatabase.DefaultInstance.GetReference("Users").GetValueAsync();
|
|
|
|
string nextID = "__Root";
|
|
|
|
|
|
|
|
for (int i = 0; i < snapshot.ChildrenCount; i++)
|
|
|
|
{
|
|
|
|
try
|
2024-12-26 20:14:15 +00:00
|
|
|
{
|
2025-01-02 10:44:51 +00:00
|
|
|
var child = snapshot.Child(nextID);
|
|
|
|
if (child == null)
|
2024-12-26 20:14:15 +00:00
|
|
|
{
|
2025-01-02 10:44:51 +00:00
|
|
|
break; // 유효하지 않으면 루프 종료
|
2024-12-26 20:14:15 +00:00
|
|
|
}
|
2025-01-02 10:44:51 +00:00
|
|
|
|
|
|
|
BlueWaterRankUserData user = new BlueWaterRankUserData
|
|
|
|
{
|
|
|
|
Name = child.Child("Name")?.Value?.ToString() ?? "Unknown",
|
|
|
|
Round = int.Parse(child.Child("Round")?.Value?.ToString() ?? "0"),
|
|
|
|
Gold = int.Parse(child.Child("Gold")?.Value?.ToString() ?? "0"),
|
|
|
|
Time = int.Parse(child.Child("Time")?.Value?.ToString() ?? "0"),
|
|
|
|
Tries = int.Parse(child.Child("Tries")?.Value?.ToString() ?? "0"),
|
|
|
|
NextID = child.Child("NextID")?.Value?.ToString()
|
|
|
|
};
|
|
|
|
|
|
|
|
nextID = user.NextID;
|
|
|
|
if (string.IsNullOrEmpty(nextID))
|
2024-12-26 20:14:15 +00:00
|
|
|
{
|
2025-01-02 10:44:51 +00:00
|
|
|
//Debug.Log($"Name : {user.Name} / Round : {user.Round} / Gold : {user.Gold} / Time : {user.Time}");
|
|
|
|
//Debug.Log("NextNAME END");
|
|
|
|
break; // 다음 nextID 유효하지 않으면 루프 종료
|
|
|
|
}
|
|
|
|
|
2025-01-03 02:44:54 +00:00
|
|
|
// Debug.Log($"Name : {user.Name} / Round : {user.Round} / Gold : {user.Gold} / Time : {user.Time}");
|
2025-01-02 10:44:51 +00:00
|
|
|
// Debug.Log("ADD");
|
|
|
|
UserDatas.Add(user);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
// Debug.LogError($"Exception occurred: {ex.Message}");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
action?.Invoke(); // 데이터 로딩 완료 시 호출
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CreateRank(Action action = null)
|
|
|
|
{
|
|
|
|
// 통신 가능 여부 체크
|
2025-01-03 02:44:54 +00:00
|
|
|
if (Application.internetReachability == NetworkReachability.NotReachable) {Debug.Log("Network ERROR!!"); return;}
|
2025-01-02 10:44:51 +00:00
|
|
|
|
|
|
|
_ = UpdateUserData(() =>
|
|
|
|
{
|
2025-01-03 02:44:54 +00:00
|
|
|
Debug.Log("Start Set this User");
|
2025-01-02 10:44:51 +00:00
|
|
|
thisUser = new BlueWaterRankUserData();
|
|
|
|
thisUser.Id = GetOriginalUserID();
|
|
|
|
thisUser.Round = int.Parse(TycoonManager.Instance.GetCurrentLevelData().Idx);
|
|
|
|
thisUser.Gold = resultUi._goldSpent;
|
|
|
|
thisUser.Time = (int)resultUi._playTime;
|
|
|
|
thisUser.Tries = ES3.Load(SaveData.Tries, 0);
|
2025-01-03 02:44:54 +00:00
|
|
|
Debug.Log("End Set this User");
|
2024-12-26 20:14:15 +00:00
|
|
|
|
2025-01-02 10:44:51 +00:00
|
|
|
int i = 0;
|
|
|
|
for (; i < 102; i++) //__Root포함 탐색 (최대 랭크 100위 + Root + 자기자신 = 102)
|
|
|
|
{
|
2025-01-03 02:44:54 +00:00
|
|
|
|
|
|
|
Debug.Log(i);
|
2025-01-02 10:44:51 +00:00
|
|
|
if (i < UserDatas.Count)
|
|
|
|
{
|
|
|
|
if (UserDatas[i].Round > thisUser.Round) {continue;}
|
|
|
|
if (UserDatas[i].Round == thisUser.Round && UserDatas[i].Gold > thisUser.Gold) {continue;}
|
|
|
|
if (UserDatas[i].Round == thisUser.Round && UserDatas[i].Gold == thisUser.Gold && UserDatas[i].Time < thisUser.Time) {continue;}
|
|
|
|
if (UserDatas[i].Round == thisUser.Round && UserDatas[i].Gold == thisUser.Gold && UserDatas[i].Time == thisUser.Time && UserDatas[i].Tries <= thisUser.Tries) {continue;}
|
|
|
|
//100위까지 랭크가 기입되어 있고 100위권 안에 들어갔을때
|
|
|
|
//마지막 항목은 삭제...
|
|
|
|
if (UserDatas.Count >= 100) //__Root포함 100개
|
2024-12-26 20:14:15 +00:00
|
|
|
{
|
2025-01-02 10:44:51 +00:00
|
|
|
//Debug.Log("Remove : " + UserDatas[^2].NextID);
|
|
|
|
_reference.Child("Users").Child(UserDatas[^2].NextID).RemoveValueAsync(); //삭제
|
2024-12-26 20:14:15 +00:00
|
|
|
}
|
2025-01-02 10:44:51 +00:00
|
|
|
|
|
|
|
UserDatas.Insert(i , thisUser);
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (i >= 101) { UserDatas.Add(thisUser);break;} //랭크 밖... 101위
|
|
|
|
else{ UserDatas.Add(thisUser); } //랭크 등록갯수가 100개 미만일경우
|
|
|
|
|
|
|
|
if (i == 1) //__Root(1등) 처리
|
|
|
|
{
|
|
|
|
_reference.Child("Users").Child("__Root").Child("NextID").SetValueAsync(thisUser.Id);
|
|
|
|
thisUser.NextID = UserDatas[0].NextID;
|
2025-01-03 02:44:54 +00:00
|
|
|
//Debug.Log("thisUser.NextID = " + thisUser.NextID);
|
2024-12-26 20:14:15 +00:00
|
|
|
}
|
2025-01-02 10:44:51 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_reference.Child("Users").Child(UserDatas[i - 2].NextID).Child("NextID").SetValueAsync(thisUser.Id);
|
|
|
|
thisUser.NextID = UserDatas[i-1].NextID;
|
2025-01-03 02:44:54 +00:00
|
|
|
//Debug.Log("thisUser.NextID = " + thisUser.NextID);
|
2025-01-02 10:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WriteUserData(thisUser);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
thisUser.NextID = null;
|
|
|
|
|
|
|
|
action?.Invoke();
|
|
|
|
});
|
|
|
|
|
2024-12-26 20:14:15 +00:00
|
|
|
}
|
2025-01-02 10:44:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
private void WriteUserData(BlueWaterRankUserData userData)
|
|
|
|
{
|
|
|
|
_reference.Child("Users").Child(userData.Id).Child("Name").SetValueAsync("Empty");
|
|
|
|
_reference.Child("Users").Child(userData.Id).Child("Round").SetValueAsync(userData.Round);
|
|
|
|
_reference.Child("Users").Child(userData.Id).Child("Gold").SetValueAsync(userData.Gold);
|
|
|
|
_reference.Child("Users").Child(userData.Id).Child("Time").SetValueAsync(userData.Time);
|
|
|
|
_reference.Child("Users").Child(userData.Id).Child("Tries").SetValueAsync(userData.Tries);
|
|
|
|
_reference.Child("Users").Child(userData.Id).Child("Language").SetValueAsync(CultureInfo.CurrentCulture.Name);
|
|
|
|
_reference.Child("Users").Child(userData.Id).Child("NextID").SetValueAsync(userData.NextID);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WirteUserDataName(string changename) //이름변경
|
2024-12-26 20:14:15 +00:00
|
|
|
{
|
2025-01-02 10:44:51 +00:00
|
|
|
_reference.Child("Users").Child(thisUser.Id).Child("Name").SetValueAsync(changename);
|
|
|
|
}
|
|
|
|
|
2025-01-03 02:44:54 +00:00
|
|
|
private string GetOriginalUserID()
|
2025-01-02 10:44:51 +00:00
|
|
|
{
|
|
|
|
string uniqueID = Environment.MachineName;
|
|
|
|
DateTime utcNow = DateTime.UtcNow;
|
|
|
|
TimeZoneInfo pstZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
|
|
|
|
DateTime pstTime = TimeZoneInfo.ConvertTimeFromUtc(utcNow, pstZone);
|
|
|
|
uniqueID += pstTime.ToString("-yyyy-MM-dd-HH-mm-ss");
|
|
|
|
|
|
|
|
return uniqueID;
|
2024-12-26 20:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|