/// <summary> /// Retrieves the upgrade level of the virtual good with the given <c>goodItemId</c>. /// For Example: /// Let's say there's a strength attribute to one of the characters in your game and you provide /// your users with the ability to upgrade that strength on a scale of 1-3. /// This is what you've created: /// 1. <c>SingleUseVG</c> for "strength". /// 2. <c>UpgradeVG</c> for strength 'level 1'. /// 3. <c>UpgradeVG</c> for strength 'level 2'. /// 4. <c>UpgradeVG</c> for strength 'level 3'. /// In the example, this function will retrieve the upgrade level for "strength" (1, 2, or 3). /// </summary> /// <param name="goodItemId">Good item identifier.</param> /// <returns>The good upgrade level.</returns> /// <exception cref="VirtualItemNotFoundException">Thrown if the item is not found.</exception> public static int GetGoodUpgradeLevel(string goodItemId) { SoomlaUtils.LogDebug(TAG, "Checking " + goodItemId + " upgrade level"); VirtualGood good = (VirtualGood)StoreInfo.GetItemByItemId(goodItemId); if (good == null) { SoomlaUtils.LogError(TAG, "You tried to get the level of a non-existant virtual good."); return(0); } UpgradeVG upgradeVG = VirtualGoodsStorage.GetCurrentUpgrade(good); if (upgradeVG == null) { return(0); //no upgrade } UpgradeVG first = StoreInfo.GetFirstUpgradeForVirtualGood(goodItemId); int level = 1; while (first.ItemId != upgradeVG.ItemId) { first = (UpgradeVG)StoreInfo.GetItemByItemId(first.NextItemId); level++; } return(level); }
/// <summary> /// Upgrades the virtual good with the given <c>goodItemId</c> by doing the following: /// 1. Checks if the good is currently upgraded or if this is the first time being upgraded. /// 2. If the good is currently upgraded, upgrades to the next upgrade in the series. /// In case there are no more upgrades available(meaning the current upgrade is the last available), /// the function returns. /// 3. If the good has never been upgraded before, the function upgrades it to the first /// available upgrade with the first upgrade of the series. /// </summary> /// <param name="goodItemId">Good item identifier.</param> /// <exception cref="VirtualItemNotFoundException">Thrown if the item is not found.</exception> public static void UpgradeGood(string goodItemId) { SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY Calling UpgradeGood with: " + goodItemId); VirtualGood good = (VirtualGood)StoreInfo.GetItemByItemId(goodItemId); UpgradeVG upgradeVG = VirtualGoodsStorage.GetCurrentUpgrade(good); if (upgradeVG != null) { String nextItemId = upgradeVG.NextItemId; if (string.IsNullOrEmpty(nextItemId)) { return; } UpgradeVG vgu = (UpgradeVG)StoreInfo.GetItemByItemId(nextItemId); vgu.Buy(""); } else { UpgradeVG first = StoreInfo.GetFirstUpgradeForVirtualGood(goodItemId); if (first != null) { first.Buy(""); } } }
/// <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> /// Checks if the virtual good with the given <c>goodItemId</c> is currently equipped. /// </summary> /// <param name="goodItemId">Id of the virtual good who we want to know if is equipped.</param> /// <returns>True if the virtual good is equipped, false otherwise.</returns> /// <exception cref="VirtualItemNotFoundException">Thrown if the item is not found.</exception> public static bool IsVirtualGoodEquipped(string goodItemId) { SoomlaUtils.LogDebug(TAG, "Checking if " + goodItemId + " is equipped"); EquippableVG good = (EquippableVG)StoreInfo.GetItemByItemId(goodItemId); return(VirtualGoodsStorage.IsEquipped(good));; }
/// <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> /// This function gives a curtain amout of <c>VirtualGood</c>s according to the given amount and the amount in the pack. /// </summary> /// <param name="amount">amount the amount of the specific item to be given.</param> /// <param name="notify">notify of change in user's balance of current virtual item.</param> public override int Give(int amount, bool notify) { SingleUseVG good = null; try { good = (SingleUseVG)StoreInfo.GetItemByItemId(GoodItemId); } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "SingleUseVG with itemId: " + GoodItemId + " doesn't exist! Can't give this pack."); return(0); } return(VirtualGoodsStorage.Add(good, GoodAmount * amount, notify)); }
/// <summary> /// Removes all upgrades from the virtual good with the given <c>goodItemId</c>. /// </summary> /// <param name="goodItemId">Id of the good whose upgrades are to be removed.</param> /// <exception cref="VirtualItemNotFoundException">Thrown if the item is not found.</exception> public static void RemoveGoodUpgrades(string goodItemId) { SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY Calling RemoveGoodUpgrades with: " + goodItemId); List <UpgradeVG> upgrades = StoreInfo.GetUpgradesForVirtualGood(goodItemId); foreach (UpgradeVG upgrade in upgrades) { VirtualGoodsStorage.Remove(upgrade, 1, true); } VirtualGood good = (VirtualGood)StoreInfo.GetItemByItemId(goodItemId); VirtualGoodsStorage.RemoveUpgrades(good); }
/// <summary> /// Retrieves the current upgrade of the good with the given id. /// </summary> /// <param name="goodItemId">Id of the good whose upgrade we want to fetch. </param> /// <returns>The good's current upgrade.</returns> /// <exception cref="VirtualItemNotFoundException">Thrown if the item is not found.</exception> public static string GetGoodCurrentUpgrade(string goodItemId) { SoomlaUtils.LogDebug(TAG, "Checking " + goodItemId + " current upgrade"); VirtualGood good = (VirtualGood)StoreInfo.GetItemByItemId(goodItemId); UpgradeVG upgradeVG = VirtualGoodsStorage.GetCurrentUpgrade(good); if (upgradeVG == null) { return(""); } return(upgradeVG.ItemId); }
/// <summary> /// Takes upgrade from the user, or in other words DOWNGRADES the associated /// <code>VirtualGood</code> (mGood). /// Checks if the current Upgrade is really associated with the <code>VirtualGood</code> and: /// </summary> /// <param name="amount">NOT USED HERE!.</param> /// <param name="notify">see parent.</param> public override int Take(int amount, bool notify) { VirtualGood good = null; try { good = (VirtualGood)StoreInfo.GetItemByItemId(GoodItemId); } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "VirtualGood with itemId: " + GoodItemId + " doesn't exist! Can't downgrade."); return(0); } UpgradeVG upgradeVG = VirtualGoodsStorage.GetCurrentUpgrade(good); // Case: Upgrade is not assigned to this Virtual Good if (upgradeVG != this) { SoomlaUtils.LogError(TAG, "You can't take an upgrade that's not currently assigned." + "The UpgradeVG " + Name + " is not assigned to " + "the VirtualGood: " + good.Name); return(0); } if (!string.IsNullOrEmpty(PrevItemId)) { UpgradeVG prevUpgradeVG = null; // Case: downgrade is not possible because previous upgrade does not exist try { prevUpgradeVG = (UpgradeVG)StoreInfo.GetItemByItemId(PrevItemId); } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "Previous UpgradeVG with itemId: " + PrevItemId + " doesn't exist! Can't downgrade."); return(0); } // Case: downgrade is successful! SoomlaUtils.LogDebug(TAG, "Downgrading " + good.Name + " to: " + prevUpgradeVG.Name); VirtualGoodsStorage.AssignCurrentUpgrade(good, prevUpgradeVG, notify); } // Case: first Upgrade in the series - so we downgrade to NO upgrade. else { SoomlaUtils.LogDebug(TAG, "Downgrading " + good.Name + " to NO-UPGRADE"); VirtualGoodsStorage.RemoveUpgrades(good, notify); } return(base.Take(amount, notify)); }
/// <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> /// Checks currently equipped good in given <c>category</c> /// </summary> /// <param name="category">Category we want to check</param> /// <returns>EquippableVG otherwise null</returns> public static EquippableVG GetEquippedVirtualGood(VirtualCategory category) { SoomlaUtils.LogDebug(TAG, "Checking equipped goood in " + category.Name + " category"); foreach (string goodItemId in category.GoodItemIds) { EquippableVG good = (EquippableVG)StoreInfo.GetItemByItemId(goodItemId); if (good != null && good.Equipping == EquippableVG.EquippingModel.CATEGORY && VirtualGoodsStorage.IsEquipped(good) && StoreInfo.GetCategoryForVirtualGood(goodItemId) == category) { return(good); } } SoomlaUtils.LogError(TAG, "There is no virtual good equipped in " + category.Name + " category"); return(null); }
/// <summary> /// Assigns the current upgrade to the associated <code>VirtualGood</code> (mGood). /// </summary> /// <param name="amount">NOT USED HERE!</param> /// <param name="notify">notify of change in user's balance of current virtual item.</param> public override int Give(int amount, bool notify) { SoomlaUtils.LogDebug(TAG, "Assigning " + Name + " to: " + GoodItemId); VirtualGood good = null; try { good = (VirtualGood)StoreInfo.GetItemByItemId(GoodItemId); } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "VirtualGood with itemId: " + GoodItemId + " doesn't exist! Can't upgrade."); return(0); } VirtualGoodsStorage.AssignCurrentUpgrade(good, this, notify); return(base.Give(amount, notify)); }
/// <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> /// Determines if the user is in a state that allows him/her to buy an <code>UpgradeVG</code> /// This method enforces allowing/rejecting of upgrades here so users won't buy them when /// they are not supposed to. /// If you want to give your users free upgrades, use the <code>give</code> function. /// </summary> /// <returns><c>true</c>, if can buy, <c>false</c> otherwise.</returns> protected override bool canBuy() { VirtualGood good = null; try { good = (VirtualGood)StoreInfo.GetItemByItemId(GoodItemId); } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "VirtualGood with itemId: " + GoodItemId + " doesn't exist! Returning NO (can't buy)."); return(false); } UpgradeVG upgradeVG = VirtualGoodsStorage.GetCurrentUpgrade(good); return(((upgradeVG == null && string.IsNullOrEmpty(PrevItemId)) || (upgradeVG != null && ((upgradeVG.NextItemId == this.ItemId) || (upgradeVG.PrevItemId == this.ItemId)))) && base.canBuy()); }
/// <summary> /// <see cref="Soomla.Store.VirtualItem"/> /// </summary> public override int ResetBalance(int balance, bool notify) { return(VirtualGoodsStorage.SetBalance(this, balance, notify)); }
/// <summary> /// Will take a curtain amount of this single use virtual good. /// </summary> /// <param name="amount">the amount of the specific item to be taken.</param> /// <param name="notify">notify of change in user's balance of current virtual item.</param> public override int Take(int amount, bool notify) { return(VirtualGoodsStorage.Remove(this, amount, notify)); }
/// <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> /// Will give a curtain amount of this single use virtual good. /// </summary> /// <param name="amount">amount the amount of the specific item to be given.</param> /// <param name="notify">notify of change in user's balance of current virtual item.</param> public override int Give(int amount, bool notify) { return(VirtualGoodsStorage.Add(this, amount, notify)); }
/// <summary> /// Unequips the current <code>EquippableVG</code> /// </summary> /// <param name="notify">if true, the relevant event will be posted when unequipped.</param> public void Unequip(bool notify) { VirtualGoodsStorage.UnEquip(this, notify); }
/// <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); }