Search in sources :

Example 1 with VirtualItem

use of com.soomla.store.domain.VirtualItem 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 2 with VirtualItem

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

the class PurchaseWithVirtualItem method buy.

/**
 * Buys the virtual item with other virtual items.
 *
 * @throws InsufficientFundsException
 */
@Override
public void buy(String payload) throws InsufficientFundsException {
    SoomlaUtils.LogDebug(TAG, "Trying to buy a " + getAssociatedItem().getName() + " with " + mAmount + " pieces of " + mTargetItemId);
    VirtualItem item = null;
    try {
        item = StoreInfo.getVirtualItem(mTargetItemId);
    } catch (VirtualItemNotFoundException e) {
        SoomlaUtils.LogError(TAG, "Target virtual item doesn't exist !");
        return;
    }
    BusProvider.getInstance().post(new ItemPurchaseStartedEvent(getAssociatedItem().getItemId()));
    VirtualItemStorage storage = StorageManager.getVirtualItemStorage(item);
    assert storage != null;
    int balance = storage.getBalance(item.getItemId());
    if (balance < mAmount) {
        throw new InsufficientFundsException(mTargetItemId);
    }
    storage.remove(item.getItemId(), mAmount);
    getAssociatedItem().give(1);
    BusProvider.getInstance().post(new ItemPurchasedEvent(getAssociatedItem().getItemId(), payload));
}
Also used : ItemPurchaseStartedEvent(com.soomla.store.events.ItemPurchaseStartedEvent) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) VirtualItem(com.soomla.store.domain.VirtualItem) VirtualItemStorage(com.soomla.store.data.VirtualItemStorage) InsufficientFundsException(com.soomla.store.exceptions.InsufficientFundsException) ItemPurchasedEvent(com.soomla.store.events.ItemPurchasedEvent)

Example 3 with VirtualItem

use of com.soomla.store.domain.VirtualItem 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 VirtualItem

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

the class StoreInventory method giveVirtualItem.

/**
 * Gives your user the given amount of the virtual item with the given <code>itemId</code>.
 * For example, when your user plays your game for the first time you GIVE him/her 1000 gems.
 *
 * NOTE: This action is different than buy -
 * You use <code>give(int amount)</code> to give your user something for free.
 * You use <code>buy()</code> to give your user something and you get something in return.
 *
 * @param itemId id of the virtual item to be given
 * @param amount amount of the item to be given
 * @throws VirtualItemNotFoundException
 */
public static void giveVirtualItem(String itemId, int amount) throws VirtualItemNotFoundException {
    VirtualItem item = StoreInfo.getVirtualItem(itemId);
    item.give(amount);
}
Also used : VirtualItem(com.soomla.store.domain.VirtualItem) PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem)

Example 5 with VirtualItem

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

the class StoreInventory method takeVirtualItem.

/**
 * Takes from your user the given amount of the virtual item with the given <code>itemId</code>.
 * For example, when your user requests a refund you need to TAKE the item he/she is returning.
 *
 * @param itemId id of the virtual item to be taken
 * @param amount amount of the item to be given
 * @throws VirtualItemNotFoundException
 */
public static void takeVirtualItem(String itemId, int amount) throws VirtualItemNotFoundException {
    VirtualItem item = StoreInfo.getVirtualItem(itemId);
    item.take(amount);
}
Also used : VirtualItem(com.soomla.store.domain.VirtualItem) PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem)

Aggregations

VirtualItem (com.soomla.store.domain.VirtualItem)6 PurchasableVirtualItem (com.soomla.store.domain.PurchasableVirtualItem)5 UpgradeVG (com.soomla.store.domain.virtualGoods.UpgradeVG)3 VirtualCategory (com.soomla.store.domain.VirtualCategory)2 VirtualCurrency (com.soomla.store.domain.virtualCurrencies.VirtualCurrency)2 VirtualCurrencyPack (com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack)2 EquippableVG (com.soomla.store.domain.virtualGoods.EquippableVG)2 VirtualGood (com.soomla.store.domain.virtualGoods.VirtualGood)2 InsufficientFundsException (com.soomla.store.exceptions.InsufficientFundsException)2 VirtualItemNotFoundException (com.soomla.store.exceptions.VirtualItemNotFoundException)2 PurchaseType (com.soomla.store.purchaseTypes.PurchaseType)2 PurchaseWithMarket (com.soomla.store.purchaseTypes.PurchaseWithMarket)2 PurchaseWithVirtualItem (com.soomla.store.purchaseTypes.PurchaseWithVirtualItem)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 VirtualItemStorage (com.soomla.store.data.VirtualItemStorage)1 LifetimeVG (com.soomla.store.domain.virtualGoods.LifetimeVG)1 SingleUsePackVG (com.soomla.store.domain.virtualGoods.SingleUsePackVG)1 SingleUseVG (com.soomla.store.domain.virtualGoods.SingleUseVG)1