/// <summary> /// This function refreshes a local set of objects that will hold your user's balances in memory for quick /// and more efficient fetching for your game UI. /// This way, we save many JNI or static calls to native platforms. /// /// NOTE: You don't need to call this function as it's automatically called when the game initializes. /// NOTE: This is less useful when you work in editor. /// </summary> public static void RefreshLocalInventory() { SoomlaUtils.LogDebug(TAG, "Refreshing local inventory"); localItemBalances = new Dictionary <string, int> (); localUpgrades = new Dictionary <string, LocalUpgrade>(); localEquippedGoods = new HashSet <string>(); foreach (VirtualCurrency item in StoreInfo.Currencies) { localItemBalances[item.ItemId] = VirtualCurrencyStorage.GetBalance(item); } foreach (VirtualGood item in StoreInfo.Goods) { localItemBalances[item.ItemId] = VirtualGoodsStorage.GetBalance(item); UpgradeVG upgrade = VirtualGoodsStorage.GetCurrentUpgrade(item); if (upgrade != null) { int upgradeLevel = GetGoodUpgradeLevel(item.ItemId); localUpgrades.AddOrUpdate(item.ItemId, new LocalUpgrade { itemId = upgrade.ItemId, level = upgradeLevel }); } if (item is EquippableVG) { if (VirtualGoodsStorage.IsEquipped((EquippableVG)item)) { localEquippedGoods.Add(item.ItemId); } } } }
/// <summary> /// Buys the purchasable virtual item. /// Implementation in subclasses will be according to specific type of purchase. /// </summary> /// <param name="payload">a string you want to be assigned to the purchase. This string /// is saved in a static variable and will be given bacl to you when the /// purchase is completed.</param> /// <exception cref="Soomla.Store.InsufficientFundsException">throws InsufficientFundsException</exception> public override void Buy(string payload) { SoomlaUtils.LogDebug("SOOMLA PurchaseWithVirtualItem", "Trying to buy a " + AssociatedItem.Name + " with " + Amount + " pieces of " + TargetItemId); VirtualItem item = null; try { item = StoreInfo.GetItemByItemId(TargetItemId); } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "Target virtual item doesn't exist !"); return; } JSONObject eventJSON = new JSONObject(); eventJSON.AddField("itemId", AssociatedItem.ItemId); StoreEvents.Instance.onItemPurchaseStarted(eventJSON.print()); int balance = item.GetBalance(); if (item is VirtualCurrency) { balance = VirtualCurrencyStorage.GetBalance(item); } else { balance = VirtualGoodsStorage.GetBalance(item); } if (balance < Amount) { throw new InsufficientFundsException(TargetItemId); } item.Take(Amount); AssociatedItem.Give(1); eventJSON = new JSONObject(); eventJSON.AddField("itemId", AssociatedItem.ItemId); eventJSON.AddField("payload", payload); StoreEvents.Instance.onItemPurchased(eventJSON.print()); }
/// <summary> /// Will fetch the balance for the current currency according to its type. /// </summary> /// <returns>The balance.</returns> public override int GetBalance() { return(VirtualCurrencyStorage.GetBalance(this)); }