Search in sources :

Example 11 with VirtualItemNotFoundException

use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.

the class StoreInventory method getGoodUpgradeLevel.

/**
     * Retrieves the upgrade level of the virtual good with the given <code>goodItemId</code>.
     *
     * 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. <code>SingleUseVG</code> for "strength"
     *  2. <code>UpgradeVG</code> for strength 'level 1'.
     *  3. <code>UpgradeVG</code> for strength 'level 2'.
     *  4. <code>UpgradeVG</code> for strength 'level 3'.
     * In the example, this function will retrieve the upgrade level for "strength" (1, 2, or 3).
     *
     * @param goodItemId id of the virtual good whose upgrade level we want to know
     * @return upgrade level of the good with the given id
     * @throws VirtualItemNotFoundException
     */
public static int getGoodUpgradeLevel(String goodItemId) throws VirtualItemNotFoundException {
    VirtualGood good = (VirtualGood) StoreInfo.getVirtualItem(goodItemId);
    String upgradeVGItemId = StorageManager.getVirtualGoodsStorage().getCurrentUpgrade(good.getItemId());
    UpgradeVG upgradeVG = null;
    try {
        upgradeVG = (UpgradeVG) StoreInfo.getVirtualItem(upgradeVGItemId);
    } catch (VirtualItemNotFoundException e) {
        SoomlaUtils.LogError("SOOMLA StoreInventory", "This is BAD! Can't find the current upgrade (" + upgradeVGItemId + ") of: " + good.getItemId());
        return 0;
    }
    if (upgradeVG == null) {
        //no upgrade
        return 0;
    }
    UpgradeVG first = StoreInfo.getGoodFirstUpgrade(goodItemId);
    int level = 1;
    while (!first.equals(upgradeVG)) {
        first = (UpgradeVG) StoreInfo.getVirtualItem(first.getNextItemId());
        level++;
    }
    return level;
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) UpgradeVG(com.soomla.store.domain.virtualGoods.UpgradeVG) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException)

Example 12 with VirtualItemNotFoundException

use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.

the class StoreInventory method resetAllItemsBalances.

public static boolean resetAllItemsBalances(HashMap<String, HashMap<String, Object>> replaceBalances) {
    if (replaceBalances == null) {
        return false;
    }
    SoomlaUtils.LogDebug(TAG, "Resetting balances");
    clearCurrentState();
    SoomlaUtils.LogDebug(TAG, "Current state was cleared");
    try {
        for (String itemId : replaceBalances.keySet()) {
            HashMap<String, Object> updatedValues = replaceBalances.get(itemId);
            VirtualItem item = null;
            try {
                item = StoreInfo.getVirtualItem(itemId);
            } catch (VirtualItemNotFoundException e) {
                SoomlaUtils.LogError(TAG, "The given itemId " + itemId + " was not found. Can't force it.");
                continue;
            }
            Object rawBalance = updatedValues.get("balance");
            if (rawBalance != null) {
                Integer updatedBalance = (Integer) rawBalance;
                if (item != null) {
                    item.resetBalance(updatedBalance, false);
                    SoomlaUtils.LogDebug(TAG, "finished balance sync for itemId: " + itemId);
                }
            }
            Object rawEquippedState = updatedValues.get("equipped");
            if (rawEquippedState != null) {
                try {
                    EquippableVG equippableItem = (EquippableVG) item;
                    if (equippableItem != null) {
                        Boolean equipState = (Boolean) rawEquippedState;
                        if (equipState) {
                            equippableItem.equip(false);
                        } else {
                            equippableItem.unequip(false);
                        }
                    }
                    SoomlaUtils.LogDebug(TAG, "finished equip balance sync for itemId: " + itemId);
                } catch (NotEnoughGoodsException e) {
                    SoomlaUtils.LogError(TAG, "the item " + itemId + " was not purchased, so cannot be equipped");
                } catch (ClassCastException exx) {
                    SoomlaUtils.LogError(TAG, "tried to equip a non-equippable item: " + itemId);
                }
            }
            Object rawCurrentUpgrade = updatedValues.get("currentUpgrade");
            if (rawCurrentUpgrade != null) {
                String currentUpgradeId = (String) rawCurrentUpgrade;
                if (!TextUtils.isEmpty(currentUpgradeId)) {
                    try {
                        UpgradeVG upgradeVG = (UpgradeVG) StoreInfo.getVirtualItem(currentUpgradeId);
                        upgradeVG.give(1, false);
                        SoomlaUtils.LogDebug(TAG, "finished upgrade balance sync for itemId: " + itemId);
                    } catch (VirtualItemNotFoundException ex) {
                        SoomlaUtils.LogError(TAG, "The given upgradeId " + currentUpgradeId + " was not found. Can't force it.");
                    } catch (ClassCastException ex) {
                        SoomlaUtils.LogError(TAG, "The given upgradeId was of a non UpgradeVG VirtualItem. Can't force it.");
                    }
                }
            }
        }
        return true;
    } catch (Exception e) {
        SoomlaUtils.LogError(TAG, "Unknown error has occurred while resetting item balances " + e.getMessage());
    }
    return false;
}
Also used : UpgradeVG(com.soomla.store.domain.virtualGoods.UpgradeVG) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) VirtualItem(com.soomla.store.domain.VirtualItem) PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem) EquippableVG(com.soomla.store.domain.virtualGoods.EquippableVG) NotEnoughGoodsException(com.soomla.store.exceptions.NotEnoughGoodsException) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) NotEnoughGoodsException(com.soomla.store.exceptions.NotEnoughGoodsException) InsufficientFundsException(com.soomla.store.exceptions.InsufficientFundsException)

