use of com.soomla.store.domain.VirtualItem 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.VirtualItem in project android-store by soomla.
the class PurchaseWithVirtualItem method buy.
/**
* Buys the virtual item with other virtual items.
*
* @throws InsufficientFundsException
*/
@Override
public void buy(String payload) throws InsufficientFundsException {
SoomlaUtils.LogDebug(TAG, "Trying to buy a " + getAssociatedItem().getName() + " with " + mAmount + " pieces of " + mTargetItemId);
VirtualItem item = null;
try {
item = StoreInfo.getVirtualItem(mTargetItemId);
} catch (VirtualItemNotFoundException e) {
SoomlaUtils.LogError(TAG, "Target virtual item doesn't exist !");
return;
}
BusProvider.getInstance().post(new ItemPurchaseStartedEvent(getAssociatedItem().getItemId()));
VirtualItemStorage storage = StorageManager.getVirtualItemStorage(item);
assert storage != null;
int balance = storage.getBalance(item.getItemId());
if (balance < mAmount) {
throw new InsufficientFundsException(mTargetItemId);
}
storage.remove(item.getItemId(), mAmount);
getAssociatedItem().give(1);
BusProvider.getInstance().post(new ItemPurchasedEvent(getAssociatedItem().getItemId(), payload));
}
use of com.soomla.store.domain.VirtualItem 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.VirtualItem in project android-store by soomla.
the class StoreInventory method giveVirtualItem.
/**
* Gives your user the given amount of the virtual item with the given <code>itemId</code>.
* For example, when your user plays your game for the first time you GIVE him/her 1000 gems.
*
* NOTE: This action is different than buy -
* You use <code>give(int amount)</code> to give your user something for free.
* You use <code>buy()</code> to give your user something and you get something in return.
*
* @param itemId id of the virtual item to be given
* @param amount amount of the item to be given
* @throws VirtualItemNotFoundException
*/
public static void giveVirtualItem(String itemId, int amount) throws VirtualItemNotFoundException {
VirtualItem item = StoreInfo.getVirtualItem(itemId);
item.give(amount);
}
use of com.soomla.store.domain.VirtualItem in project android-store by soomla.
the class StoreInventory method takeVirtualItem.
/**
* Takes from your user the given amount of the virtual item with the given <code>itemId</code>.
* For example, when your user requests a refund you need to TAKE the item he/she is returning.
*
* @param itemId id of the virtual item to be taken
* @param amount amount of the item to be given
* @throws VirtualItemNotFoundException
*/
public static void takeVirtualItem(String itemId, int amount) throws VirtualItemNotFoundException {
VirtualItem item = StoreInfo.getVirtualItem(itemId);
item.take(amount);
}
Aggregations