Exemplo n.º 1
0
    /**
     * Retrieves the balance of the given virtual item.
     *
     * @param item the required virtual item
     * @return the balance of the required virtual item
     */
    public int getBalance(VirtualItem item){
        SoomlaUtils.LogDebug(mTag, "fetching balance for virtual item with itemId: "
                + item.getItemId());

        String itemId = item.getItemId();
        String key = keyBalance(itemId);
        String val = KeyValueStorage.GetValue(key);

        int balance = 0;
        if (val != null) {
            try
            {
                balance = int.Parse(val);
            }
            catch (Exception e)
            {
                SoomlaUtils.LogError(mTag, "Error casting string to int value: "+val+" "+e.Message);
                return 0;
            }
        }

        SoomlaUtils.LogDebug(mTag, "the balance for " + item.getItemId() + " is " + balance);

        return balance;
    }
Exemplo n.º 2
0
        /**
         * Adds the given amount of items to the storage, and if notify is true
         * posts the change in the balance to the event bus.
         *
         * @param item the required virtual item
         * @param amount the amount of items to add
         * @param notify if true posts balance change event
         * @return new balance
         */
        public int add(VirtualItem item, int amount, bool notify)
        {
            SoomlaUtils.LogDebug(mTag, "adding " + amount + " " + item.getName());

            String itemId = item.getItemId();
            int balance = getBalance(item);
            if (balance < 0) { /* in case the user "adds" a negative value */
            balance = 0;
            amount = 0;
            }
            String balanceStr = (balance + amount).ToString();
            String key = keyBalance(itemId);
            KeyValueStorage.SetValue(key, balanceStr);

            if (notify) {
            postBalanceChangeEvent(item, balance+amount, amount);
            }

            return balance + amount;
        }
Exemplo n.º 3
0
    /**
     * 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.Add(virtualItem.getItemId(), virtualItem);

        if (virtualItem is VirtualCurrency) {
            for(int i=0; i<mCurrencies.Count; i++) {
                if (mCurrencies[i].getItemId() == virtualItem.getItemId()) {
                    mCurrencies.RemoveAt(i);
                    break;
                }
            }
            mCurrencies.Add((VirtualCurrency)virtualItem);
        }

        if (virtualItem is VirtualCurrencyPack) {
            VirtualCurrencyPack vcp = (VirtualCurrencyPack)virtualItem;
            PurchaseType purchaseType = vcp.GetPurchaseType();
            if (purchaseType is PurchaseWithMarket) {
                mPurchasableItems.Add(((PurchaseWithMarket) purchaseType).getMarketItem()
                        .getProductId(), vcp);
            }

            for(int i=0; i<mCurrencyPacks.Count; i++) {
                if (mCurrencyPacks[i].getItemId() == vcp.getItemId()) {
                    mCurrencyPacks.RemoveAt(i);
                    break;
                }
            }
            mCurrencyPacks.Add(vcp);
        }

        if (virtualItem is VirtualGood) {
            VirtualGood vg = (VirtualGood)virtualItem;

            if (vg is UpgradeVG) {
                List<UpgradeVG> upgrades = mGoodsUpgrades[((UpgradeVG) vg).getGoodItemId()];
                if (upgrades == null) {
                    upgrades = new List<UpgradeVG>();
                    mGoodsUpgrades.Add(((UpgradeVG) vg).getGoodItemId(), upgrades);
                }
                upgrades.Add((UpgradeVG) vg);
            }

            PurchaseType purchaseType = vg.GetPurchaseType();
            if (purchaseType is PurchaseWithMarket) {
                mPurchasableItems.Add(((PurchaseWithMarket) purchaseType).getMarketItem()
                        .getProductId(), vg);
            }

            for(int i=0; i<mGoods.Count; i++) {
                if (mGoods[i].getItemId() == vg.getItemId()) {
                    mGoods.RemoveAt(i);
                    break;
                }
            }
            mGoods.Add(vg);
        }
        /*
        if (virtualItem is NonConsumableItem) {
            NonConsumableItem non = (NonConsumableItem) virtualItem;

            PurchaseType purchaseType = non.GetPurchaseType();
            if (purchaseType is PurchaseWithMarket) {
                mPurchasableItems.Add(((PurchaseWithMarket) purchaseType).getMarketItem()
                        .getProductId(), non);
            }

            for(int i=0; i<mNonConsumables.Count; i++) {
                if (mNonConsumables[i].getItemId() == non.getItemId()) {
                    mNonConsumables.RemoveAt(i);
                    break;
                }
            }
            mNonConsumables.Add(non);
        }*/
    }
Exemplo n.º 4
0
    /**
     * Sets the balance of the given virtual item to be the given balance, and if notify is true
     * posts the change in the balance to the event bus.
     *
     * @param item the required virtual item
     * @param balance the new balance to be set
     * @param notify if notify is true post balance change event
     * @return the balance of the required virtual item
     */
    public int setBalance(VirtualItem item, int balance, bool notify) {
        SoomlaUtils.LogDebug(mTag, "setting balance " + balance + " to " + item.getName() + ".");

        int oldBalance = getBalance(item);
        if (oldBalance == balance) {
            return balance;
        }

        String itemId = item.getItemId();

        String balanceStr = balance.ToString();
        String key = keyBalance(itemId);

        KeyValueStorage.SetValue(key, balanceStr);

        if (notify) {
            postBalanceChangeEvent(item, balance, 0);
        }

        return balance;
    }
