GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/buyingstorehandler.cpp Lines: 1 42 2.4 %
Date: 2018-11-12 Branches: 0 52 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2018  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/buyingstorehandler.h"
22
23
#include "being/being.h"
24
#include "being/playerinfo.h"
25
26
#include "const/net/inventory.h"
27
28
#include "net/eathena/messageout.h"
29
#include "net/eathena/protocolout.h"
30
31
#include "utils/foreach.h"
32
33
#include "resources/item/shopitem.h"
34
35
#include "debug.h"
36
37
extern int packetVersion;
38
extern int itemIdLen;
39
40
namespace EAthena
41
{
42
43
BuyingStoreHandler::BuyingStoreHandler()
44
{
45
    buyingStoreHandler = this;
46
}
47
48
BuyingStoreHandler::~BuyingStoreHandler()
49
{
50
    buyingStoreHandler = nullptr;
51
}
52
53
void BuyingStoreHandler::create(const std::string &name,
54
                                const int maxMoney,
55
                                const bool flag,
56
                                const STD_VECTOR<ShopItem*> &items) const
57
{
58
    if (packetVersion < 20100303)
59
        return;
60
    createOutPacket(CMSG_BUYINGSTORE_CREATE);
61
    outMsg.writeInt16(CAST_S16(89 + items.size() * (6 + itemIdLen)), "len");
62
    outMsg.writeInt32(maxMoney, "limit money");
63
    outMsg.writeInt8(static_cast<int8_t>(flag), "flag");
64
    outMsg.writeString(name, 80, "store name");
65
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
66
    {
67
        const ShopItem *const item = *it;
68
        outMsg.writeItemId(item->getId(), "item id");
69
        outMsg.writeInt16(CAST_S16(item->getQuantity()), "amount");
70
        outMsg.writeInt32(item->getPrice(), "price");
71
    }
72
}
73
74
void BuyingStoreHandler::close() const
75
{
76
    if (packetVersion < 20100420)
77
        return;
78
    createOutPacket(CMSG_BUYINGSTORE_CLOSE);
79
    PlayerInfo::enableVending(false);
80
}
81
82
void BuyingStoreHandler::open(const Being *const being) const
83
{
84
    if (being == nullptr)
85
        return;
86
    if (packetVersion < 20100420)
87
        return;
88
    createOutPacket(CMSG_BUYINGSTORE_OPEN);
89
    outMsg.writeBeingId(being->getId(), "account id");
90
}
91
92
void BuyingStoreHandler::sell(const Being *const being,
93
                              const int storeId,
94
                              const Item *const item,
95
                              const int amount) const
96
{
97
    if ((being == nullptr) || (item == nullptr))
98
        return;
99
    if (packetVersion < 20100420)
100
        return;
101
102
    createOutPacket(CMSG_BUYINGSTORE_SELL);
103
    outMsg.writeInt16(18, "len");
104
    outMsg.writeBeingId(being->getId(), "account id");
105
    outMsg.writeInt32(storeId, "store id");
106
    outMsg.writeInt16(CAST_S16(
107
        item->getInvIndex() + INVENTORY_OFFSET),
108
        "index");
109
    outMsg.writeItemId(item->getId(), "item id");
110
    outMsg.writeInt16(CAST_S16(amount), "amount");
111
}
112
113
2
}  // namespace EAthena