use of com.soomla.store.domain.virtualGoods.VirtualGood 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();
}
use of com.soomla.store.domain.virtualGoods.VirtualGood 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;
}
use of com.soomla.store.domain.virtualGoods.VirtualGood in project android-store by soomla.
the class SoomlaStore method restoreTransactions.
/**
* Restoring old purchases for the current user (device).
*/
public void restoreTransactions() {
if (mInAppBillingService == null) {
SoomlaUtils.LogError(TAG, "Billing service is not loaded. Can't invoke restoreTransactions.");
return;
}
mInAppBillingService.initializeBillingService(new IabCallbacks.IabInitListener() {
@Override
public void success(boolean alreadyInBg) {
if (!alreadyInBg) {
notifyIabServiceStarted();
}
SoomlaUtils.LogDebug(TAG, "Setup successful, restoring purchases");
IabCallbacks.OnRestorePurchasesListener restorePurchasesListener = new IabCallbacks.OnRestorePurchasesListener() {
@Override
public void success(List<IabPurchase> purchases) {
SoomlaUtils.LogDebug(TAG, "Transactions restored");
if (purchases.size() > 0) {
if (SoomlaConfig.logDebug) {
String ownedSkus = "";
for (IabPurchase purchase : purchases) {
ownedSkus += purchase.getSku() + " / ";
}
SoomlaUtils.LogDebug(TAG, "Got owned items: " + ownedSkus);
}
handleSuccessfulPurchases(purchases, true, new HandleSuccessfulPurchasesFinishedHandler() {
@Override
public void onFinished() {
// Restore transactions always finished successfully even if
// something wrong happened when handling a specific item.
BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(true));
}
});
} else {
BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(true));
}
}
@Override
public void fail(String message) {
BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(false));
handleErrorResult(UnexpectedStoreErrorEvent.ErrorCode.GENERAL, message);
}
@Override
public void verificationStarted(List<IabPurchase> purchases) {
handleVerificationStarted(purchases);
}
};
IabCallbacks.OnRestorePurchasesListener restoreSubscriptionsListener = new IabCallbacks.OnRestorePurchasesListener() {
@Override
public void success(List<IabPurchase> purchases) {
// collect subscription ids list
List<String> subscriptionIds = new ArrayList<String>();
for (IabPurchase purchase : purchases) {
subscriptionIds.add(purchase.getSku());
}
// collect subscriptionVG list
List<VirtualGood> subscriptions = new ArrayList<VirtualGood>();
for (VirtualGood good : StoreInfo.getGoods()) {
if ((good.getPurchaseType() instanceof PurchaseWithMarket) && ((PurchaseWithMarket) good.getPurchaseType()).isSubscription()) {
subscriptions.add(good);
}
}
// give unset subscriptions and take expired
for (VirtualGood subscription : subscriptions) {
String productId = ((PurchaseWithMarket) subscription.getPurchaseType()).getMarketItem().getProductId();
if (subscriptionIds.contains(productId)) {
// TODO: is here should be 1 to give? Maybe current item has not only just 0/1 state
subscription.give(1, false);
} else {
try {
subscription.take(StoreInventory.getVirtualItemBalance(subscription.getItemId()), false);
} catch (VirtualItemNotFoundException ex) {
// possibly unreachable block
}
}
}
// TODO: Should we notify user about repaired or expired subscriptions?
}
@Override
public void fail(String message) {
SoomlaUtils.LogDebug(TAG, "Subscriptions restoring failed: " + message);
}
@Override
public void verificationStarted(List<IabPurchase> purchases) {
// should we do it in subscription restoring? possibly it should be empty
}
};
try {
mInAppBillingService.restorePurchasesAsync(restorePurchasesListener);
} catch (IllegalStateException ex) {
SoomlaUtils.LogError(TAG, "Can't proceed with restorePurchases. error: " + ex.getMessage());
restorePurchasesListener.fail("Can't proceed with restorePurchases. error: " + ex.getMessage());
}
}
@Override
public void fail(String message) {
reportIabInitFailure(message);
}
});
}
use of com.soomla.store.domain.virtualGoods.VirtualGood 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.VirtualGood 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;
}
Aggregations