GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/buyingstorehandler.cpp Lines: 1 41 2.4 %
Date: 2017-11-29 Branches: 0 52 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/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
39
namespace EAthena
40
{
41
42
BuyingStoreHandler::BuyingStoreHandler()
43
{
44
    buyingStoreHandler = this;
45
}
46
47
BuyingStoreHandler::~BuyingStoreHandler()
48
{
49
    buyingStoreHandler = nullptr;
50
}
51
52
void BuyingStoreHandler::create(const std::string &name,
53
                                const int maxMoney,
54
                                const bool flag,
55
                                const STD_VECTOR<ShopItem*> &items) const
56
{
57
    if (packetVersion < 20100303)
58
        return;
59
    createOutPacket(CMSG_BUYINGSTORE_CREATE);
60
    outMsg.writeInt16(CAST_S16(89 + items.size() * 8), "len");
61
    outMsg.writeInt32(maxMoney, "limit money");
62
    outMsg.writeInt8(static_cast<int8_t>(flag), "flag");
63
    outMsg.writeString(name, 80, "store name");
64
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
65
    {
66
        const ShopItem *const item = *it;
67
        outMsg.writeInt16(CAST_S16(item->getId()), "item id");
68
        outMsg.writeInt16(CAST_S16(item->getQuantity()), "amount");
69
        outMsg.writeInt32(item->getPrice(), "price");
70
    }
71
}
72
73
void BuyingStoreHandler::close() const
74
{
75
    if (packetVersion < 20100420)
76
        return;
77
    createOutPacket(CMSG_BUYINGSTORE_CLOSE);
78
    PlayerInfo::enableVending(false);
79
}
80
81
void BuyingStoreHandler::open(const Being *const being) const
82
{
83
    if (being == nullptr)
84
        return;
85
    if (packetVersion < 20100420)
86
        return;
87
    createOutPacket(CMSG_BUYINGSTORE_OPEN);
88
    outMsg.writeBeingId(being->getId(), "account id");
89
}
90
91
void BuyingStoreHandler::sell(const Being *const being,
92
                              const int storeId,
93
                              const Item *const item,
94
                              const int amount) const
95
{
96
    if ((being == nullptr) || (item == nullptr))
97
        return;
98
    if (packetVersion < 20100420)
99
        return;
100
101
    createOutPacket(CMSG_BUYINGSTORE_SELL);
102
    outMsg.writeInt16(18, "len");
103
    outMsg.writeBeingId(being->getId(), "account id");
104
    outMsg.writeInt32(storeId, "store id");
105
    outMsg.writeInt16(CAST_S16(
106
        item->getInvIndex() + INVENTORY_OFFSET),
107
        "index");
108
    outMsg.writeInt16(CAST_S16(item->getId()), "item id");
109
    outMsg.writeInt16(CAST_S16(amount), "amount");
110
}
111
112
4
}  // namespace EAthena