diff options
author | blue112 <bluesansdouze@gmail.com> | 2009-04-21 15:31:21 +0200 |
---|---|---|
committer | Jared Adams <jaxad0127@gmail.com> | 2009-04-21 08:35:52 -0600 |
commit | fa96d81b86914adb3d8608f8ab4f734d3286fcd4 (patch) | |
tree | 422d35e88df76f05f40b5308fa842ce8a0ceeb3b /src | |
parent | 18bb2946b4acfe8d3ac9184a453898718488e556 (diff) | |
download | mana-client-fa96d81b86914adb3d8608f8ab4f734d3286fcd4.tar.gz mana-client-fa96d81b86914adb3d8608f8ab4f734d3286fcd4.tar.bz2 mana-client-fa96d81b86914adb3d8608f8ab4f734d3286fcd4.tar.xz mana-client-fa96d81b86914adb3d8608f8ab4f734d3286fcd4.zip |
Trade system
Handler & GUI modification, to make the three steps systems working.
Protocol changes for the same reason
Player.cpp changes because it uses the cancel method which signature has changed
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/trade.cpp | 63 | ||||
-rw-r--r-- | src/gui/trade.h | 8 | ||||
-rw-r--r-- | src/net/tmwserv/gameserver/player.cpp | 2 | ||||
-rw-r--r-- | src/net/tmwserv/protocol.h | 15 | ||||
-rw-r--r-- | src/net/tmwserv/tradehandler.cpp | 16 |
5 files changed, 73 insertions, 31 deletions
diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index c67b0b7b..7ed41387 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -44,20 +44,22 @@ #include "utils/stringutils.h" #include "utils/strprintf.h" + #include <sstream> #include <guichan/font.hpp> -#define CAPTION_PROPOSE _("Propose trade") -#define CAPTION_WAIT _("Waiting") -#define CAPTION_ACCEPT _("Confirm trade") +#define CAPTION_PROPOSE _("!Propose trade") +#define CAPTION_CONFIRMED _("Confirmed. Waiting...") +#define CAPTION_ACCEPT _("!Agree trade") +#define CAPTION_ACCEPTED _("Agreed. Waiting...") TradeWindow::TradeWindow(): Window(_("Trade: You")), mMyInventory(new Inventory(INVENTORY_SIZE)), mPartnerInventory(new Inventory(INVENTORY_SIZE)), mStatus(PROPOSING) -{ +{ setWindowName("Trade"); setResizable(true); setCloseButton(true); @@ -69,12 +71,13 @@ TradeWindow::TradeWindow(): getFont()->getWidth(_("Trade")) ? _("OK") : _("Trade"); - Button *addButton = new Button(_("Add"), "add", this); + mAddButton = new Button(_("Add"), "add", this); mOkButton = new Button("", "", this); // Will be filled in later int width = std::max(mOkButton->getFont()->getWidth(CAPTION_PROPOSE), - mOkButton->getFont()->getWidth(CAPTION_WAIT)); + mOkButton->getFont()->getWidth(CAPTION_CONFIRMED)); width = std::max(width, mOkButton->getFont()->getWidth(CAPTION_ACCEPT)); + width = std::max(width, mOkButton->getFont()->getWidth(CAPTION_ACCEPTED)); mOkButton->setWidth(8 + width); @@ -92,10 +95,10 @@ TradeWindow::TradeWindow(): mMoneyLabel = new Label(strprintf(_("You get %s."), "")); gcn::Label *mMoneyLabel2 = new Label(_("You give:")); - + mMoneyField = new TextField; mMoneyField->setWidth(40); - Button *moneyChange = new Button(_("Change"), "money", this); + mMoneyChangeButton = new Button(_("Change"), "money", this); place(1, 0, mMoneyLabel); place(0, 1, myScroll).setPadding(3); @@ -104,9 +107,9 @@ TradeWindow::TradeWindow(): place = getPlacer(0, 0); place(0, 0, mMoneyLabel2); place(1, 0, mMoneyField); - place(2, 0, moneyChange).setHAlign(LayoutCell::LEFT); + place(2, 0, mMoneyChangeButton).setHAlign(LayoutCell::LEFT); place = getPlacer(0, 2); - place(0, 0, addButton); + place(0, 0, mAddButton); place(1, 0, mOkButton); Layout &layout = getLayout(); layout.extend(0, 2, 2, 1); @@ -129,13 +132,13 @@ void TradeWindow::setMoney(int amount) mMoneyLabel->setCaption(strprintf(_("You get %s."), Units::formatCurrency(amount).c_str())); mMoneyLabel->adjustSize(); - setStatus(PREPARING); + //setStatus(PREPARING); } void TradeWindow::addItem(int id, bool own, int quantity) { (own ? mMyInventory : mPartnerInventory)->addItem(id, quantity); - setStatus(PREPARING); + //setStatus(PREPARING); } void TradeWindow::addItem(int id, bool own, int quantity, bool equipment) @@ -172,9 +175,11 @@ void TradeWindow::reset() mPartnerInventory->clear(); mOkOther = false; mOkMe = false; - mMoneyLabel->setCaption(strprintf(_("You get %s."), "")); + mMoneyLabel->setCaption(strprintf(_("You get %s."), "0GP")); mMoneyField->setEnabled(true); mMoneyField->setText(""); + mAddButton->setEnabled(true); + mMoneyChangeButton->setEnabled(true); setStatus(PREPARING); } @@ -186,7 +191,11 @@ void TradeWindow::receivedOk(bool own) mOkOther = true; if (mOkMe && mOkOther) + { + mOkMe = false; + mOkOther = false; setStatus(ACCEPTING); + } } void TradeWindow::tradeItem(Item *item, int quantity) @@ -221,18 +230,22 @@ void TradeWindow::setStatus(Status s) mOkButton->setActionEventId("ok"); break; case PROPOSING: - mOkButton->setCaption(CAPTION_WAIT); + mOkButton->setCaption(CAPTION_CONFIRMED); mOkButton->setActionEventId(""); break; case ACCEPTING: mOkButton->setCaption(CAPTION_ACCEPT); mOkButton->setActionEventId("trade"); break; + case ACCEPTED: + mOkButton->setCaption(CAPTION_ACCEPTED); + mOkButton->setActionEventId(""); + break; default: break; } - mOkButton->setEnabled(s != PROPOSING); + mOkButton->setEnabled((s != PROPOSING && s != ACCEPTED)); } void TradeWindow::action(const gcn::ActionEvent &event) @@ -241,6 +254,9 @@ void TradeWindow::action(const gcn::ActionEvent &event) if (event.getId() == "add") { + if(mStatus != PREPARING) + return; + if (!inventoryWindow->isVisible()) return; @@ -281,19 +297,32 @@ void TradeWindow::action(const gcn::ActionEvent &event) else if (event.getId() == "ok") { mMoneyField->setEnabled(false); + mAddButton->setEnabled(false); + mMoneyChangeButton->setEnabled(false); + receivedOk(true); + setStatus(PROPOSING); Net::getTradeHandler()->confirm(); } else if (event.getId() == "trade") { + receivedOk(true); + setStatus(ACCEPTED); Net::getTradeHandler()->finish(); - setStatus(PROPOSING); } else if (event.getId() == "money") { + if(mStatus != PREPARING) + return; + int v = atoi(mMoneyField->getText().c_str()); + int curMoney = player_node->getMoney(); + if(v > curMoney) + { + localChatTab->chatLog(_("You don't have enough money"), BY_SERVER); + v = curMoney; + } Net::getTradeHandler()->setMoney(v); mMoneyField->setText(strprintf("%d", v)); - setStatus(PREPARING); } } diff --git a/src/gui/trade.h b/src/gui/trade.h index 2260dcc1..3c292496 100644 --- a/src/gui/trade.h +++ b/src/gui/trade.h @@ -114,9 +114,10 @@ class TradeWindow : public Window, gcn::ActionListener, gcn::SelectionListener private: enum Status { - PREPARING, /**< Players are adding items. */ - PROPOSING, /**< Local player is proposing a trade. */ - ACCEPTING /**< Distant player is proposing a trade. */ + PREPARING, /**< Players are adding items. (1) */ + PROPOSING, /**< Local player has confirmed the trade. (1) */ + ACCEPTING, /**< Accepting the trade. (2) */ + ACCEPTED /**< Local player has accepted the trade. */ }; /** @@ -134,6 +135,7 @@ class TradeWindow : public Window, gcn::ActionListener, gcn::SelectionListener gcn::Label *mMoneyLabel; gcn::Button *mAddButton; gcn::Button *mOkButton; + gcn::Button *mMoneyChangeButton; gcn::TextField *mMoneyField; Status mStatus; diff --git a/src/net/tmwserv/gameserver/player.cpp b/src/net/tmwserv/gameserver/player.cpp index a3e1458c..3f05c954 100644 --- a/src/net/tmwserv/gameserver/player.cpp +++ b/src/net/tmwserv/gameserver/player.cpp @@ -81,7 +81,7 @@ void Net::GameServer::Player::requestTrade(int id) void Net::GameServer::Player::acceptTrade(bool accept) { - MessageOut msg(accept ? PGMSG_TRADE_ACCEPT : PGMSG_TRADE_CANCEL); + MessageOut msg(accept ? PGMSG_TRADE_REQUEST : PGMSG_TRADE_CANCEL); Net::GameServer::connection->send(msg); } diff --git a/src/net/tmwserv/protocol.h b/src/net/tmwserv/protocol.h index 7eb0b931..57926611 100644 --- a/src/net/tmwserv/protocol.h +++ b/src/net/tmwserv/protocol.h @@ -129,12 +129,15 @@ enum { GPMSG_TRADE_COMPLETE = 0x02C3, // - PGMSG_TRADE_CANCEL = 0x02C4, // - GPMSG_TRADE_CANCEL = 0x02C5, // - - PGMSG_TRADE_ACCEPT = 0x02C6, // - - GPMSG_TRADE_ACCEPT = 0x02C7, // - - PGMSG_TRADE_ADD_ITEM = 0x02C8, // B slot, B amount - GPMSG_TRADE_ADD_ITEM = 0x02C9, // W item id, B amount - PGMSG_TRADE_SET_MONEY = 0x02CA, // L amount - GPMSG_TRADE_SET_MONEY = 0x02CB, // L amount + PGMSG_TRADE_AGREED = 0x02C6, // - + GPMSG_TRADE_AGREED = 0x02C7, // - + PGMSG_TRADE_CONFIRM = 0x02C8, // - + GPMSG_TRADE_CONFIRM = 0x02C9, // - + PGMSG_TRADE_ADD_ITEM = 0x02CA, // B slot, B amount + GPMSG_TRADE_ADD_ITEM = 0x02CB, // W item id, B amount + PGMSG_TRADE_SET_MONEY = 0x02CC, // L amount + GPMSG_TRADE_SET_MONEY = 0x02CD, // L amount + GPMSG_TRADE_BOTH_CONFIRM = 0x02CE, // - PGMSG_USE_ITEM = 0x0300, // B slot GPMSG_USE_RESPONSE = 0x0301, // B error GPMSG_BEINGS_DAMAGE = 0x0310, // { W being id, W amount }* diff --git a/src/net/tmwserv/tradehandler.cpp b/src/net/tmwserv/tradehandler.cpp index 2b10bac1..ae190c96 100644 --- a/src/net/tmwserv/tradehandler.cpp +++ b/src/net/tmwserv/tradehandler.cpp @@ -71,13 +71,16 @@ TradeHandler::TradeHandler(): GPMSG_TRADE_CANCEL, GPMSG_TRADE_START, GPMSG_TRADE_COMPLETE, - GPMSG_TRADE_ACCEPT, + GPMSG_TRADE_AGREED, + GPMSG_TRADE_BOTH_CONFIRM, + GPMSG_TRADE_CONFIRM, GPMSG_TRADE_ADD_ITEM, GPMSG_TRADE_SET_MONEY, 0 }; handledMessages = _messages; tradeHandler = this; + } void TradeHandler::setAcceptTradeRequests(bool acceptTradeRequests) @@ -127,10 +130,14 @@ void TradeHandler::handleMessage(MessageIn &msg) tradeWindow->setVisible(true); break; - case GPMSG_TRADE_ACCEPT: + case GPMSG_TRADE_BOTH_CONFIRM: tradeWindow->receivedOk(false); break; + case GPMSG_TRADE_AGREED: + tradeWindow->receivedOk(false); + break; + case GPMSG_TRADE_CANCEL: localChatTab->chatLog("Trade canceled.", BY_SERVER); tradeWindow->setVisible(false); @@ -187,12 +194,13 @@ void TradeHandler::setMoney(int amount) void TradeHandler::confirm() { - // TODO + MessageOut msg(PGMSG_TRADE_CONFIRM); + Net::GameServer::connection->send(msg); } void TradeHandler::finish() { - MessageOut msg(PGMSG_TRADE_ACCEPT); + MessageOut msg(PGMSG_TRADE_AGREED); Net::GameServer::connection->send(msg); } |