GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/shopitems.h Lines: 4 6 66.7 %
Date: 2021-03-17 Branches: 0 0 0.0 %

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
#ifndef GUI_MODELS_SHOPITEMS_H
25
#define GUI_MODELS_SHOPITEMS_H
26
27
#include "enums/resources/item/itemtype.h"
28
29
#include "enums/simpletypes/itemcolor.h"
30
31
#include "gui/models/listmodel.h"
32
33
#include "utils/cast.h"
34
#include "utils/vector.h"
35
36
#include "localconsts.h"
37
38
class ShopItem;
39
40
/**
41
 * This class handles the list of items available in a shop.
42
 *
43
 * The addItem routine can automatically check, if an item already exists and
44
 * only adds duplicates to the old item, if one is found. The original
45
 * distribution of the duplicates can be retrieved from the item.
46
 *
47
 * This functionality can be enabled in the constructor.
48
 */
49
class ShopItems final : public ListModel
50
{
51
    public:
52
        /**
53
         * Constructor.
54
         *
55
         * @param mergeDuplicates lets the Shop look for duplicate entries and
56
         *                        merges them to one item.
57
         */
58
        ShopItems(const bool mergeDuplicates,
59
                  const std::string &currency);
60
61
        A_DELETE_COPY(ShopItems)
62
63
        ~ShopItems() override final;
64
65
        /**
66
         * Adds an item to the list.
67
         */
68
        ShopItem *addItem(const int id,
69
                          const ItemTypeT type,
70
                          const ItemColor color,
71
                          const int amount,
72
                          const int price);
73
74
        /**
75
         * Adds an item to the list (used by sell dialog). Looks for
76
         * duplicate entries, if mergeDuplicates was turned on.
77
         *
78
         * @param inventoryIndex the inventory index of the item
79
         * @param id the id of the item
80
         * @param amount number of available copies of the item
81
         * @param price price of the item
82
         */
83
        ShopItem *addItem2(const int inventoryIndex,
84
                           const int id,
85
                           const ItemTypeT type,
86
                           const ItemColor color,
87
                           const int amount,
88
                           const int price);
89
90
        ShopItem *addItemNoDup(const int id,
91
                               const ItemTypeT type,
92
                               const ItemColor color,
93
                               const int amount,
94
                               const int price);
95
96
        /**
97
         * Returns the number of items in the shop.
98
         */
99
18
        int getNumberOfElements() override final A_WARN_UNUSED
100
36
        { return CAST_S32(mShopItems.size()); }
101
102
        bool empty() const noexcept2 A_WARN_UNUSED
103
        { return mShopItems.empty(); }
104
105
        /**
106
         * Returns the name of item number i in the shop.
107
         *
108
         * @param i the index to retrieve
109
         */
110
        std::string getElementAt(int i) override final A_WARN_UNUSED;
111
112
        /**
113
         * Returns the item number i in the shop.
114
         */
115
        ShopItem *at(const size_t i) const A_WARN_UNUSED;
116
117
        /**
118
         * Removes an element from the shop.
119
         *
120
         * @param i index to remove
121
         */
122
        void erase(const unsigned int i);
123
124
        /**
125
         * Removes an element from the shop and destroy it.
126
         *
127
         * @param i index to remove
128
         */
129
        void del(const unsigned int i);
130
131
        /**
132
         * Clears the list of items in the shop.
133
         */
134
        void clear();
135
136
        STD_VECTOR<ShopItem*> &items() A_WARN_UNUSED
137
2
        { return mShopItems; }
138
139
        STD_VECTOR<ShopItem*> &allItems() A_WARN_UNUSED
140
        { return mAllShopItems; }
141
142
        void setMergeDuplicates(const bool b)
143
3
        { mMergeDuplicates = b; }
144
145
        void updateList();
146
147
    private:
148
        /**
149
         * Searches the current items in the shop for the specified
150
         * id and returns the item if found, or 0 else.
151
         *
152
         * @return the item found or 0
153
         */
154
        ShopItem *findItem(const int id,
155
                           const ItemColor color) const A_WARN_UNUSED;
156
157
        bool findInAllItems(STD_VECTOR<ShopItem*>::iterator &it,
158
                            const ShopItem *const item);
159
160
        /** The list of items in the shop. */
161
        STD_VECTOR<ShopItem*> mAllShopItems;
162
163
        STD_VECTOR<ShopItem*> mShopItems;
164
165
        std::string mCurrency;
166
167
        /** Look for duplicate entries on addition. */
168
        bool mMergeDuplicates;
169
};
170
171
#endif  // GUI_MODELS_SHOPITEMS_H