use of com.soomla.store.domain.virtualCurrencies.VirtualCurrency in project android-store by soomla.
the class StoreExampleActivity method onCreate.
/**
* Called when the activity starts.
* Displays the main UI screen of the game.
*
* @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.main);
// PurchasingManager.registerObserver(new PurchasingObserver(this));
mRobotView = (ImageView) findViewById(R.id.drag_img);
mRobotView.setOnTouchListener(new MyTouchListener());
findViewById(R.id.rightbox).setOnDragListener(new MyDragListener());
Typeface font = Typeface.createFromAsset(getAssets(), "GoodDog.otf");
((TextView) findViewById(R.id.title_text)).setTypeface(font);
((TextView) findViewById(R.id.main_text)).setTypeface(font);
/*
Initialize SoomlaStore and EventHandler before the store is opened.
Compute your public key (that you got from the Android Market publisher site).
Instead of just storing the entire literal string here embedded in the program,
construct the key at runtime from pieces or use bit manipulation (for example,
XOR with some other string) to hide the actual key. The key itself is not secret
information, but we don't want to make it easy for an adversary to replace the
public key with one of their own and then fake messages from the server.
Generally, encryption keys/passwords should only be kept in memory
long enough to perform the operation they need to perform.
*/
IStoreAssets storeAssets = new MuffinRushAssets();
mEventHandler = new ExampleEventHandler(mHandler, this);
Soomla.initialize("[CUSTOM SECRET HERE]");
SoomlaConfig.logDebug = true;
SoomlaStore.getInstance().initialize(storeAssets);
GooglePlayIabService.AllowAndroidTestPurchases = true;
GooglePlayIabService iabService = GooglePlayIabService.getInstance();
iabService.setPublicKey("xxx");
iabService.configVerifyPurchases(new HashMap<String, Object>() {
{
put("clientId", "xxx.apps.googleusercontent.com");
put("clientSecret", "xxx");
put("refreshToken", "1/xxx");
}
});
// FOR TESTING PURPOSES ONLY: Check if it's a first run, if so add 10000 currencies.
SharedPreferences prefs = SoomlaApp.getAppContext().getSharedPreferences(SoomlaConfig.PREFS_NAME, Context.MODE_PRIVATE);
boolean initialized = prefs.getBoolean(FIRST_RUN, false);
if (!initialized) {
try {
for (VirtualCurrency currency : storeAssets.getCurrencies()) {
StoreInventory.giveVirtualItem(currency.getItemId(), 10000);
}
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(FIRST_RUN, true);
edit.commit();
} catch (VirtualItemNotFoundException e) {
SoomlaUtils.LogError("Example Activity", "Couldn't add first 10000 currencies.");
}
}
}
use of com.soomla.store.domain.virtualCurrencies.VirtualCurrency 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.virtualCurrencies.VirtualCurrency 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.virtualCurrencies.VirtualCurrency 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.virtualCurrencies.VirtualCurrency 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);
}
}
Aggregations