UINT CGPlayerShopSaleOutHandler::Execute( CGPlayerShopSaleOut* pPacket, Player* pPlayer )
{
	__ENTER_FUNCTION

	GamePlayer* pGamePlayer = (GamePlayer*)pPlayer ;
	Assert( pGamePlayer ) ;

	Obj_Human* pHuman = pGamePlayer->GetHuman() ;
	Assert( pHuman ) ;

	Scene* pScene = pHuman->getScene() ;
	if( pScene==NULL )
	{
		Assert(FALSE) ;
		return PACKET_EXE_ERROR ;
	}
	//检查线程执行资源是否正确
	Assert( MyGetCurrentThreadID()==pScene->m_ThreadID ) ;

	_PLAYERSHOP_GUID		nShopID		= pPacket->GetShopID();			//商店ID
	BYTE					bSaleOut	=	pPacket->GetSaleOut();
	UINT					uPrice		=	pPacket->GetPrice();
	BYTE					nSerial		=	pPacket->GetSerial();
	GCPlayerShopError		MsgError;

	PlayerShopManager*	pPlayerShopManager	= pScene->GetPlayerShopManager();
	PlayerShop*		pPlayerShop		= pPlayerShopManager->GetPlayerShopByGUID(nShopID);
	Assert(pPlayerShop);

	if(pPlayerShop->GetOwnerGuid() != pHuman->GetGUID())
	{//只有店主有权限关闭此商店
		g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopSaleOutHandler::Name=%s Not Owner"
			,pHuman->GetName()) ;
		return PACKET_EXE_CONTINUE ;
	}

	if(pPlayerShop->GetShopStatus() == STATUS_PLAYER_SHOP_ON_SALE && bSaleOut)
	{//已经售出了,不能再卖了
		g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopSaleOutHandler::Name=%s Already Sale Out"
			,pHuman->GetName()) ;
		return PACKET_EXE_CONTINUE ;
	}

	if(pPlayerShop->GetShopStatus() != STATUS_PLAYER_SHOP_ON_SALE && !bSaleOut)
	{//还没盘出,不能再盘入了
		g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopSaleOutHandler::Name=%s Already Sale Out"
			,pHuman->GetName()) ;
		return PACKET_EXE_CONTINUE ;
	}

	//还有店开着呢
	for(INT i = 0; i<MAX_STALL_NUM_PER_SHOP;i++ )
	{
		PlayerStallBox* pPlayerStallBox = pPlayerShop->GetPlayerStallBoxByIndex(i);
		if(pPlayerStallBox->GetStallStatus() == STALL_OPEN)
		{
			MsgError.SetID(PLAYERSHOP_MSG::ERR_SHOP_STILL_STALL_OPEN);
			pGamePlayer->SendPacket(&MsgError);
			g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopSaleOutHandler::Name=%s Stall %d is still open"
				,pHuman->GetName(), i) ;
			return PACKET_EXE_CONTINUE ;
		}
	}

	if(nSerial != pPlayerShop->GetSerial())
	{//序列号更改
		MsgError.SetID(PLAYERSHOP_MSG::ERR_SHOP_SALE_OUT_SERIAL_IS_CHANGED);
		pGamePlayer->SendPacket(&MsgError);
		g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopSaleOutHandler::Name=%s Serials had changed"
			,pHuman->GetName()) ;
		return PACKET_EXE_CONTINUE ;
	}

	//本金不足
	//判断当前本金是否≥30金 *商业指数*已开柜台数
	pPlayerShopManager->ClampComFactor();
	UINT uMoneyLevel =  static_cast<UINT>((FLOAT)pPlayerShop->GetNumStallOpened()*(FLOAT)pPlayerShopManager->GetComFactor()*300000.0);
	if(pPlayerShop->GetBaseMoney() < uMoneyLevel)
	{
		MsgError.SetID(PLAYERSHOP_MSG::ERR_SHOP_BASE_MONEY_TOO_LOW);
		pGamePlayer->SendPacket(&MsgError);
		g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopSaleOutHandler::Name=%s Base money is not enough"
			,pHuman->GetName()) ;
		
		return PACKET_EXE_CONTINUE ;
	}

	//标志此店已经售出
	if(bSaleOut)
	{
		//金钱不够盘出
		if(pHuman->GetMoney()<150000*pPlayerShopManager->GetComFactor())
		{
			MsgError.SetID(PLAYERSHOP_MSG::ERR_SHOP_NOT_ENOUTH_MONEY_TO_SALE_OUT);
			pGamePlayer->SendPacket(&MsgError);
			g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopSaleOutHandler::Name=%s Base money is not enough"
				,pHuman->GetName()) ;
			return PACKET_EXE_CONTINUE ;
		}

		pPlayerShop->SetShopStatus(STATUS_PLAYER_SHOP_ON_SALE);
		pPlayerShop->SetSaleOutPrice(uPrice);
	}
	else
	{
		//金钱不够盘回
		if(pHuman->GetMoney()<50000*pPlayerShopManager->GetComFactor())
		{
			MsgError.SetID(PLAYERSHOP_MSG::ERR_SHOP_NOT_ENOUTH_MONEY_TO_BUY_BACK);
			pGamePlayer->SendPacket(&MsgError);
			g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopSaleOutHandler::Name=%s cancel buy money "
				,pHuman->GetName()) ;
			return PACKET_EXE_CONTINUE ;
		}

		pPlayerShop->SetShopStatus(STATUS_PLAYER_SHOP_OPEN);
		pPlayerShop->SetSaleOutPrice(0);
	}

	BYTE uSerial = pPlayerShop->IncSerial();


	//通知客户端
	GCPlayerShopSaleOut MsgToClient;
	MsgToClient.SetSaleOut(bSaleOut);
	MsgToClient.SetSerial(uSerial);
	pGamePlayer->SendPacket(&MsgToClient);

	g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopSaleOutHandler::ObjName=%s Sale out = %d"
		,pHuman->GetName(), bSaleOut);

	return PACKET_EXE_CONTINUE ;

	__LEAVE_FUNCTION

		return PACKET_EXE_ERROR ;
}
UINT CGPlayerShopNameHandler::Execute( CGPlayerShopName* pPacket, Player* pPlayer )
{
	__ENTER_FUNCTION
	GamePlayer* pGamePlayer = (GamePlayer*)pPlayer ;
	Assert( pGamePlayer ) ;

	Obj_Human* pHuman = pGamePlayer->GetHuman() ;
	Assert( pHuman ) ;

	Scene* pScene = pHuman->getScene() ;
	if( pScene==NULL )
	{
		Assert(FALSE) ;
		return PACKET_EXE_ERROR ;
	}
	//检查线程执行资源是否正确
	Assert( MyGetCurrentThreadID()==pScene->m_ThreadID ) ;
	_PLAYERSHOP_GUID	nShopID				=	pPacket->GetShopID();				//商店ID
	BYTE				PlayerShopNameSize	=	pPacket->GetPlayerShopNameSize();	//商店描述
	CHAR*				PlayerShopName		=	pPacket->GetPlayerShopName();		//获得商店名

	PlayerShopManager*	pPlayerShopManager = pScene->GetPlayerShopManager();
	PlayerShop*		pPlayerShop		= pPlayerShopManager->GetPlayerShopByGUID(nShopID);
	Assert(pPlayerShop);

	//是不是自己的店.只有店主有资格更改此属性
	BOOL bIsMine = (pHuman->GetGUID() == pPlayerShop->GetOwnerGuid())? TRUE:FALSE;
	if(bIsMine == FALSE )
	{
		//log...
		return PACKET_EXE_CONTINUE;
	}


	if(pPlayerShop->GetShopStatus() == STATUS_PLAYER_SHOP_ON_SALE)
	{//商店未开张
		g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopNameHandler::Name=%s shop close"
			,pHuman->GetName()) ;
		return PACKET_EXE_CONTINUE ;
	}

	//如果玩家身上的金钱≥10金 *商业指数,修改成功。
	pPlayerShopManager->ClampComFactor();
	FLOAT fNeedMoney = (FLOAT)100000.0*pPlayerShopManager->GetComFactor();
	if(pHuman->GetMoney()<(UINT)fNeedMoney)
	{
		//通知客户端购买商店成功
		GCPlayerShopError Msg;
		Msg.SetID(PLAYERSHOP_MSG::ERR_SHOP_NOT_ENOUTH_MONEY_TO_CHANGE_NAME);
		pHuman->GetPlayer()->SendPacket(&Msg);
	}

	//扣钱
	pHuman->SetMoney(pHuman->GetMoney() - (UINT)fNeedMoney);
	
	//更改店名
	pPlayerShop->SetShopName(PlayerShopName, PlayerShopNameSize);

	//更改称号
	CHAR	szMsgTitle[256] = {0};
	sprintf(szMsgTitle, "%s大掌柜", PlayerShopName);
	pHuman->SetShangDianName(szMsgTitle,(BYTE)strlen(szMsgTitle));
	pHuman->UpdateTitlesToClient();
	//通知客户端商店名修改成功
	GCPlayerShopError Msg;
	Msg.SetID(PLAYERSHOP_MSG::ERR_SHOP_SUCCESS_CHANGE_NAME);
	pHuman->GetPlayer()->SendPacket(&Msg);

	g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopNameHandler::Name=%s"
		,pHuman->GetName()) ;
		return PACKET_EXE_CONTINUE ;

	__LEAVE_FUNCTION

		return PACKET_EXE_ERROR ;
}
UINT CGPlayerShopEstablishHandler::Execute( CGPlayerShopEstablish* pPacket, Player* pPlayer )
{
	__ENTER_FUNCTION

	GamePlayer* pGamePlayer = (GamePlayer*)pPlayer ;
	Assert( pGamePlayer ) ;

	Obj_Human* pHuman = pGamePlayer->GetHuman() ;
	Assert( pHuman ) ;

	Scene* pScene = pHuman->getScene() ;
	if( pScene==NULL )
	{
		Assert(FALSE) ;
		return PACKET_EXE_ERROR ;
	}
	//检查线程执行资源是否正确
	Assert( MyGetCurrentThreadID()==pScene->m_ThreadID ) ;

	CHAR* pszShopName		=	pPacket->GetShopName();
	BYTE  nShopNameSize		=	pPacket->GetShopNameSize();
	BYTE  nShopType			=	pPacket->GetType();
	SceneID_t sceneid		=	pScene->SceneID();
	GCPlayerShopError	MsgError;

	PlayerShopManager*	pPlayerShopManager = pScene->GetPlayerShopManager();
	pPlayerShopManager->ClampComFactor();
	FLOAT CommercialFactor = pPlayerShopManager->GetComFactor();
	UINT uCost = (UINT)(300000*CommercialFactor*2*1.03);

	//金钱验证
	if(uCost>pHuman->GetMoney())
	{
		g_pLog->FastSaveLog( LOG_FILE_1, "ERROR: CGPlayerShopApplyHandler::Name=%s Cost = %d Money = %d ", pHuman->GetName(), uCost, pHuman->GetMoney() ) ;
		MsgError.SetID(PLAYERSHOP_MSG::ERR_NOT_ENOUGH_MONEY_TO_NEW);
		pGamePlayer->SendPacket(&MsgError);
		return PACKET_EXE_CONTINUE ;
	}

	//需要再验证是不是已经建立满了商店了
	if(nShopType == PLAYERSHOP_MSG::TYPE_ITEM)
	{
		if( !pHuman->GetShopGuid(0).isNull() )
		{
			PlayerShop* pCurShop = pPlayerShopManager->GetPlayerShopByGUID(pHuman->GetShopGuid(0));
			if(!pCurShop)
			{
				pHuman->GetShopGuid(0).Reset();
			}
			else if(pCurShop->GetOwnerGuid() == pHuman->GetGUID())
			{
				g_pLog->FastSaveLog( LOG_FILE_1, "ERROR: CGPlayerShopApplyHandler::Name=%s Can not create shop", pHuman->GetName()) ;
				MsgError.SetID(PLAYERSHOP_MSG::ERR_SHOP_ALREADY_EXIST);
				pGamePlayer->SendPacket(&MsgError);
				return PACKET_EXE_CONTINUE ;
			}
			else
			{
				pHuman->GetShopGuid(0).Reset();
			}
		}
	}
	else if(nShopType == PLAYERSHOP_MSG::TYPE_PET)
	{
		if( !pHuman->GetShopGuid(1).isNull() )
		{
			PlayerShop* pCurShop = pPlayerShopManager->GetPlayerShopByGUID(pHuman->GetShopGuid(1));
			if(!pCurShop)
			{
				pHuman->GetShopGuid(1).Reset();
			}
            else if(pCurShop->GetOwnerGuid() == pHuman->GetGUID())
			{
				g_pLog->FastSaveLog( LOG_FILE_1, "ERROR: CGPlayerShopApplyHandler::Name=%s Can not create shop", pHuman->GetName()) ;
				MsgError.SetID(PLAYERSHOP_MSG::ERR_SHOP_ALREADY_EXIST);
				pGamePlayer->SendPacket(&MsgError);
				return PACKET_EXE_CONTINUE ;
			}
			else
			{
				pHuman->GetShopGuid(1).Reset();
			}
		}
	}
	else
	{
		Assert(0);
		g_pLog->FastSaveLog( LOG_FILE_1, "ERROR: CGPlayerShopApplyHandler::Name=%s Can not create shop", pHuman->GetName(), uCost, pHuman->GetMoney() ) ;
		MsgError.SetID(PLAYERSHOP_MSG::ERR_SHOP_ALREADY_EXIST);
		pGamePlayer->SendPacket(&MsgError);
		return PACKET_EXE_CONTINUE ;
	}
	//...

	//重名验证
	for(UINT i =0; i<MAX_SHOP_NUM_PER_SCENE; i++)
	{
		PlayerShop* pTempShop = pPlayerShopManager->GetPlayerShopByIndex(i);
		if(!pTempShop)
		{
			continue;
		}
		else
		{
			if(!strcmp(pszShopName, pTempShop->GetShopName()))
			{
				g_pLog->FastSaveLog( LOG_FILE_1, "ERROR: CGPlayerShopApplyHandler::Name = %s This name has already existed", pHuman->GetName()) ;
				MsgError.SetID(PLAYERSHOP_MSG::ERR_NAME_ALREADY_EXISTED);
				pGamePlayer->SendPacket(&MsgError);
				return PACKET_EXE_CONTINUE ;
			}
		}
	}

	//New一个商店
	INT nIndex	= pPlayerShopManager->NewPlayerShop();

	if(nIndex < 0)
	{
		MsgError.SetID(PLAYERSHOP_MSG::ERR_NOT_ENOUGH_SHOP_IN_POOL);
		pGamePlayer->SendPacket(&MsgError);
		g_pLog->FastSaveLog( LOG_FILE_1, "ERROR: CGPlayerShopEstablishHandler::Name=%s OUT OF SHOP POOL", pHuman->GetName()) ;
		return PACKET_EXE_CONTINUE ;
	}

	PlayerShop*			pPlayerShop	= pPlayerShopManager->GetPlayerShopByIndex(nIndex);
	
	//时间采样
	UINT  nYear		=	(UINT)g_pTimeManager->GetYear();
	BYTE  nMonth	=	(BYTE)g_pTimeManager->GetMonth();
	BYTE  nDay		=	(BYTE)g_pTimeManager->GetDay();
	BYTE  nHour		=	(BYTE)g_pTimeManager->GetHour();
	BYTE  nMin		=	(BYTE)g_pTimeManager->GetMinute();

	//开张了
	pPlayerShop->SetShopStatus(STATUS_PLAYER_SHOP_OPEN);
	//商店类型
	if(nShopType == 1)
	{//物品点
		pPlayerShop->SetShopType(TYPE_PLAYER_SHOP_ITEM);
		pHuman->SetShopGuid(0,pPlayerShop->GetShopGUID());
	}
	else
	{//宠物店
		pPlayerShop->SetShopType(TYPE_PLAYER_SHOP_PET);
		pHuman->SetShopGuid(1,pPlayerShop->GetShopGUID());
	}
	//冲入本金
	pPlayerShop->SetBaseMoney(uCost/2);
	//本金上限
	pPlayerShop->SetMaxBaseMoney(uCost/2);
	//冲入投资金
	pPlayerShop->SetProfitMoney(uCost/2);
	//商店名
	pPlayerShop->SetShopName(pszShopName, nShopNameSize);
	//商店描述
	pPlayerShop->SetShopDesc("",0);
	//店主名
	pPlayerShop->SetOwnerName(pHuman->GetName());
	//店主GUID
	pPlayerShop->SetOwnerGuid(pHuman->GetGUID());
	//商店的建立时间
	pPlayerShop->SetFoundedYear(nYear);
	pPlayerShop->SetFoundedMonth(nMonth);
	pPlayerShop->SetFoundedDay(nDay);
	pPlayerShop->SetFoundedHour(nHour);
	pPlayerShop->SetFoundedMin(nMin);
	//设置开放的柜台数
	pPlayerShop->SetNumStallOpened(1);
	//设置开张的柜台数
	pPlayerShop->SetNumStallOnSale(0);

	//柜台开张
	PlayerStallBox*		pNewPlayerStallBox	= pPlayerShop->GetPlayerStallBoxByIndex(0);
	pNewPlayerStallBox->SetStallStatus(PLAYER_SHOP::STALL_CLOSE);

	//扣钱
	pHuman->SetMoney(pHuman->GetMoney()-uCost);

	MONEY_LOG_PARAM	MoneyLogParam;
	MoneyLogParam.CharGUID	=	pHuman->GetGUID();
	MoneyLogParam.TargetGUID	=	pPlayerShop->GetOwnerGuid();
	MoneyLogParam.OPType	=	MONEY_CREATE_PLAYERSHOP_OUTCOME;	
	MoneyLogParam.Count		=	uCost;
	MoneyLogParam.SceneID	=	pHuman->getScene()->SceneID();
	MoneyLogParam.XPos		=	pHuman->getWorldPos()->m_fX;
	MoneyLogParam.ZPos		=	pHuman->getWorldPos()->m_fZ;
	SaveMoneyLog(&MoneyLogParam);

	//"AAA(店铺唯一ID)开张大吉,大掌柜BBB(玩家自身名字)恭迎惠顾。" 
	CHAR	szMsgChat[256] = {0};
	sprintf(szMsgChat, "@*;SrvMsg;CHAT_PS_OPEN;%s;%s", pszShopName, pHuman->GetName());
	GWChat* pChatPacket = (GWChat*)(g_pPacketFactoryManager->CreatePacket(PACKET_GW_CHAT)) ;
	pChatPacket->SetSourGUID(pHuman->GetGUID());
	pChatPacket->SetChatType( CHAT_TYPE_SYSTEM ) ;
	pChatPacket->SetContexSize( (BYTE)strlen(szMsgChat)) ;
	pChatPacket->SetContex( szMsgChat ) ;

	g_pServerManager->SendPacket( pChatPacket, INVALID_ID );

	CHAR	szMsgTitle[256] = {0};
	sprintf(szMsgTitle, "%s大掌柜", pszShopName);
	pHuman->SetShangDianName(szMsgTitle,(BYTE)strlen(szMsgTitle));
	pHuman->UpdateTitlesToClient();

	GCPlayerShopEstablish Msg;
	Msg.SetShopName(pPlayerShop->GetShopName());
	pGamePlayer->SendPacket(&Msg);

	g_pLog->FastSaveLog( LOG_FILE_1, "CGPlayerShopEstablishHandler::Name=%s"
		,pHuman->GetName()) ;

	return PACKET_EXE_CONTINUE ;

	__LEAVE_FUNCTION

	return PACKET_EXE_ERROR ;
}