ManaPlus
npcselldialog.cpp
Go to the documentation of this file.
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 
25 
26 #include "being/playerinfo.h"
27 
28 #include "const/sound.h"
29 
31 
32 #include "gui/models/shopitems.h"
33 
34 #include "gui/widgets/button.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 
54  mNpcId(npcId)
55 {
56 }
57 
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  {
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()),
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  {
100  close();
101  }
102  else
103  {
104  const int selectedItem = mShopItemList->getSelected();
105  ShopItem *const item = mShopItems->at(selectedItem);
107  item->update();
108  if (mConfirmButton != nullptr)
109  mConfirmButton->setEnabled(true);
110  }
111 }
112 
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();
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  {
140  }
141  else
142  {
143  // All were sold
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 
155 {
157  Window::close();
158 }
const bool Advanced_true
Definition: advanced.h:30
const bool Advanced_false
Definition: advanced.h:30
int BeingId
Definition: beingid.h:30
Net::BuySellHandler * buySellHandler
Definition: net.cpp:100
const std::string & getId() const
Definition: actionevent.h:122
int getId() const
Definition: item.h:81
virtual unsigned int getRowHeight() const
Definition: listbox.h:156
int getSelected() const
Definition: listbox.h:168
void setSelected(const int selected)
Definition: listbox.cpp:399
virtual void close() const =0
virtual void sellItems(std::vector< ShopItem * > &items) const =0
virtual void sellItem(const BeingId beingId, const int itemId, const int amount) const =0
NpcSellDialog(const BeingId npcId)
BeingId mNpcId
Definition: npcselldialog.h:57
void sellAction(const ActionEvent &event)
void sellManyItems(const std::string &eventId)
Definition: rect.h:74
int y
Definition: rect.h:214
int height
Definition: rect.h:224
int mMaxItems
Definition: selldialog.h:150
Advanced mAdvanced
Definition: selldialog.h:154
void updateButtonsAndLabels()
Definition: selldialog.cpp:346
ShopItems * mShopItems
Definition: selldialog.h:147
Slider * mSlider
Definition: selldialog.h:146
Button * mConfirmButton
Definition: selldialog.h:138
int mAmountItems
Definition: selldialog.h:151
ShopListBox * mShopItemList
Definition: selldialog.h:142
int mPlayerMoney
Definition: selldialog.h:149
int getCurrentInvIndex() const
Definition: shopitem.h:108
void increaseUsedQuantity(const int amount)
Definition: shopitem.cpp:159
void update()
Definition: shopitem.cpp:119
int getPrice() const
Definition: shopitem.h:132
int sellCurrentDuplicate(const int quantity)
Definition: shopitem.cpp:141
std::vector< ShopItem * > & allItems()
Definition: shopitems.h:139
void del(const unsigned int i)
Definition: shopitems.cpp:161
ShopItem * at(const size_t i) const
Definition: shopitems.cpp:129
void setValue(const double value)
Definition: slider.cpp:474
virtual void showPart(const Rect &rectangle)
Definition: widget.cpp:511
void setEnabled(const bool enabled)
Definition: widget.h:352
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
virtual void close()
Definition: window.cpp:902
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
#define _(s)
Definition: gettext.h:35
const bool IsSell_true
Definition: issell.h:30
const bool Modal_true
Definition: modal.h:30
bool info(InputEvent &event)
Definition: commands.cpp:57
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
ServerTypeT getNetworkType()
Definition: net.cpp:189
bool isItemProtected(const int id)
Definition: playerinfo.cpp:515
Net::NpcHandler * npcHandler
Definition: net.cpp:93
static const std::string SOUND_REQUEST
Definition: sound.h:29
std::string strprintf(const char *const format,...)