Mana
Loading...
Searching...
No Matches
buysellhandler.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 "inventory.h"
27#include "item.h"
28#include "localplayer.h"
29#include "playerinfo.h"
30
31#include "gui/buydialog.h"
32#include "gui/buyselldialog.h"
33#include "gui/selldialog.h"
34
35#include "net/tmwa/messagein.h"
36#include "net/tmwa/protocol.h"
37
38#include "utils/gettext.h"
39
40namespace TmwAthena {
41
43{
44 static const Uint16 _messages[] = {
50 0
51 };
52 mNpcId = 0;
53 handledMessages = _messages;
54}
55
57{
58 int n_items;
59
60 switch (msg.getId())
61 {
64 {
65 mNpcId = msg.readInt32();
67 }
68 break;
69
70 case SMSG_NPC_BUY:
71 msg.readInt16(); // length
72 n_items = (msg.getLength() - 4) / 11;
75
76 for (int k = 0; k < n_items; k++)
77 {
78 int value = msg.readInt32();
79 msg.readInt32(); // DCvalue
80 msg.readInt8(); // type
81 int itemId = msg.readInt16();
82 mBuyDialog->addItem(itemId, 0, value);
83 }
84 break;
85
86 case SMSG_NPC_SELL:
87 msg.readInt16(); // length
88 n_items = (msg.getLength() - 4) / 10;
89 if (n_items > 0)
90 {
91 auto *dialog = new SellDialog(mNpcId);
92 dialog->setMoney(PlayerInfo::getAttribute(MONEY));
93
94 for (int k = 0; k < n_items; k++)
95 {
96 int index = msg.readInt16() - INVENTORY_OFFSET;
97 int value = msg.readInt32();
98 msg.readInt32(); // OCvalue
99
100 Item *item = PlayerInfo::getInventory()->getItem(index);
101
102 if (item && !item->isEquipped())
103 dialog->addItem(item, value);
104 }
105 }
106 else
107 {
108 serverNotice(_("Nothing to sell."));
109 }
110 break;
111
113 if (msg.readInt8() != 0)
114 {
115 // Reset player money since buy dialog already assumed purchase
116 // would go fine
118 serverNotice(_("Unable to buy."));
119 }
120 break;
121
123 if (msg.readInt8() != 0)
124 serverNotice(_("Unable to sell."));
125 break;
126 }
127}
128
129} // namespace TmwAthena
The buy dialog.
Definition buydialog.h:42
void addItem(int id, int amount, int price)
Adds an item to the shop inventory.
void setMoney(int amount)
Sets the amount of available money.
A dialog to choose between buying or selling at a shop.
Item * getItem(int index) const
Returns the item at the specified index.
Definition inventory.cpp:44
Represents one or more instances of a certain item type.
Definition item.h:35
bool isEquipped() const
Returns whether this item is equipped.
Definition item.h:79
const uint16_t * handledMessages
The sell dialog.
Definition selldialog.h:39
void handleMessage(MessageIn &msg) override
Used for parsing an incoming message from eAthena.
Definition messagein.h:35
uint16_t readInt16()
Reads an unsigned 16-bit integer from the message.
Definition messagein.cpp:57
uint8_t readInt8()
Reads an unsigned 8-bit integer from the message.
Definition messagein.cpp:46
uint16_t getId() const
Returns the message ID.
Definition messagein.h:42
uint32_t readInt32()
Reads an unsigned 32-bit integer from the message.
Definition messagein.cpp:69
unsigned int getLength() const
Returns the message length.
Definition messagein.h:47
void serverNotice(const std::string &message)
Definition event.h:319
#define _(s)
Definition gettext.h:38
Inventory * getInventory()
Returns the player's inventory.
BuySellState getBuySellState()
Returns the current buy, sell, or related interaction the player is involved in.
int getAttribute(int id)
Returns the value of the given attribute.
Warning: buffers and other variables are shared, so there can be only one connection active at a time...
@ SMSG_NPC_BUY_RESPONSE
Definition protocol.h:224
@ SMSG_NPC_BUY_SELL_CHOICE
Definition protocol.h:218
@ SMSG_NPC_BUY
Definition protocol.h:220
@ SMSG_NPC_SELL_RESPONSE
Definition protocol.h:225
@ SMSG_NPC_SELL
Definition protocol.h:221
@ MONEY
Definition playerinfo.h:34
@ BUYSELL_CHOOSING
Definition playerinfo.h:67