using UnityEngine; namespace BulletHellTemplate { /// /// Handles saving and loading inventory-related data locally (such as item upgrade levels), /// and optionally syncing specific items to the backend. /// public class InventorySave : MonoBehaviour { public static InventorySave Singleton; private void Awake() { if (Singleton == null) { Singleton = this; } else { Destroy(gameObject); } } /// /// 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. /// /// Unique GUID identifying the purchased item. /// The new upgrade level. 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}"); } } /// /// (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). /// /// Unique GUID of the item. /// The new level. 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); } } /// /// Gets the item upgrade level from local data, returns 0 if not found. /// /// GUID of the purchased item. /// The upgrade level, or 0 if not found. public static int GetItemUpgradeLevel(string uniqueItemGuid) { return PlayerSave.GetItemUpgradeLevel(uniqueItemGuid); } /// /// 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. /// 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 ); } } /// /// Returns the unique item GUID equipped in a specific slot for a given character, /// or empty string if none is equipped. /// public static string GetEquippedItemForSlot(int characterId, string slotName) { return PlayerSave.GetCharacterSlotItem(characterId, slotName) ?? ""; } /// /// 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. /// 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); } } }