GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/barterhandler.cpp Lines: 0 56 0.0 %
Date: 2021-03-17 Branches: 0 58 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/barterhandler.h"
23
24
#include "net/eathena/barterrecv.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 packetVersionMain;
35
extern int packetVersionRe;
36
extern int packetVersionZero;
37
extern int itemIdLen;
38
39
namespace EAthena
40
{
41
42
BarterHandler::BarterHandler() :
43
    Net::BarterHandler()
44
{
45
    barterHandler = this;
46
    BarterRecv::mBuyDialog = nullptr;
47
}
48
49
BarterHandler::~BarterHandler()
50
{
51
    barterHandler = nullptr;
52
}
53
54
void BarterHandler::close() const
55
{
56
    if (packetVersionMain < 20190116 &&
57
        packetVersionRe < 20190116 &&
58
        packetVersionZero < 20181226)
59
    {
60
        return;
61
    }
62
63
    createOutPacket(CMSG_NPC_BARTER_CLOSE);
64
}
65
66
void BarterHandler::buyItems(const STD_VECTOR<ShopItem*> &items) const
67
{
68
    if (packetVersionMain < 20190116 &&
69
        packetVersionRe < 20190116 &&
70
        packetVersionZero < 20181226)
71
    {
72
        return;
73
    }
74
    int cnt = 0;
75
    const int pairSize = 10 + itemIdLen;
76
77
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
78
    {
79
        const ShopItem *const item = *it;
80
        const int usedQuantity = item->getUsedQuantity();
81
        const ItemTypeT type = item->getType();
82
        if (usedQuantity == 0)
83
            continue;
84
        if (type == ItemType::Weapon ||
85
            type == ItemType::Armor ||
86
            type == ItemType::PetEgg ||
87
            type == ItemType::PetArmor)
88
        {
89
            cnt += item->getUsedQuantity();
90
        }
91
        else
92
        {
93
            cnt ++;
94
        }
95
    }
96
97
    if (cnt > 100)
98
        return;
99
100
    createOutPacket(CMSG_NPC_MARKET_BUY);
101
    outMsg.writeInt16(CAST_S16(4 + pairSize * cnt), "len");
102
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
103
    {
104
        ShopItem *const item = *it;
105
        const int usedQuantity = item->getUsedQuantity();
106
        if (usedQuantity == 0)
107
            continue;
108
        item->increaseQuantity(usedQuantity);
109
        item->increaseUsedQuantity(-usedQuantity);
110
        item->update();
111
        const ItemTypeT type = item->getType();
112
        if (type == ItemType::Weapon ||
113
            type == ItemType::Armor ||
114
            type == ItemType::PetEgg ||
115
            type == ItemType::PetArmor)
116
        {
117
            for (int f = 0; f < usedQuantity; f ++)
118
            {
119
                outMsg.writeItemId(item->getId(),
120
                    "item id");
121
                outMsg.writeInt32(CAST_S16(1), "amount");
122
                // +++ need use player inventory index
123
                outMsg.writeInt16(0, "inv index");
124
                outMsg.writeInt32(item->getCurrentInvIndex(),
125
                    "inv index");
126
            }
127
        }
128
        else
129
        {
130
            outMsg.writeItemId(item->getId(),
131
                              "item id");
132
            outMsg.writeInt32(CAST_S16(usedQuantity), "amount");
133
            // +++ need use player inventory index
134
            outMsg.writeInt16(0, "inv index");
135
            outMsg.writeInt32(item->getCurrentInvIndex(),
136
                "inv index");
137
        }
138
    }
139
}
140
141
}  // namespace EAthena