Search in sources :

Example 1 with PurchaseWithMarket

use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.

the class StorePacksActivity method onCreate.

/**
 * Called when the activity starts.
 *
 * @param savedInstanceState if the activity should be re-initialized after previously being
 *                           shut down then this <code>Bundle</code> will contain the most
 *                           recent data, otherwise it will be null.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    LinearLayout getMore = (LinearLayout) findViewById(R.id.getMore);
    TextView title = (TextView) findViewById(R.id.title);
    getMore.setVisibility(View.INVISIBLE);
    title.setText("Virtual Currency Packs");
    mImages = generateImagesHash();
    mStoreAdapter = new StoreAdapter();
    /* configuring the list with an adapter */
    final Activity activity = this;
    ListView list = (ListView) findViewById(R.id.list);
    list.setAdapter(mStoreAdapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            /*
                * The user decided to make an actual purchase of virtual goods. We try to buy() the
                * user's desired good and SoomlaStore tells us if the user has enough funds to
                * make the purchase. If he/she doesn't have enough then an
                * InsufficientFundsException will be thrown.
                */
            PurchaseWithMarket pwm = null;
            VirtualCurrencyPack pack = StoreInfo.getCurrencyPacks().get(i);
            pwm = (PurchaseWithMarket) pack.getPurchaseType();
            try {
                pwm.buy("this is just a payload");
            } catch (InsufficientFundsException e) {
                AlertDialog ad = new AlertDialog.Builder(activity).create();
                // This blocks the 'BACK' button
                ad.setCancelable(false);
                ad.setMessage("Can't continue with purchase (You don't have enough muffins !)");
                ad.setButton(DialogInterface.BUTTON_NEGATIVE, "OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                ad.show();
            }
            /* fetching the currency balance and placing it in the balance label */
            TextView muffinsBalance = (TextView) activity.findViewById(R.id.balance);
            muffinsBalance.setText("" + StorageManager.getVirtualCurrencyStorage().getBalance(StoreInfo.getCurrencies().get(0).getItemId()));
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Activity(android.app.Activity) VirtualCurrencyPack(com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) ListView(android.widget.ListView) InsufficientFundsException(com.soomla.store.exceptions.InsufficientFundsException) TextView(android.widget.TextView) AdapterView(android.widget.AdapterView) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket) LinearLayout(android.widget.LinearLayout)

Example 2 with PurchaseWithMarket

use of com.soomla.store.purchaseTypes.PurchaseWithMarket 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 3 with PurchaseWithMarket

use of com.soomla.store.purchaseTypes.PurchaseWithMarket 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 4 with PurchaseWithMarket

use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.

the class StoreInfo method addVG.

/**
 * Adds the given virtual good to <code>StoreInfo</code>'s <code>mGoods</code>,
 * <code>mVirtualItems</code>, and if the good has purchase type <code>PurchaseWithMarket</code>
 * then it is also added to <code>mPurchasableItems</code>.
 *
 * @param g virtual good to be added
 */
private static void addVG(VirtualGood g) {
    mGoods.add(g);
    mVirtualItems.put(g.getItemId(), g);
    PurchaseType purchaseType = g.getPurchaseType();
    if (purchaseType instanceof PurchaseWithMarket) {
        mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), g);
    }
}
Also used : PurchaseType(com.soomla.store.purchaseTypes.PurchaseType) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket)

Example 5 with PurchaseWithMarket

use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.

the class PurchasableVirtualItem method toJSONObject.

/**
 * @{inheritDoc}
 */
@Override
public JSONObject toJSONObject() {
    JSONObject parentJsonObject = super.toJSONObject();
    JSONObject jsonObject = new JSONObject();
    try {
        Iterator<?> keys = parentJsonObject.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            jsonObject.put(key, parentJsonObject.get(key));
        }
        JSONObject purchasableObj = new JSONObject();
        if (mPurchaseType instanceof PurchaseWithMarket) {
            purchasableObj.put(StoreJSONConsts.PURCHASE_TYPE, StoreJSONConsts.PURCHASE_TYPE_MARKET);
            MarketItem mi = ((PurchaseWithMarket) mPurchaseType).getMarketItem();
            purchasableObj.put(StoreJSONConsts.PURCHASE_MARKET_ITEM, mi.toJSONObject());
        } else if (mPurchaseType instanceof PurchaseWithVirtualItem) {
            purchasableObj.put(StoreJSONConsts.PURCHASE_TYPE, StoreJSONConsts.PURCHASE_TYPE_VI);
            purchasableObj.put(StoreJSONConsts.PURCHASE_VI_ITEMID, ((PurchaseWithVirtualItem) mPurchaseType).getTargetItemId());
            purchasableObj.put(StoreJSONConsts.PURCHASE_VI_AMOUNT, ((PurchaseWithVirtualItem) mPurchaseType).getAmount());
        }
        jsonObject.put(StoreJSONConsts.PURCHASABLE_ITEM, purchasableObj);
    } catch (JSONException e) {
        SoomlaUtils.LogError(TAG, "An error occurred while generating JSON object.");
    }
    return jsonObject;
}
Also used : PurchaseWithVirtualItem(com.soomla.store.purchaseTypes.PurchaseWithVirtualItem) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket)

Aggregations

PurchaseWithMarket (com.soomla.store.purchaseTypes.PurchaseWithMarket)9 VirtualGood (com.soomla.store.domain.virtualGoods.VirtualGood)5 VirtualCurrencyPack (com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack)4 PurchaseType (com.soomla.store.purchaseTypes.PurchaseType)4 PurchasableVirtualItem (com.soomla.store.domain.PurchasableVirtualItem)3 VirtualCurrency (com.soomla.store.domain.virtualCurrencies.VirtualCurrency)3 UpgradeVG (com.soomla.store.domain.virtualGoods.UpgradeVG)3 PurchaseWithVirtualItem (com.soomla.store.purchaseTypes.PurchaseWithVirtualItem)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 VirtualCategory (com.soomla.store.domain.VirtualCategory)2 VirtualItem (com.soomla.store.domain.VirtualItem)2 LifetimeVG (com.soomla.store.domain.virtualGoods.LifetimeVG)2 LinkedList (java.util.LinkedList)2 JSONObject (org.json.JSONObject)2 Activity (android.app.Activity)1 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1