#include "gamesrv.h"

#include "StdAfx.h"
#include "PvPScript.h"
#include "Tokenizer.h"

cPvPScript* cPvPScript::mpPvPScript = NULL;


cPvPScript::cPvPScript(void)
{

	/// 싱글톤
	if( !mpPvPScript )
	{
		mpPvPScript = this;
	}

}

cPvPScript::~cPvPScript(void)
{
	Release();
}


bool cPvPScript::Init()
{
	Release();

	if( LoadDMInfo() == false )
		return false;

	if( LoadMemberGive() == false )
		return false;

	return true;
}


void cPvPScript::Release()
{
	///
	cHashMap::cIterator i = mDMInfoMap.Begin();
	cHashMap::cIterator iend = mDMInfoMap.End();
	for( ; i!=iend; i++ )
	{
		cArray* pAry = (cArray*)(*i).mSecond;
		if( pAry )
		{
			for( unsigned long i = 0 ; pAry->GetSize() > i ; ++i )
			{
				sDMInfo* pDMInfo = (sDMInfo*)(*pAry)[i];
				SAFE_DELETE( pDMInfo );
			}
			pAry->Clear();

			SAFE_DELETE( pAry );
		}
	}
	mDMInfoMap.Clear();

	///
	i = mDMMemberGiveMap.Begin();
	iend = mDMMemberGiveMap.End();
	for( ; i!=iend; i++ )
	{
		sDMMemberGive* info = (sDMMemberGive*)(*i).mSecond;
		SAFE_DELETE(info);
	}
	mDMMemberGiveMap.Clear();
}

bool cPvPScript::LoadDMInfo()
{
	cFileLoader loader;
	cString pathName = "./Script/resource/PVP_Map.txt";

	if( loader.Open( pathName, true ) == false )
	{
		assert( 0 && "failed to load PVP_Character.txt" );
		return false;
	}

	cTokenizer tokenizer( loader.GetBufferPtr(), loader.GetSize(), " \t\r\n", pathName.Cstr() );
	cString str;

	sDMInfo* pDMInfo = 0;
	while( tokenizer.IsEnd() == false )
	{
		pDMInfo = new sDMInfo;

        // 전장 종류
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mType = static_cast<unsigned char>(str.ToInt());

		// 맵번호
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mMapNumber = (unsigned short)str.ToInt();

		// 최소 레벨
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mMinLevel = static_cast<unsigned char>(str.ToInt());

		// 최대 레벨
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mMaxLevel = static_cast<unsigned char>(str.ToInt());

		// kill-일반
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mKillPoint = str.ToInt();

		// kill-수장
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mLeaderKillPoint = str.ToInt();

		// death
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mDeathPoint = str.ToInt();

		// 자기세력포인트-승리
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mWinMyForcePoint = str.ToInt();

		// 동맹세력포인트-승리
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mWinFamilyForcePoint = str.ToInt();

		// 자기세력포인트-패배
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mLoseMyForcePoint = str.ToInt();

		// 구간 전장 포인트
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mLoseCheckPoint = str.ToInt();

		// 자기세력친밀도-승리
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mWinMyForceFriendly = str.ToInt();

		// 동맹세력친밀도-승리
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mWinFamilyForceFriendly = str.ToInt();

		// 자기세력친밀도-패배
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mLoseMyForceFriendly = str.ToInt();

		// 구간 친밀도 포인트
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		pDMInfo->mLoseCheckFriendly = str.ToInt();

		cArray* pAry = (cArray*)mDMInfoMap.GetAt( pDMInfo->mType );
		if( pAry == 0 )
		{
			pAry = new cArray;
			if( mDMInfoMap.Insert( pDMInfo->mType, pAry) == false )
			{
				assert(0);
				delete pAry;
				goto ERR;
			}
		}
		pAry->PushBack( pDMInfo );
	}

	return true;

ERR:
	SAFE_DELETE(pDMInfo);
	return false;
}

bool cPvPScript::LoadMemberGive()
{
	cFileLoader loader;
	cString pathName = "./Script/resource/PVP_Character.txt";

	if( loader.Open( pathName, true ) == false )
	{
		assert( 0 && "failed to load PVP_Character.txt" );
		return false;
	}

	cTokenizer tokenizer( loader.GetBufferPtr(), loader.GetSize(), " \t\r\n", pathName.Cstr() );
	cString str;

	sDMMemberGive* giveInfo = 0;
	while( tokenizer.IsEnd() == false )
	{
		sDMMemberGive* giveInfo = new sDMMemberGive;

		// 캐릭터 레벨
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		giveInfo->mLevel = static_cast<unsigned char>(str.ToInt());

		// 돈 보상-승리
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		giveInfo->mWinMoney = str.ToInt();

		// 돈 보상-패배
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		giveInfo->mLoseMoney = str.ToInt();

		// EXP 보상-승리(%)
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		giveInfo->mWinExp = str.ToInt();

		// EXP 보상-패배(%)
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		giveInfo->mLoseExp = str.ToInt();

		// SXP 보상-승리(%)
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		giveInfo->mWinSxp = str.ToInt();

		// SXP 보상-패배(%)
		if( tokenizer.GetNext( &str ) == false )	{ goto ERR; }
		giveInfo->mLoseSxp = str.ToInt();

		if( mDMMemberGiveMap.Insert( giveInfo->mLevel, giveInfo ) == false )
		{
			assert(0);
			goto ERR;
		}
	}

	return true;

ERR:
	SAFE_DELETE( giveInfo );
	return false;
}


sDMInfo* cPvPScript::GetDmInfo( unsigned char type, unsigned char level )
{
	sDMInfo* pDMInfo = NULL;
	cArray* pAry = (cArray*)mDMInfoMap.GetAt( type );
	if( pAry != NULL )
	{
		for( unsigned long i = 0 ; pAry->GetSize() > i ; ++i )
		{
			pDMInfo = (sDMInfo*)(*pAry)[i];

			if( pDMInfo == NULL )
				continue;

			if( pDMInfo->mMinLevel <= level && pDMInfo->mMaxLevel >= level )
                return pDMInfo;
		}
	}

	return NULL;
}


sDMMemberGive* cPvPScript::GetMemberGive( unsigned char level )
{
	return (sDMMemberGive*)mDMMemberGiveMap.GetAt( level );
}