示例#1
0
void CPlayerMgr::ReleasePlayer(UINT32 dwConnID)
{
	CPlayer * pPlayer = GetPlayerByConnID(dwConnID);
	if(pPlayer == NULL)
	{
		printf("Fatal Error  CPlayerMgr::ReleasePlayer Cannnot Find Player ConnID = %u ",dwConnID);
		return;
	}
	
	int iAccountID = pPlayer->GetAccountID();
	std::string strName	   = pPlayer->GetName();
	pPlayer->OnDisconnected(strName);

	delete pPlayer;

	m_MapConnID2Player.erase(dwConnID);
	m_MapAccountID2Player.erase(iAccountID);
	m_MapName2Player.erase(strName);

}
int32_t CItemRebulidEvent::SendEnterRoomNotice(EntityType nEntityType)
{
	int32_t ret = S_OK;
	CItemRebulidCtl &stItemRebulidCtl = GET_ITEMREBULIDCTL_INSTANCE();
	int32_t nPlayerCount = 0;
	RoleID arrRoleID[MaxRebulidCountPer];
	stItemRebulidCtl.GetRebulidRoleID(arrRoleID,MaxRebulidCountPer,nPlayerCount,nEntityType);

	for(int32_t i = 0;i < nPlayerCount;i++)
	{
		CPlayer *pPlayer = NULL;
		PlayerIndex nPlayerIndex = enmInvalidPlayerIndex;
		ret = g_PlayerMgt.GetPlayer(arrRoleID[i], pPlayer, nPlayerIndex);
		//没有此玩家
		if(ret < 0 || pPlayer == NULL)
		{
			WRITE_ERROR_LOG("rebulid itemserver:player does no exist{nRoleID=%d}",arrRoleID[i]);
			continue;
		}
		//进入房间通知
		CEnterRoomNotice stEnterRoomNotice;
		stEnterRoomNotice.nRoleID = pPlayer->GetRoleID();
		stEnterRoomNotice.strRoleName = pPlayer->GetRoleName();
		stEnterRoomNotice.nAccountID = pPlayer->GetAccountID();
		stEnterRoomNotice.nServerID = g_FrameConfigMgt.GetFrameBaseConfig().GetServerID();
		stEnterRoomNotice.nPlayerType = pPlayer->GetPlayerType();
		stEnterRoomNotice.nIdentityType = pPlayer->GetIdentityType();
		if(pPlayer->IsHideEnter())
		{
			stEnterRoomNotice.nEnterRoomType = enmEnterRoomType_Hid;
		}

		int32_t nEnterCount = 0;
		RoomID arrEnterRoomID[MaxEnterRoomCount];
		pPlayer->GetEnterRoomInfo(arrEnterRoomID,nEnterCount,MaxEnterRoomCount);

		for(int32_t j = 0;j < nEnterCount;j++)
		{
			CRoom *pRoom = NULL;
			RoomIndex nRoomIndex = enmInvalidRoomIndex;
			ret = g_RoomMgt.GetRoom(arrEnterRoomID[j], pRoom, nRoomIndex);
			if(ret < 0 || pRoom == NULL)
			{
				WRITE_ERROR_LOG("get room error!ret=0x%08x{roomid=%d}\n",ret,arrEnterRoomID[j]);
				continue;
			}
			stEnterRoomNotice.nRoomID = arrEnterRoomID[j];
			stEnterRoomNotice.nRoleRank = pRoom->GetRoleRank(pPlayer->GetRoleID());
			stEnterRoomNotice.nRoomType = pRoom->GetRoomType();
			WRITE_NOTICE_LOG("rebulid itemserver:send rebulid enter room notice to item {nRoomID=%d, nRoleID=%d}",arrEnterRoomID[j],pPlayer->GetRoleID());
			SendMessageNotifyToServer(MSGID_RSMS_ENTER_ROOM_NOTICE, &stEnterRoomNotice,arrEnterRoomID[j] , enmTransType_Broadcast, pPlayer->GetRoleID(), nEntityType);
		}
	}
	if(nPlayerCount == MaxRebulidCountPer)
	{
		WRITE_NOTICE_LOG("rebulid :start other timer to rebulid %d!",nEntityType );
		ret = StartTimer(nEntityType);
	}
	else
	{
		WRITE_NOTICE_LOG("rebulid :rebulid %d ok!",nEntityType);
		stItemRebulidCtl.SetItemRebulidSuccess(nEntityType);
	}
	return ret;
}
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;
}