GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/npcselldialog.cpp Lines: 5 54 9.3 %
Date: 2017-11-29 Branches: 3 42 7.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "gui/windows/npcselldialog.h"
24
25
#include "being/playerinfo.h"
26
27
#include "gui/windows/confirmdialog.h"
28
29
#include "gui/models/shopitems.h"
30
31
#include "gui/widgets/button.h"
32
#include "gui/widgets/createwidget.h"
33
#include "gui/widgets/shoplistbox.h"
34
#include "gui/widgets/slider.h"
35
36
#include "net/buysellhandler.h"
37
#include "net/net.h"
38
#include "net/npchandler.h"
39
40
#include "resources/iteminfo.h"
41
42
#include "utils/gettext.h"
43
#include "utils/stringutils.h"
44
45
#include "debug.h"
46
47
2
NpcSellDialog::NpcSellDialog(const BeingId npcId) :
48
    SellDialog(IsSell_true,
49
2
        (Net::getNetworkType() != ServerType::TMWATHENA) ?
50
        Advanced_true : Advanced_false),
51
4
    mNpcId(npcId)
52
{
53
2
}
54
55
void NpcSellDialog::sellAction(const ActionEvent &event)
56
{
57
    const std::string &eventId = event.getId();
58
    const int selectedItem = mShopItemList->getSelected();
59
    const ShopItem *const item = mShopItems->at(selectedItem);
60
    if ((item == nullptr) || PlayerInfo::isItemProtected(item->getId()))
61
        return;
62
63
    if (eventId == "presell")
64
    {
65
        if (mAmountItems <= 0 || mAmountItems > mMaxItems)
66
            return;
67
68
        const ItemInfo &info = ItemDB::get(item->getId());
69
        if (info.isProtected())
70
        {
71
            ConfirmDialog *const dialog = CREATEWIDGETR(ConfirmDialog,
72
                // TRANSLATORS: sell confirmation header
73
                _("sell item"),
74
                // TRANSLATORS: sell confirmation message
75
                strprintf(_("Do you really want to sell %s?"),
76
                    info.getName().c_str()),
77
                SOUND_REQUEST,
78
                false,
79
                Modal_true);
80
            dialog->addActionListener(this);
81
            return;
82
        }
83
    }
84
85
    if (mAdvanced == Advanced_true)
86
        sellManyItems(event.getId());
87
    else
88
        sellOneItem();
89
}
90
91
void NpcSellDialog::sellManyItems(const std::string &eventId)
92
{
93
    if (eventId == "confirm")
94
    {
95
        npcHandler->sellItems(mShopItems->allItems());
96
        close();
97
    }
98
    else
99
    {
100
        const int selectedItem = mShopItemList->getSelected();
101
        ShopItem *const item = mShopItems->at(selectedItem);
102
        item->increaseUsedQuantity(mAmountItems);
103
        item->update();
104
        if (mConfirmButton != nullptr)
105
            mConfirmButton->setEnabled(true);
106
    }
107
}
108
109
void NpcSellDialog::sellOneItem()
110
{
111
    if (mAmountItems <= 0 || mAmountItems > mMaxItems)
112
        return;
113
114
    const int selectedItem = mShopItemList->getSelected();
115
    ShopItem *const item = mShopItems->at(selectedItem);
116
    // Attempt sell
117
    mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice();
118
    mMaxItems -= mAmountItems;
119
    while (mAmountItems > 0)
120
    {
121
        // This order is important, item->getCurrentInvIndex() would
122
        // return the inventory index of the next Duplicate otherwise.
123
        const int itemIndex = item->getCurrentInvIndex();
124
        const int sellCount = item->sellCurrentDuplicate(mAmountItems);
125
        npcHandler->sellItem(mNpcId, itemIndex, sellCount);
126
        mAmountItems -= sellCount;
127
    }
128
129
    mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice();
130
    mAmountItems = 1;
131
    mSlider->setValue(0);
132
133
    if (mMaxItems != 0)
134
    {
135
        updateButtonsAndLabels();
136
    }
137
    else
138
    {
139
        // All were sold
140
        mShopItemList->setSelected(-1);
141
        mShopItems->del(selectedItem);
142
143
        Rect scroll;
144
        scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1);
145
        scroll.height = mShopItemList->getRowHeight();
146
        mShopItemList->showPart(scroll);
147
    }
148
}
149
150
void NpcSellDialog::close()
151
{
152
    buySellHandler->close();
153
    Window::close();
154

6
}