ManaPlus
shopitems.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 
24 #include "gui/models/shopitems.h"
25 
27 
28 #include "utils/dtor.h"
29 #include "utils/foreach.h"
30 
31 #include "debug.h"
32 
33 ShopItems::ShopItems(const bool mergeDuplicates,
34  const std::string &currency) :
35  ListModel(),
36  mAllShopItems(),
37  mShopItems(),
38  mCurrency(currency),
39  mMergeDuplicates(mergeDuplicates)
40 {
41 }
42 
44 {
45  clear();
46 }
47 
48 std::string ShopItems::getElementAt(int i)
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 }
58 
60  const ItemTypeT type,
61  const ItemColor color,
62  const int amount,
63  const int price)
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 }
76 
78  const ItemTypeT type,
79  const ItemColor color,
80  const int amount,
81  const int price)
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 }
98 
99 ShopItem *ShopItems::addItem2(const int inventoryIndex,
100  const int id,
101  const ItemTypeT type,
102  const ItemColor color,
103  const int quantity,
104  const int price)
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 }
128 
129 ShopItem *ShopItems::at(const size_t i) const
130 {
131  if (i >= mShopItems.size())
132  return nullptr;
133 
134  return mShopItems.at(i);
135 }
136 
137 bool ShopItems::findInAllItems(STD_VECTOR<ShopItem*>::iterator &it,
138  const ShopItem *const item)
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 }
148 
149 void ShopItems::erase(const unsigned int i)
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 }
160 
161 void ShopItems::del(const unsigned int i)
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 }
173 
175 {
177  mAllShopItems.clear();
178  mShopItems.clear();
179 }
180 
182  const ItemColor color) const
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 }
197 
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 }
#define CAST_U32
Definition: cast.h:31
int getId() const
Definition: item.h:81
ItemColor getColor() const
Definition: item.h:181
void addDuplicate(const int inventoryIndex, const int quantity)
Definition: shopitem.cpp:124
bool isVisible() const
Definition: shopitem.h:146
ShopItems(const bool mergeDuplicates, const std::string &currency)
Definition: shopitems.cpp:33
bool mMergeDuplicates
Definition: shopitems.h:168
std::vector< ShopItem * > mAllShopItems
Definition: shopitems.h:161
ShopItem * addItemNoDup(const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
Definition: shopitems.cpp:77
std::string getElementAt(int i)
Definition: shopitems.cpp:48
void del(const unsigned int i)
Definition: shopitems.cpp:161
ShopItem * addItem2(const int inventoryIndex, const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
Definition: shopitems.cpp:99
ShopItem * addItem(const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
Definition: shopitems.cpp:59
void erase(const unsigned int i)
Definition: shopitems.cpp:149
std::string mCurrency
Definition: shopitems.h:165
std::vector< ShopItem * > mShopItems
Definition: shopitems.h:163
void updateList()
Definition: shopitems.cpp:198
bool findInAllItems(std::vector< ShopItem * >::iterator &it, const ShopItem *const item)
Definition: shopitems.cpp:137
ShopItem * findItem(const int id, const ItemColor color) const
Definition: shopitems.cpp:181
void clear()
Definition: shopitems.cpp:174
ShopItem * at(const size_t i) const
Definition: shopitems.cpp:129
void delete_all(Container &c)
Definition: dtor.h:56
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
uint16_t ItemColor
Definition: itemcolor.h:30
ItemType ::T ItemTypeT
Definition: itemtype.h:43