Beispiel #1
0
        /// <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);
            }
        }