Exemplo n.º 5
0
    /**
     * Removes the given amount from the given virtual item's balance, and if notify is true
     * posts the change in the balance to the event bus.
     *
     * @param item is the virtual item to remove the given amount from
     * @param amount is the amount to remove
     * @param notify if notify is true post balance change event
     * @return new balance
     */
    public int remove(VirtualItem item, int amount, bool notify){
        SoomlaUtils.LogDebug(mTag, "Removing " + amount + " " + item.getName() + ".");

        String itemId = item.getItemId();
        int balance = getBalance(item) - amount;
        if (balance < 0) {
            balance = 0;
            amount = 0;
        }
        String balanceStr = balance.ToString();
        String key = keyBalance(itemId);
        KeyValueStorage.SetValue(key, balanceStr);

        if (notify) {
            postBalanceChangeEvent(item, balance, -1*amount);
        }

        return balance;
    }
Exemplo n.º 6
0
        private void buildShopLine(VirtualItem item)
        {
            StackPanel stackP = new StackPanel();
            stackP.Orientation = System.Windows.Controls.Orientation.Horizontal;
            stackP.Margin = new Thickness(0, 15, 0, 0);

            StackPanel buttonStack = new StackPanel();
            buttonStack.Orientation = System.Windows.Controls.Orientation.Vertical;

            StackPanel textStack = new StackPanel();
            textStack.Orientation = System.Windows.Controls.Orientation.Vertical;

            stackP.Children.Add(buttonStack);
            stackP.Children.Add(textStack);

            Button buy = new Button();
            buy.Margin = new Thickness(0, 0, 10, 0);
            buy.Click += buyItem;
            buy.Content = "buy";
            buy.CommandParameter = item.getItemId();
            buy.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            buy.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            buttonStack.Children.Add(buy);

            if(item is EquippableVG)
            {
                Button equip = new Button();
                equip.Name = item.getItemId()+"equip";
                equip.Margin = new Thickness(0, 0, 10, 0);
                equip.CommandParameter = item.getItemId();
                equip.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                equip.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                EquippableVG evg = (EquippableVG)item;
                if (StoreInventory.isVirtualGoodEquipped(item.getItemId()))
                {
                    equip.Content = "unequip";
                    equip.Click += unequipItem;
                }
                else
                {
                    equip.Content = "equip";
                    equip.Click += equipItem;
                }
                buttonStack.Children.Add(equip);
            }

            TextBlock balance = new TextBlock();
            balance.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            balance.Margin = new Thickness(0,0,10,0);
            if (!(item is VirtualCurrencyPack) && !(item is NonConsumableItem))
            {
                balance.Text = "balance: "+StoreInventory.getVirtualItemBalance(item.getItemId()).ToString();
            }
            balance.Name = item.getItemId() + "balance";
            textStack.Children.Add(balance);

            TextBlock name = new TextBlock();
            name.Margin = new Thickness(0, 0, 10, 0);
            name.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            name.Text = "id: "+item.getItemId();
            textStack.Children.Add(name);

            TextBlock price = new TextBlock();
            price.Margin = new Thickness(0, 0, 10, 0);
            price.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            TextBlock currency = new TextBlock();
            currency.Margin = new Thickness(0, 0, 10, 0);
            currency.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            if(item is PurchasableVirtualItem)
            {
                PurchasableVirtualItem pvi = (PurchasableVirtualItem)item;
                if(pvi.GetPurchaseType() is PurchaseWithVirtualItem)
                {
                    PurchaseWithVirtualItem purchaseType = (PurchaseWithVirtualItem)pvi.GetPurchaseType();
                    price.Text = "price: "+purchaseType.getAmount().ToString();
                    currency.Text = "currency: "+purchaseType.getTargetItemId();
                }
                if (pvi.GetPurchaseType() is PurchaseWithMarket)
                {
                    PurchaseWithMarket purchaseType = (PurchaseWithMarket)pvi.GetPurchaseType();
                    price.Text = "price: "+purchaseType.getMarketItem().getMarketPrice();
                    
                }

                textStack.Children.Add(price);
                textStack.Children.Add(currency);
            }

            if (item is VirtualCurrencyPack)
            {
                VirtualCurrencyPack vcp = (VirtualCurrencyPack)item;

                TextBlock currencyId = new TextBlock();
                currencyId.Text = "give currency: "+vcp.getCurrencyItemId().ToString();

                TextBlock currencyAmount = new TextBlock();
                currencyAmount.Text = "give amount: "+vcp.getCurrencyAmount().ToString();
                textStack.Children.Add(currencyId);
                textStack.Children.Add(currencyAmount);
            }

            MainStackPanel.Children.Add(stackP);
        }