GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/item/shopitem.cpp Lines: 0 55 0.0 %
Date: 2021-03-17 Branches: 0 48 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
#include "resources/item/shopitem.h"
25
26
#include "resources/iteminfo.h"
27
28
#include "utils/stringutils.h"
29
30
#ifdef TMWA_SUPPORT
31
#include "net/net.h"
32
#endif  // TMWA_SUPPORT
33
34
#include "resources/db/unitsdb.h"
35
36
#include "debug.h"
37
38
ShopItem::ShopItem(const int inventoryIndex,
39
                   const int id,
40
                   const ItemTypeT type,
41
                   const ItemColor color,
42
                   const int quantity,
43
                   const int price,
44
                   const std::string &currency) :
45
    Item(id, type, 0, 0, color,
46
         Identified_true,
47
         Damaged_false,
48
         Favorite_false,
49
         Equipm_false,
50
         Equipped_false),
51
    mDisplayName(),
52
    mCurrency(currency),
53
    mDuplicates(),
54
    mPrice(price),
55
    mUsedQuantity(0),
56
    mShowQuantity(true),
57
    mVisible(true),
58
    mDisabled(false)
59
{
60
    updateDisplayName(quantity);
61
    setInvIndex(inventoryIndex);
62
    addDuplicate(inventoryIndex, quantity);
63
}
64
65
ShopItem::ShopItem(const int id,
66
                   const ItemTypeT type,
67
                   const ItemColor color,
68
                   const int price,
69
                   const std::string &currency) :
70
    Item(id, type, 0, 0, color,
71
         Identified_true,
72
         Damaged_false,
73
         Favorite_false,
74
         Equipm_false,
75
         Equipped_false),
76
    mDisplayName(),
77
    mCurrency(currency),
78
    mDuplicates(),
79
    mPrice(price),
80
    mUsedQuantity(0),
81
    mShowQuantity(false),
82
    mVisible(true),
83
    mDisabled(false)
84
{
85
    updateDisplayName(0);
86
    setInvIndex(-1);
87
    addDuplicate(-1, 0);
88
}
89
90
ShopItem::~ShopItem()
91
{
92
    /** Clear all remaining duplicates on Object destruction. */
93
    while (!mDuplicates.empty())
94
    {
95
        delete mDuplicates.top();
96
        mDuplicates.pop();
97
    }
98
}
99
100
void ShopItem::updateDisplayName(const int quantity)
101
{
102
#ifdef TMWA_SUPPORT
103
    if (Net::getNetworkType() == ServerType::TMWATHENA)
104
        mDisplayName = std::string(getInfo().getName());
105
    else
106
#endif  // TMWA_SUPPORT
107
        mDisplayName = std::string(getInfo().getName(mColor));
108
    if (mPrice != 0)
109
    {
110
        mDisplayName.append(" (").append(
111
            UnitsDb::formatCurrency(mCurrency, mPrice)).append(") ");
112
    }
113
    if (mShowQuantity && quantity > 1)
114
        mDisplayName.append("[").append(toString(quantity)).append("]");
115
    if (mUsedQuantity > 0)
116
        mDisplayName.append(" +").append(toString(mUsedQuantity));
117
}
118
119
void ShopItem::update()
120
{
121
    updateDisplayName(mQuantity);
122
}
123
124
void ShopItem::addDuplicate(const int inventoryIndex, const int quantity)
125
{
126
    DuplicateItem *const di = new DuplicateItem;
127
    di->inventoryIndex = inventoryIndex;
128
    di->quantity = quantity;
129
    mDuplicates.push(di);
130
    mQuantity += quantity;
131
}
132
133
void ShopItem::addDuplicate()
134
{
135
    DuplicateItem *const di = new DuplicateItem;
136
    di->inventoryIndex = -1;
137
    di->quantity = 0;
138
    mDuplicates.push(di);
139
}
140
141
int ShopItem::sellCurrentDuplicate(const int quantity)
142
{
143
    DuplicateItem* dupl = mDuplicates.top();
144
    if (dupl == nullptr)
145
        return 0;
146
147
    const int sellCount = quantity <= dupl->quantity
148
        ? quantity : dupl->quantity;
149
    dupl->quantity -= sellCount;
150
    mQuantity -= sellCount;
151
    if (dupl->quantity == 0)
152
    {
153
        delete dupl;
154
        mDuplicates.pop();
155
    }
156
    return sellCount;
157
}
158
159
void ShopItem::increaseUsedQuantity(const int amount)
160
{
161
    if (mShowQuantity && (mQuantity != 0))
162
    {
163
        if (mQuantity < mUsedQuantity + amount ||
164
            mUsedQuantity + amount < 0)
165
        {
166
            return;
167
        }
168
    }
169
    else if (mUsedQuantity + amount < 0)
170
    {
171
        return;
172
    }
173
174
    mUsedQuantity += amount;
175
}