⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trademanager.cpp

📁 天之炼狱1服务器端源文件游戏服务端不完整
💻 CPP
📖 第 1 页 / 共 2 页
字号:
////////////////////////////////////////////////////////////////////////////////// Filename    : TradeManager.cpp// Written by  : 辫己刮// Description :  ////////////////////////////////////////////////////////////////////////////////#include "TradeManager.h"#include "Creature.h"#include "Slayer.h"#include "Vampire.h"#include "Ousters.h"#include "Item.h"#include "ItemUtil.h"#include "Inventory.h"#include "Player.h"#include "ItemMap.h"#include "DB.h"#include "LogClient.h"#include "VSDateTime.h"#include "FlagSet.h"#include "EventItemUtil.h"#include "GamePlayer.h"#include "VariableManager.h"#include "Gpackets/GCTradeFinish.h"////////////////////////////////////////////////////////////////////////////////// class TradeInfo member methods////////////////////////////////////////////////////////////////////////////////TradeInfo::TradeInfo()	throw (){	__BEGIN_TRY		m_Gold      = 0;	m_Status    = TRADE_TRADING;	Timeval currentTime;	getCurrentTime(currentTime);	m_LastOKTime = currentTime;	__END_CATCH}TradeInfo::~TradeInfo()	throw (){	__BEGIN_TRY		m_ItemList.clear();	__END_CATCH}bool TradeInfo::hasItem(Item* pItem)	throw (){	__BEGIN_TRY	list<Item*>::const_iterator itr = m_ItemList.begin();	for (; itr != m_ItemList.end(); itr++)	{		if (pItem == (*itr)) return true;	}	return false;	__END_CATCH}bool TradeInfo::addItem(Item* pItem)	throw (){	__BEGIN_TRY	if (hasItem(pItem)) return false;	m_ItemList.push_back(pItem);	return true;	__END_CATCH}bool TradeInfo::removeItem(Item* pItem)	throw (){	__BEGIN_TRY		list<Item*>::iterator itr = m_ItemList.begin();	for (; itr != m_ItemList.end(); itr++)	{		if (pItem == (*itr))		{			m_ItemList.erase(itr);			return true;		}	}	return false;	__END_CATCH}void TradeInfo::clearAll(void)	throw(){	__BEGIN_TRY	m_ItemList.clear();	m_Gold   = 0;	m_Status = TRADE_TRADING;	__END_CATCH}void TradeInfo::setNextTime(const Timeval& currentTime){	m_LastOKTime.tv_sec = (currentTime.tv_sec + 4);	m_LastOKTime.tv_usec = currentTime.tv_usec;}bool TradeInfo::isValidOKTime(const Timeval& currentTime){	if (m_LastOKTime < currentTime) return true;	return false;}////////////////////////////////////////////////////////////////////////////////// class TradeManager member methods////////////////////////////////////////////////////////////////////////////////TradeManager::TradeManager()	throw (){	__BEGIN_TRY	__END_CATCH}TradeManager::~TradeManager()	throw (){	__BEGIN_TRY	hash_map<string, TradeInfo*>::iterator itr = m_InfoMap.begin();	for (; itr != m_InfoMap.end(); itr++)	{		TradeInfo* pInfo = itr->second;		SAFE_DELETE(pInfo);	}	m_InfoMap.clear();		__END_CATCH}void TradeManager::init()	throw (){	__BEGIN_TRY	__END_CATCH}bool TradeManager::hasTradeInfo(const string & Name)	throw (){	__BEGIN_TRY	hash_map<string, TradeInfo*>::iterator itr = m_InfoMap.find(Name);	if (itr == m_InfoMap.end()) return false;	return true;	__END_CATCH}TradeInfo* TradeManager::getTradeInfo(const string & Name)	throw (){	__BEGIN_TRY	hash_map<string, TradeInfo*>::iterator itr = m_InfoMap.find(Name);	if (itr == m_InfoMap.end()) return NULL;	return itr->second;	__END_CATCH}void TradeManager::addTradeInfo(TradeInfo* pInfo)	throw (DuplicatedException){	__BEGIN_TRY	Assert(pInfo != NULL);	hash_map<string, TradeInfo*>::iterator itr = m_InfoMap.find(pInfo->getMainName());	if (itr != m_InfoMap.end()) throw DuplicatedException();	m_InfoMap[pInfo->getMainName()] = pInfo;			__END_CATCH}void TradeManager::removeTradeInfo(const string & Name)	throw (NoSuchElementException){	__BEGIN_TRY		hash_map<string, TradeInfo*>::iterator itr = m_InfoMap.find(Name);	if (itr == m_InfoMap.end())	{		cerr << "TradeManager::removeTradeInfo() : NoSuchElementException" << endl;		// 狼固 绝绰芭 鞍酒辑 公矫茄促.		// by sigi. 2002.8.31		//throw NoSuchElementException();		return;	}	TradeInfo* pInfo = itr->second;	m_InfoMap.erase(itr);	SAFE_DELETE(pInfo);	__END_CATCH}void TradeManager::initTrade(Creature* pCreature1, Creature* pCreature2)	throw (){	__BEGIN_TRY	// 笛 吝俊 窍唱扼档 背券 沥焊啊 粮犁窍搁 帮鄂窍促.	if (hasTradeInfo(pCreature1->getName()) || 		hasTradeInfo(pCreature2->getName()))	{		throw ("TradeManager::initTrade() : Trade info already exist!");	}	TradeInfo* pInfo1 = new TradeInfo();	pInfo1->setMainName(pCreature1->getName());	pInfo1->setTargetName(pCreature2->getName());	pInfo1->setStatus(TRADE_TRADING);	addTradeInfo(pInfo1);	TradeInfo* pInfo2 = new TradeInfo();	pInfo2->setMainName(pCreature2->getName());	pInfo2->setTargetName(pCreature1->getName());	pInfo2->setStatus(TRADE_TRADING);	addTradeInfo(pInfo2);	__END_CATCH}int TradeManager::canTrade(Creature* pCreature1, Creature* pCreature2)	throw (){	__BEGIN_TRY	try {	// 器牢磐啊 澄捞搁 帮鄂茄促.	if (pCreature1 == NULL || pCreature2 == NULL) return 0;	// 荤恩尝府 背券阑 秦具父 茄促.	if (!pCreature1->isPC() || !pCreature2->isPC()) return 0;	// 促弗 辆练尝府绰 背券且 荐 绝促.	if (!isSameRace(pCreature1, pCreature2)) return 0;	// 背券 沥焊啊 积己登绢 乐瘤 臼促搁 背券且 荐 绝促.	if (!isTrading(pCreature1, pCreature2)) return 0;	TradeInfo* pInfo1 = getTradeInfo(pCreature1->getName());	TradeInfo* pInfo2 = getTradeInfo(pCreature2->getName());	if (pInfo1==NULL || pInfo2==NULL)	// by sigi. 2002.12.25		return 0;	// 笛 吝俊 窍唱扼档 背券阑 倾遏窍绊 乐瘤 臼促搁 背券且 荐 绝促.	if (pInfo1->getStatus() != TRADE_FINISH || pInfo2->getStatus() != TRADE_FINISH) 		return 0;	// 鞘夸茄 函荐甸阑 霖厚茄促.	list<Item*>      tradeList1  = pInfo1->getItemList();	list<Item*>      tradeList2  = pInfo2->getItemList();	ItemMap          itemMap1;	ItemMap          itemMap2;	Inventory*       pInventory1 = NULL;	Inventory*       pInventory2 = NULL;	bool             bTradeGiftBox = false;	int				 EventGiftBoxCount = 0;	if (pCreature1->isSlayer() && pCreature2->isSlayer())	{		Slayer* pSlayer1 = dynamic_cast<Slayer*>(pCreature1);		Slayer* pSlayer2 = dynamic_cast<Slayer*>(pCreature2);		pInventory1 = new Inventory(pSlayer1->getInventory());		pInventory2 = new Inventory(pSlayer2->getInventory());		pInventory1->setDeleteAllFlag(false);		pInventory2->setDeleteAllFlag(false);	}	else if (pCreature1->isVampire() && pCreature2->isVampire())	{		Vampire* pVampire1 = dynamic_cast<Vampire*>(pCreature1);		Vampire* pVampire2 = dynamic_cast<Vampire*>(pCreature2);		pInventory1 = new Inventory(pVampire1->getInventory());		pInventory2 = new Inventory(pVampire2->getInventory());		pInventory1->setDeleteAllFlag(false);		pInventory2->setDeleteAllFlag(false);	}	else if (pCreature1->isOusters() && pCreature2->isOusters())	{		Ousters* pOusters1 = dynamic_cast<Ousters*>(pCreature1);		Ousters* pOusters2 = dynamic_cast<Ousters*>(pCreature2);		pInventory1 = new Inventory(pOusters1->getInventory());		pInventory2 = new Inventory(pOusters2->getInventory());		pInventory1->setDeleteAllFlag(false);		pInventory2->setDeleteAllFlag(false);	}	else throw Error("TradeManager::canTrade() : 辆练捞 促福磊唱!");	// throw Error肺 官厕. by sigi. 2002.12.25	// 刚历 阿磊狼 牢亥配府俊辑 背券且 酒捞袍甸阑 力芭茄促.	for (list<Item*>::iterator itr = tradeList1.begin(); itr != tradeList1.end(); itr++)	{		Item* pItem = (*itr);		if (pInventory1->hasItem(pItem->getObjectID()))		{			// 急拱 惑磊 背券 捞亥飘 (急拱 惑磊绰 促弗 酒捞袍苞 窃膊 Trade 瞪 荐 绝促! - 秒家促			if ( pItem->getItemClass() == Item::ITEM_CLASS_EVENT_GIFT_BOX				 && pItem->getItemType() > 1 && pItem->getItemType() < 6 )			{				/*				if ( tradeList1.size() != 1 )					goto ErrorCode;				*/				bTradeGiftBox = true;			}			if ( pItem->getItemClass() == Item::ITEM_CLASS_EVENT_GIFT_BOX					&& pItem->getItemType() >= 16 && pItem->getItemType() <= 18 )			{				EventGiftBoxCount++;			}			pInventory1->deleteItem(pItem->getObjectID());			itemMap1.addItem(pItem);		}		else goto ErrorCode;	}	for (list<Item*>::iterator itr = tradeList2.begin(); itr != tradeList2.end(); itr++)	{		Item* pItem = (*itr);		if (pInventory2->hasItem(pItem->getObjectID()))		{			// 急拱 惑磊 背券 捞亥飘 (急拱 惑磊绰 促弗 酒捞袍苞 窃膊 Trade 瞪 荐 绝促! - 秒家促			if ( pItem->getItemClass() == Item::ITEM_CLASS_EVENT_GIFT_BOX				 && pItem->getItemType() > 1 && pItem->getItemType() < 6 )			{				/*				if ( tradeList2.size() != 1 )					goto ErrorCode;				*/				// 惑措啊 GiftBox 甫 棵府瘤 臼疽促搁 Trade 甫 且 荐 绝促!				if ( !bTradeGiftBox )				{					SAFE_DELETE(pInventory1);					SAFE_DELETE(pInventory2);					return 2;				}				bTradeGiftBox = false;			}			if ( pItem->getItemClass() == Item::ITEM_CLASS_EVENT_GIFT_BOX					&& pItem->getItemType() >= 16 && pItem->getItemType() <= 18 )			{				EventGiftBoxCount--;			}			pInventory2->deleteItem(pItem->getObjectID());			itemMap2.addItem(pItem);		}		else goto ErrorCode;	}	// 茄疙捞扼档 GiftBox 甫 棵府瘤 臼疽栏搁 bTradeGiftBox 啊 true 啊 登辑 倒酒柯促	if ( bTradeGiftBox || EventGiftBoxCount != 0 )	{		SAFE_DELETE(pInventory1);		SAFE_DELETE(pInventory2);		return 2;	}	// 捞力 辑肺狼 牢亥配府俊促 背券且 酒捞袍甸阑 歹秦夯促.	for (ItemMap::iterator itr = itemMap1.begin(); itr != itemMap1.end(); itr++)	{		Item* pItem = itr->second;		// 酒捞袍阑 歹窍带 吝 窍唱扼档 歹且 荐 绝促搁, false甫 府畔茄促.		if (!pInventory2->addItem(pItem)) goto ErrorCode;	}	for (ItemMap::iterator itr = itemMap2.begin(); itr != itemMap2.end(); itr++)	{		Item* pItem = itr->second;		// 酒捞袍阑 歹窍带 吝 窍唱扼档 歹且 荐 绝促搁, false甫 府畔茄促.		if (!pInventory1->addItem(pItem)) goto ErrorCode;	}	SAFE_DELETE(pInventory1);	SAFE_DELETE(pInventory2);	// 促 歹且 荐 乐菌促搁, true甫 府畔茄促.	return 1;ErrorCode:	SAFE_DELETE(pInventory1);	SAFE_DELETE(pInventory2);	return 0;	} catch (Throwable &t) {	// by sigi. 2002.12.25		filelog("tradeError.txt", "C1=%s, C2=%s, %s", pCreature1->getName().c_str(), pCreature2->getName().c_str(), t.toString().c_str());		// trade 阂啊..父 犬牢秦林搁 等促.		return 0;	}	__END_CATCH}void TradeManager::processTrade(Creature* pCreature1, Creature* pCreature2)	throw (){	__BEGIN_TRY	if (!canTrade(pCreature1, pCreature2))	{		throw ("TradeManager::processTrade() : 酒, 揪官. 背券档 救 登绰单, 恐 背券矫虐绰单?");	}	// 鞘夸茄 函荐甸阑 霖厚茄促.	TradeInfo*  pInfo1      = getTradeInfo(pCreature1->getName());	TradeInfo*  pInfo2      = getTradeInfo(pCreature2->getName());	list<Item*> tradeList1  = pInfo1->getItemList();	list<Item*> tradeList2  = pInfo2->getItemList();	ItemMap     itemMap1;	ItemMap     itemMap2;	Gold_t      tradeGold1  = pInfo1->getGold();	Gold_t      tradeGold2  = pInfo2->getGold();	Inventory*  pInventory1 = NULL;	Inventory*  pInventory2 = NULL;	Slayer*     pSlayer1    = NULL;	Slayer*     pSlayer2    = NULL;	Vampire*    pVampire1   = NULL;	Vampire*    pVampire2   = NULL;	Ousters*    pOusters1   = NULL;	Ousters*    pOusters2   = NULL;	PlayerCreature*    pPlayerCreature1  = dynamic_cast<PlayerCreature*>(pCreature1);	PlayerCreature*    pPlayerCreature2  = dynamic_cast<PlayerCreature*>(pCreature2);	bool check1 = pPlayerCreature1->checkDBGold( pPlayerCreature1->getGold() + tradeGold1 );	bool check2 = pPlayerCreature2->checkDBGold( pPlayerCreature2->getGold() + tradeGold2 );	if ( !check1 )	{		filelog( "GoldBug.log", "TradeManager::processTrade : 捣捞 救 嘎嚼聪促. [%s:%s]", pPlayerCreature1->getName().c_str(), pPlayerCreature1->getPlayer()->getID().c_str() );	}	if ( !check2 )	{		filelog( "GoldBug.log", "TradeManager::processTrade : 捣捞 救 嘎嚼聪促. [%s:%s]", pPlayerCreature2->getName().c_str(), pPlayerCreature2->getPlayer()->getID().c_str() );	}	if ( !check1 || !check2 )	{		GamePlayer* pGamePlayer1 = dynamic_cast<GamePlayer*>(pPlayerCreature1->getPlayer());		GamePlayer* pGamePlayer2 = dynamic_cast<GamePlayer*>(pPlayerCreature2->getPlayer());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -