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

📄 zonegroupmanager.cpp

📁 天之炼狱1服务器端源文件游戏服务端不完整
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//--------------------------------------------------------------------------------//// Filename    : ZoneGroupManager.cpp// Written By  : Reiot// Description :////--------------------------------------------------------------------------------// include files#include "ZoneGroupManager.h"#include "ZoneGroup.h"#include "ZonePlayerManager.h"#include "ZoneInfoManager.h"#include "PCManager.h"#include "Portal.h"#include "DB.h"#include "LogClient.h"#include "Tile.h"#include "GamePlayer.h"#include "LoginServerManager.h"#include "IncomingPlayerManager.h"#include <stdio.h>#include <list>#include <hash_map>//--------------------------------------------------------------------------------// constructor//--------------------------------------------------------------------------------ZoneGroupManager::ZoneGroupManager () 	throw (): m_ZoneGroups(10){	__BEGIN_TRY	__END_CATCH}	//--------------------------------------------------------------------------------// destructor//--------------------------------------------------------------------------------ZoneGroupManager::~ZoneGroupManager () 	throw (){	__BEGIN_TRY	hash_map< ZoneGroupID_t , ZoneGroup *>::iterator itr = m_ZoneGroups.begin();	for (; itr != m_ZoneGroups.end(); itr++)	{		ZoneGroup* pZoneGroup = itr->second;		SAFE_DELETE(pZoneGroup);	}	// 秦浆甘救俊 乐绰 葛电 pair 甸阑 昏力茄促.	m_ZoneGroups.clear();	__END_CATCH}	//--------------------------------------------------------------------------------// initialize zone manager//--------------------------------------------------------------------------------void ZoneGroupManager::init () 	throw (Error){	__BEGIN_TRY	load();					__END_CATCH}	//--------------------------------------------------------------------------------//// load data from database//// 单捞鸥海捞胶俊 楷搬秦辑 ZoneGroup 阑 肺靛秦柯促.////--------------------------------------------------------------------------------void ZoneGroupManager::load ()	throw (Error){	__BEGIN_TRY	__BEGIN_DEBUG	Statement* pStmt = NULL;	list<ZoneGroupID_t> ZoneGroupIDList;	// 刚历 粮 弊缝 酒捞叼甸阑 佬绰促.	BEGIN_DB	{		pStmt = g_pDatabaseManager->getConnection("DARKEDEN")->createStatement();		Result* pResult = pStmt->executeQuery("SELECT ZoneGroupID FROM ZoneGroupInfo ORDER BY ZoneGroupID");		while (pResult->next())		{			ZoneGroupID_t ID = pResult->getInt(1);			ZoneGroupIDList.push_back(ID);		}		SAFE_DELETE(pStmt);	}	END_DB(pStmt)	list<ZoneGroupID_t>::iterator itr = ZoneGroupIDList.begin();	for (; itr != ZoneGroupIDList.end(); itr++)	{		ZoneGroupID_t ID = (*itr);		// 秦寸窍绰 ID狼 粮 弊缝阑 积己窍绊, 概聪历俊促 歹茄促.		ZoneGroup* pZoneGroup = new ZoneGroup(ID);		ZonePlayerManager* pZonePlayerManager = new ZonePlayerManager();		pZonePlayerManager->setZGID( ID );		pZoneGroup->setZonePlayerManager(pZonePlayerManager);		addZoneGroup(pZoneGroup);		// 捞 粮 弊缝俊 加窍绰 粮狼 沥焊甫 佬绢甸捞绊, 檬扁拳秦具 茄促.		BEGIN_DB		{			pStmt = g_pDatabaseManager->getConnection("DARKEDEN")->createStatement();			//Result* pResult = pStmt->executeQuery("SELECT ZoneID FROM ZoneInfo WHERE ZoneGroupID = %d", ID);			Result* pResult = pStmt->executeQuery(					"SELECT ZoneID FROM ZoneInfo WHERE ZoneGroupID = %d ORDER BY ZoneID", (int)ID);			while (pResult->next())			{				ZoneID_t zoneID = pResult->getInt(1);				// 粮 按眉甫 积己, 檬扁拳茄 饶, 粮弊缝俊 眠啊茄促.				Zone* pZone = new Zone(zoneID);				Assert(pZone != NULL);												pZone->setZoneGroup(pZoneGroup);				pZoneGroup->addZone(pZone);				//--------------------------------------------------------------------------------				// 鉴辑俊 蜡狼且 巴.				// 郴何俊辑 NPC 甫 肺爹窍霸 登绰单.. AtFirst-SetPosition 牧叼记-咀记阑 荐青且锭				// ZoneGroupManager 俊 立辟窍霸 等促. 蝶扼辑, 刚历 ZGM俊 眠啊茄 饶 檬扁拳甫 秦具 茄促.				//--------------------------------------------------------------------------------				printf("\n@@@@@@@@@@@@@@@ [%d]th ZONE INITIALIZATION START @@@@@@@@@@@@@@@\n", zoneID);				pZone->init();				printf("\n@@@@@@@@@@@@@@@ [%d]th ZONE INITIALIZATION SUCCESS @@@@@@@@@@@@@@@\n", zoneID);			}			SAFE_DELETE(pStmt);		}		END_DB(pStmt)	}	ZoneGroupIDList.clear();	/*	Statement* pStmt1 = NULL;	try 	{		pStmt1 = g_pDatabaseManager->getConnection("DARKEDEN")->createStatement();		// ZoneGroupID 甫 佬绢柯促.		//Result* pResult1 = pStmt1->executeQuery("SELECT ZoneGroupID FROM ZoneGroupInfo ORDER BY ZoneGroupID");		Result* pResult1 = pStmt1->executeQuery("SELECT ZoneGroupID FROM ZoneGroupInfo");		while (pResult1->next()) 		{			ZoneGroupID_t zoneGroupID = pResult1->getInt(1);			// ZoneGroup 按眉客 ZonePlayerManager 按眉甫 积己茄促.			ZoneGroup* pZoneGroup = new ZoneGroup(zoneGroupID);			ZonePlayerManager* pZonePlayerManager = new ZonePlayerManager();			pZoneGroup->setZonePlayerManager(pZonePlayerManager);			// 粮弊缝阑 粮弊缝概聪历俊 眠啊茄促.			addZoneGroup(pZoneGroup);				// 漂沥 ZoneGroupID 甫 啊柳 粮 沥焊甫 佬绢柯促.			Statement* pStmt2 = NULL;						BEGIN_DB			{				pStmt2 = g_pDatabaseManager->getConnection("DARKEDEN")->createStatement();				Result* pResult2 = pStmt2->executeQuery("SELECT ZoneID FROM ZoneInfo WHERE ZoneGroupID = %d", zoneGroupID);						while (pResult2->next()) 				{					__BEGIN_DEBUG					ZoneID_t zoneID = pResult2->getInt(1);					// 粮 按眉甫 积己, 檬扁拳茄 饶, 粮弊缝俊 眠啊茄促.					Zone* pZone = new Zone (zoneID);					Assert(pZone != NULL);														pZone->setZoneGroup(pZoneGroup);					pZoneGroup->addZone(pZone);					//--------------------------------------------------------------------------------					// 鉴辑俊 蜡狼且 巴.					// 郴何俊辑 NPC 甫 肺爹窍霸 登绰单.. AtFirst-SetPosition 牧叼记-咀记阑 荐青且锭					// ZoneGroupManager 俊 立辟窍霸 等促. 蝶扼辑, 刚历 ZGM俊 眠啊茄 饶 檬扁拳甫 秦具 茄促.					//--------------------------------------------------------------------------------					printf("\n@@@@@@@@@@@@@@@ [%d]th ZONE INITIALIZATION START @@@@@@@@@@@@@@@\n", zoneID);					pZone->init();					printf("\n@@@@@@@@@@@@@@@ [%d]th ZONE INITIALIZATION SUCCESS @@@@@@@@@@@@@@@\n", zoneID);					__END_DEBUG				}				SAFE_DELETE(pStmt2);			}			END_DB(pStmt2)		}		SAFE_DELETE(pStmt1);	} 	catch (SQLQueryException & sqe) 	{		SAFE_DELETE(pStmt1);		throw Error(sqe.toString());	}	*/	__END_DEBUG	__END_CATCH}//--------------------------------------------------------------------------------// save data to database//--------------------------------------------------------------------------------void ZoneGroupManager::save ()	throw (Error){	__BEGIN_TRY	throw UnsupportedError();	__END_CATCH}//--------------------------------------------------------------------------------// add zone to zone manager//--------------------------------------------------------------------------------void ZoneGroupManager::addZoneGroup (ZoneGroup* pZoneGroup) 	throw (Error){	__BEGIN_TRY	hash_map< ZoneGroupID_t , ZoneGroup *>::iterator itr = m_ZoneGroups.find(pZoneGroup->getZoneGroupID());		if (itr != m_ZoneGroups.end())		// 度鞍篮 酒捞叼啊 捞固 粮犁茄促绰 家府促. - -;		throw Error("duplicated zone id");	// itr 捞 啊府虐绰	m_ZoneGroups[ pZoneGroup->getZoneGroupID() ] = pZoneGroup;	__END_CATCH}//--------------------------------------------------------------------------------// get zone from zone manager//--------------------------------------------------------------------------------ZoneGroup* ZoneGroupManager::getZoneGroupByGroupID (ZoneGroupID_t ZoneGroupID) const	throw (NoSuchElementException){	__BEGIN_TRY			ZoneGroup* pZoneGroup = NULL;	hash_map< ZoneGroupID_t , ZoneGroup *>::const_iterator itr = m_ZoneGroups.find(ZoneGroupID);		if (itr != m_ZoneGroups.end()) {		pZoneGroup = itr->second;	} else {		// 弊繁 粮 酒捞叼甫 茫阑 荐 绝菌阑 锭		StringStream msg;		msg << "ZoneGroupID : " << ZoneGroupID;		throw NoSuchElementException(msg.toString());	}	return pZoneGroup;	__END_CATCH}//--------------------------------------------------------------------------------// Delete zone from zone manager//--------------------------------------------------------------------------------void ZoneGroupManager::deleteZoneGroup (ZoneGroupID_t zoneID) 	throw (NoSuchElementException){	__BEGIN_TRY			hash_map< ZoneGroupID_t , ZoneGroup *>::iterator itr = m_ZoneGroups.find(zoneID);		if (itr != m_ZoneGroups.end()) 	{		// 粮阑 昏力茄促.		SAFE_DELETE(itr->second);		// pair甫 昏力茄促.		m_ZoneGroups.erase(itr);	} 	else 	{		// 弊繁 粮 酒捞叼甫 茫阑 荐 绝菌阑 锭		StringStream msg;		msg << "ZoneGroupID : " << zoneID;		throw NoSuchElementException(msg.toString());	}	__END_CATCH}	//--------------------------------------------------------------------------------// get zone from zone manager//--------------------------------------------------------------------------------ZoneGroup* ZoneGroupManager::getZoneGroup (ZoneGroupID_t zoneID) const	throw (NoSuchElementException){	__BEGIN_TRY			ZoneGroup* pZoneGroup = NULL;	hash_map< ZoneGroupID_t , ZoneGroup *>::const_iterator itr = m_ZoneGroups.find(zoneID);		if (itr != m_ZoneGroups.end()) {		pZoneGroup = itr->second;	} else {		// 弊繁 粮 酒捞叼甫 茫阑 荐 绝菌阑 锭		StringStream msg;		msg << "ZoneGroupID : " << zoneID;		throw NoSuchElementException(msg.toString());	}	return pZoneGroup;	__END_CATCH}void   ZoneGroupManager::broadcast(Packet* pPacket) 	throw (Error){	ZoneGroup* pZoneGroup = NULL;	hash_map< ZoneGroupID_t , ZoneGroup *>::const_iterator itr = m_ZoneGroups.begin();		for (; itr != m_ZoneGroups.end(); itr++)	{		pZoneGroup = itr->second;		pZoneGroup->getZonePlayerManager()->broadcastPacket( pPacket );	}	}void ZoneGroupManager::outputLoadValue()	throw (Error){	//------------------------------------------------------------------	// ZoneGroup load	//------------------------------------------------------------------	ofstream file("loadBalance.txt", ios::app);	VSDateTime current = VSDateTime::currentDateTime();	file << current.toString() << endl;	hash_map< ZoneGroupID_t , ZoneGroup* >::const_iterator itr;	for (itr = m_ZoneGroups.begin() ; itr != m_ZoneGroups.end() ; itr ++) 	{		ZoneGroup* pZoneGroup = itr->second;		file << "[" << (int)pZoneGroup->getZoneGroupID() << "] ";		const hash_map< ZoneID_t, Zone* >& zones = pZoneGroup->getZones();		hash_map< ZoneID_t, Zone* >::const_iterator iZone;		// 阿 Zone狼 loadValue甫 备茄促.		int totalLoad = 0;		for (iZone=zones.begin(); iZone!=zones.end(); iZone++)		{			Zone* pZone = iZone->second;			int load = pZone->getLoadValue();			int playerLoad = pZone->getPCCount();			file << (int)pZone->getZoneID() << "(" << load << ", " << playerLoad << ") ";			totalLoad += load;		}		file << " = " << totalLoad << endl;	}	file << endl;	file.close();}//---------------------------------------------------------------------------// make Balanced LoadInfo//---------------------------------------------------------------------------//// bForce : balacing且 鞘夸啊 绝促绊 魄窜登绰 版快俊档 //          碍力肺 ZoneGroup阑 balancing且 版快俊 荤侩等促.//// Zone付促狼 10檬埃狼 loop 贸府 雀荐甫 load蔼栏肺 茄促.// 拌魂俊 祈狼甫 困秦辑 角力 load绰 促澜苞 鞍狼 沥狼茄促.////     load = (loadLimit - load)*loadMultiplier;////---------------------------------------------------------------------------bool 	ZoneGroupManager::makeBalancedLoadInfo(LOAD_INFOS& loadInfos, bool bForce)	throw (Error){	const int maxGroup  		= m_ZoneGroups.size();	// zoneGroup 荐	//const int loadMultiplier 	= 5;					// load 啊吝摹 - 蠢赴 局甸阑 歹 蠢府促...扼绊 窍扁 困茄 巴.	const int loadLimit 		= 500;					// load 蔼 力茄 - sleep俊 狼秦辑 力茄蹬辑 风橇 贸府雀荐 500捞 弥绊促.	const int stableLoad 		= 120;					// 救沥利牢 load - 捞 沥档搁 balancing捞 鞘夸绝促绊 积阿登绰 荐霖	//const int minLoadGap 		= 20 * loadMultiplier;	// load balancing阑 窍扁 困茄 load 瞒捞 - 弥绊~弥历狼 瞒捞啊 老沥 蔼 捞惑捞绢具瘤 balancing捞 狼固乐促.	const int minLoadGap 		= 20;	// load balancing阑 窍扁 困茄 load 瞒捞 - 弥绊~弥历狼 瞒捞啊 老沥 蔼 捞惑捞绢具瘤 balancing捞 狼固乐促.	const int averageLoadPercent = 90;					// 茄 group狼 load % 力茄. 100栏肺 秦档 登摆瘤父 90沥档啊 宝满篮芭 鞍促.	int i;	//LOAD_INFOS 	loadInfos;	GROUPS		groups;	hash_map< ZoneGroupID_t , ZoneGroup* >::const_iterator itr;	// 傈眉 load	int totalLoad = 0;	//------------------------------------------------------------------	// ZoneGroup付促 loadValue 炼荤 	//------------------------------------------------------------------	int maxLoadValue = 0;	int minLoadValue = loadLimit;	for (itr = m_ZoneGroups.begin() ; itr != m_ZoneGroups.end() ; itr ++) 	{		ZoneGroup* pZoneGroup = itr->second;		const hash_map< ZoneID_t, Zone* >& zones = pZoneGroup->getZones();		hash_map< ZoneID_t, Zone* >::const_iterator iZone;		// 阿 Zone狼 loadValue甫 备茄促.		for (iZone=zones.begin(); iZone!=zones.end(); iZone++)		{			Zone* pZone = iZone->second;			int load = pZone->getLoadValue();			load = min( load, loadLimit );			// 10~500			maxLoadValue = max(maxLoadValue, load);			minLoadValue = min(minLoadValue, load);			// 箭磊 利篮霸 蠢赴 芭促.			// 拌魂狼 祈狼甫 困秦辑 箭磊甫 第笼?绰促. --> 奴 箭磊 何窍啊 奴 吧肺 官槽促.			// player箭磊甫 何窍啊吝摹肺 荤侩茄促.			// playerLoad = 1 ~ 20沥档?			int playerLoad = pZone->getPCCount()/10;			playerLoad = max(1, playerLoad);			//load = (loadLimit - load)*loadMultiplier;	// 何窍 啊吝摹			load = (loadLimit - load)*playerLoad;	// 何窍 啊吝摹			LoadInfo* pInfo = new LoadInfo;			pInfo->id 			= pZone->getZoneID();			pInfo->oldGroupID 	= itr->first;			pInfo->groupID 		= -1;			pInfo->load 		= load;			// 何窍客 zoneID肺 捞风绢柳 key			DWORD key = (load << 8) | pInfo->id;			loadInfos[key] = pInfo;			totalLoad += load;		}	}	//------------------------------------------------------------------	//	// balancing捞 鞘夸茄瘤 犬牢	//	//------------------------------------------------------------------	if (!bForce)	{		int loadBoundary = stableLoad;		//int loadBoundary = ( loadLimit - stableLoad ) * loadMultiplier;		// 何窍 茄拌 荐摹焊促 累芭唱		// min~max 何窍 荐摹 瞒捞啊 老沥荐摹 捞窍捞搁		// load balancing且 鞘夸啊 绝促.		//if (maxLoad <= loadBoundary

⌨️ 快捷键说明

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