Search in sources :

Example 1 with VirtualCurrencyPack

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

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

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

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

the class StoreInfo method replaceVirtualItem.

/**
     * Replaces an old virtual item with a new one by doing the following:
     * 1. Determines the type of the given virtual item.
     * 2. Looks for the given virtual item in the relevant list, according to its type.
     * 3. If found, removes it.
     * 4. Adds the given virtual item.
     *
     * @param virtualItem the virtual item that replaces the old one if exists.
     */
public static void replaceVirtualItem(VirtualItem virtualItem) {
    mVirtualItems.put(virtualItem.getItemId(), virtualItem);
    if (virtualItem instanceof VirtualCurrency) {
        for (int i = 0; i < mCurrencies.size(); i++) {
            if (mCurrencies.get(i).getItemId().equals(virtualItem.getItemId())) {
                mCurrencies.remove(i);
                break;
            }
        }
        mCurrencies.add((VirtualCurrency) virtualItem);
    }
    if (virtualItem instanceof VirtualCurrencyPack) {
        VirtualCurrencyPack vcp = (VirtualCurrencyPack) virtualItem;
        PurchaseType purchaseType = vcp.getPurchaseType();
        if (purchaseType instanceof PurchaseWithMarket) {
            mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), vcp);
        }
        for (int i = 0; i < mCurrencyPacks.size(); i++) {
            if (mCurrencyPacks.get(i).getItemId().equals(vcp.getItemId())) {
                mCurrencyPacks.remove(i);
                break;
            }
        }
        mCurrencyPacks.add(vcp);
    }
    if (virtualItem instanceof VirtualGood) {
        VirtualGood vg = (VirtualGood) virtualItem;
        if (vg instanceof UpgradeVG) {
            List<UpgradeVG> upgrades = mGoodsUpgrades.get(((UpgradeVG) vg).getGoodItemId());
            if (upgrades == null) {
                upgrades = new ArrayList<UpgradeVG>();
                mGoodsUpgrades.put(((UpgradeVG) vg).getGoodItemId(), upgrades);
            }
            upgrades.add((UpgradeVG) vg);
        }
        PurchaseType purchaseType = vg.getPurchaseType();
        if (purchaseType instanceof PurchaseWithMarket) {
            mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), vg);
        }
        for (int i = 0; i < mGoods.size(); i++) {
            if (mGoods.get(i).getItemId().equals(vg.getItemId())) {
                mGoods.remove(i);
                break;
            }
        }
        mGoods.add(vg);
    }
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) UpgradeVG(com.soomla.store.domain.virtualGoods.UpgradeVG) PurchaseType(com.soomla.store.purchaseTypes.PurchaseType) VirtualCurrencyPack(com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket) VirtualCurrency(com.soomla.store.domain.virtualCurrencies.VirtualCurrency)

Example 5 with VirtualCurrencyPack

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

VirtualCurrencyPack (com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack)5 VirtualCurrency (com.soomla.store.domain.virtualCurrencies.VirtualCurrency)4 UpgradeVG (com.soomla.store.domain.virtualGoods.UpgradeVG)4 VirtualGood (com.soomla.store.domain.virtualGoods.VirtualGood)4 PurchaseWithMarket (com.soomla.store.purchaseTypes.PurchaseWithMarket)4 VirtualCategory (com.soomla.store.domain.VirtualCategory)3 PurchaseType (com.soomla.store.purchaseTypes.PurchaseType)3 PurchasableVirtualItem (com.soomla.store.domain.PurchasableVirtualItem)2 VirtualItem (com.soomla.store.domain.VirtualItem)2 EquippableVG (com.soomla.store.domain.virtualGoods.EquippableVG)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 PurchaseWithVirtualItem (com.soomla.store.purchaseTypes.PurchaseWithVirtualItem)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 Activity (android.app.Activity)1