GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/vendinghandler.cpp Lines: 1 66 1.5 %
Date: 2017-11-29 Branches: 0 78 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/vendinghandler.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
#include "net/eathena/vendingrecv.h"
31
32
#include "utils/foreach.h"
33
34
#include "resources/item/shopitem.h"
35
36
#include "debug.h"
37
38
namespace EAthena
39
{
40
41
VendingHandler::VendingHandler()
42
{
43
    vendingHandler = this;
44
    VendingRecv::mBuyDialog = nullptr;
45
}
46
47
VendingHandler::~VendingHandler()
48
{
49
    vendingHandler = nullptr;
50
}
51
52
void VendingHandler::close() const
53
{
54
    createOutPacket(CMSG_VENDING_CLOSE);
55
    PlayerInfo::enableVending(false);
56
}
57
58
void VendingHandler::open(const Being *const being) const
59
{
60
    if (being == nullptr)
61
        return;
62
63
    createOutPacket(CMSG_VENDING_LIST_REQ);
64
    outMsg.writeBeingId(being->getId(), "account id");
65
}
66
67
void VendingHandler::buy(const Being *const being,
68
                         const int index,
69
                         const int amount) const
70
{
71
    if (being == nullptr)
72
        return;
73
74
    createOutPacket(CMSG_VENDING_BUY);
75
    outMsg.writeInt16(12, "len");
76
    outMsg.writeBeingId(being->getId(), "account id");
77
    outMsg.writeInt16(CAST_S16(amount), "amount");
78
    outMsg.writeInt16(CAST_S16(index), "index");
79
}
80
81
void VendingHandler::buyItems(const Being *const being,
82
                              const STD_VECTOR<ShopItem*> &items) const
83
{
84
    int cnt = 0;
85
    const int pairSize = 4;
86
87
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
88
    {
89
        ShopItem *const item = *it;
90
        const int usedQuantity = item->getUsedQuantity();
91
        if (usedQuantity == 0)
92
            continue;
93
        cnt ++;
94
    }
95
96
    if (cnt > 100)
97
        return;
98
99
    createOutPacket(CMSG_VENDING_BUY);
100
    outMsg.writeInt16(CAST_S16(4 + 4 + pairSize * cnt), "len");
101
    outMsg.writeBeingId(being->getId(), "account id");
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
        outMsg.writeInt16(CAST_S16(usedQuantity), "amount");
112
        outMsg.writeInt16(CAST_S16(item->getInvIndex()), "index");
113
    }
114
}
115
116
void VendingHandler::buy2(const Being *const being,
117
                          const int vendId,
118
                          const int index,
119
                          const int amount) const
120
{
121
    if (being == nullptr)
122
        return;
123
124
    createOutPacket(CMSG_VENDING_BUY2);
125
    outMsg.writeInt16(16, "len");
126
    outMsg.writeBeingId(being->getId(), "account id");
127
    outMsg.writeInt32(vendId, "vend id");
128
    outMsg.writeInt16(CAST_S16(amount), "amount");
129
    outMsg.writeInt16(CAST_S16(index), "index");
130
}
131
132
void VendingHandler::createShop(const std::string &name,
133
                                const bool flag,
134
                                const STD_VECTOR<ShopItem*> &items) const
135
{
136
    createOutPacket(CMSG_VENDING_CREATE_SHOP);
137
    outMsg.writeInt16(CAST_S16(85 + items.size() * 8), "len");
138
    outMsg.writeString(name, 80, "shop name");
139
    outMsg.writeInt8(CAST_S8(flag ? 1 : 0), "flag");
140
    FOR_EACH (STD_VECTOR<ShopItem*>::const_iterator, it, items)
141
    {
142
        const ShopItem *const item = *it;
143
        outMsg.writeInt16(CAST_S16(
144
            item->getInvIndex() + INVENTORY_OFFSET), "index");
145
        outMsg.writeInt16(CAST_S16(item->getQuantity()), "amount");
146
        outMsg.writeInt32(item->getPrice(), "price");
147
    }
148
}
149
150
4
}  // namespace EAthena