From 336ce321a9b996e56a61a5364bb32124efa84dd9 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sun, 13 Oct 2013 14:45:28 +0200 Subject: Added GPMSG_NPC_BUYSELL_RESPONSE Without this message it is not possible to reliably know how many items were traded with an NPC at the client side. It helps with updating the shop's inventory. --- src/common/manaserv_protocol.h | 1 + src/game-server/buysell.cpp | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/common/manaserv_protocol.h b/src/common/manaserv_protocol.h index 9d562037..0382efb5 100644 --- a/src/common/manaserv_protocol.h +++ b/src/common/manaserv_protocol.h @@ -172,6 +172,7 @@ enum { PGMSG_NPC_STRING = 0x02D4, // W being id, S string GPMSG_NPC_NUMBER = 0x02D5, // W being id, D max, D min, D default GPMSG_NPC_STRING = 0x02D6, // W being id + GPMSG_NPC_BUYSELL_RESPONSE = 0x02D7, // B error, W item id, W amount PGMSG_TRADE_REQUEST = 0x02E0, // W being id GPMSG_TRADE_REQUEST = 0x02E1, // W being id GPMSG_TRADE_START = 0x02E2, // - diff --git a/src/game-server/buysell.cpp b/src/game-server/buysell.cpp index 97f97c85..27b0437b 100644 --- a/src/game-server/buysell.cpp +++ b/src/game-server/buysell.cpp @@ -155,33 +155,36 @@ bool BuySell::start(Entity *actor) void BuySell::perform(unsigned id, int amount) { - Inventory inv(mChar); + MessageOut msg(GPMSG_NPC_BUYSELL_RESPONSE); + for (TradedItems::iterator i = mItems.begin(), i_end = mItems.end(); i != i_end; ++i) { - auto *beingComponent = mChar->getComponent(); - if (i->itemId != id) continue; + + Inventory inv(mChar); + + auto *beingComponent = mChar->getComponent(); + const double currentMoney = beingComponent->getAttributeBase(mCurrency); + if (i->amount && i->amount <= amount) amount = i->amount; + if (mSell) { amount -= inv.remove(id, amount); - const double currentMoney = - beingComponent->getAttributeBase(mCurrency); beingComponent->setAttribute(*mChar, mCurrency, currentMoney + amount * i->cost); } else { - const double currentMoney = - beingComponent->getAttributeBase(mCurrency); amount = std::min(amount, ((int)currentMoney) / i->cost); amount -= inv.insert(id, amount); beingComponent->setAttribute(*mChar, mCurrency, currentMoney - amount * i->cost); } + if (i->amount) { i->amount -= amount; @@ -190,6 +193,14 @@ void BuySell::perform(unsigned id, int amount) mItems.erase(i); } } + + msg.writeInt8(ERRMSG_OK); + msg.writeInt16(id); + msg.writeInt16(amount); + mChar->getComponent()->getClient()->send(msg); return; } + + msg.writeInt8(ERRMSG_FAILURE); + mChar->getComponent()->getClient()->send(msg); } -- cgit v1.2.3-70-g09d2