GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/barterhandler.cpp Lines: 0 56 0.0 %
Date: 2019-10-15 Branches: 0 58 0.0 %

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