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

📄 streetstallmanager.cpp

📁 墨香最新私服
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "stdafx.h"
#include "StreetStall.h"
#include "StreetStallManager.h"
#include "usertable.h"
#include "PackedData.h"
#include "ObjectStateManager.h"
#include "mapdbmsgparser.h"
#include "Player.h"
#include "itemmanager.h"
#include "CheckRoutine.h"

GLOBALTON(cStreetStallManager)

cStreetStallManager::cStreetStallManager()
{
	m_mpStreetStall = new CMemoryPoolTempl<cStreetStall>;
	m_mpStreetStall->Init( 100, 50, "StreetStallPool" );

	m_StallTable.Initialize(128);
}

cStreetStallManager::~cStreetStallManager()
{
	cStreetStall* pStall;
	m_StallTable.SetPositionHead();
	while(pStall = m_StallTable.GetData())
	{
		m_mpStreetStall->Free(pStall);
	}
	m_StallTable.RemoveAll();

	SAFE_DELETE(m_mpStreetStall);
}


cStreetStall* cStreetStallManager::CreateStreetStall( CPlayer* pOwner, char* title )
{
	// µ¿ÀÏ Ç÷¹À̾ ¸¸µç »óÁ¡ÀÌ Á¸ÀçÇÏ´ÂÁö È®ÀÎÇØ¾ßÇϳª ??
	ASSERT( !m_StallTable.GetData(pOwner->GetID()) );

	cStreetStall* pStall = m_mpStreetStall->Alloc();
	pStall->Init();

	ASSERT( pStall );
	if( pStall == NULL )
		return NULL;

	pStall->SetOwner( pOwner );
	pOwner->SetStreetStallTitle( title );
	
	m_StallTable.Add(pStall,pOwner->GetID());

	return pStall;
}

void cStreetStallManager::DeleteStreetStall( CPlayer* pOwner )
{
	ASSERT(m_StallTable.GetData(pOwner->GetID()));
	cStreetStall* pStall = m_StallTable.GetData(pOwner->GetID());
	
	if( pStall )
	{
		pStall->EmptyCellAll();
		pStall->DeleteGuestAll();
		m_mpStreetStall->Free( pStall );
		m_StallTable.Remove(pOwner->GetID());
	}
}


BOOL cStreetStallManager::IsExist( cStreetStall* pStall )
{
	if( m_StallTable.GetData(pStall->GetOwner()->GetID()) == NULL ) 
		return FALSE;

	return TRUE;
}
/*
BOOL cStreetStallManager::GuestIn( CPlayer* pOwner, CPlayer* pGuest )
{
	cStreetStall* pStall = m_StallTable.GetData(pOwner->GetID());
	if( pStall )
	{
		pStall->AddGuest( pGuest );
		return TRUE;
	}

	return FALSE;
}

void cStreetStallManager::GuestOut( DWORD OwnerId, CPlayer* pGuest )
{
	CPlayer* pOwner = (CPlayer*)g_pUserTable->FindUser( OwnerId ); // ¿À³Ê°¡ ÀÖ´ÂÁö ¹°¾î º»´Ù.
	if( pOwner == NULL ) return;

	cStreetStall* pStall = m_StallTable.GetData(OwnerId);
	if( pStall )
		pStall->DeleteGuest( pGuest );
}
*/
cStreetStall* cStreetStallManager::FindStreetStall( CPlayer* pOwner )
{
	return m_StallTable.GetData(pOwner->GetID());
}


void cStreetStallManager::StreetStallMode( CPlayer* pPlayer, char* title, BOOL bMode )
{
	MSG_STREETSTALL_TITLE msg;
	msg.Category = MP_STREETSTALL;
	msg.dwObjectID = pPlayer->GetID();

	if(bMode ==  TRUE)
	{
		OBJECTSTATEMGR_OBJ->StartObjectState(pPlayer, eObjectState_StreetStall_Owner, 0);
		msg.Protocol = MP_STREETSTALL_START;
		//strcpy( msg.Title, title );
		SafeStrCpy( msg.Title, title, MAX_STREETSTALL_TITLELEN );
	}
	else
	{
		OBJECTSTATEMGR_OBJ->EndObjectState(pPlayer, eObjectState_StreetStall_Owner, 0);
		msg.Protocol = MP_STREETSTALL_END;
	}

	PACKEDDATA_OBJ->QuickSendExceptObjectSelf(pPlayer,&msg,sizeof(msg));
}

