bool CPlayerManager::onCrossServerRequestRet(stMsgCrossServerRequestRet* pResult,Json::Value* vJsValue)
{
	if ( eCrossSvrReq_SelectTakeIn == pResult->nRequestType && eCrossSvrReqSub_SelectPlayerData == pResult->nRequestSubType )
	{
		stMsgRequestPlayerDataRet msgBack ;
		msgBack.nRet = 0 ;
		msgBack.isDetail = pResult->vArg[3] ;
		CPlayer* pPlayer = GetPlayerByUserUID(pResult->vArg[0]);
		if ( pPlayer == nullptr )
		{
			CLogMgr::SharedLogMgr()->ErrorLog("why this player is null , can not , funck!") ;
			return true ;
		}

		stPlayerDetailDataClient stData ;
		stData.nCurrentRoomID = 0 ;
		CAutoBuffer auB (sizeof(msgBack) + sizeof(stPlayerDetailDataClient));
		if ( pPlayer )
		{
			CPlayerTaxas* pTaxasData = (CPlayerTaxas*)pPlayer->GetComponent(ePlayerComponent_PlayerTaxas);
			stData.nCurrentRoomID = pResult->nReqOrigID;
			if ( msgBack.isDetail )
			{
				pPlayer->GetBaseData()->GetPlayerDetailData(&stData);
				pTaxasData->getTaxasData(&stData.tTaxasData);
				if ( vJsValue )
				{
					stData.tTaxasData.nPlayTimes += (*vJsValue)["playTimes"].asUInt();
					stData.tTaxasData.nWinTimes += (*vJsValue)["winTimes"].asUInt();
					stData.tTaxasData.nSingleWinMost = (*vJsValue)["singleMost"].asUInt() < stData.tTaxasData.nSingleWinMost ? stData.tTaxasData.nSingleWinMost : (*vJsValue)["singleMost"].asUInt();
				}
				else
				{
					CLogMgr::SharedLogMgr()->PrintLog("targe player not sit down uid = %llu",pResult->vArg[0]);
				}
			}
			else
			{
				pPlayer->GetBaseData()->GetPlayerBrifData(&stData) ;
			}

			if ( pResult->vArg[1] )
			{
				stData.nCoin += pResult->vArg[2] ;
			}
			else
			{
				stData.nDiamoned += pResult->vArg[2] ;
			}
			stData.nCurrentRoomID = pResult->nReqOrigID;
			auB.addContent(&msgBack,sizeof(msgBack));
			auB.addContent(&stData,msgBack.isDetail ? sizeof(stPlayerDetailDataClient) : sizeof(stPlayerBrifData) ) ;
			CGameServerApp::SharedGameServerApp()->sendMsg(pResult->nTargetID,auB.getBufferPtr(),auB.getContentSize()) ;
			CLogMgr::SharedLogMgr()->PrintLog("select take in ret , send player data");
			return true ;
		}
		return true ;
	}
	return false ;
}
bool CPlayerManager::onCrossServerRequest(stMsgCrossServerRequest* pRequest , eMsgPort eSenderPort,Json::Value* vJsValue)
{
	if ( pRequest->nRequestType == eCrossSvrReq_Inform )
	{
		CPlayer* pp = GetPlayerByUserUID(pRequest->nTargetID) ;
		if ( pp )
		{
			((CPlayerMailComponent*)pp->GetComponent(ePlayerComponent_Mail))->ReciveMail(((char*)pRequest) + sizeof(stMsgCrossServerRequest),pRequest->nJsonsLen);
		}
		else
		{
			CPlayerMailComponent::PostMailToDB(((char*)pRequest) + sizeof(stMsgCrossServerRequest),pRequest->nJsonsLen,pRequest->nTargetID);
		}
		return true ;
	}
	return false ;
}
bool CPlayerManager::ProcessTaxasServerMsg( stMsg* prealMsg , eMsgPort eSenderPort , uint32_t nRoomID  )
{
	if ( eSenderPort != ID_MSG_PORT_TAXAS )
	{
		return false ;
	}

	switch ( prealMsg->usMsgType )
	{
	case MSG_TP_REQUEST_PLAYER_DATA:
		{
			stMsgRequestTaxasPlayerData* pData = (stMsgRequestTaxasPlayerData*)prealMsg;
			stMsgRequestTaxasPlayerDataRet msgBack ;
			msgBack.nRoomID = nRoomID ;
			msgBack.nRet = 0 ;
			CPlayer* pPlayer = GetPlayerBySessionID(pData->nSessionID) ;
			if ( pPlayer == NULL )
			{
				msgBack.nRet = 1 ;
				CGameServerApp::SharedGameServerApp()->SendMsg(pData->nSessionID,(char*)&msgBack,sizeof(msgBack)) ;
				CLogMgr::SharedLogMgr()->ErrorLog("why can not find player session from taxas server session id = %d",pData->nSessionID ) ;
				return true ;
			}

			if ( pPlayer->GetTaxasRoomID() )
			{
				msgBack.nRet = 2 ;
				CGameServerApp::SharedGameServerApp()->SendMsg(pData->nSessionID,(char*)&msgBack,sizeof(msgBack)) ;
				CLogMgr::SharedLogMgr()->ErrorLog("can not enter room already in other room id = %d  session id = %d",pPlayer->GetTaxasRoomID(),pData->nSessionID ) ;

				// may have error  order leave 
				stMsgOrderTaxasPlayerLeave msg ;
				msg.nRoomID = pPlayer->GetTaxasRoomID() ;
				CGameServerApp::SharedGameServerApp()->SendMsg(pPlayer->GetSessionID(),(char*)&msg,sizeof(msg)) ;
				return true ;
			}

			msgBack.tData.nPhotoID = pPlayer->GetBaseData()->GetPhotoID();
			memset(msgBack.tData.cName,0,sizeof(msgBack.tData.cName) );
			sprintf_s(msgBack.tData.cName,sizeof(msgBack.tData.cName),"%s",pPlayer->GetBaseData()->GetPlayerName()) ;
			msgBack.tData.nSessionID = pData->nSessionID ;
			msgBack.tData.nSex = pPlayer->GetBaseData()->GetSex();
			msgBack.tData.nUserUID = pPlayer->GetUserUID();
			msgBack.tData.nVipLevel = pPlayer->GetBaseData()->GetVipLevel() ;
			CGameServerApp::SharedGameServerApp()->SendMsg(pData->nSessionID,(char*)&msgBack,sizeof(msgBack)) ;
			pPlayer->SetStayInTaxasRoomID(nRoomID) ;
		}
		break;
	case MSG_TP_REQUEST_TAKE_IN_MONEY:
		{
			stMsgRequestTaxasPlayerTakeInCoin* pRet = (stMsgRequestTaxasPlayerTakeInCoin*)prealMsg ;
			stMsgRequestTaxasPlayerTakeInCoinRet msgBack ;
			CPlayer* pPlayer = GetPlayerBySessionID(pRet->nPlayerSessionID ) ;
			if ( pPlayer == NULL )
			{
				msgBack.nRet = 2 ;
				CGameServerApp::SharedGameServerApp()->SendMsg(pRet->nPlayerSessionID,(char*)&msgBack,sizeof(msgBack)) ;
				CLogMgr::SharedLogMgr()->ErrorLog("MSG_TP_REQUEST_TAKE_IN_MONEY why can not find player session from taxas server session id = %d",pRet->nPlayerSessionID ) ;
				return true ;
			}

			if ( pPlayer->GetBaseData()->SetTakeInCoin(pRet->nMoneyToTakeIn,pRet->bIsDiamond) )
			{
				msgBack.nRet = 0 ;
				msgBack.nMoneyToTakeIn = pRet->nMoneyToTakeIn ;
				msgBack.nRoomID = nRoomID ;
				msgBack.nSeatIdx = pRet->nSeatIdx ;
				msgBack.bIsDiamond = pRet->bIsDiamond ;
				CGameServerApp::SharedGameServerApp()->SendMsg(pRet->nPlayerSessionID,(char*)&msgBack,sizeof(msgBack)) ;
			}
			else
			{
				msgBack.nRet = 1 ;
				CGameServerApp::SharedGameServerApp()->SendMsg(pRet->nPlayerSessionID,(char*)&msgBack,sizeof(msgBack)) ;
			}
		}
		break;
	case MSG_TP_INFORM_STAND_UP:
		{
			stMsgInformTaxasPlayerStandUp* pRet = (stMsgInformTaxasPlayerStandUp*)prealMsg ;
			CPlayer* pPlayer = NULL ;
			if ( pRet->nUserUID )
			{
				pPlayer = GetPlayerByUserUID(pRet->nUserUID ) ;
			}
			else
			{
				pPlayer = GetPlayerBySessionID(pRet->nSessionID ) ;
			}

			if ( pPlayer == NULL )
			{
				CLogMgr::SharedLogMgr()->ErrorLog("MSG_TP_INFORM_STAND_UP why can not find player uid from taxas server uid = %d",pRet->nSessionID ) ;
				return true ;
			}

			if ( pPlayer )
			{
				pPlayer->GetBaseData()->CacluateTaxasRoomMoney(pRet->nTakeInMoney,pRet->bIsDiamond) ;
			}
		}
		break;
	case MSG_TP_INFORM_LEAVE:
		{
			stMsgInformTaxasPlayerLeave* pRet = (stMsgInformTaxasPlayerLeave*)prealMsg ;
			CPlayer* pPlayer = GetPlayerByUserUID(pRet->nUserUID) ;
			if ( pPlayer == NULL )
			{
				CLogMgr::SharedLogMgr()->ErrorLog("MSG_TP_INFORM_LEAVE why can not find player uid from taxas server uid = %d",pRet->nUserUID ) ;
				return true ;
			}
			pPlayer->SetStayInTaxasRoomID(0) ;
		}
		break;
	default:
		return false ;
	}
	return true ;
}
bool CPlayerManager::ProcessPublicMessage( stMsg* prealMsg , eMsgPort eSenderPort , uint32_t nSessionID  )
{
	switch ( prealMsg->usMsgType )
	{
	case MSG_CROSS_SERVER_REQUEST:
		{
			stMsgCrossServerRequest* pRet = (stMsgCrossServerRequest*)prealMsg ;

			Json::Value* pJsValue = nullptr ;
			Json::Value rootValue ;
			if ( pRet->nJsonsLen )
			{
				Json::Reader reader;
				char* pstr = ((char*)&pRet->nJsonsLen) + sizeof(pRet->nJsonsLen) ;
				reader.parse(pstr,pstr + pRet->nJsonsLen,rootValue,false);
				pJsValue = &rootValue ;
			}

			if ( onCrossServerRequest(pRet,eSenderPort,pJsValue) == false )
			{
				CPlayer* pPlayer = GetPlayerByUserUID(pRet->nTargetID);
				assert(pPlayer&&"this request no one to process or target id error");
				if ( pPlayer && pPlayer->onCrossServerRequest(pRet,eSenderPort,pJsValue) )
				{
					return true ;
				}
				CLogMgr::SharedLogMgr()->ErrorLog("cross request type = %d , subType = %d ,unprocessed",pRet->nRequestType,pRet->nRequestSubType);
				return false;
			}

			return true ;
		}
		break;
	case MSG_CROSS_SERVER_REQUEST_RET:
		{
			stMsgCrossServerRequestRet* pRet = (stMsgCrossServerRequestRet*)prealMsg ;
			Json::Value* pJsValue = nullptr ;
			Json::Value rootValue ;
			if ( pRet->nJsonsLen )
			{
				Json::Reader reader;
				char* pstr = ((char*)&pRet->nJsonsLen) + sizeof(pRet->nJsonsLen) ;
				reader.parse(pstr,pstr + pRet->nJsonsLen,rootValue,false);
				pJsValue = &rootValue ;
			}

			if ( onCrossServerRequestRet(pRet,pJsValue) == false )
			{
				CPlayer* pPlayer = GetPlayerByUserUID(pRet->nTargetID);
				assert(pPlayer&&"this request no one to process or target id error");
				if ( pPlayer && pPlayer->onCrossServerRequestRet(pRet,pJsValue) )
				{
					return true ;
				}
				CLogMgr::SharedLogMgr()->ErrorLog("cross request result type = %d , subType = %d ,unprocessed",pRet->nRequestType,pRet->nRequestSubType);
				return false;
			}
			return true ;
		}
		break;
	case MSG_REQUEST_PLAYER_DATA:
		{
			stMsgRequestPlayerData* pRet = (stMsgRequestPlayerData*)prealMsg ;
			stMsgRequestPlayerDataRet msgBack ;
			msgBack.nRet = 0 ;
			msgBack.isDetail = pRet->isDetail ;
			CPlayer* pPlayer = GetPlayerByUserUID(pRet->nPlayerUID);

			stPlayerDetailDataClient stData ;
			stData.nCurrentRoomID = 0 ;
			CAutoBuffer auB (sizeof(msgBack) + sizeof(stPlayerDetailData));
			if ( pPlayer )
			{
				CPlayerTaxas* pTaxasData = (CPlayerTaxas*)pPlayer->GetComponent(ePlayerComponent_PlayerTaxas);
				stData.nCurrentRoomID = pTaxasData->getCurRoomID() ;

				if ( stData.nCurrentRoomID )  // select take in
				{
					stMsgCrossServerRequest msgReq ;
					msgReq.nJsonsLen = 0 ;
					msgReq.nReqOrigID = nSessionID ;
					msgReq.nRequestSubType = eCrossSvrReqSub_SelectPlayerData ;
					msgReq.nRequestType = eCrossSvrReq_SelectTakeIn ;
					msgReq.nTargetID = stData.nCurrentRoomID ;
					msgReq.cSysIdentifer = ID_MSG_PORT_TAXAS ;
					msgReq.vArg[0] = pRet->nPlayerUID;
					msgReq.vArg[1] = pRet->isDetail ;
					CGameServerApp::SharedGameServerApp()->sendMsg(nSessionID,(char*)&msgReq,sizeof(msgReq)) ;
					CLogMgr::SharedLogMgr()->PrintLog("select take in for player detail") ;
					return true ;
				}

				if ( pRet->isDetail )
				{
					pPlayer->GetBaseData()->GetPlayerDetailData(&stData);
					pTaxasData->getTaxasData(&stData.tTaxasData);
				}
				else
				{
					pPlayer->GetBaseData()->GetPlayerBrifData(&stData) ;
				}
				
				auB.addContent(&msgBack,sizeof(msgBack));
				auB.addContent(&stData,pRet->isDetail ? sizeof(stPlayerDetailDataClient) : sizeof(stPlayerBrifData) ) ;
				CGameServerApp::SharedGameServerApp()->sendMsg(nSessionID,auB.getBufferPtr(),auB.getContentSize()) ;
				return true ;
			}

			if ( m_tPlayerDataCaher.getPlayerData(pRet->nPlayerUID,&stData,pRet->isDetail) )
			{
				auB.addContent(&msgBack,sizeof(msgBack));
				auB.addContent(&stData,pRet->isDetail ? sizeof(stPlayerDetailDataClient) : sizeof(stPlayerBrifData) ) ;
				CGameServerApp::SharedGameServerApp()->sendMsg(nSessionID,auB.getBufferPtr(),auB.getContentSize()) ;
				CLogMgr::SharedLogMgr()->PrintLog("get player data from cahe") ;
				return true ;
			}

			CLogMgr::SharedLogMgr()->PrintLog("req detail player not online , req from db target uid = %u",pRet->nPlayerUID) ;
			stMsgSelectPlayerData msgReq ;
			msgReq.isDetail = pRet->isDetail ;
			msgReq.nReqPlayerSessionID = nSessionID  ;
			msgReq.nTargetPlayerUID = pRet->nPlayerUID ;
			CGameServerApp::SharedGameServerApp()->sendMsg(nSessionID,(char*)&msgReq,sizeof(msgReq)) ;
		}
		break;
	case MSG_SELECT_DB_PLAYER_DATA:
		{
			stMsgSelectPlayerDataRet* pRet = (stMsgSelectPlayerDataRet*)prealMsg ;
			stMsgRequestPlayerDataRet msgBack ;
			msgBack.nRet = pRet->nRet ;
			msgBack.isDetail = pRet->isDetail ;
			if ( pRet->nRet )
			{
				CGameServerApp::SharedGameServerApp()->sendMsg(pRet->nReqPlayerSessionID,(char*)&msgBack,sizeof(msgBack)) ;
			}
			else
			{
				m_tPlayerDataCaher.cachePlayerData(pRet);
				CAutoBuffer auB (sizeof(msgBack) + sizeof(stPlayerDetailDataClient));
				auB.addContent(&msgBack,sizeof(msgBack));
				uint16_t nLen = pRet->isDetail ? sizeof(stPlayerDetailDataClient) : sizeof(stPlayerBrifData) ;
				auB.addContent((char*)prealMsg + sizeof(stMsgSelectPlayerDataRet),nLen );
				CGameServerApp::SharedGameServerApp()->sendMsg(pRet->nReqPlayerSessionID,auB.getBufferPtr(),auB.getContentSize()) ;
			}
			CLogMgr::SharedLogMgr()->PrintLog("session id = %d req play data ret = %d",pRet->nReqPlayerSessionID,pRet->nRet ) ;
		}
		break;
	case MSG_PLAYER_LOGIN:
		{
			stMsgOnPlayerLogin* pmsgenter = (stMsgOnPlayerLogin*)prealMsg ;
			CPlayer* pPlayer = GetPlayerBySessionID(nSessionID) ;
			if ( pPlayer != NULL && pPlayer->GetUserUID() == pmsgenter->nUserUID )
			{
				CLogMgr::SharedLogMgr()->ErrorLog("double nSession, this nSessionID already have player, already login , do not login again  id = %d? ",nSessionID) ;
				return true ;
			}

			if ( pPlayer != NULL && pPlayer->GetUserUID() != pmsgenter->nUserUID ) // switch account in main scene 
			{
				// disconnect pre player 
				OnPlayerOffline(pPlayer);
			}

			if ( ProcessIsAlreadyLogin(pmsgenter->nUserUID,nSessionID) )
			{
				return true ;
			}

			// is offline peer 
			MAP_UID_PLAYERS::iterator iterOfflien = m_vOfflinePlayers.begin();
			for ( ; iterOfflien != m_vOfflinePlayers.end(); ++iterOfflien )
			{
				if ( iterOfflien->second && iterOfflien->first == pmsgenter->nUserUID )
				{
					iterOfflien->second->OnReactive(nSessionID) ;
					AddPlayer(iterOfflien->second) ;
					m_vOfflinePlayers.erase(iterOfflien) ;
					return true ;
				}
			}

			CPlayer* pNew = new CPlayer ;
			pNew->Init(pmsgenter->nUserUID,nSessionID ) ;
			AddPlayer(pNew) ;
		}
		break;
	case MSG_DISCONNECT_CLIENT:
	case MSG_PLAYER_LOGOUT:
		{
			CPlayer* pPlayer = GetPlayerBySessionID(nSessionID) ;
			if ( pPlayer )
			{
				// post online event ;
				CLogMgr::SharedLogMgr()->PrintLog("player disconnect session id = %d",nSessionID);
				OnPlayerOffline(pPlayer) ;
			}
			else
			{
				CLogMgr::SharedLogMgr()->ErrorLog("client disconnect ! client is NULL session id = %d",nSessionID) ;
			}
			//#ifdef _DEBUG
			LogState();
			//#endif
		}
		break;
	case MSG_TP_ORDER_LEAVE:
		{
			stMsgOrderTaxasPlayerLeaveRet* pRet = (stMsgOrderTaxasPlayerLeaveRet*)prealMsg ;
			CPlayer* pp = GetPlayerByUserUID(pRet->nUserUID) ;
			if (!pp)
			{
				CLogMgr::SharedLogMgr()->ErrorLog("uid = %d not find , so can not inform leave",pRet->nUserUID);
			}
			else
			{
				pp->OnMessage(prealMsg,eSenderPort);
			}
		}
		break;
	case MSG_TP_INFORM_LEAVE:
		{
			stMsgInformTaxasPlayerLeave* pRet = (stMsgInformTaxasPlayerLeave*)prealMsg ;
			CPlayer* pp = GetPlayerByUserUID(pRet->nUserUID) ;
			if (!pp)
			{
				CLogMgr::SharedLogMgr()->ErrorLog("uid = %d not find , so can not inform leave",pRet->nUserUID);
			}
			else
			{
				pp->OnMessage(prealMsg,eSenderPort);
			}
		}
		break;
	case MSG_TP_SYNC_PLAYER_DATA:
		{
			stMsgSyncTaxasPlayerData* pRet = (stMsgSyncTaxasPlayerData*)prealMsg ;
			CPlayer* pp = GetPlayerByUserUID(pRet->nUserUID) ;
			if (!pp)
			{
				CLogMgr::SharedLogMgr()->ErrorLog("uid = %d not find , so can not sys data",pRet->nUserUID);
			}
			else
			{
				pp->OnMessage(prealMsg,eSenderPort);
			}
		}
		break;
	default:
		return false;
	}

	return true ;
}