use of com.soomla.store.domain.virtualGoods.VirtualGood in project android-store by soomla.
the class StoreGoodsActivity method onGoodBalanceChanged.
/**
* Receives the given <code>goodBalanceChangedEvent</code>. Upon notification, fetches the
* good associated with the given <code>goodBalanceChangedEvent</code> and displays its price
* and the balance.
*
* @param goodBalanceChangedEvent the event received
*/
@Subscribe
public void onGoodBalanceChanged(GoodBalanceChangedEvent goodBalanceChangedEvent) {
VirtualGood good = null;
try {
good = (VirtualGood) StoreInfo.getVirtualItem(goodBalanceChangedEvent.getGoodItemId());
int id = 0;
for (int i = 0; i < StoreInfo.getGoods().size(); i++) {
if (StoreInfo.getGoods().get(i).getItemId().equals(good.getItemId())) {
id = i;
break;
}
}
ListView list = (ListView) findViewById(R.id.list);
TextView info = (TextView) list.getChildAt(id).findViewById(R.id.item_info);
PurchaseType purchaseType = good.getPurchaseType();
if (purchaseType instanceof PurchaseWithVirtualItem) {
info.setText("price: " + ((PurchaseWithVirtualItem) purchaseType).getAmount() + " balance: " + goodBalanceChangedEvent.getBalance());
}
} catch (VirtualItemNotFoundException e) {
SoomlaUtils.LogDebug("StoreGoodsActivity", e.getMessage());
}
}
use of com.soomla.store.domain.virtualGoods.VirtualGood 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;
}
use of com.soomla.store.domain.virtualGoods.VirtualGood in project android-store by soomla.
the class StoreInventory method removeUpgrades.
/**
* Removes all upgrades from the virtual good with the given <code>goodItemId</code>.
*
* @param goodItemId id of the virtual good we want to remove all upgrades from
* @throws VirtualItemNotFoundException
*/
public static void removeUpgrades(String goodItemId) throws VirtualItemNotFoundException {
List<UpgradeVG> upgrades = StoreInfo.getGoodUpgrades(goodItemId);
for (UpgradeVG upgrade : upgrades) {
StorageManager.getVirtualGoodsStorage().remove(upgrade.getItemId(), 1, true);
}
VirtualGood good = (VirtualGood) StoreInfo.getVirtualItem(goodItemId);
StorageManager.getVirtualGoodsStorage().removeUpgrades(good.getItemId());
}
use of com.soomla.store.domain.virtualGoods.VirtualGood 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;
}
use of com.soomla.store.domain.virtualGoods.VirtualGood in project android-store by soomla.
the class StoreInventory method upgradeVirtualGood.
/**
* Upgrades the virtual good with the given <code>goodItemId</code> by doing the following:
* 1. Checks if the good is currently upgraded or if this is the first time being upgraded.
* 2. If the good is currently upgraded, upgrades to the next upgrade in the series, or in
* other words, <code>buy()</code>s the next upgrade. In case there are no more upgrades
* available(meaning the current upgrade is the last available), the function returns.
* 3. If the good has never been upgraded before, the function upgrades it to the first
* available upgrade, or in other words, <code>buy()</code>s the first upgrade in the series.
*
* @param goodItemId the id of the virtual good to be upgraded
* @throws VirtualItemNotFoundException
* @throws InsufficientFundsException
*/
public static void upgradeVirtualGood(String goodItemId) throws VirtualItemNotFoundException, InsufficientFundsException {
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) {
String nextItemId = upgradeVG.getNextItemId();
if (TextUtils.isEmpty(nextItemId)) {
return;
}
UpgradeVG vgu = (UpgradeVG) StoreInfo.getVirtualItem(nextItemId);
vgu.buy("");
} else {
UpgradeVG first = StoreInfo.getGoodFirstUpgrade(goodItemId);
if (first != null) {
first.buy("");
}
}
}
Aggregations