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-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 
24 
25 #include "being/playerinfo.h"
26 
27 #include "const/sound.h"
28 
30 
31 #include "gui/models/shopitems.h"
32 
33 #include "gui/widgets/button.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 
53  mNpcId(npcId)
54 {
55 }
56 
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  {
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()),
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  {
99  close();
100  }
101  else
102  {
103  const int selectedItem = mShopItemList->getSelected();
104  ShopItem *const item = mShopItems->at(selectedItem);
106  item->update();
107  if (mConfirmButton != nullptr)
108  mConfirmButton->setEnabled(true);
109  }
110 }
111 
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  {
139  }
140  else
141  {
142  // All were sold
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 
154 {
156  Window::close();
157 }
#define _(s)
Definition: gettext.h:34
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
void setSelected(const int selected)
Definition: listbox.cpp:398
int sellCurrentDuplicate(const int quantity)
Definition: shopitem.cpp:140
int mAmountItems
Definition: selldialog.h:150
ShopItem * at(const size_t i) const
Definition: shopitems.cpp:128
const bool Advanced_false
Definition: advanced.h:29
BeingId mNpcId
Definition: npcselldialog.h:56
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
Definition: rect.h:72
int BeingId
Definition: beingid.h:29
static const std::string SOUND_REQUEST
Definition: sound.h:28
void updateButtonsAndLabels()
Definition: selldialog.cpp:345
void sellAction(const ActionEvent &event)
Slider * mSlider
Definition: selldialog.h:145
virtual void sellItem(const BeingId beingId, const int itemId, const int amount) const =0
int getCurrentInvIndex() const
Definition: shopitem.h:107
Advanced mAdvanced
Definition: selldialog.h:153
void update()
Definition: shopitem.cpp:118
const std::string & getName() const
Definition: iteminfo.h:73
NpcSellDialog(const BeingId npcId)
virtual void close() const =0
virtual void close()
Definition: window.cpp:901
bool isProtected() const
Definition: iteminfo.h:300
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
int getPrice() const
Definition: shopitem.h:131
int mMaxItems
Definition: selldialog.h:149
bool info(InputEvent &event)
Definition: commands.cpp:56
Button * mConfirmButton
Definition: selldialog.h:137
void sellManyItems(const std::string &eventId)
int y
Definition: rect.h:213
const std::string & getId() const
Definition: actionevent.h:121
ShopItems * mShopItems
Definition: selldialog.h:146
int getSelected() const
Definition: listbox.h:167
void increaseUsedQuantity(const int amount)
Definition: shopitem.cpp:158
int height
Definition: rect.h:223
void setEnabled(const bool enabled)
Definition: widget.h:351
const bool IsSell_true
Definition: issell.h:29
ShopListBox * mShopItemList
Definition: selldialog.h:141
ServerTypeT getNetworkType()
Definition: net.cpp:184
bool isItemProtected(const int id)
Definition: playerinfo.cpp:512
int getId() const
Definition: item.h:80
const bool Advanced_true
Definition: advanced.h:29
std::vector< ShopItem * > & allItems()
Definition: shopitems.h:138
void setValue(const double value)
Definition: slider.cpp:473
Net::BuySellHandler * buySellHandler
Definition: net.cpp:97
int mPlayerMoney
Definition: selldialog.h:148
virtual void showPart(const Rect &rectangle)
Definition: widget.cpp:510
void del(const unsigned int i)
Definition: shopitems.cpp:160
virtual unsigned int getRowHeight() const
Definition: listbox.h:155
Net::NpcHandler * npcHandler
Definition: net.cpp:90
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
virtual void sellItems(std::vector< ShopItem *> &items) const =0