int32_t CExitRoomNoticeMessageEvent::OnMessageEvent(MessageHeadSS * pMsgHead, IMsgBody* pMsgBody,
		const uint16_t nOptionLen, const void *pOptionData)
{
	if(pMsgBody==NULL || pMsgHead==NULL)
	{
		WRITE_ERROR_LOG("null pointer:{pMsgBody=0x%08x, pMsgHead=0x%08x}\n",pMsgBody,pMsgHead);
		return E_NULLPOINTER;
	}

	CExitRoomNotice *pExitRoomNotice = dynamic_cast<CExitRoomNotice *>(pMsgBody);
	if(NULL == pExitRoomNotice)
	{
		WRITE_ERROR_LOG("null pointer:{pExitRoomNotice=0x%08x, nRoleID=%d, nRoomID=%d}\n",pExitRoomNotice,pMsgHead->nRoleID,pMsgHead->nRoomID);
		return E_NULLPOINTER;
	}

	CRoom *pRoom = NULL;
	int32_t ret = g_RoomMgt.GetRoom(pExitRoomNotice->nRoomID, pRoom);
	if(ret < 0 || pRoom == NULL)
	{
		WRITE_ERROR_LOG("It's not found room object!{nRoomID=%d, nRoleID=%d}\n", pExitRoomNotice->nRoomID, pExitRoomNotice->nRoleID);
		return E_NULLPOINTER;
	}

	CPlayer *pPlayer = NULL;
	ret = g_PlayerMgt.GetPlayer(pExitRoomNotice->nRoleID, pPlayer);
	if(ret < 0 || pPlayer == NULL)
	{
		WRITE_ERROR_LOG("It's not found player object!{nRoomID=%d, nRoleID=%d}\n", pExitRoomNotice->nRoomID, pExitRoomNotice->nRoleID);
		return E_NULLPOINTER;
	}

	g_RoomMgt.DeletePlayer(pExitRoomNotice->nRoomID, pExitRoomNotice->nRoleID);
	WRITE_NOTICE_LOG("player exit room{nRoomID=%d,nRoleID=%d}\n", pExitRoomNotice->nRoomID, pExitRoomNotice->nRoleID);

	//如果玩家是机器人(没有下线通知)
	if(pPlayer->GetPlayerType() == enmPlayerType_Rebot && pPlayer->GetCurEnterRoomCount() <= 0)
	{
		//销毁玩家
		g_PlayerMgt.DestroyPlayer(pExitRoomNotice->nRoleID);
		//销毁playercache
		g_PlayerCacheMgt(pMsgHead->nSourceID)->DestoryPlayerCache(pExitRoomNotice->nRoleID);
	}

	return S_OK;
}
FRAME_HALLSERVER_NAMESPACE_BEGIN

