Beispiel #1
0
void CExchangeGoods::TheEnd()
{
	if (0 == m_dwOtherPlayerGID)
        return;

	CPlayer *pPlayer = static_cast<CPlayer *>(this);
	if (!pPlayer)
		return;

    CPlayer *pSrcPlayer  = 0;
    CPlayer *pDestPlayer = 0;
    bool isSrc = false;

	// 如果对方是交易目标
	if (m_dwOtherPlayerGID == m_dwDestPlayerGID)
    {
        isSrc = true;
        pSrcPlayer = pPlayer;
        pDestPlayer = (CPlayer *)GetPlayerByGID(m_dwDestPlayerGID)->DynamicCast(IID_PLAYER);
		if (!pDestPlayer || !pDestPlayer->InExchange())
			return;

		if (!pDestPlayer->m_CurHp)
		{
			rfalse(4, 1, "ExchangeGoods.cpp - TheEnd() - !pDestPlayer->m_CurHp");
			return;
		}

		MY_ASSERT(pDestPlayer->m_CurHp);
    }
    else if (m_dwOtherPlayerGID == m_dwSrcPlayerGID)
    {
        pDestPlayer = pPlayer;
        pSrcPlayer = (CPlayer *)GetPlayerByGID(m_dwSrcPlayerGID)->DynamicCast(IID_PLAYER);
		if (!pSrcPlayer || !pSrcPlayer->InExchange())
			return;

		if (!pDestPlayer->m_CurHp)
		{
			rfalse(4, 1, "ExchangeGoods.cpp - TheEnd() - !pSrcPlayer->m_CurHp");
			return;
		}

		MY_ASSERT(pSrcPlayer->m_CurHp);
    }
 
	pSrcPlayer->ExchangeCancel(pDestPlayer, isSrc);
}
Beispiel #2
0
void CExchangeGoods::CommitExchange(CPlayer *pDestPlayer, bool isSrc, DWORD dwVerifyID4CardPoint)
{
	if (!pDestPlayer)
		return;

	CPlayer *pSrcPlayer = static_cast<CPlayer *>(this);
	if (!pSrcPlayer)
		return;

	CPlayer *pPlayer = isSrc ? pSrcPlayer : pDestPlayer;
    SExchangeBox *pExchangeBox = isSrc ? &m_MyBox : &m_YouBox;

	if (!pPlayer || !pExchangeBox)
	{
		rfalse(4, 1, "ExchangeGoods.cpp - CommitExchange() - !pPlayer || !pExchangeBox");
		return;
	}

	// 还没有确认
	if (!(m_MyBox.m_bLocked && m_YouBox.m_bLocked))
	{
		TalkToDnid(pPlayer->m_ClientIndex, "一方或双方没有锁定,无法提交交易");
		return;
	}

	if (pExchangeBox->m_bCommit)
		return;

    pExchangeBox->m_bCommit = true;

	if (!(m_MyBox.m_bCommit && m_YouBox.m_bCommit))
    {
		TalkToDnid(pPlayer->m_ClientIndex, "对方还没有提交,请等待");

		// 通知交易双方,有一个人已经确认了
		SAExchangeOperationMsg opmsg;
        opmsg.dwSrcGID	= pSrcPlayer->GetGID();
        opmsg.dwDestGID = pDestPlayer->GetGID();
        opmsg.isSrc		= isSrc;
        opmsg.operation = SAExchangeOperationMsg::COMMIT;

		g_StoreMessage(pSrcPlayer->m_ClientIndex, &opmsg, sizeof(SAExchangeOperationMsg));
		g_StoreMessage(pDestPlayer->m_ClientIndex, &opmsg, sizeof(SAExchangeOperationMsg));
        return;
    }

	// 双方都已确认,可以开始交易了,检测金钱的合法性
	MY_ASSERT(pSrcPlayer->m_Property.m_Money >= m_MyBox.m_dwMoney);
	MY_ASSERT(pDestPlayer->m_Property.m_Money >= m_YouBox.m_dwMoney);

	if ((pSrcPlayer->m_Property.m_Money < m_MyBox.m_dwMoney) || (pDestPlayer->m_Property.m_Money < m_YouBox.m_dwMoney))
		return;
	
	bool succeed = false;
	// 这里要确认他们至少交换过一个道具,或者一分钱
	if (0 == m_MyBox.m_dwMoney && 0 == m_YouBox.m_dwMoney && !m_MyBox.m_SellNumber && !m_YouBox.m_SellNumber)
		goto goto_finish;
	else
	{
		// 检验交易发起方能否容纳金钱
		DWORD canGet = 0xffffffff - pSrcPlayer->m_Property.m_Money + m_MyBox.m_dwMoney;
		if (canGet < m_YouBox.m_dwMoney)
		{
			TalkToDnid(pSrcPlayer->m_ClientIndex, "您钱包太满了!");
			TalkToDnid(pDestPlayer->m_ClientIndex, "对方钱包太满了!");

			// 交易发起方无法容纳金钱
			pSrcPlayer->ExchangeCancel(pDestPlayer, isSrc);
			return;
		}

		// 检测交易目的方能否容纳金钱
		canGet = 0xffffffff - pDestPlayer->m_Property.m_Money + m_YouBox.m_dwMoney;
		if (canGet < m_MyBox.m_dwMoney)
		{
			TalkToDnid(pSrcPlayer->m_ClientIndex, "对方钱包太满了!");
			TalkToDnid(pDestPlayer->m_ClientIndex, "你钱包太满了!");

			// 交易目的方无法容纳金钱
			pSrcPlayer->ExchangeCancel(pDestPlayer, isSrc);
			return;
		}

		std::list<SAddItemInfo> itemList;

		// 检查交易目的玩家的背包状况
		for (int i=0; i<m_YouBox.m_SellNumber; i++)
			itemList.push_back(SAddItemInfo(m_YouBox.m_GoodsArray[i].wIndex, m_YouBox.m_GoodsArray[i].overlap));

		if (itemList.size() && !pSrcPlayer->CanAddItems(itemList, m_MyBox.m_SellNumber))
		{
			// 交易发起方的背包无法满足交易目的道具
			pSrcPlayer->SendStatusMsg(SANotifyStatus::NS_EXT_SELF_PACKAGE_LACK);
			pDestPlayer->SendStatusMsg(SANotifyStatus::NS_EXT_DEST_PACKAGE_LACK);

			pSrcPlayer->ExchangeCancel(pDestPlayer, isSrc);
			return;
		}
		
		itemList.clear();
		
		for (int i=0; i<m_MyBox.m_SellNumber; i++)
			itemList.push_back(SAddItemInfo(m_MyBox.m_GoodsArray[i].wIndex, m_MyBox.m_GoodsArray[i].overlap));
		
		if (itemList.size() && !pDestPlayer->CanAddItems(itemList, m_YouBox.m_SellNumber))
		{
			// 交易目的的背包无法满足交易目的道具
			pSrcPlayer->SendStatusMsg(SANotifyStatus::NS_EXT_DEST_PACKAGE_LACK);
			pDestPlayer->SendStatusMsg(SANotifyStatus::NS_EXT_SELF_PACKAGE_LACK);

			pSrcPlayer->ExchangeCancel(pDestPlayer, isSrc);
			return;
		}
	
	
		succeed = true; //背包检测成功,位置足够,可以开始交易,首先删除道具
		for (int i=0; i<m_MyBox.m_SellNumber; i++)
		{
			SPackageItem *pItem = pSrcPlayer->FindItemByPos(m_MyBox.m_GoodsArray[i].wCellPos, XYD_FT_ONLYLOCK);
			if (!pItem)
			{
				rfalse(4, 1, "ExchangeGoods.cpp - CommitExchange() - !pItem - 2");
				return;
			}
			MY_ASSERT(pItem);

			pSrcPlayer->DelItem(*pItem, "交易删除", true);
		}
	
		for (int i=0; i<m_YouBox.m_SellNumber; i++)
		{
			SPackageItem *pItem = pDestPlayer->FindItemByPos(m_YouBox.m_GoodsArray[i].wCellPos, XYD_FT_ONLYLOCK);
			if (!pItem)
			{
				rfalse(4, 1, "ExchangeGoods.cpp - CommitExchange() - !pItem");
				return;
			}
			MY_ASSERT(pItem);

			pDestPlayer->DelItem(*pItem, "交易删除", true);
		}
		
		// 给交易发起方添加道具
		for (int i=0; i<m_YouBox.m_SellNumber; i++)
		{
			const SItemBaseData *pData = CItemService::GetInstance().GetItemBaseData(m_YouBox.m_GoodsArray[i].wIndex);
			if (!pData)
			{
				rfalse(4, 1, "ExchangeGoods.cpp - CommitExchange() - !pData");
				return;
			}
			MY_ASSERT(pData);
		
			WORD willCreate = pSrcPlayer->TryPossibleMerge(m_YouBox.m_GoodsArray[i].wIndex, m_YouBox.m_GoodsArray[i].overlap, pData->m_Overlay);
			pSrcPlayer->InsertItem(m_YouBox.m_GoodsArray[i].wIndex, willCreate, pData, m_YouBox.m_GoodsArray[i], false);
		}

		// 给交易目的放添加道具
		for (int i=0; i<m_MyBox.m_SellNumber; i++)
		{
			const SItemBaseData *pData = CItemService::GetInstance().GetItemBaseData(m_MyBox.m_GoodsArray[i].wIndex);
			if (!pData)
			{
				rfalse(4, 1, "ExchangeGoods.cpp - CommitExchange() - !pData - 2");
			}
			MY_ASSERT(pData);

			WORD willCreate = pDestPlayer->TryPossibleMerge(m_MyBox.m_GoodsArray[i].wIndex, m_MyBox.m_GoodsArray[i].overlap, pData->m_Overlay);
			pDestPlayer->InsertItem(m_MyBox.m_GoodsArray[i].wIndex, willCreate, pData, m_MyBox.m_GoodsArray[i], false);
		}

		if (m_MyBox.m_dwMoney || m_YouBox.m_dwMoney)
		{
			pSrcPlayer->m_Property.m_Money  -= m_MyBox.m_dwMoney;
			pDestPlayer->m_Property.m_Money -= m_YouBox.m_dwMoney;

			pSrcPlayer->m_Property.m_Money	+= m_YouBox.m_dwMoney;
			pDestPlayer->m_Property.m_Money += m_MyBox.m_dwMoney;
	
			pSrcPlayer->m_PlayerPropertyStatus[XA_UNBIND_MONEY-XA_MAX_EXP] = true;
			pDestPlayer->m_PlayerPropertyStatus[XA_UNBIND_MONEY-XA_MAX_EXP] = true;
		}
	}
	
goto_finish:
	// 交易完成,清除当前的交易状态
	memset((CExchangeGoods*)pSrcPlayer, 0, sizeof(CExchangeGoods));
	memset((CExchangeGoods*)pDestPlayer, 0, sizeof(CExchangeGoods));

	// 通知客户端交易成功完成,可以关闭交易窗口了
	SAExchangeOperationMsg opmsg;
	opmsg.dwSrcGID	= pSrcPlayer->GetGID();
	opmsg.dwDestGID = pDestPlayer->GetGID();
	opmsg.isSrc		= isSrc;
	opmsg.operation = SAExchangeOperationMsg::SUCCEED;
	g_StoreMessage(pSrcPlayer->m_ClientIndex, &opmsg, sizeof(SAExchangeOperationMsg));
	g_StoreMessage(pDestPlayer->m_ClientIndex, &opmsg, sizeof(SAExchangeOperationMsg));


	//发送交易成功后保存数据
	if (!succeed) return;

	// 保存玩家道具
	SPackageItem *pItems = pSrcPlayer->m_Property.m_BaseGoods;
	memset(pItems, 0, PackageAllCells * sizeof(SPackageItem));
	for (DWORD i = 0; i < PackageAllCells; i++, pItems++)
	{
		SPackageItem *curItem = pSrcPlayer->FindItemByPos(i, XYD_FT_WHATEVER);
		if (curItem)
			*pItems = *curItem;
	}


	SPackageItem *pdestItems = pDestPlayer->m_Property.m_BaseGoods;
	memset(pdestItems, 0, PackageAllCells * sizeof(SPackageItem));
	for (DWORD i = 0; i < PackageAllCells; i++, pdestItems++)
	{
		SPackageItem *curItem = pDestPlayer->FindItemByPos(i, XYD_FT_WHATEVER);
		if (curItem)
			*pdestItems = *curItem;
	}

	SDoubleSavePlayerMsg sdsavemsg;
	dwt::strcpy(sdsavemsg.account[0],pSrcPlayer->GetAccount(),MAX_ACCOUNT);
    dwt::strcpy(sdsavemsg.account[1],pDestPlayer->GetAccount(),MAX_ACCOUNT);
	sdsavemsg.gid[0] = pSrcPlayer->GetGID();
	sdsavemsg.gid[1] = pDestPlayer->GetGID();
	
	SFixProperty *pSreData = dynamic_cast<SFixProperty *>(&pSrcPlayer->m_Property);
	SFixProperty *pDestData =dynamic_cast<SFixProperty *>(&pDestPlayer->m_Property);
	memcpy(&sdsavemsg.data[0],pSreData,sizeof(SFixProperty));
	memcpy(&sdsavemsg.data[1],pDestData,sizeof(SFixProperty));
	int num = SectionMessageManager::getInstance().evaluateDevidedAmount(sizeof(sdsavemsg));
	int id = 0;
	BOOL ret;
	for (int i = 0;i < num;i++)
	{
		SSectionMsg sMsg;
		id = SectionMessageManager::getInstance().devideMessage(i,num,&sMsg,&sdsavemsg,sizeof(sdsavemsg),id);
		ret = SendToLoginServer(&sMsg,sizeof(SSectionMsg));
	}
	//SectionMessageManager::getInstance().
	//SendToLoginServer(&sdsavemsg,sizeof(SDoubleSavePlayerMsg));
//	g_StoreMessage(pSrcPlayer->m_ClientIndex,&sdsavemsg,sizeof(SDoubleSavePlayerMsg));
	

	return;
}
Beispiel #3
0
void CExchangeGoods::RecvExchangeOperationMsg(struct SQExchangeOperationMsg *pMsg)
{
	if (!pMsg)
	{
		rfalse(4, 1, "ExchangeGoods.cpp - RecvExchangeOperationMsg() - !pMsg");
		return;
	}

	// 交易中的操作(锁定、确认、取消)
	CPlayer *pPlayer = static_cast<CPlayer *>(this);
	if (!pPlayer)
        return;

	CPlayer *pSrcPlayer  = 0;
    CPlayer *pDestPlayer = 0;

	bool isSrc = false;

	// 如果交易已经结束或者本身就没开始
	if (!pPlayer->InExchange())
		return;

	// 找到两个交易人
    if (pPlayer->GetGID() == pMsg->dwSrcGID)
    {
		isSrc = true;
        pSrcPlayer = pPlayer;
        pDestPlayer = (CPlayer *)GetPlayerByGID(pMsg->dwDestGID)->DynamicCast(IID_PLAYER);
		if (!pDestPlayer || !pDestPlayer->InExchange())
			return;
    }
    else if (pPlayer->GetGID() == pMsg->dwDestGID)
    {
        pSrcPlayer = (CPlayer *)GetPlayerByGID(pMsg->dwSrcGID)->DynamicCast(IID_PLAYER);
        pDestPlayer = pPlayer;
		if (!pSrcPlayer || !pSrcPlayer->InExchange())
			return;
    }

    // 判断是否是确定的交易双方
    if ((pSrcPlayer->m_dwSrcPlayerGID   != pSrcPlayer->GetGID())  ||
        (pSrcPlayer->m_dwDestPlayerGID  != pDestPlayer->GetGID()) ||
        (pSrcPlayer->m_dwOtherPlayerGID != pDestPlayer->GetGID()))
        return;

	MY_ASSERT(pSrcPlayer->m_CurHp && pDestPlayer->m_CurHp);

	// 进入具体交易部分,所有的交易都由交易发起人来处理
	if (pMsg->operation == SQExchangeOperationMsg::LOCK || pMsg->operation == SQExchangeOperationMsg::UNLOCK)
	{
		// 有一方打算锁定/解锁定
		pSrcPlayer->LockExchange(pDestPlayer, isSrc, pMsg->operation == SQExchangeOperationMsg::LOCK ? true : false);
	}
    else if (pMsg->operation == SQExchangeOperationMsg::COMMIT)
	{
		// 有一方打算交易确认
        pSrcPlayer->CommitExchange(pDestPlayer, isSrc);
	}
	else if (pMsg->operation == SQExchangeOperationMsg::CANCEL)
	{
		// 有一方打算取消交易
		pSrcPlayer->ExchangeCancel(pDestPlayer, isSrc);
	}
}