GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/markethandler.cpp Lines: 0 61 0.0 %
Date: 2017-11-29 Branches: 0 62 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  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/markethandler.h"
22
23
#include "net/eathena/marketrecv.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 packetVersion;
34
35
namespace EAthena
36
{
37
38
MarketHandler::MarketHandler() :
39
    Net::MarketHandler()
40
{
41
    marketHandler = this;
42
    MarketRecv::mBuyDialog = nullptr;
43
}
44
45
MarketHandler::~MarketHandler()
46
{
47
    marketHandler = nullptr;
48
}
49
50
void MarketHandler::close() const
51
{
52
    if (packetVersion < 20131218)
53
        return;
54
55
    createOutPacket(CMSG_NPC_MARKET_CLOSE);
56
}
57
58
void MarketHandler::buyItem(const int itemId,
59
                            const ItemTypeT type,
60
                            const ItemColor color A_UNUSED,
61
                            const int amount) const
62
{
63
    if (packetVersion < 20131218)
64
        return;
65
    const bool nonStack = type == ItemType::Weapon ||
66
        type == ItemType::Armor ||
67
        type == ItemType::PetEgg ||
68
        type == ItemType::PetArmor;
69
    int cnt = nonStack ? amount : 1;
70
    const int amount2 = nonStack ? 1 : amount;
71
    if (cnt > 100)
72
        cnt = 100;
73
74
    createOutPacket(CMSG_NPC_MARKET_BUY);
75
    outMsg.writeInt16(CAST_S16(4 + 6 * cnt), "len");
76
    for (int f = 0; f < cnt; f ++)
77
    {
78
        outMsg.writeInt16(CAST_S16(itemId), "item id");
79
        outMsg.writeInt32(CAST_S16(amount2), "amount");
80
    }
81
}
82
83
void MarketHandler::buyItems(const STD_VECTOR<ShopItem*> &items) const
84
{
85
    if (packetVersion < 20131218)
86
        return;
87
    int cnt = 0;
88
    const int pairSize = 6;
89
90
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
91
    {
92
        const ShopItem *const item = *it;
93
        const int usedQuantity = item->getUsedQuantity();
94
        const ItemTypeT type = item->getType();
95
        if (usedQuantity == 0)
96
            continue;
97
        if (type == ItemType::Weapon ||
98
            type == ItemType::Armor ||
99
            type == ItemType::PetEgg ||
100
            type == ItemType::PetArmor)
101
        {
102
            cnt += item->getUsedQuantity();
103
        }
104
        else
105
        {
106
            cnt ++;
107
        }
108
    }
109
110
    if (cnt > 100)
111
        return;
112
113
    createOutPacket(CMSG_NPC_MARKET_BUY);
114
    outMsg.writeInt16(CAST_S16(4 + pairSize * cnt), "len");
115
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
116
    {
117
        ShopItem *const item = *it;
118
        const int usedQuantity = item->getUsedQuantity();
119
        if (usedQuantity == 0)
120
            continue;
121
        item->increaseQuantity(usedQuantity);
122
        item->increaseUsedQuantity(-usedQuantity);
123
        item->update();
124
        const ItemTypeT type = item->getType();
125
        if (type == ItemType::Weapon ||
126
            type == ItemType::Armor ||
127
            type == ItemType::PetEgg ||
128
            type == ItemType::PetArmor)
129
        {
130
            for (int f = 0; f < usedQuantity; f ++)
131
            {
132
                outMsg.writeInt16(CAST_S16(item->getId()),
133
                    "item id");
134
                outMsg.writeInt32(CAST_S16(1), "amount");
135
            }
136
        }
137
        else
138
        {
139
            outMsg.writeInt16(CAST_S16(item->getId()), "item id");
140
            outMsg.writeInt32(CAST_S16(usedQuantity), "amount");
141
        }
142
    }
143
}
144
145
}  // namespace EAthena