GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/buysellrecv.cpp Lines: 1 47 2.1 %
Date: 2018-12-09 Branches: 0 40 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2018  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "net/eathena/buysellrecv.h"
24
25
#include "notifymanager.h"
26
27
#include "being/playerinfo.h"
28
29
#include "const/resources/currency.h"
30
31
#include "enums/resources/notifytypes.h"
32
33
#include "gui/windows/buydialog.h"
34
35
#include "gui/widgets/createwidget.h"
36
37
#include "net/messagein.h"
38
39
#include "net/ea/buysellrecv.h"
40
41
#include "net/eathena/npcrecv.h"
42
43
#include "resources/beinginfo.h"
44
45
#include "resources/db/npcdb.h"
46
47
#include "debug.h"
48
49
extern int itemIdLen;
50
51
namespace EAthena
52
{
53
54
void BuySellRecv::processNpcBuy(Net::MessageIn &msg)
55
{
56
    msg.readInt16("len");
57
    const int sz = 9 + itemIdLen;
58
    const int n_items = (msg.getLength() - 4) / sz;
59
60
    const BeingTypeId npcId = NpcRecv::mNpcTypeId;
61
    std::string currency;
62
63
    if (npcId != BeingTypeId_zero)
64
    {
65
        const BeingInfo *const info = NPCDB::get(npcId);
66
        if (info != nullptr)
67
            currency = info->getCurrency();
68
        else
69
            currency = DEFAULT_CURRENCY;
70
    }
71
    else
72
    {
73
        currency = DEFAULT_CURRENCY;
74
    }
75
76
    CREATEWIDGETV(Ea::BuySellRecv::mBuyDialog, BuyDialog,
77
        Ea::BuySellRecv::mNpcId,
78
        currency);
79
    Ea::BuySellRecv::mBuyDialog->setMoney(
80
        PlayerInfo::getAttribute(Attributes::MONEY));
81
82
    for (int k = 0; k < n_items; k++)
83
    {
84
        const int value = msg.readInt32("price");
85
        msg.readInt32("dc value?");
86
        const ItemTypeT type = static_cast<ItemTypeT>(
87
            msg.readUInt8("type"));
88
        const int itemId = msg.readItemId("item id");
89
        const ItemColor color = ItemColor_one;
90
        Ea::BuySellRecv::mBuyDialog->addItem(itemId, type, color, 0, value);
91
    }
92
    Ea::BuySellRecv::mBuyDialog->sort();
93
}
94
95
void BuySellRecv::processNpcSellResponse(Net::MessageIn &msg)
96
{
97
    switch (msg.readUInt8("result"))
98
    {
99
        case 0:
100
            NotifyManager::notify(NotifyTypes::SOLD);
101
            break;
102
        case 1:
103
        default:
104
            NotifyManager::notify(NotifyTypes::SELL_FAILED);
105
            break;
106
        case 2:
107
            NotifyManager::notify(NotifyTypes::SELL_TRADE_FAILED);
108
            break;
109
        case 3:
110
            NotifyManager::notify(NotifyTypes::SELL_UNSELLABLE_FAILED);
111
            break;
112
    }
113
}
114
115
void BuySellRecv::processNpcBuyResponse(Net::MessageIn &msg)
116
{
117
    const uint8_t response = msg.readUInt8("response");
118
    if (response == 0U)
119
    {
120
        NotifyManager::notify(NotifyTypes::BUY_DONE);
121
        return;
122
    }
123
    switch (response)
124
    {
125
        case 1:
126
            NotifyManager::notify(NotifyTypes::BUY_FAILED_NO_MONEY);
127
            break;
128
129
        case 2:
130
            NotifyManager::notify(NotifyTypes::BUY_FAILED_OVERWEIGHT);
131
            break;
132
133
        case 3:
134
            NotifyManager::notify(NotifyTypes::BUY_FAILED_TOO_MANY_ITEMS);
135
            break;
136
137
        default:
138
            NotifyManager::notify(NotifyTypes::BUY_FAILED);
139
            break;
140
    }
141
}
142
143
2
}  // namespace EAthena