use of com.soomla.store.domain.virtualGoods.UpgradeVG 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();
}
use of com.soomla.store.domain.virtualGoods.UpgradeVG 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;
}
use of com.soomla.store.domain.virtualGoods.UpgradeVG 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;
}
use of com.soomla.store.domain.virtualGoods.UpgradeVG 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);
}
}
use of com.soomla.store.domain.virtualGoods.UpgradeVG 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();
}
}
Aggregations