void StoreBScene::onNodeLoaded(CCNode *pNode, CCNodeLoader *pNodeLoader) {

    CC_UNUSED_PARAM(pNode);
    CC_UNUSED_PARAM(pNodeLoader);

    CC_ASSERT(mBackgroundNode);
    CC_ASSERT(mMainNode);
    CC_ASSERT(mTopNode);
    CC_ASSERT(mBottomNode);
    CC_ASSERT(mMuffinAmount);

    for (unsigned int i = 0; i < GOODS_NUMBER; i++) {
        CC_ASSERT(mGoodTitles[i]       );
        CC_ASSERT(mGoodDescriptions[i] );
        CC_ASSERT(mPrices[i]           );

        CCSoomlaError *soomlaError = NULL;
        CCVirtualItem *virtualItem = CCStoreInfo::sharedStoreInfo()->getItemByItemId(
                mGoodTitles[i]->getString(), &soomlaError);
        if (soomlaError) {
            CCStoreUtils::logException("StoreBScene", soomlaError);
            continue;
        }
        CC_ASSERT(virtualItem);
        CCPurchasableVirtualItem *purchasableVirtualItem = dynamic_cast<CCPurchasableVirtualItem *>(virtualItem);
        CC_ASSERT(purchasableVirtualItem);
        CCPurchaseWithMarket *purchaseWithMarket = dynamic_cast<CCPurchaseWithMarket *>(purchasableVirtualItem->getPurchaseType());
        CC_ASSERT(purchaseWithMarket);
        double price = purchaseWithMarket->getMarketItem()->getPrice()->getValue();

        mGoodTitles[i]->setString(virtualItem->getName()->getCString());
        mGoodDescriptions[i]->setString(virtualItem->getDescription()->getCString());
        mPrices[i]->setString(CCString::createWithFormat("%.2f", price)->getCString());

    }

    applyScaleForNode(this);
    fill(mBackgroundNode);
    shiftToTop(mTopNode);
    shiftToBottom(mBottomNode);

    this->setKeypadEnabled(true);

    CCSoomlaError *soomlaError = NULL;
    int balance = CCStoreInventory::sharedStoreInventory()->getItemBalance("currency_muffin", &soomlaError);
    if (soomlaError) {
        CCStoreUtils::logException("StoreBScene", soomlaError);
        balance = 0;
        CCLog("Soomla balance error");
    }
    updateCurrencyBalance(CCInteger::create(balance));
}
예제 #2
0
    void CCPurchasableVirtualItem::putPurchaseTypeToDict(__Dictionary *dict) {
        __Dictionary *purchasableObj = __Dictionary::create();

        if (dynamic_cast<CCPurchaseWithMarket *>(mPurchaseType)) {
            purchasableObj->setObject(__String::create(JSON_PURCHASE_TYPE_MARKET), JSON_PURCHASE_TYPE);

            CCPurchaseWithMarket *purchaseWithMarket = (CCPurchaseWithMarket *)mPurchaseType;
            CCMarketItem *mi = purchaseWithMarket->getMarketItem();
            purchasableObj->setObject(mi->toDictionary(), JSON_PURCHASE_MARKET_ITEM);
        }
        else if (dynamic_cast<CCPurchaseWithVirtualItem *>(mPurchaseType)) {
            CCPurchaseWithVirtualItem *purchaseWithVirtualItem = (CCPurchaseWithVirtualItem *)mPurchaseType;
            purchasableObj->setObject(__String::create(JSON_PURCHASE_TYPE_VI), JSON_PURCHASE_TYPE);
            purchasableObj->setObject(purchaseWithVirtualItem->getItemId(), JSON_PURCHASE_VI_ITEMID);
            purchasableObj->setObject(purchaseWithVirtualItem->getAmount(), JSON_PURCHASE_VI_AMOUNT);
        } else {
            CC_ASSERT(false);
        }

        dict->setObject(purchasableObj, JSON_PURCHASABLE_ITEM);
    }
    bool CCStoreEventDispatcher::init() {

        if (!CCAbstractAggregatedEventHandler::init()) {
            return false;
        }

        CCSoomlaEventDispatcher *eventDispatcher = CCSoomlaEventDispatcher::getInstance();

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_BILLING_NOT_SUPPORTED,
                [this](__Dictionary *parameters) {
                    this->onBillingNotSupported();
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_BILLING_SUPPORTED,
                [this](__Dictionary *parameters) {
                    this->onBillingSupported();
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_CURRENCY_BALANCE_CHANGED,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    __Integer *balance = (__Integer *)(parameters->objectForKey("balance"));
                    __Integer *amountAdded = (__Integer *)(parameters->objectForKey("amountAdded"));
                    CCError *error = NULL;
                    CCVirtualCurrency *virtualCurrency =
                            dynamic_cast<CCVirtualCurrency *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_CURRENCY_BALANCE_CHANGED, error);
                        return;
                    }
                    CC_ASSERT(virtualCurrency);
                    this->onCurrencyBalanceChanged(virtualCurrency, balance->getValue(), amountAdded->getValue());
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_GOOD_BALANCE_CHANGED,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    __Integer *balance = (__Integer *)(parameters->objectForKey("balance"));
                    __Integer *amountAdded = (__Integer *)(parameters->objectForKey("amountAdded"));
                    CCError *error = NULL;
                    CCVirtualGood *virtualGood =
                            dynamic_cast<CCVirtualGood *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_GOOD_BALANCE_CHANGED, error);
                        return;
                    }
                    CC_ASSERT(virtualGood);
                    this->onGoodBalanceChanged(virtualGood, balance->getValue(), amountAdded->getValue());
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_GOOD_EQUIPPED,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    CCError *error = NULL;
                    CCEquippableVG *equippableVG =
                            dynamic_cast<CCEquippableVG *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_GOOD_EQUIPPED, error);
                        return;
                    }
                    CC_ASSERT(equippableVG);
                    this->onGoodEquipped(equippableVG);
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_GOOD_UNEQUIPPED,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    CCError *error = NULL;
                    CCEquippableVG *equippableVG =
                            dynamic_cast<CCEquippableVG *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_GOOD_UNEQUIPPED, error);
                        return;
                    }
                    CC_ASSERT(equippableVG);
                    this->onGoodUnEquipped(equippableVG);
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_GOOD_UPGRADE,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    __String *vguItemId = (__String *)(parameters->objectForKey("vguItemId"));

                    CCError *error;

                    error = NULL;
                    CCVirtualGood *virtualGood =
                            dynamic_cast<CCVirtualGood *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_GOOD_UPGRADE, error);
                        return;
                    }
                    CC_ASSERT(virtualGood);

                    error = NULL;
                    CCUpgradeVG *upgradeVG =
                            dynamic_cast<CCUpgradeVG *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(vguItemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_GOOD_UPGRADE, error);
                        return;
                    }
                    CC_ASSERT(upgradeVG);
                    this->onGoodUpgrade(virtualGood, upgradeVG);
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_ITEM_PURCHASED,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    CCError *error = NULL;
                    CCPurchasableVirtualItem *purchasableVirtualItem =
                            dynamic_cast<CCPurchasableVirtualItem *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_ITEM_PURCHASED, error);
                        return;
                    }
                    CC_ASSERT(purchasableVirtualItem);
                    this->onItemPurchased(purchasableVirtualItem);
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_ITEM_PURCHASE_STARTED,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    CCError *error = NULL;
                    CCPurchasableVirtualItem *purchasableVirtualItem = dynamic_cast<CCPurchasableVirtualItem *>(
                            CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_ITEM_PURCHASE_STARTED, error);
                        return;
                    }
                    CC_ASSERT(purchasableVirtualItem);
                    this->onItemPurchaseStarted(purchasableVirtualItem);
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_MARKET_PURCHASE_CANCELED,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    CCError *error = NULL;
                    CCPurchasableVirtualItem *purchasableVirtualItem =
                            dynamic_cast<CCPurchasableVirtualItem *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_MARKET_PURCHASE_CANCELED, error);
                        return;
                    }
                    CC_ASSERT(purchasableVirtualItem);
                    this->onMarketPurchaseCancelled(purchasableVirtualItem);
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_MARKET_PURCHASE,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    CCError *error = NULL;
                    CCPurchasableVirtualItem *purchasableVirtualItem =
                            dynamic_cast<CCPurchasableVirtualItem *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_MARKET_PURCHASE, error);
                        return;
                    }
                    CC_ASSERT(purchasableVirtualItem);
                    __String *token = (__String *)(parameters->objectForKey("token"));
                    __String *payload = (__String *)(parameters->objectForKey("payload"));
                    __SetIterator i;
                    this->onMarketPurchase(purchasableVirtualItem, token, payload);
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_MARKET_PURCHASE_STARTED,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    CCError *error = NULL;
                    CCPurchasableVirtualItem *purchasableVirtualItem = dynamic_cast<CCPurchasableVirtualItem *>(
                            CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_MARKET_PURCHASE_STARTED, error);
                        return;
                    }
                    CC_ASSERT(purchasableVirtualItem);
                    this->onMarketPurchaseStarted(purchasableVirtualItem);
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_MARKET_PURCHASE_STARTED,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    CCError *error = NULL;
                    CCPurchasableVirtualItem *purchasableVirtualItem =
                            dynamic_cast<CCPurchasableVirtualItem *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_MARKET_PURCHASE_STARTED, error);
                        return;
                    }
                    CC_ASSERT(purchasableVirtualItem);
                    this->onMarketPurchaseStarted(purchasableVirtualItem);
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_MARKET_ITEMS_REFRESHED,
                [this](__Dictionary *parameters) {
                    __Array *marketItemDicts = (__Array *)(parameters->objectForKey("marketItems"));
                    __Array *marketItems = __Array::create();

                    CCError *error = NULL;
                    __Dictionary *marketItem = NULL;
                    for (unsigned int i = 0; i < marketItemDicts->count(); i++) {
                        marketItem = dynamic_cast<__Dictionary *>(marketItemDicts->getObjectAtIndex(i));
                        CC_ASSERT(marketItem);
                        __String *productId = dynamic_cast<__String *>(marketItem->objectForKey("productId"));
                        __String *marketPrice = dynamic_cast<__String *>(marketItem->objectForKey("marketPrice"));
                        __String *marketTitle = dynamic_cast<__String *>(marketItem->objectForKey("marketTitle"));
                        __String *marketDescription = dynamic_cast<__String *>(marketItem->objectForKey("marketDesc"));

                        CCPurchasableVirtualItem *pvi = CCStoreInfo::sharedStoreInfo()->getPurchasableItemWithProductId(
                                productId->getCString(), &error);
                        if (error) {
                            CCStoreUtils::logException(CCStoreConsts::EVENT_MARKET_ITEMS_REFRESHED, error);
                            return;
                        }
                        CC_ASSERT(pvi);

                        CCPurchaseWithMarket *purchaseWithMarket = dynamic_cast<CCPurchaseWithMarket *>(pvi->getPurchaseType());
                        CC_ASSERT(purchaseWithMarket);
                        CCMarketItem *mi = purchaseWithMarket->getMarketItem();
                        mi->setMarketPrice(marketPrice);
                        mi->setMarketTitle(marketTitle);
                        mi->setMarketDescription(marketDescription);
                        pvi->save();

                        marketItems->addObject(purchaseWithMarket);
                    }

                    this->onMarketItemsRefreshed(marketItems);
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_MARKET_ITEMS_REFRESH_STARTED,
                [this](__Dictionary *parameters) {
                    this->onMarketItemsRefreshStarted();
                });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_MARKET_PURCHASE_VERIFICATION,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    CCError *error = NULL;
                    CCPurchasableVirtualItem *purchasableVirtualItem =
                            dynamic_cast<CCPurchasableVirtualItem *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_MARKET_PURCHASE_VERIFICATION, error);
                        return;
                    }
                    CC_ASSERT(purchasableVirtualItem);
                    this->onMarketPurchaseVerification(purchasableVirtualItem);
               });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_RESTORE_TRANSACTION_FINISHED,
                [this](__Dictionary *parameters) {
                    __Bool *success = (__Bool *)(parameters->objectForKey("success"));
                    this->onRestoreTransactionsFinished(success->getValue());
               });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_RESTORE_TRANSACTION_STARTED,
                [this](__Dictionary *parameters) {
                    this->onRestoreTransactionsStarted();
               });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_UNEXPECTED_ERROR_IN_STORE,
                [this](__Dictionary *parameters) {
                    this->onUnexpectedErrorInStore();
               });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_STORE_CONTROLLER_INITIALIZED,
                [this](__Dictionary *parameters) {
                    this->onStoreControllerInitialized();
               });

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_MARKET_REFUND,
                [this](__Dictionary *parameters) {
                    __String *itemId = (__String *)(parameters->objectForKey("itemId"));
                    CCError *error = NULL;
                    CCPurchasableVirtualItem *purchasableVirtualItem =
                            dynamic_cast<CCPurchasableVirtualItem *>(CCStoreInfo::sharedStoreInfo()->getItemByItemId(itemId->getCString(), &error));
                    if (error) {
                        CCStoreUtils::logException(CCStoreConsts::EVENT_MARKET_REFUND, error);
                        return;
                    }
                    CC_ASSERT(purchasableVirtualItem);
                    this->onMarketRefund(purchasableVirtualItem);
               });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_IAB_SERVICE_STARTED,
                [this](__Dictionary *parameters) {
                    this->onIabServiceStarted();
               });

        eventDispatcher->registerEventHandler(CCStoreConsts::EVENT_IAB_SERVICE_STOPPED,
                [this](__Dictionary *parameters) {
                    this->onIabServiceStopped();
               });
#endif
        return true;
    }