// =============================
/* Change Guild Leader: http://herc.ws/board/topic/13842-safely-transfer-guild-master/
	----------------------------
	by: Legend
	compatible w/: Hercules
	----------------------------
	Version: 1.2
	* Fixed wrong variable usage
	* Query through sql than "getguild***" functions
	  to allow query without old guild leader to be online.
	* Added delay after guild leader transfer since we
	  are using sql query which has certain seconds before it
	  updates. (for safety purposes)
	----------------------------
	special thanks to:
	* Dastgir (for pointing out the wrong variable usage)
	----------------------------
	Description:
	* Allows Game Masters to safely transfer
	  guild leader ownership to any members of the guild.
	* Can be triggered through "@guild" command or
	  can also be triggered through NPC click.
	* Be careful and you must check the correct spelling of the name before 
	  proceeding ( Upper & lower case letters and etc.)
	* No need for the old guild leader to be online.
	* New guild leader must be online.
	* Certain checks were applied for safe transfer purpose.
	----------------------------
	Comments:
		Please report if bugs were found.
		Drop me a message: http://herc.ws/board/user/5387-legend/
*/
// =============================

-	script	changegm	-1,{
	doevent "changegm::OnMain";
	end;

OnMain:
	if (getgmlevel() < .reqlvl) {
		mes .npc$;
		mes "You are not allowed to use this.";
		close;
	}
	mes .npc$;
	mes "Please provide the ^FF0000old^000000 guild leader name of the character correctly.";
	mes "^FF0000NOTE^000000:";
	mes "^FF0000-^000000 Double check the spelling.";
	mes "^FF0000-^000000 The character name you";
	mes "  will be providing should";
	mes "  be a guild leader.";
	mes "^FF0000-^000000 New Guild leader should be online.";
	input .@char$;
	next;
	.@check = query_sql("SELECT `account_id` FROM `char` WHERE `name`='"+escape_sql(.@char$)+"'",.@acid);
	if (!.@check) {
		mes .npc$;
		mes "Character [ ^FF0000"+.@char$+"^000000 ] doesn't exist!";
		close;
	}
	query_sql("SELECT `guild_id` FROM `char` WHERE `name`='"+escape_sql(.@char$)+"'",.@gldid);
	if (!.@gldid){
		mes .npc$;
		mes "Character [ ^FF0000"+.@char$+"^000000 ] isn't registered to any guild!";
		close;
	}
	if (.gcdone == .@gldid){
		if ( .delay > gettimetick(2) ){
		mes .npc$;
		mes "You must wait [^FF0000"+(.delay - gettimetick(2))+"^000000] seconds,";
		mes .@char$+"'s guild has been currently";
		mes "modified and changed their";
		mes "guild leader authority.";
		close;
		}
	}
	query_sql("SELECT `name` , `master` , `guild_lv` FROM `guild` WHERE `guild_id` = '"+.@gldid+"'",.@gname$,.@gldmstr$,.@glvl);
	if (.@char$ != .@gldmstr$){
		mes .npc$;
		mes "Character [ ^FF0000"+.@char$+"^000000 ] isn't a guild master!";
		close;
	}
	mes .npc$;
	mes "Information:";
	mes "Guild Name: ^ff0000"+.@gname$+"^000000";
	mes "Guild Level: ^ff0000"+.@glvl+"^000000";
	mes "Guild Master: ^ff0000"+.@gldmstr$+"^000000";
	mes "Master, what would like to do?";
	next;
	switch(select("Change Guild Leader:Cancel")){
		case 1:
			mes .npc$;
			mes "Are you sure you wanted to change the guild leader?";
			next;
			if (select("Yes","No") != 2){
				mes .npc$;
				mes "Enter the ^FF0000new^000000 guild leader's name.";
				input .@cgl$;
				next;
				mes .npc$;
				mes "Please ^FF0000re-enter^000000 the name again for confirmation.";
				input .@cgl2$;
				next;
				if (.@cgl$ != .@cgl2$) {
					mes .npc$;
					mes "The names you entered doesn't match.";
					close;
				}
				.@check = query_sql("SELECT `account_id` FROM `char` WHERE `name`='"+escape_sql(.@cgl$)+"'",.@acid);
				if (!.@check){
					mes .npc$;
					mes "Character [ ^FF0000"+.@cgl$+"^000000 ] doesn't exist!";
					close;
				}
				if (.@cgl$ == .@char$) {
					mes .npc$;
					mes "Character [ ^FF0000"+.@cgl$+"^000000 ] is already the leader of [ ^FF0000"+.@gname$+"^000000 ] Guild.";
					close;
				}
				if (!isloggedin(getcharid(3,.@cgl$), getcharid(0,.@cgl$))){
					mes .npc$;
					mes "Character [ ^FF0000"+.@cgl$+"^000000 ] must be online to proceed!";
					close;
				}
				if (getcharid(2,.@cgl$) != .@gldid){
					mes .npc$;
					mes "Character [ ^FF0000"+.@cgl$+"^000000 ] is not a member of [ ^FF0000"+.@gname$+"^000000 ] Guild.";
					close;
				}
				mes .npc$;
				mes "Please confirm the following details before we proceed,";
				mes "Current Leader: ^FF0000"+.@char$+"^000000";
				mes "New Leader: ^FF0000"+.@cgl$+"^000000";
				mes "Would you like to proceed?";
				next;
				if (select("Close","^FF0000Confirmed^000000!") != 1) {
					close2;
					if (.gcdone == .@gldid){
						if ( .delay > gettimetick(2) ){
							message strcharinfo(0), "Transfer failed!";
							mes .npc$;
							mes "You must wait [^FF0000"+(.delay - gettimetick(2))+"^000000] seconds,";
							mes .@char$+"'s guild has been currently";
							mes "modified and changed their";
							mes "guild leader authority.";
							close;
						}
					}
					message strcharinfo(0), "You have succesfully transferred the guild!";
					announce strcharinfo(0)+" : The [ "+.@gname$+" ] Guild previously owned by "+.@char$+" succesfully transferred its ownership to "+.@cgl$+"!",bc_all,0xe57c00;
					guildchangegm(.@gldid,.@cgl$);
					.gcdone = .@gldid;
					if (.d)
					.delay = gettimetick(2) + .d;
					sleep2 1000;
					if (isloggedin(getcharid(3,.@cgl$), getcharid(0,.@cgl$))){
						atcommand "@kick "+.@cgl$;
					}
					if (isloggedin(getcharid(3,.@char$), getcharid(0,.@char$))){
						atcommand "@kick "+.@char$;
					}
					end;
				}
				close;			
			}
			close;
			
		case 2:
			close;
	}

OnInit:
	.reqlvl = 99; // Allowed GM level to use the NPC.
	.npc$ = "[ Guild Manager ]"; // You can change the NPC name through this.
	
// -- PLEASE DO NOT TOUCH THIS(FOR SAFETY PURPOSE). --
	.d = 60; // delay in seconds.
	bindatcmd "guildsetup",strnpcinfo(3)+"::OnMain",.reqlvl,.reqlvl;
	end;
}