Search in sources :

Example 1 with EquippableVG

use of com.soomla.store.domain.virtualGoods.EquippableVG in project android-store by soomla.

the class StoreInventory method allItemsBalances.

public static HashMap<String, HashMap<String, Object>> allItemsBalances() {
    SoomlaUtils.LogDebug(TAG, "Fetching all items balances");
    HashMap<String, HashMap<String, Object>> itemsDict = new HashMap<String, HashMap<String, Object>>();
    SoomlaUtils.LogDebug(TAG, "Fetching balances for Currencies");
    // we're cloning the list to avoid situations where someone else tries to manipulate list while we iterate
    List<VirtualCurrency> currencies = new ArrayList<VirtualCurrency>(StoreInfo.getCurrencies());
    for (VirtualCurrency currency : currencies) {
        HashMap<String, Object> updatedValues = new HashMap<String, Object>();
        updatedValues.put("balance", StorageManager.getVirtualCurrencyStorage().getBalance(currency.getItemId()));
        itemsDict.put(currency.getItemId(), updatedValues);
    }
    SoomlaUtils.LogDebug(TAG, "Fetching balances for Goods");
    // we're cloning the list to avoid situations where someone else tries to manipulate list while we iterate
    List<VirtualGood> goods = new ArrayList<VirtualGood>(StoreInfo.getGoods());
    for (VirtualGood good : goods) {
        HashMap<String, Object> updatedValues = new HashMap<String, Object>();
        updatedValues.put("balance", StorageManager.getVirtualGoodsStorage().getBalance(good.getItemId()));
        if (good instanceof EquippableVG) {
            updatedValues.put("equipped", StorageManager.getVirtualGoodsStorage().isEquipped(good.getItemId()));
        }
        if (StoreInfo.hasUpgrades(good.getItemId())) {
            String vguId = StorageManager.getVirtualGoodsStorage().getCurrentUpgrade(good.getItemId());
            updatedValues.put("currentUpgrade", (TextUtils.isEmpty(vguId) ? "none" : vguId));
        }
        itemsDict.put(good.getItemId(), updatedValues);
    }
    return itemsDict;
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EquippableVG(com.soomla.store.domain.virtualGoods.EquippableVG) VirtualCurrency(com.soomla.store.domain.virtualCurrencies.VirtualCurrency)

Example 2 with EquippableVG

use of com.soomla.store.domain.virtualGoods.EquippableVG in project android-store by soomla.

the class StoreInfo method toJSONObject.

/**
 * Converts <code>StoreInfo</code> to a <code>JSONObject</code>.
 *
 * @return a <code>JSONObject</code> representation of <code>StoreInfo</code>.
 */
public static JSONObject toJSONObject() {
    JSONArray currencies = new JSONArray();
    for (VirtualCurrency c : mCurrencies) {
        currencies.put(c.toJSONObject());
    }
    JSONArray currencyPacks = new JSONArray();
    for (VirtualCurrencyPack pack : mCurrencyPacks) {
        currencyPacks.put(pack.toJSONObject());
    }
    JSONObject goods = new JSONObject();
    JSONArray suGoods = new JSONArray();
    JSONArray ltGoods = new JSONArray();
    JSONArray eqGoods = new JSONArray();
    JSONArray paGoods = new JSONArray();
    JSONArray upGoods = new JSONArray();
    for (VirtualGood good : mGoods) {
        if (good instanceof SingleUseVG) {
            suGoods.put(good.toJSONObject());
        } else if (good instanceof UpgradeVG) {
            upGoods.put(good.toJSONObject());
        } else if (good instanceof EquippableVG) {
            eqGoods.put(good.toJSONObject());
        } else if (good instanceof SingleUsePackVG) {
            paGoods.put(good.toJSONObject());
        } else if (good instanceof LifetimeVG) {
            ltGoods.put(good.toJSONObject());
        }
    }
    JSONArray categories = new JSONArray();
    for (VirtualCategory cat : mCategories) {
        categories.put(cat.toJSONObject());
    }
    JSONObject jsonObject = new JSONObject();
    try {
        goods.put(StoreJSONConsts.STORE_GOODS_SU, suGoods);
        goods.put(StoreJSONConsts.STORE_GOODS_LT, ltGoods);
        goods.put(StoreJSONConsts.STORE_GOODS_EQ, eqGoods);
        goods.put(StoreJSONConsts.STORE_GOODS_PA, paGoods);
        goods.put(StoreJSONConsts.STORE_GOODS_UP, upGoods);
        jsonObject.put(StoreJSONConsts.STORE_CATEGORIES, categories);
        jsonObject.put(StoreJSONConsts.STORE_CURRENCIES, currencies);
        jsonObject.put(StoreJSONConsts.STORE_GOODS, goods);
        jsonObject.put(StoreJSONConsts.STORE_CURRENCYPACKS, currencyPacks);
    } catch (JSONException e) {
        SoomlaUtils.LogError(TAG, "An error occurred while generating JSON object.");
    }
    return jsonObject;
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) UpgradeVG(com.soomla.store.domain.virtualGoods.UpgradeVG) LifetimeVG(com.soomla.store.domain.virtualGoods.LifetimeVG) VirtualCategory(com.soomla.store.domain.VirtualCategory) JSONObject(org.json.JSONObject) SingleUseVG(com.soomla.store.domain.virtualGoods.SingleUseVG) JSONArray(org.json.JSONArray) VirtualCurrencyPack(com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack) JSONException(org.json.JSONException) EquippableVG(com.soomla.store.domain.virtualGoods.EquippableVG) SingleUsePackVG(com.soomla.store.domain.virtualGoods.SingleUsePackVG) VirtualCurrency(com.soomla.store.domain.virtualCurrencies.VirtualCurrency)

Example 3 with EquippableVG

use of com.soomla.store.domain.virtualGoods.EquippableVG 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 4 with EquippableVG

use of com.soomla.store.domain.virtualGoods.EquippableVG in project android-store by soomla.

the class StoreInventory method unEquipVirtualGood.

/**
 * Unequips the virtual good with the given <code>goodItemId</code>. Unequipping means that the
 * user decides to stop using the virtual good he/she is currently using.
 * For more details and examples see {@link EquippableVG}.
 *
 * @param goodItemId id of the virtual good to be unequipped
 * @throws VirtualItemNotFoundException
 * @throws ClassCastException
 */
public static void unEquipVirtualGood(String goodItemId) throws VirtualItemNotFoundException, ClassCastException {
    EquippableVG good = (EquippableVG) StoreInfo.getVirtualItem(goodItemId);
    good.unequip();
}
Also used : EquippableVG(com.soomla.store.domain.virtualGoods.EquippableVG)

Example 5 with EquippableVG

use of com.soomla.store.domain.virtualGoods.EquippableVG in project android-store by soomla.

the class StoreInfo method fromJSONObject.

/**
 * Transforms given jsonObject to StoreInfo
 *
 * @param jsonObject
 * @throws JSONException
 */
private static void fromJSONObject(JSONObject jsonObject) throws JSONException {
    mVirtualItems = new HashMap<String, VirtualItem>();
    mPurchasableItems = new HashMap<String, PurchasableVirtualItem>();
    mGoodsCategories = new HashMap<String, VirtualCategory>();
    mGoodsUpgrades = new HashMap<String, List<UpgradeVG>>();
    mCurrencyPacks = new LinkedList<VirtualCurrencyPack>();
    mGoods = new LinkedList<VirtualGood>();
    mCategories = new LinkedList<VirtualCategory>();
    mCurrencies = new LinkedList<VirtualCurrency>();
    if (jsonObject.has(StoreJSONConsts.STORE_CURRENCIES)) {
        JSONArray virtualCurrencies = jsonObject.getJSONArray(StoreJSONConsts.STORE_CURRENCIES);
        for (int i = 0; i < virtualCurrencies.length(); i++) {
            JSONObject o = virtualCurrencies.getJSONObject(i);
            VirtualCurrency c = new VirtualCurrency(o);
            mCurrencies.add(c);
            mVirtualItems.put(c.getItemId(), c);
        }
    }
    if (jsonObject.has(StoreJSONConsts.STORE_CURRENCYPACKS)) {
        JSONArray currencyPacks = jsonObject.getJSONArray(StoreJSONConsts.STORE_CURRENCYPACKS);
        for (int i = 0; i < currencyPacks.length(); i++) {
            JSONObject o = currencyPacks.getJSONObject(i);
            VirtualCurrencyPack pack = new VirtualCurrencyPack(o);
            mCurrencyPacks.add(pack);
            mVirtualItems.put(pack.getItemId(), pack);
            PurchaseType purchaseType = pack.getPurchaseType();
            if (purchaseType instanceof PurchaseWithMarket) {
                mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), pack);
            }
        }
    }
    // For example: VGU and VGP depend on other VGs
    if (jsonObject.has(StoreJSONConsts.STORE_GOODS)) {
        JSONObject virtualGoods = jsonObject.getJSONObject(StoreJSONConsts.STORE_GOODS);
        if (virtualGoods.has(StoreJSONConsts.STORE_GOODS_SU)) {
            JSONArray suGoods = virtualGoods.getJSONArray(StoreJSONConsts.STORE_GOODS_SU);
            for (int i = 0; i < suGoods.length(); i++) {
                JSONObject o = suGoods.getJSONObject(i);
                SingleUseVG g = new SingleUseVG(o);
                addVG(g);
            }
        }
        if (virtualGoods.has(StoreJSONConsts.STORE_GOODS_LT)) {
            JSONArray ltGoods = virtualGoods.getJSONArray(StoreJSONConsts.STORE_GOODS_LT);
            for (int i = 0; i < ltGoods.length(); i++) {
                JSONObject o = ltGoods.getJSONObject(i);
                LifetimeVG g = new LifetimeVG(o);
                addVG(g);
            }
        }
        if (virtualGoods.has(StoreJSONConsts.STORE_GOODS_EQ)) {
            JSONArray eqGoods = virtualGoods.getJSONArray(StoreJSONConsts.STORE_GOODS_EQ);
            for (int i = 0; i < eqGoods.length(); i++) {
                JSONObject o = eqGoods.getJSONObject(i);
                EquippableVG g = new EquippableVG(o);
                addVG(g);
            }
        }
        if (virtualGoods.has(StoreJSONConsts.STORE_GOODS_PA)) {
            JSONArray paGoods = virtualGoods.getJSONArray(StoreJSONConsts.STORE_GOODS_PA);
            for (int i = 0; i < paGoods.length(); i++) {
                JSONObject o = paGoods.getJSONObject(i);
                SingleUsePackVG g = new SingleUsePackVG(o);
                addVG(g);
            }
        }
        if (virtualGoods.has(StoreJSONConsts.STORE_GOODS_UP)) {
            JSONArray upGoods = virtualGoods.getJSONArray(StoreJSONConsts.STORE_GOODS_UP);
            for (int i = 0; i < upGoods.length(); i++) {
                JSONObject o = upGoods.getJSONObject(i);
                UpgradeVG g = new UpgradeVG(o);
                addVG(g);
                List<UpgradeVG> upgrades = mGoodsUpgrades.get(g.getGoodItemId());
                if (upgrades == null) {
                    upgrades = new ArrayList<UpgradeVG>();
                    mGoodsUpgrades.put(g.getGoodItemId(), upgrades);
                }
                upgrades.add(g);
            }
        }
    }
    // Categories depend on virtual goods. That's why the have to be initialized after!
    if (jsonObject.has(StoreJSONConsts.STORE_CATEGORIES)) {
        JSONArray virtualCategories = jsonObject.getJSONArray(StoreJSONConsts.STORE_CATEGORIES);
        for (int i = 0; i < virtualCategories.length(); i++) {
            JSONObject o = virtualCategories.getJSONObject(i);
            VirtualCategory category = new VirtualCategory(o);
            mCategories.add(category);
            for (String goodItemId : category.getGoodsItemIds()) {
                mGoodsCategories.put(goodItemId, category);
            }
        }
    }
    // Remove this code when no longer needed.
    if (mNonConsumableMigrationNeeded) {
        SoomlaUtils.LogDebug(TAG, "NonConsumables balance migration is required. Doing it now.");
        nonConsBalancesToLTVGs();
    }
}
Also used : PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem) VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) UpgradeVG(com.soomla.store.domain.virtualGoods.UpgradeVG) LifetimeVG(com.soomla.store.domain.virtualGoods.LifetimeVG) VirtualCurrencyPack(com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack) VirtualCategory(com.soomla.store.domain.VirtualCategory) SingleUseVG(com.soomla.store.domain.virtualGoods.SingleUseVG) VirtualItem(com.soomla.store.domain.VirtualItem) PurchaseWithVirtualItem(com.soomla.store.purchaseTypes.PurchaseWithVirtualItem) PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) PurchaseType(com.soomla.store.purchaseTypes.PurchaseType) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) EquippableVG(com.soomla.store.domain.virtualGoods.EquippableVG) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket) SingleUsePackVG(com.soomla.store.domain.virtualGoods.SingleUsePackVG) VirtualCurrency(com.soomla.store.domain.virtualCurrencies.VirtualCurrency)