Example 13 with VirtualItemNotFoundException

use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.

the class VirtualGoodsStorage method assignCurrentUpgrade.

/**
     * Assigns a specific upgrade to the given virtual good.
     *
     * @param goodItemId the VirtualGood to upgrade
     * @param upgradeVGItemId the upgrade to assign
     * @param notify if true post event to bus
     */
public void assignCurrentUpgrade(String goodItemId, String upgradeVGItemId, boolean notify) {
    String upgradeItemId = getCurrentUpgrade(goodItemId);
    if (!TextUtils.isEmpty(upgradeItemId)) {
        try {
            UpgradeVG upgrade = (UpgradeVG) StoreInfo.getVirtualItem(upgradeItemId);
            if (upgrade != null && upgrade.getItemId().equals(upgradeVGItemId)) {
                return;
            }
        } catch (VirtualItemNotFoundException ignored) {
        }
    }
    SoomlaUtils.LogDebug(mTag, "Assigning upgrade " + upgradeVGItemId + " to virtual good: " + goodItemId);
    String key = keyGoodUpgrade(goodItemId);
    KeyValueStorage.setValue(key, upgradeVGItemId);
    if (notify) {
        BusProvider.getInstance().post(new GoodUpgradeEvent(goodItemId, upgradeVGItemId));
    }
}
Also used : UpgradeVG(com.soomla.store.domain.virtualGoods.UpgradeVG) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) GoodUpgradeEvent(com.soomla.store.events.GoodUpgradeEvent)

Aggregations

VirtualItemNotFoundException (com.soomla.store.exceptions.VirtualItemNotFoundException)13 PurchasableVirtualItem (com.soomla.store.domain.PurchasableVirtualItem)5 UpgradeVG (com.soomla.store.domain.virtualGoods.UpgradeVG)5 VirtualGood (com.soomla.store.domain.virtualGoods.VirtualGood)5 UnexpectedStoreErrorEvent (com.soomla.store.events.UnexpectedStoreErrorEvent)4 IabPurchase (com.soomla.store.billing.IabPurchase)3 TextView (android.widget.TextView)2 IabCallbacks (com.soomla.store.billing.IabCallbacks)2 VirtualItem (com.soomla.store.domain.VirtualItem)2 InsufficientFundsException (com.soomla.store.exceptions.InsufficientFundsException)2 SharedPreferences (android.content.SharedPreferences)1 Typeface (android.graphics.Typeface)1 ListView (android.widget.ListView)1 IStoreAssets (com.soomla.store.IStoreAssets)1 GooglePlayIabService (com.soomla.store.billing.google.GooglePlayIabService)1 VirtualItemStorage (com.soomla.store.data.VirtualItemStorage)1 VirtualCurrency (com.soomla.store.domain.virtualCurrencies.VirtualCurrency)1 EquippableVG (com.soomla.store.domain.virtualGoods.EquippableVG)1 GoodUpgradeEvent (com.soomla.store.events.GoodUpgradeEvent)1 ItemPurchaseStartedEvent (com.soomla.store.events.ItemPurchaseStartedEvent)1