GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/markethandler.cpp Lines: 0 62 0.0 %
Date: 2021-03-17 Branches: 0 64 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "net/eathena/markethandler.h"
23
24
#include "net/eathena/marketrecv.h"
25
#include "net/eathena/messageout.h"
26
#include "net/eathena/protocolout.h"
27
28
#include "utils/foreach.h"
29
30
#include "resources/item/shopitem.h"
31
32
#include "debug.h"
33
34
extern int packetVersion;
35
36
namespace EAthena
37
{
38
39
MarketHandler::MarketHandler() :
40
    Net::MarketHandler()
41
{
42
    marketHandler = this;
43
    MarketRecv::mBuyDialog = nullptr;
44
}
45
46
MarketHandler::~MarketHandler()
47
{
48
    marketHandler = nullptr;
49
}
50
51
void MarketHandler::close() const
52
{
53
    if (packetVersion < 20131218)
54
        return;
55
56
    createOutPacket(CMSG_NPC_MARKET_CLOSE);
57
}
58
59
void MarketHandler::buyItem(const int itemId,
60
                            const ItemTypeT type,
61
                            const ItemColor color A_UNUSED,
62
                            const int amount) const
63
{
64
    if (packetVersion < 20131218)
65
        return;
66
    const bool nonStack = type == ItemType::Weapon ||
67
        type == ItemType::Armor ||
68
        type == ItemType::PetEgg ||
69
        type == ItemType::PetArmor;
70
    int cnt = nonStack ? amount : 1;
71
    const int amount2 = nonStack ? 1 : amount;
72
    if (cnt > 100)
73
        cnt = 100;
74
75
    createOutPacket(CMSG_NPC_MARKET_BUY);
76
    outMsg.writeInt16(CAST_S16(4 + 6 * cnt), "len");
77
    for (int f = 0; f < cnt; f ++)
78
    {
79
        outMsg.writeItemId(itemId, "item id");
80
        outMsg.writeInt32(CAST_S16(amount2), "amount");
81
    }
82
}
83
84
void MarketHandler::buyItems(const STD_VECTOR<ShopItem*> &items) const
85
{
86
    if (packetVersion < 20131218)
87
        return;
88
    int cnt = 0;
89
    const int pairSize = 6;
90
91
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
92
    {
93
        const ShopItem *const item = *it;
94
        const int usedQuantity = item->getUsedQuantity();
95
        const ItemTypeT type = item->getType();
96
        if (usedQuantity == 0)
97
            continue;
98
        if (type == ItemType::Weapon ||
99
            type == ItemType::Armor ||
100
            type == ItemType::PetEgg ||
101
            type == ItemType::PetArmor)
102
        {
103
            cnt += item->getUsedQuantity();
104
        }
105
        else
106
        {
107
            cnt ++;
108
        }
109
    }
110
111
    if (cnt > 100)
112
        return;
113
114
    createOutPacket(CMSG_NPC_MARKET_BUY);
115
    outMsg.writeInt16(CAST_S16(4 + pairSize * cnt), "len");
116
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
117
    {
118
        ShopItem *const item = *it;
119
        const int usedQuantity = item->getUsedQuantity();
120
        if (usedQuantity == 0)
121
            continue;
122
        item->increaseQuantity(usedQuantity);
123
        item->increaseUsedQuantity(-usedQuantity);
124
        item->update();
125
        const ItemTypeT type = item->getType();
126
        if (type == ItemType::Weapon ||
127
            type == ItemType::Armor ||
128
            type == ItemType::PetEgg ||
129
            type == ItemType::PetArmor)
130
        {
131
            for (int f = 0; f < usedQuantity; f ++)
132
            {
133
                outMsg.writeItemId(item->getId(),
134
                    "item id");
135
                outMsg.writeInt32(CAST_S16(1), "amount");
136
            }
137
        }
138
        else
139
        {
140
            outMsg.writeItemId(item->getId(),
141
                              "item id");
142
            outMsg.writeInt32(CAST_S16(usedQuantity), "amount");
143
        }
144
    }
145
}
146
147
}  // namespace EAthena