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

3
}