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