86 lines
3.3 KiB
C#
86 lines
3.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Firebase.Firestore;
|
|
using Firebase.Auth;
|
|
using Firebase.Extensions;
|
|
|
|
namespace BulletHellTemplate
|
|
{
|
|
/// <summary>
|
|
/// Handles saving and loading reward data from Firebase and PlayerPrefs.
|
|
/// Prevents multiple calls to Firebase during initialization.
|
|
/// </summary>
|
|
public static class RewardSaveManager
|
|
{
|
|
/// <summary>
|
|
/// Loads rewards from Firestore for a specified collection and invokes a callback with the data.
|
|
/// </summary>
|
|
public static void LoadRewards(string collectionName, System.Action<Dictionary<string, object>> onLoaded)
|
|
{
|
|
FirebaseUser user = FirebaseAuth.DefaultInstance.CurrentUser;
|
|
if (user == null) return;
|
|
|
|
FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
|
|
DocumentReference docRef = db.Collection("Players").Document(user.UserId).Collection(collectionName).Document("RewardData");
|
|
docRef.GetSnapshotAsync().ContinueWithOnMainThread(task =>
|
|
{
|
|
if (task.IsCompleted && task.Result.Exists)
|
|
{
|
|
Dictionary<string, object> data = task.Result.ToDictionary();
|
|
onLoaded?.Invoke(data);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Failed to load reward data.");
|
|
onLoaded?.Invoke(null);
|
|
}
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Saves rewards to Firestore for a specified collection, and also stores them locally in PlayerPrefs.
|
|
/// </summary>
|
|
public static void SaveRewards(string collectionName, Dictionary<string, object> data)
|
|
{
|
|
FirebaseUser user = FirebaseAuth.DefaultInstance.CurrentUser;
|
|
if (user == null) return;
|
|
|
|
FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
|
|
DocumentReference docRef = db.Collection("Players").Document(user.UserId).Collection(collectionName).Document("RewardData");
|
|
docRef.SetAsync(data).ContinueWithOnMainThread(task =>
|
|
{
|
|
if (task.IsCompleted)
|
|
{
|
|
Debug.Log($"{collectionName} rewards saved successfully.");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Failed to save {collectionName} rewards: {task.Exception}");
|
|
}
|
|
});
|
|
|
|
// Save locally
|
|
SaveRewardsLocally(collectionName, data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Saves rewards data locally in PlayerPrefs as JSON.
|
|
/// </summary>
|
|
private static void SaveRewardsLocally(string collectionName, Dictionary<string, object> data)
|
|
{
|
|
string json = JsonUtility.ToJson(data);
|
|
PlayerPrefs.SetString(collectionName, json);
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads rewards data from PlayerPrefs if available.
|
|
/// </summary>
|
|
public static Dictionary<string, object> LoadRewardsLocally(string collectionName)
|
|
{
|
|
string json = PlayerPrefs.GetString(collectionName, null);
|
|
return string.IsNullOrEmpty(json) ? null : JsonUtility.FromJson<Dictionary<string, object>>(json);
|
|
}
|
|
}
|
|
}
|