GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/traderecv.cpp Lines: 1 53 1.9 %
Date: 2017-11-29 Branches: 0 31 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-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/traderecv.h"
24
25
#include "notifymanager.h"
26
27
#include "being/playerinfo.h"
28
#include "being/playerrelation.h"
29
#include "being/playerrelations.h"
30
31
#include "const/net/inventory.h"
32
33
#include "enums/resources/notifytypes.h"
34
35
#include "gui/windows/tradewindow.h"
36
37
#include "net/messagein.h"
38
#include "net/tradehandler.h"
39
40
#include "net/ea/traderecv.h"
41
42
#include "resources/inventory/inventory.h"
43
44
#include "resources/item/item.h"
45
46
#include "utils/stringutils.h"
47
48
#include "debug.h"
49
50
extern std::string tradePartnerName;
51
52
namespace TmwAthena
53
{
54
55
void TradeRecv::processTradeRequest(Net::MessageIn &msg)
56
{
57
    Ea::TradeRecv::processTradeRequestContinue(msg.readString(24, "name"));
58
}
59
60
void TradeRecv::processTradeItemAdd(Net::MessageIn &msg)
61
{
62
    const int amount = msg.readInt32("amount");
63
    const int type = msg.readInt16("type");
64
    const uint8_t identify = msg.readUInt8("identify");
65
    msg.readUInt8("attribute");
66
    const uint8_t refine = msg.readUInt8("refine");
67
    int cards[maxCards];
68
    for (int f = 0; f < maxCards; f++)
69
        cards[f] = msg.readUInt16("card");
70
71
    if (tradeWindow != nullptr)
72
    {
73
        if (type == 0)
74
        {
75
            tradeWindow->setMoney(amount);
76
        }
77
        else
78
        {
79
            tradeWindow->addItem2(type,
80
                ItemType::Unknown,
81
                cards,
82
                nullptr,
83
                4,
84
                false,
85
                amount,
86
                refine,
87
                ItemColor_one,
88
                fromBool(identify, Identified),
89
                Damaged_false,
90
                Favorite_false,
91
                Equipm_false);
92
        }
93
    }
94
}
95
96
void TradeRecv::processTradeItemAddResponse(Net::MessageIn &msg)
97
{
98
    // Trade: New Item add response (was 0x00ea, now 01b1)
99
    const int index = msg.readInt16("index") - INVENTORY_OFFSET;
100
    Item *item = nullptr;
101
    if (PlayerInfo::getInventory() != nullptr)
102
        item = PlayerInfo::getInventory()->getItem(index);
103
104
    if (item == nullptr)
105
    {
106
        if (tradeWindow != nullptr)
107
            tradeWindow->receivedOk(true);
108
        return;
109
    }
110
    const int quantity = msg.readInt16("amount");
111
112
    const uint8_t res = msg.readUInt8("status");
113
    switch (res)
114
    {
115
        case 0:
116
            // Successfully added item
117
            if (tradeWindow != nullptr)
118
            {
119
                tradeWindow->addItem2(item->getId(),
120
                    item->getType(),
121
                    item->getCards(),
122
                    nullptr,
123
                    4,
124
                    true,
125
                    quantity,
126
                    item->getRefine(),
127
                    item->getColor(),
128
                    item->getIdentified(),
129
                    item->getDamaged(),
130
                    item->getFavorite(),
131
                    item->isEquipment());
132
            }
133
            item->increaseQuantity(-quantity);
134
            break;
135
        case 1:
136
            // Add item failed - player overweighted
137
            NotifyManager::notify(NotifyTypes::
138
                TRADE_ADD_PARTNER_OVER_WEIGHT);
139
            break;
140
        case 2:
141
            // Add item failed - player has no free slot
142
            NotifyManager::notify(NotifyTypes::TRADE_ADD_PARTNER_NO_SLOTS);
143
            break;
144
        case 3:
145
            // Add item failed - non tradable item
146
            NotifyManager::notify(NotifyTypes::TRADE_ADD_UNTRADABLE_ITEM);
147
            break;
148
        default:
149
            NotifyManager::notify(NotifyTypes::TRADE_ADD_ERROR);
150
            UNIMPLEMENTEDPACKETFIELD(res);
151
            logger->log("QQQ SMSG_TRADE_ITEM_ADD_RESPONSE: "
152
                        + toString(res));
153
            break;
154
    }
155
}
156
157
void TradeRecv::processTradeResponse(Net::MessageIn &msg)
158
{
159
    if (tradePartnerName.empty() ||
160
        !playerRelations.hasPermission(tradePartnerName,
161
        PlayerRelation::TRADE))
162
    {
163
        tradeHandler->respond(false);
164
        return;
165
    }
166
    const uint8_t type = msg.readUInt8("type");
167
    Ea::TradeRecv::processTradeResponseContinue(type);
168
}
169
170
4
}  // namespace TmwAthena