void cStreetStallManager::UserLogOut( CPlayer* pPlayer )
{
	cStreetStall* pGestStall = pPlayer->GetGuestStall();
	cStreetStall* pOwnerStall = FindStreetStall( pPlayer );

//find
//m_StallTable.GetData(pOwner->GetID());
//m_StallTable.GetData(pStall->GetOwner()->GetID()

	if( pGestStall ) // »óÁ¡¿¡ ¼Õ´ÔÀ¸·Î Âü°¡ÇÑ °æ¿ì...
	{
		if( IsExist( pGestStall ) )
			pGestStall->DeleteGuest( pPlayer ); // ¼Õ´ÔÀ¸·Î ÀÖ´Â »óÁ¡¿¡¼­ ³ª¿Â´Ù.

		MSGBASE msg;
		msg.Category = MP_STREETSTALL;
		msg.Protocol = MP_STREETSTALL_CLOSE;
		msg.dwObjectID = pPlayer->GetID();
		pPlayer->SendMsg(&msg, sizeof(msg));
		return;
	}
	
	if( pOwnerStall ) // ¿î¿µÁßÀÎ »óÁ¡ÀÌ ÀÖ´Â °æ¿ì...
	{
		// ³ëÁ¡¸ðµåÁ¾·á¸¦ ¾Ë¸°´Ù.(ÀÚ±âÀÚ½ÅÀÇ »óź¯È­)
		MSG_STREETSTALL_TITLE msg;
		msg.Category = MP_STREETSTALL;
		msg.Protocol = MP_STREETSTALL_END;
		msg.dwObjectID = pPlayer->GetID();
		pPlayer->SendMsg(&msg, sizeof(msg));

		// ¼Õ´Ôµé¿¡°Ô ³ëÁ¡ÀÌ Á¾·áµÇ¾úÀ½À» ¾Ë¸°´Ù.
		MSGBASE CloseSyn;
		CloseSyn.Category = MP_STREETSTALL;
		CloseSyn.Protocol = MP_STREETSTALL_CLOSE;
		CloseSyn.dwObjectID = pPlayer->GetID();
		pOwnerStall->SendMsgGuestAll( &CloseSyn, sizeof(MSGBASE), TRUE ); 
		StreetStallMode( pPlayer, NULL, FALSE );

		DeleteStreetStall(pPlayer);

		return;
	}
}

