diff options
author | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-08-14 13:38:18 +0000 |
---|---|---|
committer | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-08-14 13:38:18 +0000 |
commit | a88aa0a9af24962f8eea11e039fcf34dade66037 (patch) | |
tree | a894a129129ef3ddefcdbcd9e660461170b7c50e /src | |
parent | 4c7acd2de27714caab933f8fc2d65a3d4a622336 (diff) | |
download | manaserv-a88aa0a9af24962f8eea11e039fcf34dade66037.tar.gz manaserv-a88aa0a9af24962f8eea11e039fcf34dade66037.tar.bz2 manaserv-a88aa0a9af24962f8eea11e039fcf34dade66037.tar.xz manaserv-a88aa0a9af24962f8eea11e039fcf34dade66037.zip |
Involved money in trade handler.
Diffstat (limited to 'src')
-rw-r--r-- | src/defines.h | 4 | ||||
-rw-r--r-- | src/game-server/buysell.cpp | 2 | ||||
-rw-r--r-- | src/game-server/character.cpp | 28 | ||||
-rw-r--r-- | src/game-server/gamehandler.cpp | 4 | ||||
-rw-r--r-- | src/game-server/trade.cpp | 43 | ||||
-rw-r--r-- | src/game-server/trade.hpp | 10 |
6 files changed, 75 insertions, 16 deletions
diff --git a/src/defines.h b/src/defines.h index 2f1fbd57..4db9b081 100644 --- a/src/defines.h +++ b/src/defines.h @@ -184,7 +184,9 @@ enum { GPMSG_TRADE_ACCEPT = 0x02C7, // - PGMSG_TRADE_ADD_ITEM = 0x02C8, // B slot, B amount GPMSG_TRADE_ADD_ITEM = 0x02C9, // W item id, B amount - PGMSG_USE_ITEM = 0x0300, // L item id + PGMSG_TRADE_SET_MONEY = 0x02CA, // L amount + GPMSG_TRADE_SET_MONEY = 0x02CB, // L amount + 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/game-server/buysell.cpp b/src/game-server/buysell.cpp index f249d9c1..9a7309e5 100644 --- a/src/game-server/buysell.cpp +++ b/src/game-server/buysell.cpp @@ -39,7 +39,7 @@ BuySell::BuySell(Character *c, bool sell): BuySell::~BuySell() { - mChar->cancelTransaction(); + mChar->setBuySell(NULL); } void BuySell::cancel() diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp index a1670389..913e05ef 100644 --- a/src/game-server/character.cpp +++ b/src/game-server/character.cpp @@ -156,15 +156,31 @@ BuySell *Character::getBuySell() const void Character::setTrading(Trade *t) { - cancelTransaction(); - mTransactionHandler = t; - mTransaction = TRANS_TRADE; + if (t) + { + cancelTransaction(); + mTransactionHandler = t; + mTransaction = TRANS_TRADE; + } + else + { + assert(mTransaction == TRANS_NONE || mTransaction == TRANS_TRADE); + mTransaction = TRANS_NONE; + } } void Character::setBuySell(BuySell *t) { - cancelTransaction(); - mTransactionHandler = t; - mTransaction = TRANS_BUYSELL; + if (t) + { + cancelTransaction(); + mTransactionHandler = t; + mTransaction = TRANS_BUYSELL; + } + else + { + assert(mTransaction == TRANS_NONE || mTransaction == TRANS_BUYSELL); + mTransaction = TRANS_NONE; + } } diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp index 7e860ab1..54598d8a 100644 --- a/src/game-server/gamehandler.cpp +++ b/src/game-server/gamehandler.cpp @@ -348,6 +348,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) case PGMSG_TRADE_CANCEL: case PGMSG_TRADE_ACCEPT: case PGMSG_TRADE_ADD_ITEM: + case PGMSG_TRADE_SET_MONEY: { Trade *t = computer.character->getTrading(); if (!t) break; @@ -360,6 +361,9 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) case PGMSG_TRADE_ACCEPT : t->accept(computer.character); break; + case PGMSG_TRADE_SET_MONEY: + t->setMoney(computer.character, message.readLong()); + break; case PGMSG_TRADE_ADD_ITEM: int slot = message.readByte(); t->addItem(computer.character, slot, message.readByte()); diff --git a/src/game-server/trade.cpp b/src/game-server/trade.cpp index 3bb25b61..d16fe422 100644 --- a/src/game-server/trade.cpp +++ b/src/game-server/trade.cpp @@ -33,7 +33,7 @@ #include "net/messageout.hpp" Trade::Trade(Character *c1, Character *c2): - mChar1(c1), mChar2(c2), mState(TRADE_INIT) + mChar1(c1), mChar2(c2), mMoney1(0), mMoney2(0), mState(TRADE_INIT) { MessageOut msg(GPMSG_TRADE_REQUEST); msg.writeShort(c1->getPublicID()); @@ -44,8 +44,8 @@ Trade::Trade(Character *c1, Character *c2): Trade::~Trade() { - mChar1->cancelTransaction(); - mChar2->cancelTransaction(); + mChar1->setTrading(NULL); + mChar2->setTrading(NULL); } void Trade::cancel(Character *c) @@ -97,6 +97,7 @@ void Trade::accept(Character *c) { std::swap(mChar1, mChar2); std::swap(mItems1, mItems2); + std::swap(mMoney1, mMoney2); } assert(c == mChar1); // First player agrees. @@ -113,7 +114,10 @@ void Trade::accept(Character *c) } Inventory v1(mChar1, true), v2(mChar2, true); - if (!perform(mItems1, v1, v2) || !perform(mItems2, v2, v1)) + if (!perform(mItems1, v1, v2) || + !perform(mItems2, v2, v1) || + !v1.changeMoney(mMoney2 - mMoney1) || + !v2.changeMoney(mMoney1 - mMoney2)) { v1.cancel(); v2.cancel(); @@ -127,6 +131,33 @@ void Trade::accept(Character *c) delete this; } +void Trade::setMoney(Character *c, int amount) +{ + if (mState == TRADE_INIT || amount < 0) return; + + /* Checking now if there is enough money is useless as it can change + later on. At worst, the transaction will be canceled at the end if + the client lied. */ + + MessageOut msg(GPMSG_TRADE_SET_MONEY); + msg.writeLong(amount); + + if (c == mChar1) + { + mMoney1 = amount; + mChar2->getClient()->send(msg); + } + else + { + assert(c == mChar2); + mMoney2 = amount; + mChar1->getClient()->send(msg); + } + + // Go back to normal run. + mState = TRADE_RUN; +} + void Trade::addItem(Character *c, int slot, int amount) { if (mState == TRADE_INIT) return; @@ -148,12 +179,12 @@ void Trade::addItem(Character *c, int slot, int amount) // Arbitrary limit to prevent a client from DOSing the server. if (items->size() >= 50) return; - Inventory inv(c, true); + Inventory inv(c); int id = inv.getItem(slot); if (id == 0) return; /* Checking now if there is enough items is useless as it can change - later on. At worst, the transaction will be cancelled at the end if + later on. At worst, the transaction will be canceled at the end if the client lied. */ TradedItem ti = { id, slot, amount }; diff --git a/src/game-server/trade.hpp b/src/game-server/trade.hpp index 33662e73..36a9fadf 100644 --- a/src/game-server/trade.hpp +++ b/src/game-server/trade.hpp @@ -64,6 +64,11 @@ class Trade */ void addItem(Character *, int slot, int amount); + /** + * Adds some money to the trade. + */ + void setMoney(Character *, int amount); + private: ~Trade(); @@ -85,9 +90,10 @@ class Trade static bool perform(TradedItems items, Inventory &inv1, Inventory &inv2); - Character *mChar1, *mChar2; /**< Characters involved. */ + Character *mChar1, *mChar2; /**< Characters involved. */ TradedItems mItems1, mItems2; /**< Traded items. */ - TradeState mState; + int mMoney1, mMoney2; /**< Traded money. */ + TradeState mState; /**< State of transaction. */ }; #endif |