int32_t CBuildPlayerDataMessageEvent::OnMessageEvent(MessageHeadSS * pMsgHead, IMsgBody* pMsgBody,
		const uint16_t nOptionLen, const void *pOptionData)
{
	if(pMsgBody == NULL || pMsgHead == NULL)
	{
		WRITE_ERROR_LOG("null pointer:{pMsgHead=0x%08x, pMsgBody=0x%08x}\n", pMsgHead, pMsgBody);
		return E_NULLPOINTER;
	}
	CBuildPlayerDataNoti *pNoti = dynamic_cast<CBuildPlayerDataNoti *>(pMsgBody);
	if(pNoti == NULL)
	{
		WRITE_ERROR_LOG("null pointer:pMsgBody transform to CBuildPlayerDataNoti class failed!{nMessageID=0x%08x, nSourceID=%d}\n",
				pMsgHead->nMessageID, pMsgHead->nSourceID);
		return E_NULLPOINTER;
	}

	WRITE_DEBUG_LOG("got build player data message notify!{nPlayerCount=%d}\n", pNoti->nPlayerCount);


	// build the player data
	for(int32_t i = 0; i < pNoti->nPlayerCount; ++i)
	{
		CPlayer* pLocalPlayer = NULL;
		int32_t ret = g_PlayerMgt.GetPlayer(pNoti->arrRoleID[i], pLocalPlayer);
		if(ret < 0 || pLocalPlayer == NULL)
		{
			// build a local player by player cache
			ret = g_PlayerMgt.CreatePlayer(pNoti->arrRoleID[i], pLocalPlayer);
			uint32_t nCount = g_PlayerMgt.GetRealPlayerCount();
			g_PlayerMgt.SetRealPlayerCount(++nCount);
			if(ret < 0 || pLocalPlayer == NULL)
			{
				WRITE_ERROR_LOG("build player:create player error!{ret=0x%08x}", ret);
				continue;
			}

			uint32_t nStartPos = pLocalPlayer->GetStartPos();
			uint32_t nEndPos = pLocalPlayer->GetEndPos();

			uint32_t nPlayerDataSize = nEndPos - nStartPos;
			if(nPlayerDataSize >= pNoti->arrPlayerDataSize[i])
			{
				memcpy(((uint8_t *)pLocalPlayer) + nStartPos, pNoti->arrPlayerData[i],
						pNoti->arrPlayerDataSize[i]);

				// verify if correctly built, print out!
				char szPlayer[MaxUpdateDataSize] = {0};
				uint32_t offset = 0;
				sprintf(szPlayer + offset, "built player!{nRoleID=%d, strRoleName=%s, nAccountID=%d, "
						"nVipLevel=%d, nUserLevel=%d, nPlayerGender=%d, nCurRoomCount=%d, ",
						pLocalPlayer->GetRoleID(), pLocalPlayer->GetRoleName(), pLocalPlayer->GetAccountID(),
						pLocalPlayer->GetVipLevel(), pLocalPlayer->GetUserLevel(), pLocalPlayer->GetPlayerGender(),
						pLocalPlayer->GetCurEnterRoomCount());
				offset = (uint32_t) strlen(szPlayer);
				if(pLocalPlayer->GetCurEnterRoomCount() > 0)
				{
					sprintf(szPlayer + offset, "{");
					offset = (uint32_t) strlen(szPlayer);

					PlayerRoomInfo arrPlayerRoomInfo[MaxEnterRoomCount];
					int32_t nActualCount = 0;
					pLocalPlayer->GetPlayerRoomInfo(arrPlayerRoomInfo, MaxEnterRoomCount, nActualCount);
					for(int32_t i = 0; i < nActualCount; ++i)
					{
						char* fmt = NULL;
						if(i < nActualCount - 1)
							fmt = "{nServerID=%d, nRoomID=%d, nPlayerState=%d}, ";
						else
							fmt = "{nServerID=%d, nRoomID=%d, nPlayerState=%d}}, ";

						sprintf(szPlayer + offset, fmt, arrPlayerRoomInfo[i].nServerID,
								arrPlayerRoomInfo[i].nRoomID, arrPlayerRoomInfo[i].nPlayerState);
						offset = (uint32_t) strlen(szPlayer);
					}
				}
				sprintf(szPlayer + offset, "nLoginTime=%ld, nTunnelIndex=%d, nServerID=%d, nOnlineTime=%lu, "
						"nLeftMoney=%d, nExperience=%d, nLastVersion=%d, nIsRobot=%d, nAdminCount=%d, ",
						pLocalPlayer->GetLoginTime(), pLocalPlayer->GetConnInfo().nTunnelIndex, pLocalPlayer->GetConnInfo().nServerID,
						pLocalPlayer->GetOnlineTime(),pLocalPlayer->GetLeftMoney(), pLocalPlayer->GetExperience(),
						pLocalPlayer->GetLastVersion(), (int32_t)pLocalPlayer->IsRobot(), pLocalPlayer->GetPlayerAdminCount());
				offset = (uint32_t) strlen(szPlayer);
				if(pLocalPlayer->GetPlayerAdminCount() > 0)
				{
					sprintf(szPlayer + offset, "{");
					offset = (uint32_t) strlen(szPlayer);

					RoomID arrRoomIDS[MaxBeAdminPerPlayer];
					int32_t nActualCount = 0;
					pLocalPlayer->GetAllAdminRoom(arrRoomIDS, MaxBeAdminPerPlayer, nActualCount);
					for(int32_t i = 0; i < nActualCount; ++i)
					{
						char* fmt = NULL;
						if(i < nActualCount - 1)
							fmt = "{nRoomID=%d, nRoleRank=%d}, ";
						else
							fmt = "{nRoomID=%d, nRoleRank=%d}}, ";

						sprintf(szPlayer + offset, fmt, arrRoomIDS[i], pLocalPlayer->GetAdminRoleRank(arrRoomIDS[i]));
						offset = (uint32_t) strlen(szPlayer);
					}
				}

				sprintf(szPlayer + offset, "nIdentityType=%u, nPlayerState=%d, nCurNewPlayerRoomCount=%u}\n",
						pLocalPlayer->GetIdentityType(), pLocalPlayer->GetPlayerState(), pLocalPlayer->GetNewPlayerRoomCount());
				offset = (uint32_t) strlen(szPlayer);

				WRITE_DEBUG_LOG("%s", szPlayer);
			}
			else
			{
				continue;
			}

			// build room player list
			RoomID arrRoomID[MaxEnterRoomCount];
			int32_t nEnterRoomCount = 0;
			pLocalPlayer->GetAllEnterRoom(arrRoomID, MaxEnterRoomCount, nEnterRoomCount);
			for(int32_t j = 0; j < nEnterRoomCount; ++j)
			{
				CRoom *pRoom = NULL;
				ret = g_RoomMgt.GetRoom(arrRoomID[j], pRoom);
				if(ret < 0 || pRoom == NULL)
				{
					WRITE_ERROR_LOG("building player data, failed to find room object!{nRoomID=%d}\n",
							arrRoomID[j]);
					continue;
				}

				if(pRoom->IsPlayerInRoom(pNoti->arrRoleID[i]))
				{
					WRITE_ERROR_LOG("building player data, the player should not in this room!{nRoleID=%d, nRoomID=%d}\n",
							pNoti->arrRoleID[i], arrRoomID[j]);
					continue;
				}

				//将玩家添加到房间对象中去
				pRoom->AddPlayer(pNoti->arrRoleID[i]);

			}
		}
	}


	return S_OK;
}