/// <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); }
/** VIRTUAL ITEMS **/ /// <summary> /// Gives your user the given amount of the virtual item with the given <c>itemId</c>. /// For example, when your user plays your game for the first time you GIVE him/her 1000 gems. /// /// NOTE: This action is different than buy - /// You use <c>give(int amount)</c> to give your user something for free. /// You use <c>buy()</c> to give your user something and you get something in return. /// </summary> /// <param name="itemId">Id of the item to be given.</param> /// <param name="amount">Amount of the item to be given.</param> /// <exception cref="VirtualItemNotFoundException">Thrown if the item is not found.</exception> public static void GiveItem(string itemId, int amount) { SoomlaUtils.LogDebug(TAG, "Giving: " + amount + " pieces of: " + itemId); VirtualItem item = StoreInfo.GetItemByItemId(itemId); item.Give(amount); }
public void onGoodUpgrade(string message, bool alsoPush) { SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY onGoodUpgrade:" + message); var eventJSON = new JSONObject(message); VirtualGood vg = (VirtualGood)StoreInfo.GetItemByItemId(eventJSON["itemId"].str); UpgradeVG vgu = null; if (eventJSON.HasField("upgradeItemId") && !string.IsNullOrEmpty(eventJSON["upgradeItemId"].str)) { vgu = (UpgradeVG)StoreInfo.GetItemByItemId(eventJSON["upgradeItemId"].str); } StoreInventory.RefreshOnGoodUpgrade(vg, vgu); StoreEvents.OnGoodUpgrade(vg, vgu); if (alsoPush) { #if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR sep.PushEventOnGoodUpgrade(vg, vgu); #endif } }
/// <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(""); } } }
protected virtual UpgradeVG _getCurrentUpgrade(VirtualGood good) { #if UNITY_EDITOR string itemId = good.ItemId; string key = keyGoodUpgrade(itemId); string upItemId = PlayerPrefs.GetString(key); if (string.IsNullOrEmpty(upItemId)) { SoomlaUtils.LogDebug(TAG, "You tried to fetch the current upgrade of " + good.ItemId + " but there's no upgrade for it."); return(null); } try { return((UpgradeVG)StoreInfo.GetItemByItemId(upItemId)); } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "The current upgrade's itemId from the DB is not found in StoreInfo."); } catch (InvalidCastException) { SoomlaUtils.LogError(TAG, "The current upgrade's itemId from the DB is not an UpgradeVG."); } return(null); #else return(null); #endif }
/// <summary> /// Handles the <c>onMarketPurchase</c> event, which is fired when a Market purchase has occurred. /// </summary> /// <param name="message">Message that contains information about the market purchase.</param> public void onMarketPurchase(string message) { Debug.Log("SOOMLA/UNITY onMarketPurchase:" + message); string[] vars = Regex.Split(message, "#SOOM#"); PurchasableVirtualItem pvi = (PurchasableVirtualItem)StoreInfo.GetItemByItemId(vars[0]); string payload = ""; string purchaseToken = ""; string orderId = ""; if (vars.Length > 1) { payload = vars[1]; } if (vars.Length > 2) { purchaseToken = vars[2]; } if (vars.Length > 3) { orderId = vars[3]; } StoreEvents.OnMarketPurchase(pvi, purchaseToken, payload, orderId); }
/// <summary> /// Handles the <c>onMarketPurchase</c> event, which is fired when a Market purchase has occurred. /// </summary> /// <param name="message">Message that contains information about the market purchase.</param> public void onMarketPurchase(string message) { Debug.Log("SOOMLA/UNITY onMarketPurchase:" + message); var eventJSON = new JSONObject(message); PurchasableVirtualItem pvi = (PurchasableVirtualItem)StoreInfo.GetItemByItemId(eventJSON["itemId"].str); string payload = ""; var extra = new Dictionary <string, string>(); if (eventJSON.HasField("payload")) { payload = eventJSON["payload"].str; } if (eventJSON.HasField("extra")) { var extraJSON = eventJSON["extra"]; if (extraJSON.keys != null) { foreach (string key in extraJSON.keys) { if (extraJSON[key] != null) { extra.Add(key, extraJSON[key].str); } } } } StoreEvents.OnMarketPurchase(pvi, payload, extra); }
/// <summary> /// Handles an <c>onGoodUnequipped</c> event, which is fired when a specific <c>EquippableVG</c> /// has been unequipped. /// </summary> /// <param name="message">Message that contains information about the <c>EquippableVG</c>.</param> public void onGoodUnequipped(string message) { SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY onVirtualGoodUnEquipped:" + message); EquippableVG vg = (EquippableVG)StoreInfo.GetItemByItemId(message); StoreEvents.OnGoodUnEquipped(vg); }
/// <summary> /// Unequips the virtual good with the given <c>goodItemId</c>. Unequipping means that the /// user decides to stop using the virtual good he/she is currently using. /// For more details and examples <see cref="com.soomla.store.domain.virtualGoods.EquippableVG"/>. /// </summary> /// <param name="goodItemId">Id of the good to be unequipped.</param> /// <exception cref="VirtualItemNotFoundException">Thrown if the item is not found.</exception> public static void UnEquipVirtualGood(string goodItemId) { SoomlaUtils.LogDebug(TAG, "UnEquipping: " + goodItemId); EquippableVG good = (EquippableVG)StoreInfo.GetItemByItemId(goodItemId); good.Unequip(); }
/// <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));; }
public static void onMarketPurchaseCancelled(MarketPurchaseCancelledEvent _Event) { SoomlaWpStore.domain.PurchasableVirtualItem purchasableVirtualItem = _Event.GetPurchasableVirtualItem(); SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY onMarketPurchaseCancelled: " + purchasableVirtualItem.getItemId()); PurchasableVirtualItem pvi = (PurchasableVirtualItem)StoreInfo.GetItemByItemId(purchasableVirtualItem.getItemId()); StoreEvents.OnMarketPurchaseCancelled(pvi); }
/// <summary> /// Checks if there is enough funds to afford <c>itemId</c>. /// </summary> /// <param name="itemId">id of item to be checked</param> /// <returns>True if there are enough funds to afford the virtual item with the given item id </returns> public static bool CanAfford(string itemId) { SoomlaUtils.LogDebug(TAG, "Checking can afford: " + itemId); PurchasableVirtualItem pvi = (PurchasableVirtualItem)StoreInfo.GetItemByItemId(itemId); return(pvi.CanAfford()); }
/// <summary> /// Buys the item with the given <c>itemId</c>. /// </summary> /// <param name="itemId">id of item to be bought</param> /// <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="VirtualItemNotFoundException">Thrown if the item to be bought is not found.</exception> /// <exception cref="InsufficientFundsException">Thrown if the user does not have enough funds.</exception> public static void BuyItem(string itemId, string payload) { SoomlaUtils.LogDebug(TAG, "Buying: " + itemId); PurchasableVirtualItem pvi = (PurchasableVirtualItem)StoreInfo.GetItemByItemId(itemId); pvi.Buy(payload); }
/// <summary> /// Handles the <c>onMarketRefund</c> event, which is fired when a Market refund has been issued. /// </summary> /// <param name="message">Message that contains information about the market refund that has occurred.</param> public void onMarketRefund(string message) { SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY onMarketRefund:" + message); PurchasableVirtualItem pvi = (PurchasableVirtualItem)StoreInfo.GetItemByItemId(message); StoreEvents.OnMarketPurchaseStarted(pvi); }
/// <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> /// Handles the <c>onMarketRefund</c> event, which is fired when a Market refund has been issued. /// </summary> /// <param name="message">Message that contains information about the market refund that has occurred.</param> public void onMarketRefund(string message) { SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY onMarketRefund:" + message); var eventJSON = new JSONObject(message); PurchasableVirtualItem pvi = (PurchasableVirtualItem)StoreInfo.GetItemByItemId(eventJSON["itemId"].str); StoreEvents.OnMarketRefund(pvi); }
private VirtualItem getTargetVirtualItem() { VirtualItem item = null; try { item = StoreInfo.GetItemByItemId(TargetItemId); } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "Target virtual item doesn't exist !"); } return(item); }
/// <summary> /// Handles an <c>onCurrencyBalanceChanged</c> event, which is fired when the balance of a specific /// <c>VirtualCurrency</c> has changed. /// </summary> /// <param name="message">Message that contains information about the currency whose balance has /// changed.</param> public void onCurrencyBalanceChanged(string message) { SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY onCurrencyBalanceChanged:" + message); string[] vars = Regex.Split(message, "#SOOM#"); VirtualCurrency vc = (VirtualCurrency)StoreInfo.GetItemByItemId(vars[0]); int balance = int.Parse(vars[1]); int amountAdded = int.Parse(vars[2]); StoreEvents.OnCurrencyBalanceChanged(vc, balance, amountAdded); }
/// <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> /// Retrieves the balance of the virtual item with the given <c>itemId</c>. /// </summary> /// <param name="itemId">Id of the virtual item to be fetched.</param> /// <returns>Balance of the virtual item with the given item id.</returns> /// <exception cref="VirtualItemNotFoundException">Thrown if the item is not found.</exception> public static int GetItemBalance(string itemId) { int amount; if (localItemBalances.TryGetValue(itemId, out amount)) { return(amount); } VirtualItem item = StoreInfo.GetItemByItemId(itemId); return(item.GetBalance()); }
/// <summary> /// Equips the virtual good with the given <c>goodItemId</c>. /// Equipping means that the user decides to currently use a specific virtual good. /// For more details and examples <see cref="com.soomla.store.domain.virtualGoods.EquippableVG"/>. /// </summary> /// <param name="goodItemId">Id of the good to be equipped.</param> /// <exception cref="VirtualItemNotFoundException">Thrown if the item is not found.</exception> /// <exception cref="NotEnoughGoodsException"></exception> public static void EquipVirtualGood(string goodItemId) { SoomlaUtils.LogDebug(TAG, "Equipping: " + goodItemId); EquippableVG good = (EquippableVG)StoreInfo.GetItemByItemId(goodItemId); try { good.Equip(); } catch (NotEnoughGoodsException e) { SoomlaUtils.LogError(TAG, "UNEXPECTED! Couldn't equip something"); throw e; } }
/// <summary> /// Takes a curtain amount of <c>VirtualCurrency</c> according to the given amount and the definition of this pack. /// </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) { VirtualCurrency currency = null; try { currency = (VirtualCurrency)StoreInfo.GetItemByItemId(CurrencyItemId); } catch (VirtualItemNotFoundException) { SoomlaUtils.LogError(TAG, "VirtualCurrency with itemId: " + CurrencyItemId + " doesn't exist! Can't take this pack."); return(0); } return(VirtualCurrencyStorage.Remove(currency, CurrencyAmount * amount, notify)); }
protected override UpgradeVG _getCurrentUpgrade(VirtualGood good) { string upgradeVGItemId; int err = vgStorage_GetCurrentUpgrade(good.ItemId, out upgradeVGItemId); IOS_ErrorCodes.CheckAndThrowException(err); if (!string.IsNullOrEmpty(upgradeVGItemId)) { return((UpgradeVG)StoreInfo.GetItemByItemId(upgradeVGItemId)); } return(null); }
/// <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); }
public static void onItemPurchaseStarted(ItemPurchaseStartedEvent _Event, bool alsoPush) { SoomlaWpStore.domain.PurchasableVirtualItem purchasableVirtualItem = _Event.GetPurchasableVirtualItem(); SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY onItemPurchaseStarted:" + purchasableVirtualItem.getItemId()); PurchasableVirtualItem pvi = (PurchasableVirtualItem)StoreInfo.GetItemByItemId(purchasableVirtualItem.getItemId()); StoreEvents.OnItemPurchaseStarted(pvi); if (alsoPush) { sep.PushEventOnItemPurchaseStarted(_Event); } }
public static void onMarketPurchase(MarketPurchaseEvent _Event) { SoomlaWpStore.domain.PurchasableVirtualItem purchasableVirtualItem = _Event.GetPurchasableVirtualItem(); String payload = _Event.GetPayload(); String token = _Event.GetToken(); Debug.Log("SOOMLA/UNITY onMarketPurchase:" + purchasableVirtualItem.getItemId() + " " + payload + " " + token); PurchasableVirtualItem pvi = (PurchasableVirtualItem)StoreInfo.GetItemByItemId(purchasableVirtualItem.getItemId()); //TODO Implement extra on WP8 onMarketPurchase Dictionary <string, string> extra = new Dictionary <string, string>(); StoreEvents.OnMarketPurchase(pvi, payload, extra); }
/// <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)); }
public static void onGoodUnequipped(GoodUnEquippedEvent _Event, bool alsoPush) { SoomlaWpStore.domain.virtualGoods.EquippableVG good = _Event.GetEquippableVG(); SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY onVirtualGoodUnEquipped:" + good.getItemId()); EquippableVG vg = (EquippableVG)StoreInfo.GetItemByItemId(good.getItemId()); StoreInventory.RefreshOnGoodUnEquipped(vg); StoreEvents.OnGoodUnEquipped(vg); if (alsoPush) { sep.PushEventOnGoodUnequipped(_Event); } }
/// <summary> /// Handles an <c>onGoodUpgrade</c> event, which is fired when a specific <c>UpgradeVG</c> has /// been upgraded/downgraded. /// </summary> /// <param name="message">Message that contains information about the good that has been /// upgraded/downgraded.</param> public void onGoodUpgrade(string message) { SoomlaUtils.LogDebug(TAG, "SOOMLA/UNITY onGoodUpgrade:" + message); string[] vars = Regex.Split(message, "#SOOM#"); VirtualGood vg = (VirtualGood)StoreInfo.GetItemByItemId(vars[0]); UpgradeVG vgu = null; if (vars.Length > 1) { vgu = (UpgradeVG)StoreInfo.GetItemByItemId(vars[1]); } StoreEvents.OnGoodUpgrade(vg, vgu); }