//----------------------------------------------------------------------
// 
// SGModifyGuildOKHandler::execute()
// 
//----------------------------------------------------------------------
void SGModifyGuildOKHandler::execute (SGModifyGuildOK* pPacket )
	 throw(ProtocolException , Error )
{
	__BEGIN_TRY __BEGIN_DEBUG_EX

#ifdef __GAME_SERVER__

	Guild* pGuild = g_pGuildManager->getGuild(pPacket->getGuildID());
	Assert(pGuild != NULL);

	if (pGuild->getState() == Guild::GUILD_STATE_WAIT && pPacket->getGuildState() == Guild::GUILD_STATE_ACTIVE )
	{
		/////////////////////////////////////////////////////////////
		// 존 추가
		/////////////////////////////////////////////////////////////
/*		if (pGuild->getServerGroupID() == g_pConfig->getPropertyInt("ServerID" ) )
		{
			// 이 게임 서버에 길드 아지트를 만든다.

			//////////////
			// Zone Info
			//////////////
			ZoneInfo* pZoneInfo = new ZoneInfo();
			pZoneInfo->setZoneID(pGuild->getZoneID());
			pZoneInfo->setZoneGroupID(2);
			pZoneInfo->setZoneType("NPC_SHOP");
			pZoneInfo->setZoneLevel(0);
			pZoneInfo->setZoneAccessMode("PUBLIC");
			pZoneInfo->setZoneOwnerID("");
			pZoneInfo->setPayPlay("");
			if (pGuild->getRace() == Guild::GUILD_RACE_SLAYER )
			{
				pZoneInfo->setSMPFilename("team_hdqrs.smp");
				pZoneInfo->setSSIFilename("team_hdqrs.ssi");
				string Name = "team - " + pGuild->getName();
				pZoneInfo->setFullName(Name);
				pZoneInfo->setShortName(Name);
			}
			else if (pGuild->getRace() == Guild::GUILD_RACE_VAMPIRE )
			{
				pZoneInfo->setSMPFilename("clan_hdqrs.smp");
				pZoneInfo->setSSIFilename("clan_hdqrs.ssi");
				string Name = "clan - " + pGuild->getName();
				pZoneInfo->setFullName(Name);
				pZoneInfo->setShortName(Name);
			}

			g_pZoneInfoManager->addZoneInfo(pZoneInfo);
			
			/////////
			// Zone
			/////////
			Zone* pZone = new Zone(pGuild->getZoneID());
			Assert(pZone != NULL);

			ZoneGroup* pZoneGroup = g_pZoneGroupManager->getZoneGroup(2);
			Assert(pZoneGroup != NULL);

			pZone->setZoneGroup(pZoneGroup);
			pZoneGroup->addZone(pZone);
			pZone->init();
		}
*/

		// 정식 길드로 변경
		pGuild->setState(Guild::GUILD_STATE_ACTIVE);

		HashMapGuildMember& Members = pGuild->getMembers();
		HashMapGuildMemberItor itr = Members.begin();
		for (; itr != Members.end(); itr++ )
		{
			GuildMember* pGuildMember = itr->second;

			// 멤버가 접속해 있다면
			__ENTER_CRITICAL_SECTION((*g_pPCFinder))

			Creature* pCreature = g_pPCFinder->getCreature_LOCKED(pGuildMember->getName());
			if (pCreature != NULL && pCreature->isPC() )
			{
				Player* pPlayer = pCreature->getPlayer();
				Assert(pPlayer != NULL);

				PlayerCreature* pPlayerCreature = dynamic_cast<PlayerCreature*>(pCreature);
				Assert(pPlayerCreature != NULL);

				// 길드 아이디를 바꿔준다.
				pPlayerCreature->setGuildID(pGuild->getID());

				// 클라이언트에 길드 아이디가 바꼈음을 알려준다.
				GCModifyGuildMemberInfo gcModifyGuildMemberInfo;
				gcModifyGuildMemberInfo.setGuildID(pGuild->getID());
				gcModifyGuildMemberInfo.setGuildName(pGuild->getName());
				gcModifyGuildMemberInfo.setGuildMemberRank(pGuildMember->getRank());
				pPlayer->sendPacket(&gcModifyGuildMemberInfo);

				// 주위에 알린다.
				Zone* pZone = pCreature->getZone();
				Assert(pZone != NULL);

				GCOtherModifyInfo gcOtherModifyInfo;
				gcOtherModifyInfo.setObjectID(pCreature->getObjectID());
				gcOtherModifyInfo.addShortData(MODIFY_GUILDID, pPlayerCreature->getGuildID());

				pZone->broadcastPacket(pCreature->getX(), pCreature->getY(), &gcOtherModifyInfo, pCreature);

				// 정식 길드가 되었음을 알림
				Statement* pStmt = NULL;
				Result* pResult = NULL;

				BEGIN_DB
				{
					pStmt = g_pDatabaseManager->getConnection("DARKEDEN" )->createStatement();
					pResult = pStmt->executeQuery("SELECT Message FROM Messages WHERE Receiver = '%s'", pGuildMember->getName().c_str());

					while (pResult->next() )
					{
						GCSystemMessage gcSystemMessage;
						gcSystemMessage.setMessage(pResult->getString(1));
						pPlayer->sendPacket(&gcSystemMessage);
					}

					pStmt->executeQuery("DELETE FROM Messages WHERE Receiver = '%s'", pGuildMember->getName().c_str());

					SAFE_DELETE(pStmt);
				}
				END_DB(pStmt)
			}

			__LEAVE_CRITICAL_SECTION((*g_pPCFinder))
		}
void EffectAlignmentRecovery::affect(Creature* pCreature)
	throw(Error)
{
	__BEGIN_TRY

	Assert(pCreature != NULL);
	Assert(pCreature->isPC());

	Timeval CurrentTime;
	getCurrentTime(CurrentTime);

	GCModifyInformation gcModifyInformation;

	Alignment_t CurrentAlignment = 0;
	Alignment_t NewAlignment = 0;

	if (pCreature->isSlayer())
	{
		Slayer* pSlayer = dynamic_cast<Slayer*>(pCreature);
		Assert(pSlayer != NULL);

		if (m_Period != 0) 
		{
			// 플레그 걸귀
			pSlayer->setFlag(Effect::EFFECT_CLASS_ALIGNMENT_RECOVERY);

			// 한 턴에 얼마나 회복 시킬 것인가.
			CurrentAlignment = pSlayer->getAlignment();
			NewAlignment     = min(10000, (int)(CurrentAlignment + m_AlignmentQuantity));

			pSlayer->setAlignment(NewAlignment);

			gcModifyInformation.addLongData(MODIFY_ALIGNMENT, NewAlignment);
			pSlayer->getPlayer()->sendPacket(&gcModifyInformation);

			WORD AlignmentSaveCount = pSlayer->getAlignmentSaveCount();
			if (AlignmentSaveCount > ALIGNMENT_SAVE_PERIOD)
			{
				StringStream msg;
				msg << "Alignment = " << NewAlignment;
				pSlayer->tinysave(msg.toString());
				AlignmentSaveCount = 0;
			}
			else AlignmentSaveCount++;
			pSlayer->setAlignmentSaveCount(AlignmentSaveCount);
		} 
		else 
		{
			// unaffect하면서 패킷이 날아갈 테니까....
			setDeadline(0);
		}

		m_Period--;
	}
	else if (pCreature->isVampire())
	{

		Vampire* pVampire = dynamic_cast<Vampire*>(pCreature);
		Assert(pVampire != NULL);

		if (m_Period != 0) 
		{
			// 플레그 걸귀
			pVampire->setFlag(Effect::EFFECT_CLASS_ALIGNMENT_RECOVERY);

			// 한 턴에 얼마나 회복 시킬 것인가.
			CurrentAlignment = pVampire->getAlignment();
			NewAlignment     = min(10000, CurrentAlignment + m_AlignmentQuantity);

			pVampire->setAlignment(NewAlignment);

			gcModifyInformation.addLongData(MODIFY_ALIGNMENT, NewAlignment);
			pVampire->getPlayer()->sendPacket(&gcModifyInformation);

			WORD AlignmentSaveCount = pVampire->getAlignmentSaveCount();
			if (AlignmentSaveCount > ALIGNMENT_SAVE_PERIOD)
			{
				StringStream msg;
				msg << "Alignment = " << NewAlignment;
				pVampire->tinysave(msg.toString());
				AlignmentSaveCount = 0;
			}
			else AlignmentSaveCount++;
			pVampire->setAlignmentSaveCount(AlignmentSaveCount);
		} 
		else
		{
			// unaffect하면서 패킷이 날아갈 테니까....
			setDeadline(0);
		}

		m_Period--;
	}
	else if (pCreature->isOusters())
	{

		Ousters* pOusters = dynamic_cast<Ousters*>(pCreature);
		Assert(pOusters != NULL);

		if (m_Period != 0) 
		{
			// 플레그 걸귀
			pOusters->setFlag(Effect::EFFECT_CLASS_ALIGNMENT_RECOVERY);

			// 한 턴에 얼마나 회복 시킬 것인가.
			CurrentAlignment = pOusters->getAlignment();
			NewAlignment     = min(10000, CurrentAlignment + m_AlignmentQuantity);

			pOusters->setAlignment(NewAlignment);

			gcModifyInformation.addLongData(MODIFY_ALIGNMENT, NewAlignment);
			pOusters->getPlayer()->sendPacket(&gcModifyInformation);

			WORD AlignmentSaveCount = pOusters->getAlignmentSaveCount();
			if (AlignmentSaveCount > ALIGNMENT_SAVE_PERIOD)
			{
				StringStream msg;
				msg << "Alignment = " << NewAlignment;
				pOusters->tinysave(msg.toString());
				AlignmentSaveCount = 0;
			}
			else AlignmentSaveCount++;
			pOusters->setAlignmentSaveCount(AlignmentSaveCount);
		} 
		else
		{
			// unaffect하면서 패킷이 날아갈 테니까....
			setDeadline(0);
		}

		m_Period--;
	}
	else
	{
		return;	// 큰 의미는 없지만..
	}

	// 성향 단계가 바뀌면 다른 사람들에게도 알려줘야 한다.  by sigi. 2002.12.28

	Alignment beforeAlignment = g_pAlignmentManager->getAlignmentType(CurrentAlignment);
	Alignment afterAlignment = g_pAlignmentManager->getAlignmentType(NewAlignment);

	if (beforeAlignment!=afterAlignment)
	{
		GCOtherModifyInfo gcOtherModifyInfo;
		gcOtherModifyInfo.setObjectID(pCreature->getObjectID());
		gcOtherModifyInfo.addShortData(MODIFY_ALIGNMENT, NewAlignment);

		Zone* pZone = pCreature->getZone();	// 2003.1.10
		Assert(pZone!=NULL);
		
		pZone->broadcastPacket(pCreature->getX(), pCreature->getY(), &gcOtherModifyInfo, pCreature);
	}

	__END_CATCH
}
//----------------------------------------------------------------------
// 
// SGDeleteGuildOKHandler::execute()
// 
//----------------------------------------------------------------------
void SGDeleteGuildOKHandler::execute (SGDeleteGuildOK* pPacket )
	 throw(ProtocolException , Error )
{
	__BEGIN_TRY

#ifdef __GAME_SERVER__

	// 길드 아지트에 있는 멤버를 warp 시킨다.
	// 길드 아지트를 삭제한다.
	// 멤버 warp와 길드 아지트 삭제 시 다른 쓰레드에서 ZoneGroup Thread 내부에서 일어나게 해야 별탈이 없을 듯 하다.
	// 일단은 걍 둔다. Portal 이 막히므로 다시 들어갈 수 없을 것이다.
	
	Assert(pPacket != NULL);

	// 길드를 가져온다.
	Guild* pGuild = g_pGuildManager->getGuild(pPacket->getGuildID());
	try { Assert(pGuild != NULL); } catch (Throwable& ) { return; }


	// 길드 활동 중인 상태에서의 해체인지 대기 중인 상태에서의 해체인지 구별한다.
	if (pGuild->getState() == Guild::GUILD_STATE_ACTIVE )
	{
		HashMapGuildMember& Members = pGuild->getMembers();
		HashMapGuildMemberItor itr = Members.begin();

		for (; itr != Members.end(); itr++ )
		{
			GuildMember* pGuildMember = itr->second;

			// 접속해 있으면
			__ENTER_CRITICAL_SECTION((*g_pPCFinder))

			Creature* pCreature = g_pPCFinder->getCreature_LOCKED(pGuildMember->getName());
			if (pCreature != NULL && pCreature->isPC() )
			{
				Player* pPlayer = pCreature->getPlayer();
				Assert(pPlayer != NULL);

				PlayerCreature* pPlayerCreature = dynamic_cast<PlayerCreature*>(pCreature);
				Assert(pPlayerCreature != NULL);

				// Slayer, Vampire 의 길드 아이디를 바꾼다.
				if (pPlayerCreature->isSlayer() )
				{
					pPlayerCreature->setGuildID(99);		// 슬레이어 가입안한 상태의 길드 ID

					// 클라이언트에 길드 아이디가 바꼈음을 알린다.
					GCModifyGuildMemberInfo gcModifyGuildMemberInfo;
					gcModifyGuildMemberInfo.setGuildID(pPlayerCreature->getGuildID());
					gcModifyGuildMemberInfo.setGuildName("");
					gcModifyGuildMemberInfo.setGuildMemberRank(GuildMember::GUILDMEMBER_RANK_DENY);
					pPlayer->sendPacket(&gcModifyGuildMemberInfo);
				}
				else if (pPlayerCreature->isVampire() )
				{
					pPlayerCreature->setGuildID(0);		// 뱀파이어 가입안한 상태의 길드 ID

					// 클라이언트에 길드 아이디가 바꼈음을 알린다.
					GCModifyGuildMemberInfo gcModifyGuildMemberInfo;
					gcModifyGuildMemberInfo.setGuildID(pPlayerCreature->getGuildID());
					gcModifyGuildMemberInfo.setGuildName("");
					gcModifyGuildMemberInfo.setGuildMemberRank(GuildMember::GUILDMEMBER_RANK_DENY);
					pPlayer->sendPacket(&gcModifyGuildMemberInfo);
				}
				else if (pPlayerCreature->isOusters() )
				{
					pPlayerCreature->setGuildID(66);		// 아우스터즈 가입안한 상태의 길드 ID

					// 클라이언트에 길드 아이디가 바꼈음을 알린다.
					GCModifyGuildMemberInfo gcModifyGuildMemberInfo;
					gcModifyGuildMemberInfo.setGuildID(pPlayerCreature->getGuildID());
					gcModifyGuildMemberInfo.setGuildName("");
					gcModifyGuildMemberInfo.setGuildMemberRank(GuildMember::GUILDMEMBER_RANK_DENY);
					pPlayer->sendPacket(&gcModifyGuildMemberInfo);
				}

				// 주위에 클라이언트에 길드 아이디가 바꼈음을 알린다.
				GCOtherModifyInfo gcOtherModifyInfo;
				gcOtherModifyInfo.setObjectID(pCreature->getObjectID());
				gcOtherModifyInfo.addShortData(MODIFY_GUILDID, pPlayerCreature->getGuildID());

				Zone* pZone = pCreature->getZone();
				Assert(pZone != NULL);

				pZone->broadcastPacket(pCreature->getX(), pCreature->getY(), &gcOtherModifyInfo, pCreature);
			}

			__LEAVE_CRITICAL_SECTION((*g_pPCFinder))

			// Guild Member 객체를 삭제한다.
			SAFE_DELETE(pGuildMember);
		}

		// 길드 멤버 맵을 삭제한다.
		Members.clear();

		// 길드 매니저에서 길드를 삭제한다.
		g_pGuildManager->deleteGuild(pGuild->getID());

		// 길드 객체를 삭제한다.
		SAFE_DELETE(pGuild);
	}
	else if (pGuild->getState() == Guild::GUILD_STATE_WAIT )
	{
		HashMapGuildMember& Members = pGuild->getMembers();
		HashMapGuildMemberItor itr = Members.begin();

		Statement* pStmt = NULL;
		Result* pResult = NULL;

		BEGIN_DB
		{
			pStmt = g_pDatabaseManager->getConnection("DARKEDEN" )->createStatement();
			
			for (; itr != Members.end(); itr++ )
			{
				GuildMember* pGuildMember = itr->second;

				// 접속해 있으면
				__ENTER_CRITICAL_SECTION((*g_pPCFinder))

				Creature* pCreature = g_pPCFinder->getCreature_LOCKED(pGuildMember->getName());
				if (pCreature != NULL && pCreature->isPC() )
				{
					Player* pPlayer = pCreature->getPlayer();
					Assert(pPlayer != NULL);

					PlayerCreature* pPlayerCreature = dynamic_cast<PlayerCreature*>(pCreature);
					Assert(pPlayerCreature != NULL);

					// 등록비를 환불한다.
					Gold_t Gold = pPlayerCreature->getGold();
					if (pGuildMember->getRank() == GuildMember::GUILDMEMBER_RANK_MASTER )
					{
						Gold = min((uint64_t)(Gold + RETURN_SLAYER_MASTER_GOLD), (uint64_t)2000000000);
					}
					else if (pGuildMember->getRank() == GuildMember::GUILDMEMBER_RANK_SUBMASTER )
					{
						Gold = min((uint64_t)(Gold + RETURN_SLAYER_SUBMASTER_GOLD), (uint64_t)2000000000);
					}

					pPlayerCreature->setGoldEx(Gold);

					GCModifyInformation gcModifyInformation;
					gcModifyInformation.addLongData(MODIFY_GOLD, Gold);
					pPlayer->sendPacket(&gcModifyInformation);

					// 메시지를 보낸다.
					pResult = pStmt->executeQuery("SELECT Message FROM Messages WHERE Receiver = '%s'", pCreature->getName().c_str());

					while (pResult->next() )
					{
						GCSystemMessage message;
						message.setMessage(pResult->getString(1));
						pPlayer->sendPacket(&message);
					}

					pStmt->executeQuery("DELETE FROM Messages WHERE Receiver = '%s'", pCreature->getName().c_str());
				}

				__LEAVE_CRITICAL_SECTION((*g_pPCFinder))

				// 길드 멤버 객체를 삭제한다.
				SAFE_DELETE(pGuildMember);
			}

			// 길드 멤버 해쉬 맵을 지운다.
			Members.clear();

			// 길드 매니저에서 길드를 삭제한다.
			g_pGuildManager->deleteGuild(pGuild->getID());
			GuildUnionManager::Instance().removeMasterGuild(pGuild->getID());

			// 길드 객체를 삭제한다.
			SAFE_DELETE(pGuild);

			SAFE_DELETE(pStmt);
		}
		END_DB(pStmt)
	}