GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/traderecv.cpp Lines: 0 61 0.0 %
Date: 2021-03-17 Branches: 0 36 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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "net/eathena/traderecv.h"
25
26
#include "itemcolormanager.h"
27
#include "notifymanager.h"
28
29
#include "being/playerinfo.h"
30
31
#include "enums/resources/notifytypes.h"
32
33
#include "gui/windows/tradewindow.h"
34
35
#include "net/messagein.h"
36
37
#include "net/ea/traderecv.h"
38
39
#include "resources/inventory/inventory.h"
40
41
#include "resources/item/item.h"
42
#include "resources/item/itemoptionslist.h"
43
44
#include "debug.h"
45
46
extern int serverVersion;
47
48
namespace EAthena
49
{
50
51
namespace TradeRecv
52
{
53
    int mQuantity = 0;
54
    int mItemIndex = -1;
55
}  // namespace TradeRecv
56
57
void TradeRecv::processTradeRequest(Net::MessageIn &msg)
58
{
59
    const std::string &partner = msg.readString(24, "name");
60
    if (msg.getVersion() >= 6)
61
    {
62
        msg.readInt32("char id");
63
        msg.readInt16("base level");
64
    }
65
    Ea::TradeRecv::processTradeRequestContinue(partner);
66
}
67
68
void TradeRecv::processTradeResponse(Net::MessageIn &msg)
69
{
70
    const uint8_t type = msg.readUInt8("type");
71
    msg.readInt32("char id");
72
    msg.readInt16("base level");
73
    Ea::TradeRecv::processTradeResponseContinue(type);
74
}
75
76
void TradeRecv::processTradeItemAdd(Net::MessageIn &msg)
77
{
78
    const int type = msg.readItemId("item id");
79
    ItemTypeT itemType = ItemType::Unknown;
80
    if (msg.getVersion() >= 20100223)
81
    {
82
        itemType = static_cast<ItemTypeT>(
83
            msg.readUInt8("item type"));
84
    }
85
    const int amount = msg.readInt32("amount");
86
    const uint8_t identify = msg.readUInt8("identify");
87
    const Damaged damaged = fromBool(msg.readUInt8("attribute"), Damaged);
88
    const uint8_t refine = msg.readUInt8("refine");
89
    int cards[maxCards];
90
    for (int f = 0; f < maxCards; f++)
91
        cards[f] = msg.readItemId("card");
92
    ItemOptionsList *options = nullptr;
93
    if (msg.getVersion() >= 20150226)
94
    {
95
        options = new ItemOptionsList;
96
        for (int f = 0; f < 5; f ++)
97
        {
98
            const uint16_t idx = msg.readInt16("option index");
99
            const uint16_t val = msg.readInt16("option value");
100
            msg.readUInt8("option param");
101
            options->add(idx, val);
102
        }
103
    }
104
105
    if (tradeWindow != nullptr)
106
    {
107
        if (type == 0)
108
        {
109
            tradeWindow->setMoney(amount);
110
        }
111
        else
112
        {
113
            tradeWindow->addItem2(type,
114
                itemType,
115
                cards,
116
                options,
117
                4,
118
                false,
119
                amount,
120
                refine,
121
                ItemColorManager::getColorFromCards(&cards[0]),
122
                fromBool(identify, Identified),
123
                damaged,
124
                Favorite_false,
125
                Equipm_false);
126
        }
127
    }
128
    delete options;
129
}
130
131
void TradeRecv::processTradeItemAddResponse(Net::MessageIn &msg)
132
{
133
    msg.readInt16("index");
134
    const uint8_t res = msg.readUInt8("fail");
135
    switch (res)
136
    {
137
        case 0:  // Successfully added item
138
        case 9:  // silent added item
139
        {
140
            Item *const item = PlayerInfo::getInventory()->getItem(
141
                mItemIndex);
142
            if (item == nullptr)
143
                return;
144
            if (tradeWindow != nullptr)
145
            {
146
                tradeWindow->addItem2(item->getId(),
147
                    item->getType(),
148
                    item->getCards(),
149
                    item->getOptions(),
150
                    4,
151
                    true,
152
                    mQuantity,
153
                    item->getRefine(),
154
                    item->getColor(),
155
                    item->getIdentified(),
156
                    item->getDamaged(),
157
                    item->getFavorite(),
158
                    item->isEquipment());
159
            }
160
            item->increaseQuantity(-mQuantity);
161
            mItemIndex = -1;
162
            mQuantity = 0;
163
            break;
164
        }
165
        case 1:
166
            // Add item failed - player overweighted
167
            NotifyManager::notify(NotifyTypes::
168
                TRADE_ADD_PARTNER_OVER_WEIGHT);
169
            break;
170
        case 2:
171
            NotifyManager::notify(NotifyTypes::TRADE_ADD_ERROR);
172
            break;
173
        default:
174
            NotifyManager::notify(NotifyTypes::TRADE_ADD_ERROR);
175
            UNIMPLEMENTEDPACKETFIELD(res);
176
            break;
177
    }
178
}
179
180
void TradeRecv::processTradeUndo(Net::MessageIn &msg)
181
{
182
    UNIMPLEMENTEDPACKET;
183
    // +++ here need clear trade window from partner side?
184
}
185
186
}  // namespace EAthena