BOOL cStreetStallManager::BuyItem( CPlayer* pOwner, cStreetStall* pStall, CPlayer* pGuest, STREETSTALL_BUYINFO* pBuyInfo )
{
	POSTYPE pos = pBuyInfo->StallPos;		// ³ëÁ¡¿¡¼­ÀÇ À§Ä¡
	sCELLINFO* pItemInfo = pStall->GetCellInfo( pos );
	ITEMBASE ItemBase = pItemInfo->sItemBase;
	POSTYPE RealPos = ItemBase.Position;	// ½ÇÁ¦ ¾ÆÀÌÅÛ Æ÷Áö¼Ç
	MONEYTYPE money = pItemInfo->dwMoney;

	CInventorySlot* pGuestInventory = (CInventorySlot*)pGuest->GetSlot( eItemTable_Inventory );
	CInventorySlot* pOwnerInventory = (CInventorySlot*)pOwner->GetSlot( eItemTable_Inventory );
	MSG_WORD		msgNack; // ¿À·ù ¸Þ¼¼Áö
	MSG_LINKITEM	DelSyn; // ³ëÁ¡»ó¿¡¼­ÀÇ »èÁ¦ ¸Þ¼¼Áö
	MSG_SELLITEM	sellMsg; // ÁÖÀο¡°Ô º¸³»´Â ¸Þ¼¼Áö
	MSG_ITEMEX		buyMsg; // ¼Õ´Ô¿¡°Ô º¸³»´Â ¸Þ¼¼Áö
	POSTYPE absPosOut;
	WORD EmptyCellPos[1];
	const ITEMBASE* pInvenItem;

	if(CanBuyItem(pOwner, pGuest, pItemInfo, EmptyCellPos, pBuyInfo, &ItemBase) == FALSE)
		goto ITEMBUY_FAILED;

	absPosOut = EmptyCellPos[0]; // »õ·Î ä¿ö ³ÖÀ» °÷

	pInvenItem = pOwnerInventory->GetItemInfoAbs(pItemInfo->sItemBase.Position);
	
	if(!CHKRT->ItemOf(pOwner, pInvenItem->Position, pInvenItem->wIconIdx,0,0,CB_EXIST|CB_ICONIDX))
	{
		goto ITEMBUY_FAILED;
	}

	if(!CHKRT->ItemOf(pGuest, absPosOut,0,0,0,CB_ICONIDX))
	{
		goto ITEMBUY_FAILED;
	}	

	ITEMBASE temp;
	if(pOwnerInventory->DeleteItemAbs(pOwner, RealPos, &temp, SS_LOCKOMIT) != EI_TRUE)
		goto ITEMBUY_FAILED;
	
	temp.Position = absPosOut;
	if(pGuestInventory->InsertItemAbs(pGuest, absPosOut, &temp, SS_LOCKOMIT) !=  EI_TRUE)
	{	
		//rollback
		temp.Position = RealPos;
		if(pOwnerInventory->InsertItemAbs(pOwner, RealPos, &temp) != EI_TRUE)
		{
			ASSERT(0);
			goto ITEMBUY_FAILED;
		}
		goto ITEMBUY_FAILED;
	}
	// ¿É¼Ç Á¤º¸ Ãß°¡ ¹× »èÁ¦
	if( ITEMMGR->IsOptionItem(ItemBase.wIconIdx, ItemBase.Durability ) )
	{
		pGuest->AddItemOption(pOwner->GetItemOption(ItemBase.Durability));
		pOwner->RemoveItemOption(ItemBase.Durability);
	}

	pOwner->SetMoney( pItemInfo->dwMoney, MONEY_ADDITION, 0, eItemTable_Inventory, eMoneyLog_GetStreetStall, pGuest->GetID());
	pGuest->SetMoney( pItemInfo->dwMoney, MONEY_SUBTRACTION, 0, eItemTable_Inventory, eMoneyLog_LoseStreetStall, pOwner->GetID());

	ItemUpdateToDB( pGuest->GetID(), ItemBase.dwDBIdx ,ItemBase.wIconIdx, ItemBase.Durability, absPosOut, 0 );

	// Log ÁÖÀÎ
	LogItemMoney(pOwner->GetID(), pOwner->GetObjectName(), pGuest->GetID(), pGuest->GetObjectName(),
		eLog_StreetStallBuyAll, pOwner->GetMoney(), pGuest->GetMoney(), pItemInfo->dwMoney,
		temp.wIconIdx, temp.dwDBIdx, 0, temp.Position, temp.Durability, pOwner->GetPlayerExpPoint());

	// ³ëÁ¡»ó¿¡¼­ »èÁ¦ÇÑ´Ù.
	pStall->EmptyCell( &ItemBase );
	
	DelSyn.Category = MP_STREETSTALL;
	DelSyn.Protocol = MP_STREETSTALL_DELETEITEM;
	DelSyn.dwObjectID = pBuyInfo->dwObjectID;
	DelSyn.ItemInfo = ItemBase;
	DelSyn.wAbsPosition = pos;

	pStall->SendMsgGuestAll( &DelSyn, sizeof(DelSyn) );

	//////////////////////////////////////////////////////
	// ÁÖÀο¡°Ô¼­ ¾ÆÀÌÅÛÀ» »©°í µ·À» ³Ö¾î ÁØ´Ù./////////////////
	sellMsg.Category = MP_STREETSTALL;
	sellMsg.Protocol = MP_STREETSTALL_SELLITEM;
	sellMsg.dwObjectID = pBuyInfo->StallOwnerID;
	sellMsg.ItemInfo = ItemBase;
	sellMsg.count = 0;
	sellMsg.dwData = pBuyInfo->dwObjectID;

	pOwner->SendMsg( &sellMsg, sizeof(sellMsg) );
	/////////////////////////////////////////////////////////////

	ItemBase.Position = absPosOut;
	
	// ¼Õ´Ô¿¡°Ô ¾ÆÀÌÅÛÀ» ÷°¡ÇÏ°í µ·À» »«´Ù.////////////////////
	buyMsg.Category = MP_STREETSTALL;
	buyMsg.Protocol = MP_STREETSTALL_BUYITEM_ACK;
	buyMsg.dwObjectID = pBuyInfo->dwObjectID;
	buyMsg.ItemInfo = ItemBase;
	buyMsg.count = 0;

	if( ITEMMGR->IsOptionItem(ItemBase.wIconIdx, ItemBase.Durability) )
	{
		buyMsg.sOptionInfo = *(pGuest->GetItemOption(ItemBase.Durability));
		buyMsg.count = 1;
	}

	pGuest->SendMsg( &buyMsg, buyMsg.GetSize() );
	/////////////////////////////////////////////////////////////

	return TRUE;

ITEMBUY_FAILED:
	msgNack.Category	= MP_STREETSTALL;
	msgNack.Protocol	= MP_STREETSTALL_BUYITEM_NACK;
	msgNack.dwObjectID	= pBuyInfo->dwObjectID;
	msgNack.wData		= 0;
	pGuest->SendMsg( &msgNack, sizeof(msgNack) );

	return FALSE;
}

