Ejemplo n.º 1
0
Archivo: buy.cpp Proyecto: B-Rich/mana
void BuyDialog::action(const gcn::ActionEvent &event)
{
    if (event.getId() == "quit")
    {
        close();
        return;
    }

    int selectedItem = mShopItemList->getSelected();

    // The following actions require a valid selection
    if (selectedItem < 0 ||
            selectedItem >= (int) mShopItems->getNumberOfElements())
    {
        return;
    }

    if (event.getId() == "slider")
    {
        mAmountItems = (int) mSlider->getValue();
        updateButtonsAndLabels();
    }
    else if (event.getId() == "inc" && mAmountItems < mMaxItems)
    {
        mAmountItems++;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (event.getId() == "dec" && mAmountItems > 1)
    {
        mAmountItems--;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (event.getId() == "max")
    {
        mAmountItems = mMaxItems;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    // TODO: Actually we'd have a bug elsewhere if this check for the number
    // of items to be bought ever fails, Bertram removed the assertions, is
    // there a better way to ensure this fails in an _obvious_ way in C++?
    else if (event.getId() == "buy" && mAmountItems > 0 &&
                mAmountItems <= mMaxItems)
    {
        Net::getNpcHandler()->buyItem(mNpcId,
                                      mShopItems->at(selectedItem)->getId(),
                                      mAmountItems);

        // Update money and adjust the max number of items that can be bought
        mMaxItems -= mAmountItems;
        int price = mShopItems->at(selectedItem)->getPrice();
        if (price < 0)
            price = 0;
        setMoney(mMoney - mAmountItems * price);
        valueChanged(gcn::SelectionEvent(mShopItemList));
    }
}
Ejemplo n.º 2
0
void SellDialog::action(const ActionEvent &event)
{
    const std::string &eventId = event.getId();

    if (eventId == "quit")
    {
        close();
        return;
    }

    const int selectedItem = mShopItemList->getSelected();

    // The following actions require a valid item selection
    if (selectedItem == -1
        || selectedItem >= mShopItems->getNumberOfElements())
    {
        return;
    }

    if (eventId == "slider")
    {
        if (mSlider)
        {
            mAmountItems = CAST_S32(mSlider->getValue());
            updateButtonsAndLabels();
        }
    }
    else if (eventId == "inc" && mSlider && mAmountItems < mMaxItems)
    {
        mAmountItems++;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "dec" && mSlider && mAmountItems > 1)
    {
        mAmountItems--;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "max" && mSlider)
    {
        mAmountItems = mMaxItems;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "presell" ||
             eventId == "sell" ||
             eventId == "yes" ||
             eventId == "confirm")
    {
        sellAction(event);
    }
}
Ejemplo n.º 3
0
void BuyDialog::setMoney(const int amount)
{
    mMoney = amount;
    mShopItemList->setPlayersMoney(amount);

    updateButtonsAndLabels();
}
Ejemplo n.º 4
0
void SellDialog::reset()
{
    mShopItems->clear();
    mSlider->setValue(0);
    mShopItemList->setSelected(-1);
    updateButtonsAndLabels();
}
Ejemplo n.º 5
0
void SellDialog::valueChanged(const gcn::SelectionEvent &event)
{
    // Reset amount of items and update labels
    mAmountItems = 1;
    mSlider->setValue(0);

    updateButtonsAndLabels();
    mSlider->gcn::Slider::setScale(1, mMaxItems);
}
Ejemplo n.º 6
0
void SellDialog::reset()
{
    mShopItems->clear();
    mSlider->setValue(0);

    // Reset previous selected item to prevent failing asserts
    mShopItemList->setSelected(-1);

    updateButtonsAndLabels();
}
Ejemplo n.º 7
0
void NpcSellDialog::sellOneItem()
{
    if (mAmountItems <= 0 || mAmountItems > mMaxItems)
        return;

    const int selectedItem = mShopItemList->getSelected();
    ShopItem *const item = mShopItems->at(selectedItem);
    // Attempt sell
    mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice();
    mMaxItems -= mAmountItems;
    while (mAmountItems > 0)
    {
        // This order is important, item->getCurrentInvIndex() would
        // return the inventory index of the next Duplicate otherwise.
        const int itemIndex = item->getCurrentInvIndex();
        const int sellCount = item->sellCurrentDuplicate(mAmountItems);
        npcHandler->sellItem(mNpcId, itemIndex, sellCount);
        mAmountItems -= sellCount;
    }

    mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice();
    mAmountItems = 1;
    mSlider->setValue(0);

    if (mMaxItems)
    {
        updateButtonsAndLabels();
    }
    else
    {
        // All were sold
        mShopItemList->setSelected(-1);
        mShopItems->del(selectedItem);

        Rect scroll;
        scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1);
        scroll.height = mShopItemList->getRowHeight();
        mShopItemList->showPart(scroll);
    }
}
Ejemplo n.º 8
0
void BuyDialog::action(const ActionEvent &event)
{
    const std::string &eventId = event.getId();
    if (eventId == "quit")
    {
        close();
        return;
    }
    else if (eventId == "sort")
    {
        sort();
        if (mSortDropDown)
            config.setValue("buySortOrder", mSortDropDown->getSelected());
        return;
    }
    else if (eventId == "namefilter")
    {
        applyNameFilter(mFilterTextField->getText());
    }

    const int selectedItem = mShopItemList->getSelected();

    // The following actions require a valid selection
    if (selectedItem < 0 || selectedItem >= mShopItems->getNumberOfElements())
        return;

    if (eventId == "slider")
    {
        mAmountItems = static_cast<int>(mSlider->getValue());
        mAmountField->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "inc" && mAmountItems < mMaxItems)
    {
        mAmountItems++;
        mSlider->setValue(mAmountItems);
        mAmountField->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "dec" && mAmountItems > 1)
    {
        mAmountItems--;
        mSlider->setValue(mAmountItems);
        mAmountField->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "max")
    {
        mAmountItems = mMaxItems;
        mSlider->setValue(mAmountItems);
        mAmountField->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "amount")
    {
        mAmountItems = mAmountField->getValue();
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "buy" && mAmountItems > 0 && mAmountItems <= mMaxItems)
    {
        ShopItem *const item = mShopItems->at(selectedItem);
        if (mNpcId == fromInt(Items, BeingId))
        {
            adminHandler->createItems(item->getId(),
                mAmountItems, item->getColor());
        }
        else if (mNpcId != fromInt(Nick, BeingId))
        {
#ifdef EATHENA_SUPPORT
            if (mNpcId == fromInt(Market, BeingId))
            {
                marketHandler->buyItem(item->getId(),
                    item->getType(),
                    item->getColor(),
                    mAmountItems);
                item->increaseQuantity(-mAmountItems);
                item->update();
            }
            else if (mNpcId == fromInt(Cash, BeingId))
            {
                cashShopHandler->buyItem(item->getPrice(),
                    item->getId(),
                    item->getColor(),
                    mAmountItems);
            }
            else
#endif
            {
                npcHandler->buyItem(mNpcId,
                    item->getId(),
                    item->getColor(),
                    mAmountItems);
            }

            updateSlider(selectedItem);
        }
        else if (mNpcId == fromInt(Nick, BeingId))
        {
#ifdef EATHENA_SUPPORT
            if (serverFeatures->haveVending())
            {
                Being *const being = actorManager->findBeingByName(mNick);
                if (being)
                {
                    vendingHandler->buy(being,
                        item->getInvIndex(),
                        mAmountItems);
                    item->increaseQuantity(-mAmountItems);
                    item->update();
                    updateSlider(selectedItem);
                }
            }
            else if (tradeWindow)
#else
            if (tradeWindow)
#endif
            {
                if (item)
                {
                    buySellHandler->sendBuyRequest(mNick,
                        item, mAmountItems);
                    tradeWindow->addAutoMoney(mNick,
                        item->getPrice() * mAmountItems);
                }
            }
        }
    }
}
Ejemplo n.º 9
0
void SellDialog::action(const gcn::ActionEvent &event)
{
    if (event.getId() == "quit")
    {
        close();
        return;
    }

    int selectedItem = mShopItemList->getSelected();

    // The following actions require a valid item selection
    if (selectedItem == -1 ||
            selectedItem >= (int) mShopItems->getNumberOfElements())
    {
        return;
    }

    if (event.getId() == "slider")
    {
        mAmountItems = (int) mSlider->getValue();
        updateButtonsAndLabels();
    }
    else if (event.getId() == "+" && mAmountItems < mMaxItems)
    {
        mAmountItems++;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (event.getId() == "-" && mAmountItems > 1)
    {
        mAmountItems--;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (event.getId() == "max")
    {
        mAmountItems = mMaxItems;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (event.getId() == "sell" && mAmountItems > 0
            && mAmountItems <= mMaxItems)
    {
        // Attempt sell
        ShopItem *item = mShopItems->at(selectedItem);
        int sellCount, itemIndex;
        mPlayerMoney +=
            mAmountItems * mShopItems->at(selectedItem)->getPrice();
        mMaxItems -= mAmountItems;
        while (mAmountItems > 0) {
            // This order is important, item->getCurrentInvIndex() would return
            // the inventory index of the next Duplicate otherwise.
            itemIndex = item->getCurrentInvIndex();
            sellCount = item->sellCurrentDuplicate(mAmountItems);
            Net::getNpcHandler()->sellItem(current_npc, itemIndex, sellCount);
            mAmountItems -= sellCount;
        }

        mPlayerMoney +=
            mAmountItems * mShopItems->at(selectedItem)->getPrice();
        mAmountItems = 1;
        mSlider->setValue(0);

        if (!mMaxItems)
        {
            // All were sold
            mShopItemList->setSelected(-1);
            delete mShopItems->at(selectedItem);
            mShopItems->erase(selectedItem);

            gcn::Rectangle scroll;
            scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1);
            scroll.height = mShopItemList->getRowHeight();
            mShopItemList->showPart(scroll);
        }
        else
        {
            mSlider->gcn::Slider::setScale(1, mMaxItems);
            // Update only when there are items left, the entry doesn't exist
            // otherwise and can't be updated
            updateButtonsAndLabels();
        }
    }
}
Ejemplo n.º 10
0
void BuyDialog::action(const gcn::ActionEvent &event)
{
    const std::string &eventId = event.getId();
    if (eventId == "quit")
    {
        close();
        return;
    }
    else if (eventId == "sort")
    {
        sort();
        if (mSortDropDown)
            config.setValue("buySortOrder", mSortDropDown->getSelected());
        return;
    }

    const int selectedItem = mShopItemList->getSelected();

    // The following actions require a valid selection
    if (selectedItem < 0 || selectedItem >= mShopItems->getNumberOfElements())
        return;

    if (eventId == "slider")
    {
        mAmountItems = static_cast<int>(mSlider->getValue());
        mAmountField->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "inc" && mAmountItems < mMaxItems)
    {
        mAmountItems++;
        mSlider->setValue(mAmountItems);
        mAmountField->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "dec" && mAmountItems > 1)
    {
        mAmountItems--;
        mSlider->setValue(mAmountItems);
        mAmountField->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "max")
    {
        mAmountItems = mMaxItems;
        mSlider->setValue(mAmountItems);
        mAmountField->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "amount")
    {
        mAmountItems = mAmountField->getValue();
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "buy" && mAmountItems > 0 && mAmountItems <= mMaxItems)
    {
        if (mNpcId == -2)
        {
            const ShopItem *const item = mShopItems->at(selectedItem);
            Net::getAdminHandler()->createItems(item->getId(),
                mAmountItems, item->getColor());
        }
        else if (mNpcId != -1)
        {
            const ShopItem *const item = mShopItems->at(selectedItem);
            Net::getNpcHandler()->buyItem(mNpcId, item->getId(),
                item->getColor(), mAmountItems);

            // Update money and adjust the max number of items
            // that can be bought
            mMaxItems -= mAmountItems;
            setMoney(mMoney -
                     mAmountItems * mShopItems->at(selectedItem)->getPrice());

            // Reset selection
            mAmountItems = 1;
            mSlider->setValue(1);
            mSlider->setScale(1, mMaxItems);
        }
        else if (tradeWindow)
        {
            const ShopItem *const item = mShopItems->at(selectedItem);
            if (item)
            {
                Net::getBuySellHandler()->sendBuyRequest(mNick,
                    item, mAmountItems);
                tradeWindow->addAutoMoney(mNick,
                    item->getPrice() * mAmountItems);
            }
        }
    }
}
Ejemplo n.º 11
0
void SellDialog::action(const gcn::ActionEvent &event)
{
    const std::string &eventId = event.getId();

    if (eventId == "quit")
    {
        close();
        return;
    }

    const int selectedItem = mShopItemList->getSelected();

    // The following actions require a valid item selection
    if (selectedItem == -1
        || selectedItem >= mShopItems->getNumberOfElements())
    {
        return;
    }

    if (eventId == "slider")
    {
        mAmountItems = static_cast<int>(mSlider->getValue());
        updateButtonsAndLabels();
    }
    else if (eventId == "inc" && mAmountItems < mMaxItems)
    {
        mAmountItems++;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "dec" && mAmountItems > 1)
    {
        mAmountItems--;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if (eventId == "max")
    {
        mAmountItems = mMaxItems;
        mSlider->setValue(mAmountItems);
        updateButtonsAndLabels();
    }
    else if ((eventId == "presell" || eventId == "sell" || eventId == "yes")
             && mAmountItems > 0 && mAmountItems <= mMaxItems)
    {
        if (mNpcId != -1)
        {
            ShopItem *const item = mShopItems->at(selectedItem);
            if (PlayerInfo::isItemProtected(item->getId()))
                return;
            if (eventId == "presell")
            {
                const ItemInfo &info = ItemDB::get(item->getId());
                if (info.isProtected())
                {
                    ConfirmDialog *const dialog = new ConfirmDialog(
                        // TRANSLATORS: sell confirmation header
                        _("sell item"),
                        // TRANSLATORS: sell confirmation message
                        strprintf(_("Do you really want to sell %s?"),
                        info.getName().c_str()), SOUND_REQUEST, false, true);
                    dialog->postInit();
                    dialog->addActionListener(this);
                    return;
                }
            }
            // Attempt sell
            mPlayerMoney +=
                mAmountItems * mShopItems->at(selectedItem)->getPrice();
            mMaxItems -= mAmountItems;
            while (mAmountItems > 0)
            {
#ifdef MANASERV_SUPPORT
                // This order is important, item->getCurrentInvIndex() would
                // return the inventory index of the next Duplicate otherwise.
                int itemIndex = item->getCurrentInvIndex();
                const int sellCount = item->sellCurrentDuplicate(mAmountItems);
                // For Manaserv, the Item id is to be given as index.
                if ((Net::getNetworkType() == ServerInfo::MANASERV))
                    itemIndex = item->getId();
#else
                // This order is important, item->getCurrentInvIndex() would
                // return the inventory index of the next Duplicate otherwise.
                const int itemIndex = item->getCurrentInvIndex();
                const int sellCount = item->sellCurrentDuplicate(mAmountItems);
#endif
                Net::getNpcHandler()->sellItem(mNpcId, itemIndex, sellCount);
                mAmountItems -= sellCount;
            }

            mPlayerMoney +=
                mAmountItems * mShopItems->at(selectedItem)->getPrice();
            mAmountItems = 1;
            mSlider->setValue(0);

            if (mMaxItems)
            {
                updateButtonsAndLabels();
            }
            else
            {
                // All were sold
                mShopItemList->setSelected(-1);
                delete mShopItems->at(selectedItem);
                mShopItems->erase(selectedItem);

                gcn::Rectangle scroll;
                scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1);
                scroll.height = mShopItemList->getRowHeight();
                mShopItemList->showPart(scroll);
            }
        }
        else
        {
            ShopItem *const item = mShopItems->at(selectedItem);
            Net::getBuySellHandler()->sendSellRequest(mNick,
                    item, mAmountItems);

            if (tradeWindow)
                tradeWindow->addAutoItem(mNick, item, mAmountItems);
        }
    }
}