Search in sources :

Example 6 with VirtualGood

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

the class StoreInfo method initializeWithStoreAssets.

/**
     * Initializes from <code>IStoreAssets</code>.
     * This happens only once - when the game is loaded for the first time.
     *
     * @param storeAssets game economy
     */
private static void initializeWithStoreAssets(IStoreAssets storeAssets) {
    // fall-back here if the json doesn't exist,
    // we load the store from the given {@link IStoreAssets}.
    mCurrencies = new LinkedList<VirtualCurrency>(Arrays.asList(storeAssets.getCurrencies()));
    mCurrencyPacks = new LinkedList<VirtualCurrencyPack>(Arrays.asList(storeAssets.getCurrencyPacks()));
    mGoods = new LinkedList<VirtualGood>(Arrays.asList(storeAssets.getGoods()));
    mCategories = new LinkedList<VirtualCategory>(Arrays.asList(storeAssets.getCategories()));
    mVirtualItems = new HashMap<String, VirtualItem>();
    mPurchasableItems = new HashMap<String, PurchasableVirtualItem>();
    mGoodsCategories = new HashMap<String, VirtualCategory>();
    mGoodsUpgrades = new HashMap<String, List<UpgradeVG>>();
    for (VirtualCurrency vi : mCurrencies) {
        mVirtualItems.put(vi.getItemId(), vi);
    }
    for (VirtualCurrencyPack vi : mCurrencyPacks) {
        mVirtualItems.put(vi.getItemId(), vi);
        PurchaseType purchaseType = vi.getPurchaseType();
        if (purchaseType instanceof PurchaseWithMarket) {
            mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), vi);
        }
    }
    for (VirtualGood vi : mGoods) {
        mVirtualItems.put(vi.getItemId(), vi);
        if (vi instanceof UpgradeVG) {
            List<UpgradeVG> upgrades = mGoodsUpgrades.get(((UpgradeVG) vi).getGoodItemId());
            if (upgrades == null) {
                upgrades = new ArrayList<UpgradeVG>();
                mGoodsUpgrades.put(((UpgradeVG) vi).getGoodItemId(), upgrades);
            }
            upgrades.add((UpgradeVG) vi);
        }
        PurchaseType purchaseType = vi.getPurchaseType();
        if (purchaseType instanceof PurchaseWithMarket) {
            mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), vi);
        }
    }
    for (VirtualCategory category : mCategories) {
        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();
    }
    save();
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem) UpgradeVG(com.soomla.store.domain.virtualGoods.UpgradeVG) PurchaseType(com.soomla.store.purchaseTypes.PurchaseType) VirtualCurrencyPack(com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack) VirtualCategory(com.soomla.store.domain.VirtualCategory) 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) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket) VirtualCurrency(com.soomla.store.domain.virtualCurrencies.VirtualCurrency)

Example 7 with VirtualGood

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

the class StoreInfo method nonConsBalancesToLTVGs.

/**
     * IStoreAssets was changed and version number was bumped but we need to check if we need to do balance migration for non-consumables.
     * The metadata in DB was deleted and we're overwriting it.
     * We just need to set the balances of the lifetime items instead of the non-consumables.
     */
private static void nonConsBalancesToLTVGs() {
    for (VirtualGood good : mGoods) {
        if ((good instanceof LifetimeVG) && good.getPurchaseType() instanceof PurchaseWithMarket) {
            String keyNonConsExist = DB_NONCONSUMABLE_KEY_PREFIX + good.getItemId() + ".exists";
            if (KeyValueStorage.getValue(keyNonConsExist) != null) {
                good.give(1);
                KeyValueStorage.deleteKeyValue(keyNonConsExist);
            }
        }
    }
    mNonConsumableMigrationNeeded = false;
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) LifetimeVG(com.soomla.store.domain.virtualGoods.LifetimeVG) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket)

Example 8 with VirtualGood

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

the class SoomlaStore method restoreTransactions.

/**
     * Restoring old purchases for the current user (device).
     */
