//@unloadnpc Guild Invitation
//@loadnpc npc/thor/guildpack/guildpack.txt

new_1-1,66,108,4	script	Guild Invitation	4_MAL_CAPTAIN,{
function recheck;
	mes(.npc$);
	.@guildmaster = getcharid(CHAR_ID_ACCOUNT);
	if (strcharinfo(PC_NAME) == getguildmaster(getcharid(CHAR_ID_GUILD)))
		mes("Hello Guild Master ^ff0000"+strcharinfo(PC_NAME)+"^000000");
	else if (strcharinfo(PC_GUILD) != "")
		mes("Hello strong warrior of ^ff0000"+getguildname(getcharid(CHAR_ID_GUILD))+"^000000");
	else
		mes("Hello ^ff0000"+strcharinfo(PC_NAME)+"^000000");

	mes("What would you like to do?");
	next;
	switch(select(
		((getgmlevel() < .GMAccess)?":":" > ^FF0000GM Menu^000000:")+
		 " > Information:"+
		  ((countitem(Invitation_Letter) && strcharinfo(PC_NAME) == getguildmaster(getcharid(CHAR_ID_GUILD)))?" > ^0055FFClaim Package^000000:":":")+
		  ((countitem(Invitation_Letter) && strcharinfo(PC_NAME) == getguildmaster(getcharid(CHAR_ID_GUILD)) && !raiseguild )?" > ^0055FFRaise Guild Level^000000:":":")+
		 " > ^777777Close^000000"
	)) {
	case 1:
		mes(.npc$);
		while(1) {
			switch(select(
				"Guild Size [ ^28bf00"+.guild_min+"^000000 ]:"+
				 " > ^777777Close^000000"
			)) {
			case 1: input .guild_min,1; break;
			default:
				close();
			}
		}
		break;

 	case 2:
		mes(.npc$);
		mes("Good day!!!");
		mes("As part of our server growth campaign, we have prepared a guild migration from other server to join our WoE.  Each member will get:");
		mes("- ^ff0000Instant 99/50 2nd Job of your choice^000000");
		mes("- ^ff0000Full CLASSIC +7 EQ with card of your choice^000000");
		mes("- ^ff0000"+callfunc("F_InsertComma",.zeny)+"z Zeny^000000");
		mes("- ^ff0000"+callfunc("F_InsertComma",.credits)+"c Credits^000000");
		mes("^ff0000Guild Master will receive:^000000");
		mes(Itemlink(CRO_Woe_Crown));
		next; 
		mes(.npc$);
		mes("Who Can Avail this package?");
		mes("- ^ff0000"+.guild_min+"^000000 members minimum");
		mes("- must be active on other server WoE");
		mes("- must be using PC Client Only.");
		break;
	case 3:
		mes(.npc$);
		mes("Please wait ...");
		// Check min guild requirements
		// get the charID and accountID of character's guild members
		getguildmember getcharid(CHAR_ID_GUILD), 1;	
		getguildmember getcharid(CHAR_ID_GUILD), 2;	
		if ( $@guildmembercount < .guild_min ) {
			mes(" - Guild Member Size: ^ff0000Failed^000000");
			dispbottom ("Guild member size must be atleast "+.guild_min+" members.");
			close();
		}
		mes(" - Guild Member Size: ^28bf00Passed^000000");

		// Check if all members are online.
		.@mobilecount = 0; .@registered = 0; .@dupe = 0;
		for ( .@i = 0; .@i < $@guildmembercount; .@i++ ) {
			if ( isloggedin( $@guildmemberaid[.@i], $@guildmembercid[.@i] ) ) {
				query_sql "SELECT `name` FROM `char` WHERE `char_id` = "+$@guildmembercid[.@i],.@name$;
				.@count_online++;
				query_sql "SELECT `last_ip`, count(`last_unique_id`) FROM login WHERE `account_id` = "+ $@guildmemberaid[.@i], .@last_ip$,.@unique;
				if(.@last_ip$==.ismobile$){
					.@mobilecount++;
					npctalk("[ "+.@name$+" ] is using Mobile Client. Please remove him from your guild to proceed.","Guild Invitation");
				}
				if(.@unique > 1){
					.@dupe++;
					npctalk("[ "+.@name$+" ] machine was used to claim previously. Please remove him from your guild to proceed.","Guild Invitation");
				}
				if(query_sql("SELECT `char_id`, `isclaimed` FROM `guildpack` WHERE `char_id` = "+$@guildmembercid[.@i],.@dummy,.@dummy2)){
					if(.@dummy) {
						if (!.@dummy2) {
							.@registered++;
						} else {
							npctalk("[ "+.@name$+" ] has previous claim in our record. Please remove him from your guild to proceed.","Guild Invitation");
						}
						
					} else {
						npctalk("[ "+.@name$+" ] was not in the list. Please remove him from your guild to proceed.","Guild Invitation");
					}
				}
			}
		}	
		
		// Check if all is online
		if ( .@count_online != $@guildmembercount ) {
			mes(" - Member's Online: ^ff0000Failed^000000");
			dispbottom("There are "+.@count_online+" / "+$@guildmembercount+" members online.");
			close();
		}
		mes(" - Member's Online: ^28bf00Passed^000000");

		// Check if using mobile
		if (.@mobilecount) {
			mes(" - All using PC: ^ff0000Failed^000000");
			dispbottom("There are "+.@mobilecount+" member in the guild is using Android client.");	
			close();
		}
		mes(" - All using PC: ^28bf00Passed^000000");

		// Check if has previous claim
		if (.@dupe) {
			mes(" - Claim Record: ^ff0000Failed^000000");
			dispbottom("There are "+.@dupe+" member in your guild that is using a machine used to claim before.");	
			close();
		}
		mes(" - Claim Record: ^28bf00Passed^000000");
		/*	
		// Check if all is listed	
		if ( .@registered < $@guildmembercount ) {
			mes(" - Claim Status: ^ff0000Failed^000000");
			dispbottom("There are "+($@guildmembercount-.@registered)+" / "+$@guildmembercount+" members is not in our list of candidate.");
			close();
		}
		*/	
		mes(" - Claim Status: ^28bf00Passed^000000");

		mes(". . .");
		mes("All checks has passed. Do you wish to proceed now?"); 
		next;
		if (select("No, we are not ready.", "Yes Please.") == 1) {
			mes(.npc$);
			mes ("Okay, let me know when you are ready.");
			close();		
		}
		if (!recheck()){
			mes(.npc$);	
			mes("Ohhh!!! System has detected some changes on your guild.");
			mes("Please try again.");	
			close();
		}		
		delitem(Invitation_Letter,1);

		// Send all member to room
		$GuildClaim = getcharid(CHAR_ID_GUILD);
		getguildmember($GuildClaim,2);
		set .@guildmembercount, $@guildmembercount;
		copyarray .@guildmemberaid[0], $@guildmemberaid[0], .@guildmembercount;
		for ( .@i = 0; .@i < .@guildmembercount; .@i ++) {
			if (attachrid(.@guildmemberaid[.@i])) {
				query_sql "SELECT `last_unique_id` FROM login WHERE account_id = "+.@guildmemberaid[.@i],.@last_unique_id$;
				query_sql("UPDATE `guildpack` SET `claimdate`= NOW(), `isclaimed`= 1, `last_unique_id` = "+escape_sql(.@last_unique_id$)+" WHERE `char_id` = "+getcharid(0) );
				Zeny = Zeny + .zeny;
				callfunc("F_UpdateCredit", .credits);
				nude();
				atcommand("@itemreset");
				getitem (Invitation_Letter,1);
				if (strcharinfo(PC_NAME) == getguildmaster(getcharid(CHAR_ID_GUILD))){
					getguildmember getcharid(CHAR_ID_GUILD), 1;	
					announce("A new guild is born!!! [ "+getguildname(getcharid(CHAR_ID_GUILD))+" ] headed by [ "+strcharinfo(PC_NAME)+" ] together with his "+$@guildmembercount+" strong army has just claimed their Guild Migration Package.  Let's give them warm welcome.", bc_all);
				}
				warp .mapName$,.warpx,.warpy;
			}
		}
		close();
	case 4:
		atcommand("@guildlvl 13");
		raiseguild = 1;
		close();
	default:
		close();
	}
	close();


function	recheck	{
		// Check min guild requirements
		// get the charID and accountID of character's guild members
		getguildmember getcharid(CHAR_ID_GUILD), 1;	
		getguildmember getcharid(CHAR_ID_GUILD), 2;	 
		if ( $@guildmembercount < .guild_min ) {
			dispbottom ("GUild member count must be atleast "+.guild_min+" members.");
			return 0;
		}
		// Check if all members are online.
		.@mobilecount = 0; .@registered = 0; .@dupe = 0;
		for ( .@i = 0; .@i < $@guildmembercount; .@i++ ) {
			if ( isloggedin( $@guildmemberaid[.@i], $@guildmembercid[.@i] ) ) {
				.@count_online++;
			}
			query_sql "SELECT `last_ip`, count(`last_unique_id`) FROM login WHERE `account_id` = "+ $@guildmemberaid[.@i], .@last_ip$,.@unique;
			if (.@last_ip$==.ismobile$) {
				.@mobilecount++;
			}
			if (.@unique > 1) {
				.@dupe++;
			}
			if (query_sql("SELECT `char_id`, `isclaimed` FROM `guildpack` WHERE `char_id` = "+$@guildmembercid[.@i],.@dummy,.@dummy2)) {
				if(.@dummy) {
					if (!.@dummy2) {
						.@registered++;
					}
				}

			}
		}	
		if ( .@count_online != $@guildmembercount ) {
			dispbottom("There are "+.@count_online+" / "+$@guildmembercount+" members online.");
			return 0;
		}
		if (.@dupe) {
			dispbottom("There are "+.@dupe+" member in your guild that is using a machine used to claim before.");	
			return 0;
		}	
		if (.@mobilecount) {
			dispbottom("There are "+.@mobilecount+" member in the guild is using Android client.");	
			return 0;
		}
		/*
		if ( .@registered < $@guildmembercount ) {
			dispbottom("There are "+($@guildmembercount-.@registered)+" / "+$@guildmembercount+" members are not invited or already claimed the package.");
			return 0;
		}
		*/
		return 1;
}

OnInit:
   .GMAccess = 99;
   .mapName$ = "office";
   .warpx = 167;
   .warpy = 135;	
   .ismobile$="188.166.223.216";	
   .npc$ = "[ "+CStr("Guild Migration Assistant","GREEN")+" ]";
   .guild_min = 12;
   .zeny = 10000000;
   .credits = 20000;		
   end;
}	
