ManaPlus
Public Member Functions | Private Member Functions | Private Attributes
ShopItems Class Reference

#include <shopitems.h>

Inheritance diagram for ShopItems:
ListModel

Public Member Functions

 ShopItems (const bool mergeDuplicates, const std::string &currency)
 
 ~ShopItems ()
 
ShopItemaddItem (const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
 
ShopItemaddItem2 (const int inventoryIndex, const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
 
ShopItemaddItemNoDup (const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
 
int getNumberOfElements ()
 
bool empty () const
 
std::string getElementAt (int i)
 
ShopItemat (const size_t i) const
 
void erase (const unsigned int i)
 
void del (const unsigned int i)
 
void clear ()
 
std::vector< ShopItem * > & items ()
 
std::vector< ShopItem * > & allItems ()
 
void setMergeDuplicates (const bool b)
 
void updateList ()
 
- Public Member Functions inherited from ListModel
 ListModel ()
 
virtual ~ListModel ()
 

Private Member Functions

ShopItemfindItem (const int id, const ItemColor color) const
 
bool findInAllItems (std::vector< ShopItem * >::iterator &it, const ShopItem *const item)
 

Private Attributes

std::vector< ShopItem * > mAllShopItems
 
std::vector< ShopItem * > mShopItems
 
std::string mCurrency
 
bool mMergeDuplicates
 

Detailed Description

This class handles the list of items available in a shop.

The addItem routine can automatically check, if an item already exists and only adds duplicates to the old item, if one is found. The original distribution of the duplicates can be retrieved from the item.

This functionality can be enabled in the constructor.

Definition at line 49 of file shopitems.h.

Constructor & Destructor Documentation

◆ ShopItems()

ShopItems::ShopItems ( const bool  mergeDuplicates,
const std::string &  currency 
)

Constructor.

Parameters
mergeDuplicateslets the Shop look for duplicate entries and merges them to one item.

Definition at line 33 of file shopitems.cpp.

34  :
35  ListModel(),
36  mAllShopItems(),
37  mShopItems(),
38  mCurrency(currency),
39  mMergeDuplicates(mergeDuplicates)
40 {
41 }
ListModel()
Definition: listmodel.h:82
bool mMergeDuplicates
Definition: shopitems.h:168
std::vector< ShopItem * > mAllShopItems
Definition: shopitems.h:161
std::string mCurrency
Definition: shopitems.h:165
std::vector< ShopItem * > mShopItems
Definition: shopitems.h:163

◆ ~ShopItems()

ShopItems::~ShopItems ( )

Definition at line 43 of file shopitems.cpp.

44 {
45  clear();
46 }
void clear()
Definition: shopitems.cpp:174

References clear().

Member Function Documentation

◆ addItem()

ShopItem * ShopItems::addItem ( const int  id,
const ItemTypeT  type,
const ItemColor  color,
const int  amount,
const int  price 
)

Adds an item to the list.

Definition at line 59 of file shopitems.cpp.

64 {
65  ShopItem *const item = new ShopItem(-1,
66  id,
67  type,
68  color,
69  amount,
70  price,
71  mCurrency);
72  mShopItems.push_back(item);
73  mAllShopItems.push_back(item);
74  return item;
75 }

References mAllShopItems, mCurrency, and mShopItems.

Referenced by SellDialog::addItem(), BuyDialog::addItem(), and ShopWindow::loadList().

◆ addItem2()

ShopItem * ShopItems::addItem2 ( const int  inventoryIndex,
const int  id,
const ItemTypeT  type,
const ItemColor  color,
const int  amount,
const int  price 
)

Adds an item to the list (used by sell dialog). Looks for duplicate entries, if mergeDuplicates was turned on.

Parameters
inventoryIndexthe inventory index of the item
idthe id of the item
amountnumber of available copies of the item
priceprice of the item

Definition at line 99 of file shopitems.cpp.

105 {
106  ShopItem *item = nullptr;
107  if (mMergeDuplicates)
108  item = findItem(id, color);
109 
110  if (item != nullptr)
111  {
112  item->addDuplicate(inventoryIndex, quantity);
113  }
114  else
115  {
116  item = new ShopItem(inventoryIndex,
117  id,
118  type,
119  color,
120  quantity,
121  price,
122  mCurrency);
123  mShopItems.push_back(item);
124  mAllShopItems.push_back(item);
125  }
126  return item;
127 }
void addDuplicate(const int inventoryIndex, const int quantity)
Definition: shopitem.cpp:124
ShopItem * findItem(const int id, const ItemColor color) const
Definition: shopitems.cpp:181

References ShopItem::addDuplicate(), findItem(), mAllShopItems, mCurrency, mMergeDuplicates, and mShopItems.

Referenced by SellDialog::addItem().

◆ addItemNoDup()

ShopItem * ShopItems::addItemNoDup ( const int  id,
const ItemTypeT  type,
const ItemColor  color,
const int  amount,
const int  price 
)

Definition at line 77 of file shopitems.cpp.

82 {
83  ShopItem *item = findItem(id, color);
84  if (item == nullptr)
85  {
86  item = new ShopItem(-1,
87  id,
88  type,
89  color,
90  amount,
91  price,
92  mCurrency);
93  mShopItems.push_back(item);
94  mAllShopItems.push_back(item);
95  }
96  return item;
97 }

References findItem(), mAllShopItems, mCurrency, and mShopItems.

Referenced by ShopWindow::addBuyItem(), and ShopWindow::addSellItem().

◆ allItems()

std::vector<ShopItem*>& ShopItems::allItems ( )
inline

Definition at line 139 of file shopitems.h.

140  { return mAllShopItems; }

References mAllShopItems.

Referenced by BuyDialog::action(), BuyDialog::applyNameFilter(), and NpcSellDialog::sellManyItems().

◆ at()

ShopItem * ShopItems::at ( const size_t  i) const

◆ clear()

void ShopItems::clear ( )

Clears the list of items in the shop.

Definition at line 174 of file shopitems.cpp.

175 {
177  mAllShopItems.clear();
178  mShopItems.clear();
179 }
void delete_all(Container &c)
Definition: dtor.h:56

References delete_all(), mAllShopItems, and mShopItems.

Referenced by ShopWindow::loadList(), SellDialog::reset(), BuyDialog::reset(), and ~ShopItems().

◆ del()

void ShopItems::del ( const unsigned int  i)

Removes an element from the shop and destroy it.

Parameters
iindex to remove

Definition at line 161 of file shopitems.cpp.

162 {
163  if (i >= CAST_U32(mShopItems.size()))
164  return;
165 
166  ShopItem *item = *(mShopItems.begin() + i);
167  STD_VECTOR<ShopItem*>::iterator it;
168  if (findInAllItems(it, item))
169  mAllShopItems.erase(it);
170  mShopItems.erase(mShopItems.begin() + i);
171  delete item;
172 }
#define CAST_U32
Definition: cast.h:31
bool findInAllItems(std::vector< ShopItem * >::iterator &it, const ShopItem *const item)
Definition: shopitems.cpp:137

References CAST_U32, findInAllItems(), mAllShopItems, and mShopItems.

Referenced by ShopWindow::action(), and NpcSellDialog::sellOneItem().

◆ empty()

bool ShopItems::empty ( ) const
inline

Definition at line 102 of file shopitems.h.

103  { return mShopItems.empty(); }

References mShopItems.

Referenced by ShopWindow::isShopEmpty().

◆ erase()

void ShopItems::erase ( const unsigned int  i)

Removes an element from the shop.

Parameters
iindex to remove

Definition at line 149 of file shopitems.cpp.

150 {
151  if (i >= CAST_U32(mShopItems.size()))
152  return;
153 
154  const ShopItem *const item = *(mShopItems.begin() + i);
155  STD_VECTOR<ShopItem*>::iterator it;
156  if (findInAllItems(it, item))
157  mAllShopItems.erase(it);
158  mShopItems.erase(mShopItems.begin() + i);
159 }

References CAST_U32, findInAllItems(), mAllShopItems, and mShopItems.

◆ findInAllItems()

bool ShopItems::findInAllItems ( std::vector< ShopItem * >::iterator &  it,
const ShopItem *const  item 
)
private

Definition at line 137 of file shopitems.cpp.

139 {
140  const STD_VECTOR<ShopItem*>::iterator it_end = mAllShopItems.end();
141  for (it = mAllShopItems.begin(); it != it_end; ++ it)
142  {
143  if (*it == item)
144  return true;
145  }
146  return false;
147 }

References mAllShopItems.

Referenced by del(), and erase().

◆ findItem()

ShopItem * ShopItems::findItem ( const int  id,
const ItemColor  color 
) const
private

Searches the current items in the shop for the specified id and returns the item if found, or 0 else.

Returns
the item found or 0

Definition at line 181 of file shopitems.cpp.

183 {
184  STD_VECTOR<ShopItem*>::const_iterator it = mShopItems.begin();
185  const STD_VECTOR<ShopItem*>::const_iterator e = mShopItems.end();
186  while (it != e)
187  {
188  ShopItem *const item = *it;
189  if (item->getId() == id && item->getColor() == color)
190  return item;
191 
192  ++it;
193  }
194 
195  return nullptr;
196 }
int getId() const
Definition: item.h:81
ItemColor getColor() const
Definition: item.h:181

References Item::getColor(), Item::getId(), and mShopItems.

Referenced by addItem2(), and addItemNoDup().

◆ getElementAt()

std::string ShopItems::getElementAt ( int  i)
virtual

Returns the name of item number i in the shop.

Parameters
ithe index to retrieve

Implements ListModel.

Definition at line 48 of file shopitems.cpp.

49 {
50  if (i < 0 || CAST_U32(i)
51  >= CAST_U32(mShopItems.size()) || (mShopItems.at(i) == nullptr))
52  {
53  return "";
54  }
55 
56  return mShopItems.at(i)->getDisplayName();
57 }

References CAST_U32, and mShopItems.

◆ getNumberOfElements()

int ShopItems::getNumberOfElements ( )
inlinevirtual

Returns the number of items in the shop.

Implements ListModel.

Definition at line 99 of file shopitems.h.

100  { return CAST_S32(mShopItems.size()); }
#define CAST_S32
Definition: cast.h:30

References CAST_S32, and mShopItems.

Referenced by SellDialog::action(), BuyDialog::action(), ShopWindow::action(), ShopListBox::mouseMoved(), ShopListBox::mouseReleased(), and ShopWindow::updateButtonsAndLabels().

◆ items()

std::vector<ShopItem*>& ShopItems::items ( )
inline

◆ setMergeDuplicates()

void ShopItems::setMergeDuplicates ( const bool  b)
inline

◆ updateList()

void ShopItems::updateList ( )

Definition at line 198 of file shopitems.cpp.

199 {
200  mShopItems.clear();
201  FOR_EACH (STD_VECTOR<ShopItem*>::iterator, it, mAllShopItems)
202  {
203  ShopItem *const item = *it;
204  if ((item != nullptr) && item->isVisible())
205  mShopItems.push_back(item);
206  }
207 }
bool isVisible() const
Definition: shopitem.h:146
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25

References FOR_EACH, ShopItem::isVisible(), mAllShopItems, and mShopItems.

Referenced by BuyDialog::applyNameFilter().

Field Documentation

◆ mAllShopItems

std::vector<ShopItem*> ShopItems::mAllShopItems
private

The list of items in the shop.

Definition at line 161 of file shopitems.h.

Referenced by addItem(), addItem2(), addItemNoDup(), allItems(), clear(), del(), erase(), findInAllItems(), and updateList().

◆ mCurrency

std::string ShopItems::mCurrency
private

Definition at line 165 of file shopitems.h.

Referenced by addItem(), addItem2(), and addItemNoDup().

◆ mMergeDuplicates

bool ShopItems::mMergeDuplicates
private

Look for duplicate entries on addition.

Definition at line 168 of file shopitems.h.

Referenced by addItem2(), and setMergeDuplicates().

◆ mShopItems

std::vector<ShopItem*> ShopItems::mShopItems
private

The documentation for this class was generated from the following files: