GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/marketrecv.cpp Lines: 1 31 3.2 %
Date: 2021-03-17 Branches: 0 36 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "net/eathena/marketrecv.h"
23
24
#include "notifymanager.h"
25
26
#include "being/playerinfo.h"
27
28
#include "const/resources/currency.h"
29
30
#include "enums/resources/notifytypes.h"
31
32
#include "gui/windows/buydialog.h"
33
34
#include "gui/widgets/createwidget.h"
35
36
#include "net/messagein.h"
37
38
#include "net/eathena/npcrecv.h"
39
40
#include "resources/beinginfo.h"
41
42
#include "resources/db/npcdb.h"
43
44
#include "debug.h"
45
46
extern int itemIdLen;
47
48
namespace EAthena
49
{
50
51
namespace MarketRecv
52
{
53
    BuyDialog *mBuyDialog = nullptr;
54
}  // namespace MarketRecv
55
56
57
void MarketRecv::processMarketOpen(Net::MessageIn &msg)
58
{
59
    const int len = (msg.readInt16("len") - 4) / (11 + itemIdLen);
60
61
    const BeingTypeId npcId = NpcRecv::mNpcTypeId;
62
    std::string currency;
63
64
    if (npcId != BeingTypeId_zero)
65
    {
66
        const BeingInfo *const info = NPCDB::get(npcId);
67
        if (info != nullptr)
68
            currency = info->getCurrency();
69
        else
70
            currency = DEFAULT_CURRENCY;
71
    }
72
    else
73
    {
74
        currency = DEFAULT_CURRENCY;
75
    }
76
77
    CREATEWIDGETV(mBuyDialog, BuyDialog,
78
        fromInt(BuyDialog::Market, BeingId),
79
        currency);
80
    mBuyDialog->setMoney(PlayerInfo::getAttribute(Attributes::MONEY));
81
82
    for (int f = 0; f < len; f ++)
83
    {
84
        const int itemId = msg.readItemId("item id");
85
        const ItemTypeT type = static_cast<ItemTypeT>(msg.readUInt8("type"));
86
        const int value = msg.readInt32("price");
87
        const int amount = msg.readInt32("amount");
88
        msg.readInt16("weight");  // +++ compare with item weight from db?
89
        const ItemColor color = ItemColor_one;
90
        mBuyDialog->addItem(itemId, type, color, amount, value);
91
    }
92
    mBuyDialog->sort();
93
}
94
95
void MarketRecv::processMarketBuyAck(Net::MessageIn &msg)
96
{
97
    const int len = (msg.readInt16("len") - 5) / (6 + itemIdLen);
98
    const int res = msg.readUInt8("result");
99
    for (int f = 0; f < len; f ++)
100
    {
101
        msg.readItemId("item id");
102
        msg.readInt16("amount");
103
        msg.readInt32("price");
104
    }
105
    if (res != 0)
106
        NotifyManager::notify(NotifyTypes::BUY_DONE);
107
    else
108
        NotifyManager::notify(NotifyTypes::BUY_FAILED);
109
}
110
111
2
}  // namespace EAthena