ManaPlus
traderecv.cpp
Go to the documentation of this file.
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/tmwa/traderecv.h"
25 
26 #include "notifymanager.h"
27 
28 #include "being/playerinfo.h"
29 #include "being/playerrelation.h"
30 #include "being/playerrelations.h"
31 
32 #include "const/net/inventory.h"
33 
35 
37 
38 #include "net/messagein.h"
39 #include "net/tradehandler.h"
40 
41 #include "net/ea/traderecv.h"
42 
44 
45 #include "resources/item/item.h"
46 
47 #include "utils/stringutils.h"
48 
49 #include "debug.h"
50 
51 extern std::string tradePartnerName;
52 
53 namespace TmwAthena
54 {
55 
57 {
58  Ea::TradeRecv::processTradeRequestContinue(msg.readString(24, "name"));
59 }
60 
62 {
63  const int amount = msg.readInt32("amount");
64  const int type = msg.readInt16("type");
65  const uint8_t identify = msg.readUInt8("identify");
66  msg.readUInt8("attribute");
67  const uint8_t refine = msg.readUInt8("refine");
68  int cards[maxCards];
69  for (int f = 0; f < maxCards; f++)
70  cards[f] = msg.readUInt16("card");
71 
72  if (tradeWindow != nullptr)
73  {
74  if (type == 0)
75  {
76  tradeWindow->setMoney(amount);
77  }
78  else
79  {
80  tradeWindow->addItem2(type,
82  cards,
83  nullptr,
84  4,
85  false,
86  amount,
87  refine,
89  fromBool(identify, Identified),
92  Equipm_false);
93  }
94  }
95 }
96 
98 {
99  // Trade: New Item add response (was 0x00ea, now 01b1)
100  const int index = msg.readInt16("index") - INVENTORY_OFFSET;
101  Item *item = nullptr;
102  if (PlayerInfo::getInventory() != nullptr)
103  item = PlayerInfo::getInventory()->getItem(index);
104 
105  if (item == nullptr)
106  {
107  if (tradeWindow != nullptr)
108  tradeWindow->receivedOk(true);
109  return;
110  }
111  const int quantity = msg.readInt16("amount");
112 
113  const uint8_t res = msg.readUInt8("status");
114  switch (res)
115  {
116  case 0:
117  // Successfully added item
118  if (tradeWindow != nullptr)
119  {
120  tradeWindow->addItem2(item->getId(),
121  item->getType(),
122  item->getCards(),
123  nullptr,
124  4,
125  true,
126  quantity,
127  item->getRefine(),
128  item->getColor(),
129  item->getIdentified(),
130  item->getDamaged(),
131  item->getFavorite(),
132  item->isEquipment());
133  }
134  item->increaseQuantity(-quantity);
135  break;
136  case 1:
137  // Add item failed - player overweighted
140  break;
141  case 2:
142  // Add item failed - player has no free slot
144  break;
145  case 3:
146  // Add item failed - non tradable item
148  break;
149  default:
152  logger->log("QQQ SMSG_TRADE_ITEM_ADD_RESPONSE: "
153  + toString(res));
154  break;
155  }
156 }
157 
159 {
160  if (tradePartnerName.empty() ||
163  {
164  tradeHandler->respond(false);
165  return;
166  }
167  const uint8_t type = msg.readUInt8("type");
169 }
170 
171 } // namespace TmwAthena
#define fromBool(val, name)
Definition: booldefines.h:49
#define maxCards
Definition: cards.h:25
Item * getItem(const int index) const
Definition: inventory.cpp:83
Definition: item.h:50
uint8_t getRefine() const
Definition: item.h:141
Equipm isEquipment() const
Definition: item.h:117
int getId() const
Definition: item.h:81
Favorite getFavorite() const
Definition: item.h:205
ItemColor getColor() const
Definition: item.h:181
Identified getIdentified() const
Definition: item.h:193
ItemTypeT getType() const
Definition: item.h:225
Damaged getDamaged() const
Definition: item.h:199
const int * getCards() const
Definition: item.h:214
void increaseQuantity(const int amount)
Definition: item.h:99
void log(const char *const log_text,...)
Definition: logger.cpp:269
virtual void respond(const bool accept) const
Definition: tradehandler.h:49
bool hasPermission(const Being *const being, const unsigned int flags) const
void setMoney(const int quantity)
void receivedOk(const bool own)
void addItem2(const int id, const ItemTypeT type, const int *const cards, const ItemOptionsList *const options, const int sz, const bool own, const int quantity, const uint8_t refine, const ItemColor color, const Identified identified, const Damaged damaged, const Favorite favorite, const Equipm equipment) const
static const int INVENTORY_OFFSET
Definition: inventory.h:27
const bool Damaged_false
Definition: damaged.h:30
const bool Equipm_false
Definition: equipm.h:30
const bool Favorite_false
Definition: favorite.h:30
bool Identified
Definition: identified.h:30
const ItemColor ItemColor_one
Definition: itemcolor.h:30
Logger * logger
Definition: logger.cpp:89
#define UNIMPLEMENTEDPACKETFIELD(field)
Definition: logger.h:59
bool msg(InputEvent &event)
Definition: chat.cpp:39
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
void processTradeResponseContinue(const uint8_t type)
Definition: traderecv.cpp:62
void processTradeRequestContinue(const std::string &partner)
Definition: traderecv.cpp:145
@ Unknown
Definition: itemtype.h:30
void notify(const unsigned int message)
@ TRADE_ADD_PARTNER_OVER_WEIGHT
Definition: notifytypes.h:94
@ TRADE_ADD_PARTNER_NO_SLOTS
Definition: notifytypes.h:95
@ TRADE_ADD_UNTRADABLE_ITEM
Definition: notifytypes.h:96
Inventory * getInventory()
Definition: playerinfo.cpp:195
void processTradeRequest(Net::MessageIn &msg)
Definition: traderecv.cpp:56
void processTradeItemAdd(Net::MessageIn &msg)
Definition: traderecv.cpp:61
void processTradeItemAddResponse(Net::MessageIn &msg)
Definition: traderecv.cpp:97
void processTradeResponse(Net::MessageIn &msg)
Definition: traderecv.cpp:158
Net::TradeHandler * tradeHandler
Definition: net.cpp:98
PlayerRelationsManager playerRelations
static const unsigned int TRADE
std::string tradePartnerName
Definition: popupmenu.cpp:101
TradeWindow * tradeWindow
Definition: tradewindow.cpp:65