public void restoreTransactions() {
    if (mInAppBillingService == null) {
        SoomlaUtils.LogError(TAG, "Billing service is not loaded. Can't invoke restoreTransactions.");
        return;
    }
    mInAppBillingService.initializeBillingService(new IabCallbacks.IabInitListener() {

        @Override
        public void success(boolean alreadyInBg) {
            if (!alreadyInBg) {
                notifyIabServiceStarted();
            }
            SoomlaUtils.LogDebug(TAG, "Setup successful, restoring purchases");
            IabCallbacks.OnRestorePurchasesListener restorePurchasesListener = new IabCallbacks.OnRestorePurchasesListener() {

                @Override
                public void success(List<IabPurchase> purchases) {
                    SoomlaUtils.LogDebug(TAG, "Transactions restored");
                    if (purchases.size() > 0) {
                        if (SoomlaConfig.logDebug) {
                            String ownedSkus = "";
                            for (IabPurchase purchase : purchases) {
                                ownedSkus += purchase.getSku() + " / ";
                            }
                            SoomlaUtils.LogDebug(TAG, "Got owned items: " + ownedSkus);
                        }
                        handleSuccessfulPurchases(purchases, true, new HandleSuccessfulPurchasesFinishedHandler() {

                            @Override
                            public void onFinished() {
                                // Restore transactions always finished successfully even if
                                // something wrong happened when handling a specific item.
                                BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(true));
                            }
                        });
                    } else {
                        BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(true));
                    }
                }

                @Override
                public void fail(String message) {
                    BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(false));
                    handleErrorResult(UnexpectedStoreErrorEvent.ErrorCode.GENERAL, message);
                }

                @Override
                public void verificationStarted(List<IabPurchase> purchases) {
                    handleVerificationStarted(purchases);
                }
            };
            IabCallbacks.OnRestorePurchasesListener restoreSubscriptionsListener = new IabCallbacks.OnRestorePurchasesListener() {

                @Override
                public void success(List<IabPurchase> purchases) {
                    // collect subscription ids list
                    List<String> subscriptionIds = new ArrayList<String>();
                    for (IabPurchase purchase : purchases) {
                        subscriptionIds.add(purchase.getSku());
                    }
                    // collect subscriptionVG list
                    List<VirtualGood> subscriptions = new ArrayList<VirtualGood>();
                    for (VirtualGood good : StoreInfo.getGoods()) {
                        if ((good.getPurchaseType() instanceof PurchaseWithMarket) && ((PurchaseWithMarket) good.getPurchaseType()).isSubscription()) {
                            subscriptions.add(good);
                        }
                    }
                    // give unset subscriptions and take expired
                    for (VirtualGood subscription : subscriptions) {
                        String productId = ((PurchaseWithMarket) subscription.getPurchaseType()).getMarketItem().getProductId();
                        if (subscriptionIds.contains(productId)) {
                            // TODO: is here should be 1 to give? Maybe current item has not only just 0/1 state
                            subscription.give(1, false);
                        } else {
                            try {
                                subscription.take(StoreInventory.getVirtualItemBalance(subscription.getItemId()), false);
                            } catch (VirtualItemNotFoundException ex) {
                            // possibly unreachable block
                            }
                        }
                    }
                // TODO: Should we notify user about repaired or expired subscriptions?
                }

                @Override
                public void fail(String message) {
                    SoomlaUtils.LogDebug(TAG, "Subscriptions restoring failed: " + message);
                }

                @Override
                public void verificationStarted(List<IabPurchase> purchases) {
                // should we do it in subscription restoring? possibly it should be empty
                }
            };
            try {
                mInAppBillingService.restorePurchasesAsync(restorePurchasesListener);
            } catch (IllegalStateException ex) {
                SoomlaUtils.LogError(TAG, "Can't proceed with restorePurchases. error: " + ex.getMessage());
                restorePurchasesListener.fail("Can't proceed with restorePurchases. error: " + ex.getMessage());
            }
        }

        @Override
        public void fail(String message) {
            reportIabInitFailure(message);
        }
    });
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) IabCallbacks(com.soomla.store.billing.IabCallbacks) IabPurchase(com.soomla.store.billing.IabPurchase) ArrayList(java.util.ArrayList) List(java.util.List) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket) RestoreTransactionsFinishedEvent(com.soomla.store.events.RestoreTransactionsFinishedEvent)

Example 9 with VirtualGood

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

the class StoreInventory method getGoodCurrentUpgrade.

/**
     * Retrieves the itemId of the current upgrade of the virtual good with the given
     * <code>goodItemId</code>.
     *
     * @param goodItemId id of the virtual good whose upgrade id we want to know
     * @return upgrade id if exists, or empty string otherwise
     * @throws VirtualItemNotFoundException
     */
public static String getGoodCurrentUpgrade(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.LogDebug("SOOMLA StoreInventory", "This is BAD! Can't find the current upgrade (" + upgradeVGItemId + ") of: " + good.getItemId());
    }
    if (upgradeVG == null) {
        return "";
    }
    return upgradeVG.getItemId();
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) UpgradeVG(com.soomla.store.domain.virtualGoods.UpgradeVG) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException)

Example 10 with VirtualGood

use of com.soomla.store.domain.virtualGoods.VirtualGood 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)

Aggregations

VirtualGood (com.soomla.store.domain.virtualGoods.VirtualGood)13 UpgradeVG (com.soomla.store.domain.virtualGoods.UpgradeVG)8 VirtualCurrency (com.soomla.store.domain.virtualCurrencies.VirtualCurrency)5 VirtualItemNotFoundException (com.soomla.store.exceptions.VirtualItemNotFoundException)5 PurchaseWithMarket (com.soomla.store.purchaseTypes.PurchaseWithMarket)5 VirtualCurrencyPack (com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack)4 PurchaseType (com.soomla.store.purchaseTypes.PurchaseType)4 ArrayList (java.util.ArrayList)4 VirtualCategory (com.soomla.store.domain.VirtualCategory)3 EquippableVG (com.soomla.store.domain.virtualGoods.EquippableVG)3 LifetimeVG (com.soomla.store.domain.virtualGoods.LifetimeVG)3 PurchaseWithVirtualItem (com.soomla.store.purchaseTypes.PurchaseWithVirtualItem)3 List (java.util.List)3 ListView (android.widget.ListView)2 TextView (android.widget.TextView)2 PurchasableVirtualItem (com.soomla.store.domain.PurchasableVirtualItem)2 VirtualItem (com.soomla.store.domain.VirtualItem)2 SingleUsePackVG (com.soomla.store.domain.virtualGoods.SingleUsePackVG)2 SingleUseVG (com.soomla.store.domain.virtualGoods.SingleUseVG)2 LinkedList (java.util.LinkedList)2