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