Mana
Loading...
Searching...
No Matches
tradehandler.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "actorspritemanager.h"
25#include "event.h"
26#include "item.h"
27#include "localplayer.h"
28#include "playerinfo.h"
29
30#include "gui/confirmdialog.h"
31#include "gui/tradewindow.h"
32
33#include "net/net.h"
34
39
40#include "utils/gettext.h"
41#include "utils/stringutils.h"
42
43extern std::string tradePartnerName;
45
47
48namespace ManaServ {
49
50extern Connection *gameServerConnection;
51
55namespace {
56 struct RequestTradeListener : public gcn::ActionListener
57 {
58 void action(const gcn::ActionEvent &event) override
59 {
60 if (event.getId() == "yes")
61 {
65 }
66 else
68 }
69 } listener;
70}
71
73 mAcceptTradeRequests(true)
74{
75 static const Uint16 _messages[] = {
85 0
86 };
87 handledMessages = _messages;
88 tradeHandler = this;
89}
90
91void TradeHandler::setAcceptTradeRequests(bool acceptTradeRequests)
92{
95 serverNotice(_("Accepting incoming trade requests."));
96 else
97 serverNotice(_("Ignoring incoming trade requests."));
98}
99
101{
102 switch (msg.getId())
103 {
105 {
107 if (!being || !mAcceptTradeRequests)
108 {
109 respond(false);
110 break;
111 }
112 mTrading = true;
113 tradePartnerName = being->getName();
114 tradePartnerID = being->getId();
115 auto *dlg = new ConfirmDialog(_("Request for Trade"),
116 strprintf(_("%s wants to trade with you, do you accept?"),
117 tradePartnerName.c_str()));
118 dlg->addActionListener(&listener);
119 } break;
120
122 {
123 int type = msg.readInt16();
124 int amount = msg.readInt8();
125 tradeWindow->addItem(type, false, amount);
126 } break;
127
130 break;
131
134 tradeWindow->setCaption(strprintf(_("Trading with %s"),
135 tradePartnerName.c_str()));
136 tradeWindow->setVisible(true);
137 break;
138
140 tradeWindow->receivedOk(false);
141 break;
142
144 tradeWindow->receivedOk(false);
145 break;
146
148 serverNotice(_("Trade canceled."));
149 tradeWindow->setVisible(false);
151 mTrading = false;
152 break;
153
155 serverNotice(_("Trade completed."));
156 tradeWindow->setVisible(false);
158 mTrading = false;
159 break;
160 }
161}
162
164{
165 tradePartnerName = being->getName();
166 tradePartnerID = being->getId();
167
171}
172
173void TradeHandler::respond(bool accept)
174{
177
178 if (!accept)
179 mTrading = false;
180}
181
182void TradeHandler::addItem(Item *item, int amount)
183{
185 msg.writeInt8(item->getInvIndex());
186 msg.writeInt8(amount);
188
189 tradeWindow->addItem(item->getId(), true, amount);
190 item->increaseQuantity(-amount);
191}
192
193void TradeHandler::removeItem(int slotNum, int amount)
194{
195 // TODO
196}
197
199{
201 msg.writeInt32(amount);
203}
204
210
216
222
223} // namespace ManaServ
ActorSpriteManager * actorSpriteManager
Definition game.cpp:110
Being * findBeing(int id) const
Returns a specific Being, by id;.
int getId() const
Definition actorsprite.h:63
Definition being.h:65
const std::string & getName() const
Returns the name of the being.
Definition being.h:190
An option dialog.
Represents one or more instances of a certain item type.
Definition item.h:35
int getInvIndex() const
Returns the inventory index of this item.
Definition item.h:94
void increaseQuantity(int amount)
Increases the number of items by the given amount.
Definition item.h:64
int getId() const
Returns the item id.
Definition item.h:49
void send(const ManaServ::MessageOut &msg)
Sends a message.
Used for parsing an incoming message from manaserv.
Definition messagein.h:37
uint16_t readInt16()
Reads an unsigned 16-bit integer from the message.
Definition messagein.cpp:58
uint16_t getId() const
Returns the message ID.
Definition messagein.h:44
uint32_t readInt32()
Reads an unsigned 32-bit integer from the message.
Definition messagein.cpp:75
uint8_t readInt8()
Reads an unsigned 8-bit integer from the message.
Definition messagein.cpp:43
Used for building an outgoing message to manaserv.
Definition messageout.h:37
void writeInt16(uint16_t value)
Writes an unsigned 16-bit integer to the message.
void writeInt32(uint32_t value)
Writes an unsigned 32-bit integer to the message.
void writeInt8(uint8_t value)
Writes an unsigned 8-bit integer to the message.
void addItem(Item *item, int amount) override
bool acceptTradeRequests() const
Returns whether trade requests are accepted.
void setMoney(int amount) override
void request(Being *being) override
void removeItem(int slotNum, int amount) override
void respond(bool accept) override
void confirm() override
void setAcceptTradeRequests(bool acceptTradeRequests)
Sets whether trade requests are accepted.
void handleMessage(MessageIn &msg) override
const uint16_t * handledMessages
virtual void cancel()
void addItem(int id, bool own, int quantity)
Add an item to the trade window.
void reset()
Reset both item containers.
void receivedOk(bool own)
Player received ok message from server.
void setMoney(int quantity)
Displays expected money in the trade window.
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
Definition window.cpp:282
void serverNotice(const std::string &message)
Definition event.h:319
TradeWindow * tradeWindow
Definition game.cpp:101
#define _(s)
Definition gettext.h:38
std::string tradePartnerName
Definition popupmenu.cpp:55
Net::TradeHandler * tradeHandler
Definition net.cpp:58
int tradePartnerID
Connection * gameServerConnection
TradeHandler * getTradeHandler()
Definition net.cpp:120
std::string tradePartnerName
Definition popupmenu.cpp:55
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.