GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/buyingstorerecv.cpp Lines: 1 103 1.0 %
Date: 2018-12-09 Branches: 0 48 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/buyingstorerecv.h"
22
23
#include "actormanager.h"
24
#include "notifymanager.h"
25
26
#include "being/localplayer.h"
27
#include "being/playerinfo.h"
28
29
#include "const/net/inventory.h"
30
31
#include "enums/resources/notifytypes.h"
32
33
#include "gui/windows/buyingstoreselldialog.h"
34
35
#include "gui/widgets/createwidget.h"
36
37
#include "listeners/arrowslistener.h"
38
#include "listeners/buyingstoremodelistener.h"
39
#include "listeners/buyingstoreslotslistener.h"
40
41
#include "net/messagein.h"
42
43
#include "resources/inventory/inventory.h"
44
45
#include "resources/item/item.h"
46
47
#include "debug.h"
48
49
extern int itemIdLen;
50
51
namespace EAthena
52
{
53
54
void BuyingStoreRecv::processBuyingStoreOpen(Net::MessageIn &msg)
55
{
56
    BuyingStoreSlotsListener::distributeEvent(msg.readUInt8("slots"));
57
}
58
59
void BuyingStoreRecv::processBuyingStoreCreateFailed(Net::MessageIn &msg)
60
{
61
    const int16_t result = msg.readInt16("result");
62
    const int weight = msg.readInt32("weight");
63
    switch (result)
64
    {
65
        case 1:
66
        default:
67
            NotifyManager::notify(NotifyTypes::BUYING_STORE_CREATE_FAILED);
68
            break;
69
        case 2:
70
            NotifyManager::notify(
71
                NotifyTypes::BUYING_STORE_CREATE_FAILED_WEIGHT,
72
                weight);
73
            break;
74
        case 8:
75
            NotifyManager::notify(NotifyTypes::BUYING_STORE_CREATE_EMPTY);
76
            break;
77
    }
78
}
79
80
void BuyingStoreRecv::processBuyingStoreOwnItems(Net::MessageIn &msg)
81
{
82
    const int count = (msg.readInt16("len") - 12) / (7 + itemIdLen);
83
    msg.readBeingId("account id");
84
    msg.readInt32("money limit");
85
    for (int f = 0; f < count; f ++)
86
    {
87
        msg.readInt32("price");
88
        msg.readInt16("amount");
89
        msg.readUInt8("item type");
90
        msg.readItemId("item id");
91
    }
92
    PlayerInfo::enableVending(true);
93
    BuyingStoreModeListener::distributeEvent(true);
94
}
95
96
void BuyingStoreRecv::processBuyingStoreShowBoard(Net::MessageIn &msg)
97
{
98
    if (actorManager == nullptr)
99
        return;
100
    const BeingId id = msg.readBeingId("owner id");
101
    Being *const dstBeing = actorManager->findBeing(id);
102
    if (dstBeing != nullptr)
103
    {
104
        dstBeing->setBuyBoard(msg.readString(80, "shop name"));
105
    }
106
    else
107
    {
108
        msg.readString(80, "shop name");
109
    }
110
}
111
112
void BuyingStoreRecv::processBuyingStoreHideBoard(Net::MessageIn &msg)
113
{
114
    if (actorManager == nullptr)
115
        return;
116
    const BeingId id = msg.readBeingId("owner id");
117
    Being *const dstBeing = actorManager->findBeing(id);
118
    if (dstBeing != nullptr)
119
        dstBeing->setBuyBoard(std::string());
120
    if (dstBeing == localPlayer)
121
    {
122
        PlayerInfo::enableVending(false);
123
        BuyingStoreModeListener::distributeEvent(false);
124
    }
125
}
126
127
void BuyingStoreRecv::processBuyingStoreItemsList(Net::MessageIn &msg)
128
{
129
    if (actorManager == nullptr)
130
        return;
131
    const int count = (msg.readInt16("len") - 16) / (7 + itemIdLen);
132
    const BeingId id = msg.readBeingId("account id");
133
    const int storeId = msg.readInt32("store id");
134
    // +++ in future need use it too
135
    msg.readInt32("money limit");
136
137
    const Being *const dstBeing = actorManager->findBeing(id);
138
    if (dstBeing == nullptr)
139
        return;
140
141
    SellDialog *const dialog = CREATEWIDGETR(BuyingStoreSellDialog,
142
        dstBeing->getId(),
143
        storeId);
144
    dialog->setMoney(PlayerInfo::getAttribute(Attributes::MONEY));
145
    const Inventory *const inv = PlayerInfo::getInventory();
146
    for (int f = 0; f < count; f ++)
147
    {
148
        const int price = msg.readInt32("price");
149
        const int amount = msg.readInt16("amount");
150
        const ItemTypeT itemType = static_cast<ItemTypeT>(
151
            msg.readUInt8("item type"));
152
        const int itemId = msg.readItemId("item id");
153
154
        if (inv == nullptr)
155
            continue;
156
        const Item *const item = inv->findItem(itemId, ItemColor_one);
157
        if (item == nullptr)
158
            continue;
159
        // +++ need add colors support
160
        dialog->addItem(itemId, itemType, ItemColor_one, amount, price);
161
    }
162
}
163
164
void BuyingStoreRecv::processBuyingStoreSellFailed(Net::MessageIn &msg)
165
{
166
    const int16_t result = msg.readInt16("result");
167
    switch (result)
168
    {
169
        case 3:
170
            NotifyManager::notify(
171
                NotifyTypes::BUYING_STORE_SELL_FAILED_MONEY_LIMIT);
172
            break;
173
        case 4:
174
            NotifyManager::notify(NotifyTypes::BUYING_STORE_SELL_FAILED_EMPTY);
175
            break;
176
        default:
177
            NotifyManager::notify(NotifyTypes::BUYING_STORE_SELL_FAILED);
178
            break;
179
    }
180
}
181
182
void BuyingStoreRecv::processBuyingStoreSellerSellFailed(Net::MessageIn &msg)
183
{
184
    const int16_t result = msg.readInt16("result");
185
    msg.readItemId("item id");
186
    switch (result)
187
    {
188
        case 5:
189
            NotifyManager::notify(
190
                NotifyTypes::BUYING_STORE_SELLER_SELL_FAILED_DEAL);
191
            break;
192
        case 6:
193
            NotifyManager::notify(
194
                NotifyTypes::BUYING_STORE_SELLER_SELL_FAILED_AMOUNT);
195
            break;
196
        case 7:
197
            NotifyManager::notify(
198
                NotifyTypes::BUYING_STORE_SELLER_SELL_FAILED_BALANCE);
199
            break;
200
        default:
201
            NotifyManager::notify(
202
                NotifyTypes::BUYING_STORE_SELLER_SELL_FAILED);
203
            break;
204
    }
205
}
206
207
void BuyingStoreRecv::processBuyingStoreReport(Net::MessageIn &msg)
208
{
209
    UNIMPLEMENTEDPACKET;
210
    msg.readItemId("item id");
211
    msg.readInt16("amount");
212
    if (msg.getVersion() >= 20141016)
213
    {
214
        msg.readInt32("money");
215
        msg.readInt32("money limit");
216
        msg.readInt32("char id");
217
        msg.readInt32("date");
218
    }
219
    else
220
    {
221
        msg.readInt32("money limit");
222
    }
223
}
224
225
void BuyingStoreRecv::processBuyingStoreDeleteItem(Net::MessageIn &msg)
226
{
227
    Inventory *const inventory = localPlayer != nullptr
228
        ? PlayerInfo::getInventory() : nullptr;
229
230
    const int index = msg.readInt16("index") - INVENTORY_OFFSET;
231
    const int amount = msg.readInt16("amount");
232
    msg.readInt32("price");
233
234
    if (inventory != nullptr)
235
    {
236
        if (Item *const item = inventory->getItem(index))
237
        {
238
            item->increaseQuantity(-amount);
239
            if (item->getQuantity() == 0)
240
                inventory->removeItemAt(index);
241
            ArrowsListener::distributeEvent();
242
        }
243
    }
244
}
245
246
2
}  // namespace EAthena