GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/buysellrecv.cpp Lines: 1 53 1.9 %
Date: 2017-11-29 Branches: 2 44 4.5 %

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/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
namespace EAthena
50
{
51
52
void BuySellRecv::processNpcBuy(Net::MessageIn &msg)
53
{
54
    msg.readInt16("len");
55
    const int sz = 11;
56
    const int n_items = (msg.getLength() - 4) / sz;
57
58
    const BeingTypeId npcId = NpcRecv::mNpcTypeId;
59
    std::string currency;
60
61
    if (npcId != BeingTypeId_zero)
62
    {
63
        const BeingInfo *const info = NPCDB::get(npcId);
64
        if (info != nullptr)
65
            currency = info->getCurrency();
66
        else
67
            currency = DEFAULT_CURRENCY;
68
    }
69
    else
70
    {
71
        currency = DEFAULT_CURRENCY;
72
    }
73
74
    CREATEWIDGETV(Ea::BuySellRecv::mBuyDialog, BuyDialog,
75
        Ea::BuySellRecv::mNpcId,
76
        currency);
77
    Ea::BuySellRecv::mBuyDialog->setMoney(
78
        PlayerInfo::getAttribute(Attributes::MONEY));
79
80
    for (int k = 0; k < n_items; k++)
81
    {
82
        const int value = msg.readInt32("price");
83
        msg.readInt32("dc value?");
84
        const ItemTypeT type = static_cast<ItemTypeT>(
85
            msg.readUInt8("type"));
86
        const int itemId = msg.readInt16("item id");
87
        const ItemColor color = ItemColor_one;
88
        Ea::BuySellRecv::mBuyDialog->addItem(itemId, type, color, 0, value);
89
    }
90
    Ea::BuySellRecv::mBuyDialog->sort();
91
}
92
93
void BuySellRecv::processNpcSellResponse(Net::MessageIn &msg)
94
{
95
    switch (msg.readUInt8("result"))
96
    {
97
        case 0:
98
            NotifyManager::notify(NotifyTypes::SOLD);
99
            break;
100
        case 1:
101
        default:
102
            NotifyManager::notify(NotifyTypes::SELL_FAILED);
103
            break;
104
        case 2:
105
            NotifyManager::notify(NotifyTypes::SELL_TRADE_FAILED);
106
            break;
107
        case 3:
108
            NotifyManager::notify(NotifyTypes::SELL_UNSELLABLE_FAILED);
109
            break;
110
    }
111
}
112
113
void BuySellRecv::processNpcBuyResponse(Net::MessageIn &msg)
114
{
115
    const uint8_t response = msg.readUInt8("response");
116
    if (response == 0U)
117
    {
118
        NotifyManager::notify(NotifyTypes::BUY_DONE);
119
        return;
120
    }
121
    switch (response)
122
    {
123
        case 1:
124
            NotifyManager::notify(NotifyTypes::BUY_FAILED_NO_MONEY);
125
            break;
126
127
        case 2:
128
            NotifyManager::notify(NotifyTypes::BUY_FAILED_OVERWEIGHT);
129
            break;
130
131
        case 3:
132
            NotifyManager::notify(NotifyTypes::BUY_FAILED_TOO_MANY_ITEMS);
133
            break;
134
135
        default:
136
            NotifyManager::notify(NotifyTypes::BUY_FAILED);
137
            break;
138
    };
139
}
140
141

6
}  // namespace EAthena