summaryrefslogtreecommitdiff
path: root/src/gui/trade.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/trade.cpp')
-rw-r--r--src/gui/trade.cpp161
1 files changed, 133 insertions, 28 deletions
diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp
index d7554de8..5be71a6f 100644
--- a/src/gui/trade.cpp
+++ b/src/gui/trade.cpp
@@ -40,23 +40,37 @@
#include "../localplayer.h"
#include "../units.h"
+#ifdef TMWSERV_SUPPORT
+#include "../net/tmwserv/gameserver/player.h"
+#else
#include "../net/messageout.h"
-#include "../net/protocol.h"
+#include "../net/ea/protocol.h"
+#endif
#include "../utils/gettext.h"
-#include "../utils/strprintf.h"
#include "../utils/stringutils.h"
+#include "../utils/strprintf.h"
+#ifdef TMWSERV_SUPPORT
+TradeWindow::TradeWindow():
+#else
TradeWindow::TradeWindow(Network *network):
- Window("Trade"),
+#endif
+ Window(_("Trade: You")),
+#ifdef EATHENA_SUPPORT
mNetwork(network),
mMyInventory(new Inventory(INVENTORY_SIZE, 2)),
mPartnerInventory(new Inventory(INVENTORY_SIZE, 2))
+#else
+ mMyInventory(new Inventory(INVENTORY_SIZE)),
+ mPartnerInventory(new Inventory(INVENTORY_SIZE)),
+ mStatus(PREPARING)
+#endif
{
- setWindowName(_("Trade"));
- setDefaultSize(342, 209, ImageRect::CENTER);
+ setWindowName("Trade");
setResizable(true);
setCloseButton(true);
+ setDefaultSize(342, 209, ImageRect::CENTER);
setMinWidth(342);
setMinHeight(209);
@@ -65,25 +79,40 @@ TradeWindow::TradeWindow(Network *network):
getFont()->getWidth(_("Trade")) ?
_("OK") : _("Trade");
- mAddButton = new Button(_("Add"), "add", this);
+ Button *mAddButton = new Button(_("Add"), "add", this);
+#ifdef EATHENA_SUPPORT
mOkButton = new Button(longestName, "ok", this);
-
- mMyItemContainer = new ItemContainer(mMyInventory.get(), 2);
- mMyItemContainer->setWidth(160);
+#endif
+ Button *mCancelButton = new Button(_("Cancel"), "cancel", this);
+ mTradeButton = new Button(_("Propose trade"), "trade", this);
+ mTradeButton->setWidth(8 + std::max(
+ mTradeButton->getFont()->getWidth(_("Propose trade")),
+ mTradeButton->getFont()->getWidth(_("Confirm trade"))));
+
+#ifdef TMWSERV_SUPPORT
+ mMyItemContainer = new ItemContainer(mMyInventory.get(), 4, 3, 0);
+#else
+ mMyItemContainer = new ItemContainer(mMyInventory.get(), 4, 3, 2);
+#endif
mMyItemContainer->addSelectionListener(this);
- mMyScroll = new ScrollArea(mMyItemContainer);
+ ScrollArea *mMyScroll = new ScrollArea(mMyItemContainer);
- mPartnerItemContainer = new ItemContainer(mPartnerInventory.get(), 2);
- mPartnerItemContainer->setWidth(160);
+#ifdef TMWSERV_SUPPORT
+ mPartnerItemContainer = new ItemContainer(mPartnerInventory.get(), 4, 3, 0);
+#else
+ mPartnerItemContainer = new ItemContainer(mPartnerInventory.get(), 4, 3, 2);
+#endif
mPartnerItemContainer->addSelectionListener(this);
- mPartnerScroll = new ScrollArea(mPartnerItemContainer);
+ ScrollArea *mPartnerScroll = new ScrollArea(mPartnerItemContainer);
mMoneyLabel = new Label(strprintf(_("You get %s."), ""));
- mMoneyLabel2 = new Label(_("You give:"));
+ gcn::Label *mMoneyLabel2 = new Label(_("You give:"));
+
mMoneyField = new TextField;
- mMoneyField->setWidth(50);
+ mMoneyField->setWidth(40);
+ Button *mMoneyChange = new Button(_("Change"), "money", this);
place(1, 0, mMoneyLabel);
place(0, 1, mMyScroll).setPadding(3);
@@ -92,9 +121,15 @@ TradeWindow::TradeWindow(Network *network):
place = getPlacer(0, 0);
place(0, 0, mMoneyLabel2);
place(1, 0, mMoneyField);
+ place(2, 0, mMoneyChange).setHAlign(LayoutCell::LEFT);
place = getPlacer(0, 2);
- place(6, 0, mAddButton);
- place(7, 0, mOkButton);
+ place(0, 0, mAddButton);
+#ifdef EATHENA_SUPPORT
+ place(1, 0, mOkButton);
+#else
+ place(2, 0, mTradeButton);
+ place(3, 0, mCancelButton);
+#endif
Layout &layout = getLayout();
layout.extend(0, 2, 2, 1);
layout.setRowHeight(1, Layout::AUTO_SET);
@@ -102,7 +137,9 @@ TradeWindow::TradeWindow(Network *network):
layout.setColWidth(0, Layout::AUTO_SET);
layout.setColWidth(1, Layout::AUTO_SET);
+#ifdef EATHENA_SUPPORT
mOkButton->setCaption(_("OK"));
+#endif
loadWindowState();
}
@@ -111,35 +148,37 @@ TradeWindow::~TradeWindow()
{
}
-void TradeWindow::addMoney(int amount)
+void TradeWindow::setMoney(int amount)
{
mMoneyLabel->setCaption(strprintf(_("You get %s."),
Units::formatCurrency(amount).c_str()));
mMoneyLabel->adjustSize();
+#ifdef TMWSERV_SUPPORT
+ setStatus(PREPARING);
+#endif
+}
+
+#ifdef TMWSERV_SUPPORT
+void TradeWindow::addItem(int id, bool own, int quantity)
+{
+ (own ? mMyInventory : mPartnerInventory)->addItem(id, quantity);
+ setStatus(PREPARING);
}
+#endif
+#ifdef EATHENA_SUPPORT
void TradeWindow::addItem(int id, bool own, int quantity, bool equipment)
{
if (own)
{
- mMyItemContainer->setWidth(mMyScroll->getWidth());
mMyInventory->addItem(id, quantity, equipment);
}
else
{
- mPartnerItemContainer->setWidth(mPartnerScroll->getWidth());
mPartnerInventory->addItem(id, quantity, equipment);
}
}
-void TradeWindow::removeItem(int id, bool own)
-{
- if (own)
- mMyInventory->removeItem(id);
- else
- mPartnerInventory->removeItem(id);
-}
-
void TradeWindow::changeQuantity(int index, bool own, int quantity)
{
if (own)
@@ -155,21 +194,36 @@ void TradeWindow::increaseQuantity(int index, bool own, int quantity)
else
mPartnerInventory->getItem(index)->increaseQuantity(quantity);
}
+#endif
void TradeWindow::reset()
{
mMyInventory->clear();
mPartnerInventory->clear();
+#ifdef EATHENA_SUPPORT
mOkButton->setCaption(_("OK"));
mOkButton->setActionEventId("ok");
mOkButton->setEnabled(true);
mOkOther = false;
mOkMe = false;
+#endif
mMoneyLabel->setCaption(strprintf(_("You get %s."), ""));
mMoneyField->setEnabled(true);
mMoneyField->setText("");
+#ifdef TMWSERV_SUPPORT
+ setStatus(PREPARING);
+#endif
}
+#ifdef TMWSERV_SUPPORT
+
+void TradeWindow::receivedOk()
+{
+ setStatus(ACCEPTING);
+}
+
+#else
+
void TradeWindow::receivedOk(bool own)
{
if (own)
@@ -192,8 +246,15 @@ void TradeWindow::receivedOk(bool own)
}
}
+#endif
+
void TradeWindow::tradeItem(Item *item, int quantity)
{
+#ifdef TMWSERV_SUPPORT
+ Net::GameServer::Player::tradeItem(item->getInvIndex(), quantity);
+ addItem(item->getId(), true, quantity);
+ item->increaseQuantity(-quantity);
+#else
// TODO: Our newer version of eAthena doesn't register this following
// function. Detect the actual server version, and re-enable this
// for that version only.
@@ -202,6 +263,7 @@ void TradeWindow::tradeItem(Item *item, int quantity)
outMsg.writeInt16(CMSG_TRADE_ITEM_ADD_REQUEST);
outMsg.writeInt16(item->getInvIndex());
outMsg.writeInt32(quantity);
+#endif
}
void TradeWindow::valueChanged(const gcn::SelectionEvent &event)
@@ -218,6 +280,18 @@ void TradeWindow::valueChanged(const gcn::SelectionEvent &event)
mMyItemContainer->selectNone();
}
+#ifdef TMWSERV_SUPPORT
+void TradeWindow::setStatus(Status s)
+{
+ if (s == mStatus) return;
+ mStatus = s;
+
+ mTradeButton->setCaption
+ (s == PREPARING ? _("Propose trade") : _("Confirm trade"));
+ mTradeButton->setEnabled(s != PROPOSING);
+}
+#endif
+
void TradeWindow::action(const gcn::ActionEvent &event)
{
Item *item = inventoryWindow->getSelectedItem();
@@ -249,12 +323,24 @@ void TradeWindow::action(const gcn::ActionEvent &event)
// Choose amount of items to trade
new ItemAmountWindow(AMOUNT_TRADE_ADD, this, item);
}
+
+#ifdef TMWSERV_SUPPORT
+ setStatus(PREPARING);
+#endif
}
else if (event.getId() == "cancel")
{
+ setVisible(false);
+ reset();
+ player_node->setTrading(false);
+#ifdef TMWSERV_SUPPORT
+ Net::GameServer::Player::acceptTrade(false);
+#else
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_TRADE_CANCEL_REQUEST);
+#endif
}
+#ifdef EATHENA_SUPPORT
else if (event.getId() == "ok")
{
std::stringstream tempMoney(mMoneyField->getText());
@@ -276,15 +362,34 @@ void TradeWindow::action(const gcn::ActionEvent &event)
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_TRADE_ADD_COMPLETE);
}
+#endif
else if (event.getId() == "trade")
{
+#ifdef TMWSERV_SUPPORT
+ Net::GameServer::Player::acceptTrade(true);
+ setStatus(PROPOSING);
+#else
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_TRADE_OK);
+#endif
+ }
+#ifdef TMWSERV_SUPPORT
+ else if (event.getId() == "money")
+ {
+ int v = atoi(mMoneyField->getText().c_str());
+ Net::GameServer::Player::tradeMoney(v);
+ mMoneyField->setText(strprintf("%d", v));
+ setStatus(PREPARING);
}
+#endif
}
void TradeWindow::close()
{
+#ifdef TMWSERV_SUPPORT
+ Net::GameServer::Player::acceptTrade(false);
+#else
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_TRADE_CANCEL_REQUEST);
+#endif
}