Aggregations

EquippableVG (com.soomla.store.domain.virtualGoods.EquippableVG)5 VirtualCurrency (com.soomla.store.domain.virtualCurrencies.VirtualCurrency)3 UpgradeVG (com.soomla.store.domain.virtualGoods.UpgradeVG)3 VirtualGood (com.soomla.store.domain.virtualGoods.VirtualGood)3 PurchasableVirtualItem (com.soomla.store.domain.PurchasableVirtualItem)2 VirtualCategory (com.soomla.store.domain.VirtualCategory)2 VirtualItem (com.soomla.store.domain.VirtualItem)2 VirtualCurrencyPack (com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack)2 LifetimeVG (com.soomla.store.domain.virtualGoods.LifetimeVG)2 SingleUsePackVG (com.soomla.store.domain.virtualGoods.SingleUsePackVG)2 SingleUseVG (com.soomla.store.domain.virtualGoods.SingleUseVG)2 ArrayList (java.util.ArrayList)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 InsufficientFundsException (com.soomla.store.exceptions.InsufficientFundsException)1 NotEnoughGoodsException (com.soomla.store.exceptions.NotEnoughGoodsException)1 VirtualItemNotFoundException (com.soomla.store.exceptions.VirtualItemNotFoundException)1 PurchaseType (com.soomla.store.purchaseTypes.PurchaseType)1 PurchaseWithMarket (com.soomla.store.purchaseTypes.PurchaseWithMarket)1 PurchaseWithVirtualItem (com.soomla.store.purchaseTypes.PurchaseWithVirtualItem)1