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/game-server/trade.cpp | |
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/game-server/trade.cpp')
-rw-r--r-- | src/game-server/trade.cpp | 43 |
1 files changed, 37 insertions, 6 deletions
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 }; |