BOOL cStreetStallManager::BuyDupItem( CPlayer* pOwner, cStreetStall* pStall, CPlayer* pGuest, STREETSTALL_BUYINFO* pBuyInfo )
{
	POSTYPE pos = pBuyInfo->StallPos;	// ³ëÁ¡Ã¢¿¡¼­ÀÇ À§Ä¡
	sCELLINFO* pItemInfo = pStall->GetCellInfo( pos );
	ITEMBASE ItemBase = pItemInfo->sItemBase;
	POSTYPE RealPos = ItemBase.Position; // ½ÇÁ¦ ¾ÆÀÌÅÛ Æ÷Áö¼Ç
	WORD wBuyNum = pBuyInfo->ItemInfo.Durability;
	MONEYTYPE money = pItemInfo->dwMoney*wBuyNum;
	
	CInventorySlot* pGuestInventory = (CInventorySlot *)pGuest->GetSlot( eItemTable_Inventory );
	CInventorySlot* pOwnerInventory = (CInventorySlot *)pOwner->GetSlot( eItemTable_Inventory );
		
	WORD rt = NOT_ERROR;
	POSTYPE absPosOut;
	WORD EmptyCellPos[1];
	MSG_WORD msgNack; // ¿À·ù ¸Þ¼¼Áö
	MSG_LINKITEM DelSyn; // ³ëÁ¡»ó¿¡¼­ÀÇ »èÁ¦ ¸Þ¼¼Áö
	MSG_SELLITEM sellMsg; // ÁÖÀο¡°Ô º¸³»´Â ¸Þ¼¼Áö
	MSG_ITEMEX buyMsg; // ¼Õ´Ô¿¡°Ô º¸³»´Â ¸Þ¼¼Áö
	MSG_SELLITEM updateMsg; // °ãÄ¡±â ¾ÆÀÌÅÛÀ» »ç´Â °æ¿ì¿¡ ´ëÇÑ Ã³¸® 
	const ITEMBASE* pInvenItem;

	if( ItemBase.Durability < wBuyNum )
	{
		rt = NOT_EXIST;
		goto ITEMBUY_FAILED;
	}
	if(CanBuyItem(pOwner, pGuest, pItemInfo, EmptyCellPos, pBuyInfo, &ItemBase) == FALSE)
		goto ITEMBUY_FAILED;

	absPosOut =  EmptyCellPos[0];

	pInvenItem = pOwnerInventory->GetItemInfoAbs(pItemInfo->sItemBase.Position);
	
	if(!CHKRT->ItemOf(pOwner, pInvenItem->Position, pInvenItem->wIconIdx,0,0,CB_EXIST|CB_ICONIDX))
	{
		goto ITEMBUY_FAILED;
	}
	if(!CHKRT->ItemOf(pGuest, absPosOut,0,0,0,CB_ICONIDX))
	{
		goto ITEMBUY_FAILED;
	}
	
	if( ItemBase.Durability == wBuyNum )
	{
		ITEMBASE temp;
		if(pOwnerInventory->DeleteItemAbs(pOwner, RealPos, &temp, SS_LOCKOMIT) != EI_TRUE)
			goto ITEMBUY_FAILED;
		temp.Position = absPosOut;
		if(pGuestInventory->InsertItemAbs(pGuest, absPosOut, &temp, SS_LOCKOMIT) !=  EI_TRUE)
		{
			//rollback
			temp.Position = RealPos;
			if(pOwnerInventory->InsertItemAbs(pOwner, RealPos, &temp) != EI_TRUE)
			{
				ASSERT(0);
				goto ITEMBUY_FAILED;
			}
			goto ITEMBUY_FAILED;
		}

		ItemUpdateToDB( pGuest->GetID(), temp.dwDBIdx ,temp.wIconIdx, temp.Durability, absPosOut, 0 );
		
		LogItemMoney(pOwner->GetID(), pOwner->GetObjectName(), pGuest->GetID(), pGuest->GetObjectName(),
			eLog_StreetStallBuyAll, pOwner->GetMoney(), pGuest->GetMoney(), money,
			temp.wIconIdx, temp.dwDBIdx, temp.Position, absPosOut, temp.Durability, pOwner->GetPlayerExpPoint());
		// ³ëÁ¡»ó¿¡¼­ »èÁ¦ÇÑ´Ù.

⌨️ 快捷键说明

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