162 lines
6.6 KiB
C#
162 lines
6.6 KiB
C#
using UnityEngine;
|
|
|
|
namespace BulletHellTemplate
|
|
{
|
|
/// <summary>
|
|
/// Handles saving and loading inventory-related data locally (such as item upgrade levels),
|
|
/// and optionally syncing specific items to the backend.
|
|
/// </summary>
|
|
public class InventorySave : MonoBehaviour
|
|
{
|
|
public static InventorySave Singleton;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Singleton == null)
|
|
{
|
|
Singleton = this;
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Saves the upgrade level of a purchased item locally and then updates only
|
|
/// that specific item in the backend (if initialized). Does not re-save the entire list.
|
|
/// </summary>
|
|
/// <param name="uniqueItemGuid">Unique GUID identifying the purchased item.</param>
|
|
/// <param name="level">The new upgrade level.</param>
|
|
public static async void SetItemUpgradeLevelSingle(string uniqueItemGuid, int level)
|
|
{
|
|
// 1) Save locally using PlayerSave
|
|
PlayerSave.SetItemUpgradeLevel(uniqueItemGuid, level);
|
|
|
|
// 2) Update the item in MonetizationManager
|
|
var purchasedItems = MonetizationManager.Singleton.GetPurchasedInventoryItems();
|
|
var foundItem = purchasedItems.Find(i => i.uniqueItemGuid == uniqueItemGuid);
|
|
if (foundItem != null)
|
|
{
|
|
foundItem.itemLevel = level;
|
|
|
|
// 3) Save ONLY this item in Firestore, if the backend is initialized
|
|
if (BackendManager.Singleton.CheckInitialized())
|
|
{
|
|
await BackendManager.Singleton.SavePurchasedInventoryItemAsync(foundItem);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"SetItemUpgradeLevelSingle: No purchased item found with GUID {uniqueItemGuid}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// (Original) Saves the upgrade level of a purchased item locally,
|
|
/// then re-saves the entire purchased list to the backend (and also this item individually).
|
|
/// </summary>
|
|
/// <param name="uniqueItemGuid">Unique GUID of the item.</param>
|
|
/// <param name="level">The new level.</param>
|
|
public static async void SetItemUpgradeLevel(string uniqueItemGuid, int level)
|
|
{
|
|
// 1) Save locally
|
|
PlayerSave.SetItemUpgradeLevel(uniqueItemGuid, level);
|
|
|
|
// 2) Update the item in MonetizationManager
|
|
var purchasedItems = MonetizationManager.Singleton.GetPurchasedInventoryItems();
|
|
var foundItem = purchasedItems.Find(i => i.uniqueItemGuid == uniqueItemGuid);
|
|
if (foundItem != null)
|
|
{
|
|
foundItem.itemLevel = level;
|
|
|
|
// Re-saves all items
|
|
MonetizationManager.Singleton.UpdatePurchasedItems(
|
|
MonetizationManager.Singleton.GetPurchasedCharacters(),
|
|
MonetizationManager.Singleton.GetPurchasedIcons(),
|
|
MonetizationManager.Singleton.GetPurchasedFrames(),
|
|
MonetizationManager.Singleton.GetPurchasedShopItems(),
|
|
purchasedItems
|
|
);
|
|
}
|
|
|
|
// 3) Save this single item as well
|
|
if (BackendManager.Singleton.CheckInitialized() && foundItem != null)
|
|
{
|
|
await BackendManager.Singleton.SavePurchasedInventoryItemAsync(foundItem);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the item upgrade level from local data, returns 0 if not found.
|
|
/// </summary>
|
|
/// <param name="uniqueItemGuid">GUID of the purchased item.</param>
|
|
/// <returns>The upgrade level, or 0 if not found.</returns>
|
|
public static int GetItemUpgradeLevel(string uniqueItemGuid)
|
|
{
|
|
return PlayerSave.GetItemUpgradeLevel(uniqueItemGuid);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Equips an item (assigns a GUID) in the given character's slot, saving to Firestore if needed.
|
|
/// If uniqueItemGuid is empty, it means unequip and calls RemoveCharacterItemAsync.
|
|
/// </summary>
|
|
public static async void SetEquippedItemForSlot(int characterId, string slotName, string uniqueItemGuid)
|
|
{
|
|
// 1) Store locally
|
|
PlayerSave.SetCharacterSlotItem(characterId, slotName, uniqueItemGuid);
|
|
|
|
// 2) If empty, remove from Firestore
|
|
if (string.IsNullOrEmpty(uniqueItemGuid))
|
|
{
|
|
if (BackendManager.Singleton.CheckInitialized())
|
|
{
|
|
await BackendManager.Singleton.RemoveCharacterItemAsync(characterId, slotName, "the-doc-id");
|
|
}
|
|
return;
|
|
}
|
|
|
|
// 3) Otherwise, find the purchased item data
|
|
var purchasedItems = MonetizationManager.Singleton.GetPurchasedInventoryItems();
|
|
var foundItem = purchasedItems.Find(i => i.uniqueItemGuid == uniqueItemGuid);
|
|
|
|
if (foundItem != null && BackendManager.Singleton.CheckInitialized())
|
|
{
|
|
await BackendManager.Singleton.SaveCharacterItemAsync(
|
|
characterId,
|
|
slotName,
|
|
uniqueItemGuid,
|
|
foundItem.itemId,
|
|
foundItem.itemLevel
|
|
);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the unique item GUID equipped in a specific slot for a given character,
|
|
/// or empty string if none is equipped.
|
|
/// </summary>
|
|
public static string GetEquippedItemForSlot(int characterId, string slotName)
|
|
{
|
|
return PlayerSave.GetCharacterSlotItem(characterId, slotName) ?? "";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes local references of this item, removing any equipped data.
|
|
/// Does not remove from Firebase or MonetizationManager by itself.
|
|
/// Call DeletePurchasedItemAsync if you want full removal.
|
|
/// </summary>
|
|
public static async void DeleteLocalItemData(string uniqueItemGuid)
|
|
{
|
|
// If you used PlayerSave for equipping, you might want to find which slot used it
|
|
// But for now, just remove the level key
|
|
PlayerPrefs.DeleteKey($"{uniqueItemGuid}_equipped");
|
|
PlayerPrefs.DeleteKey($"{uniqueItemGuid}_level");
|
|
PlayerPrefs.Save();
|
|
|
|
UIInventoryMenu.Singleton.UpdateInventoryUI();
|
|
await BackendManager.Singleton.DeletePurchasedInventoryItemAsync(uniqueItemGuid);
|
|
}
|
|
}
|
|
}
|