/// <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> /// Equips the current <code>EquippableVG</code>. /// The equipping is done according to the equipping model ('GLOBAL', 'CATEGORY', or 'LOCAL'). /// </summary> /// <exception cref="Soomla.Store.NotEnoughGoodsException">Throws NotEnoughGoodsException</exception> /// <param name="notify">if true, the relevant event will be posted when equipped.</param> public void Equip(bool notify) { // only if the user has bought this EquippableVG, the EquippableVG is equipped. if (VirtualGoodsStorage.GetBalance(this) > 0) { if (Equipping == EquippingModel.CATEGORY) { VirtualCategory category = null; try { category = StoreInfo.GetCategoryForVirtualGood(this.ItemId); } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "Tried to unequip all other category VirtualGoods but there was no " + "associated category. virtual good itemId: " + this.ItemId); return; } foreach (string goodItemId in category.GoodItemIds) { EquippableVG equippableVG = null; try { equippableVG = (EquippableVG)StoreInfo.GetItemByItemId(goodItemId); if (equippableVG != null && equippableVG != this) { equippableVG.Unequip(notify); } } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "On equip, couldn't find one of the itemIds " + "in the category. Continuing to the next one. itemId: " + goodItemId); } catch (System.InvalidCastException) { SoomlaUtils.LogDebug(TAG, "On equip, an error occurred. It's a debug " + "message b/c the VirtualGood may just not be an EquippableVG. " + "itemId: " + goodItemId); } } } else if (Equipping == EquippingModel.GLOBAL) { foreach (VirtualGood good in StoreInfo.Goods) { if (good != this && good is EquippableVG) { ((EquippableVG)good).Unequip(notify); } } } VirtualGoodsStorage.Equip(this, notify); } else { throw new NotEnoughGoodsException(ItemId); } }
/// <summary> /// Takes from your user exactly one <code>LifetimeVG</code>. /// </summary> /// <param name="amount">the amount of the specific item to be taken - if this input is greater than 1, /// we force amount to equal 1, because a <code>LifetimeVG</code> can only be /// given once and therefore, taken once.</param> /// <param name="notify">Notify.</param> public override int Take(int amount, bool notify) { if (amount > 1) { amount = 1; } int balance = VirtualGoodsStorage.GetBalance(this); if (balance > 0) { return(VirtualGoodsStorage.Remove(this, amount, notify)); } return(0); }
/// <summary> /// Gives your user exactly one <code>LifetimeVG</code>. /// </summary> /// <param name="amount">he amount of the specific item to be given - if this input is greater than 1, /// we force the amount to equal 1, because a <code>LifetimeVG</code> can only be given once..</param> /// <param name="notify">Notify.</param> public override int Give(int amount, bool notify) { if (amount > 1) { SoomlaUtils.LogDebug(TAG, "You tried to give more than one LifetimeVG." + "Will try to give one anyway."); amount = 1; } int balance = VirtualGoodsStorage.GetBalance(this); if (balance < 1) { return(VirtualGoodsStorage.Add(this, amount, notify)); } return(1); }
/// <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 VirtualItem according to its type. /// </summary> /// <returns>The balance.</returns> public override int GetBalance() { return(VirtualGoodsStorage.GetBalance(this)); }
/// <summary> /// etermines if the user is in a state that allows him/her to buy a <code>LifetimeVG</code>, /// by checking his/her balance of <code>LifetimeVG</code>s. /// From the definition of a <code>LifetimeVG</code>: /// If the user has a balance of 0 - he/she can buy. /// If the user has a balance of 1 or more - he/she cannot buy more. /// </summary> /// <returns>true if buying is allowed, false otherwise.</returns> protected override bool canBuy() { int balance = VirtualGoodsStorage.GetBalance(this); return(balance < 1); }