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