GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/npcselldialog.cpp Lines: 5 54 9.3 %
Date: 2018-07-14 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-2018  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 "const/sound.h"
28
29
#include "gui/windows/confirmdialog.h"
30
31
#include "gui/models/shopitems.h"
32
33
#include "gui/widgets/button.h"
34
#include "gui/widgets/createwidget.h"
35
#include "gui/widgets/shoplistbox.h"
36
#include "gui/widgets/slider.h"
37
38
#include "net/buysellhandler.h"
39
#include "net/net.h"
40
#include "net/npchandler.h"
41
42
#include "resources/iteminfo.h"
43
44
#include "utils/gettext.h"
45
#include "utils/stringutils.h"
46
47
#include "debug.h"
48
49
1
NpcSellDialog::NpcSellDialog(const BeingId npcId) :
50
    SellDialog(IsSell_true,
51
1
        (Net::getNetworkType() != ServerType::TMWATHENA) ?
52
        Advanced_true : Advanced_false),
53
1
    mNpcId(npcId)
54
{
55
1
}
56
57
void NpcSellDialog::sellAction(const ActionEvent &event)
58
{
59
    const std::string &eventId = event.getId();
60
    const int selectedItem = mShopItemList->getSelected();
61
    const ShopItem *const item = mShopItems->at(selectedItem);
62
    if ((item == nullptr) || PlayerInfo::isItemProtected(item->getId()))
63
        return;
64
65
    if (eventId == "presell")
66
    {
67
        if (mAmountItems <= 0 || mAmountItems > mMaxItems)
68
            return;
69
70
        const ItemInfo &info = ItemDB::get(item->getId());
71
        if (info.isProtected())
72
        {
73
            ConfirmDialog *const dialog = CREATEWIDGETR(ConfirmDialog,
74
                // TRANSLATORS: sell confirmation header
75
                _("sell item"),
76
                // TRANSLATORS: sell confirmation message
77
                strprintf(_("Do you really want to sell %s?"),
78
                    info.getName().c_str()),
79
                SOUND_REQUEST,
80
                false,
81
                Modal_true,
82
                nullptr);
83
            dialog->addActionListener(this);
84
            return;
85
        }
86
    }
87
88
    if (mAdvanced == Advanced_true)
89
        sellManyItems(event.getId());
90
    else
91
        sellOneItem();
92
}
93
94
void NpcSellDialog::sellManyItems(const std::string &eventId)
95
{
96
    if (eventId == "confirm")
97
    {
98
        npcHandler->sellItems(mShopItems->allItems());
99
        close();
100
    }
101
    else
102
    {
103
        const int selectedItem = mShopItemList->getSelected();
104
        ShopItem *const item = mShopItems->at(selectedItem);
105
        item->increaseUsedQuantity(mAmountItems);
106
        item->update();
107
        if (mConfirmButton != nullptr)
108
            mConfirmButton->setEnabled(true);
109
    }
110
}
111
112
void NpcSellDialog::sellOneItem()
113
{
114
    if (mAmountItems <= 0 || mAmountItems > mMaxItems)
115
        return;
116
117
    const int selectedItem = mShopItemList->getSelected();
118
    ShopItem *const item = mShopItems->at(selectedItem);
119
    // Attempt sell
120
    mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice();
121
    mMaxItems -= mAmountItems;
122
    while (mAmountItems > 0)
123
    {
124
        // This order is important, item->getCurrentInvIndex() would
125
        // return the inventory index of the next Duplicate otherwise.
126
        const int itemIndex = item->getCurrentInvIndex();
127
        const int sellCount = item->sellCurrentDuplicate(mAmountItems);
128
        npcHandler->sellItem(mNpcId, itemIndex, sellCount);
129
        mAmountItems -= sellCount;
130
    }
131
132
    mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice();
133
    mAmountItems = 1;
134
    mSlider->setValue(0);
135
136
    if (mMaxItems != 0)
137
    {
138
        updateButtonsAndLabels();
139
    }
140
    else
141
    {
142
        // All were sold
143
        mShopItemList->setSelected(-1);
144
        mShopItems->del(selectedItem);
145
146
        Rect scroll;
147
        scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1);
148
        scroll.height = mShopItemList->getRowHeight();
149
        mShopItemList->showPart(scroll);
150
    }
151
}
152
153
void NpcSellDialog::close()
154
{
155
    buySellHandler->close();
156
    Window::close();
157

3
}