Search in sources :

Example 1 with NotEnoughGoodsException

use of com.soomla.store.exceptions.NotEnoughGoodsException 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)

Aggregations

PurchasableVirtualItem (com.soomla.store.domain.PurchasableVirtualItem)1 VirtualItem (com.soomla.store.domain.VirtualItem)1 EquippableVG (com.soomla.store.domain.virtualGoods.EquippableVG)1 UpgradeVG (com.soomla.store.domain.virtualGoods.UpgradeVG)1 InsufficientFundsException (com.soomla.store.exceptions.InsufficientFundsException)1 NotEnoughGoodsException (com.soomla.store.exceptions.NotEnoughGoodsException)1 VirtualItemNotFoundException (com.soomla.store.exceptions.VirtualItemNotFoundException)1