GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/buysellrecv.cpp Lines: 1 48 2.1 %
Date: 2017-11-29 Branches: 2 20 10.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-2017  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/tmwa/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 "debug.h"
42
43
namespace TmwAthena
44
{
45
46
void BuySellRecv::processNpcBuy(Net::MessageIn &msg)
47
{
48
    msg.readInt16("len");
49
    const unsigned int n_items = (msg.getLength() - 4U) / 11;
50
    CREATEWIDGETV(Ea::BuySellRecv::mBuyDialog, BuyDialog,
51
        Ea::BuySellRecv::mNpcId,
52
        DEFAULT_CURRENCY);
53
    Ea::BuySellRecv::mBuyDialog->setMoney(
54
        PlayerInfo::getAttribute(Attributes::MONEY));
55
56
    for (unsigned int k = 0; k < n_items; k++)
57
    {
58
        const int value = msg.readInt32("price");
59
        msg.readInt32("dc value?");
60
        const ItemTypeT type = static_cast<ItemTypeT>(msg.readUInt8("type"));
61
        const int itemId = msg.readInt16("item id");
62
        const ItemColor color = ItemColor_one;
63
        Ea::BuySellRecv::mBuyDialog->addItem(itemId, type, color, 0, value);
64
    }
65
    Ea::BuySellRecv::mBuyDialog->sort();
66
}
67
68
void BuySellRecv::processNpcSellResponse(Net::MessageIn &msg)
69
{
70
    switch (msg.readUInt8("result"))
71
    {
72
        case 0:
73
            NotifyManager::notify(NotifyTypes::SOLD);
74
            break;
75
        case 1:
76
        default:
77
            NotifyManager::notify(NotifyTypes::SELL_FAILED);
78
            break;
79
        case 2:
80
            NotifyManager::notify(NotifyTypes::SELL_TRADE_FAILED);
81
            break;
82
        case 3:
83
            NotifyManager::notify(NotifyTypes::SELL_UNSELLABLE_FAILED);
84
            break;
85
    }
86
}
87
88
void BuySellRecv::processNpcBuyResponse(Net::MessageIn &msg)
89
{
90
    const uint8_t response = msg.readUInt8("response");
91
    if (response == 0U)
92
    {
93
        NotifyManager::notify(NotifyTypes::BUY_DONE);
94
        return;
95
    }
96
    // Reset player money since buy dialog already assumed purchase
97
    // would go fine
98
    if (Ea::BuySellRecv::mBuyDialog != nullptr)
99
    {
100
        Ea::BuySellRecv::mBuyDialog->setMoney(
101
            PlayerInfo::getAttribute(Attributes::MONEY));
102
    }
103
    switch (response)
104
    {
105
        case 1:
106
            NotifyManager::notify(NotifyTypes::BUY_FAILED_NO_MONEY);
107
            break;
108
109
        case 2:
110
            NotifyManager::notify(NotifyTypes::BUY_FAILED_OVERWEIGHT);
111
            break;
112
113
        case 3:
114
            NotifyManager::notify(NotifyTypes::BUY_FAILED_TOO_MANY_ITEMS);
115
            break;
116
117
        default:
118
            NotifyManager::notify(NotifyTypes::BUY_FAILED);
119
            break;
120
    };
121
}
122
123

6
}  // namespace TmwAthena