/*
create table poll_title (
poll_id int(11) unsigned not null auto_increment primary key,
poll_title varchar(70),
char_name varchar(23),
poll_time datetime,
poll_level smallint(6),
poll_status enum('0','1') default '1'
) engine = innodb;

create table poll_options (
poll_id int(11) unsigned,
poll_option tinyint(4) unsigned,
poll_option_name varchar(70),
poll_votes int(11) unsigned not null,
key (poll_id)
) engine = innodb;

create table poll_votes (
poll_id int(11) unsigned,
poll_option tinyint(4) unsigned,
account_id int(11) unsigned,
ipaddress varchar(100),
name varchar(23),
key (poll_id),
key (poll_option),
key (account_id),
key (ipaddress),
unique ( poll_id, poll_option, account_id ),
unique ( poll_id, poll_option, ipaddress )
) engine = innodb;
*/

office,132,37,3	script	Poll NPC	4_M_MINSTREL1,{
	mes "["+ strnpcinfo(1) +"]";
	query_sql "select poll_id, poll_title, char_name, date_format( poll_time, '%W %e/%b/%y %l:%i:%p' ), poll_level from poll_title order by poll_id desc limit 1", .@id, .@title$, .@creator$, .@time$, .@level;

	// Check Guild
	set .@guild_id,strcharinfo(2);
	if ( getcharid(0) != getguildmasterid(getcharid(2))) {
		mes "This poll can be viewed only by Guild Masters.";
		close;
	}
	query_sql "SELECT guild_lv FROM guild WHERE guild_id = "+getcharid(2), .@guild_lv;
	if (.@guild_lv <= 9) {
		mes "This poll can be viewed only by Guild Masters with atlease 10 Guild Level.";
		close;
	}
	if ( BaseLevel < .@level ) {
		mes "You must be level "+ .@level +" to view and vote in this Poll";
		close;
	}
	if ( !query_sql( "select account_id, ipaddress from poll_votes where poll_id = "+ .@id +" and ( account_id = "+ getcharid(3) +" or ipaddress = '"+ escape_sql( getcharip() ) +"' )", .@aid, .@ip$ ) ) {
		mes "We are about to run the most exciting feature of the game";
		mes "The War of Emperium with initial setting:";
		mes "- Only 1 castle will be opened for siege and will be rotated as follows:";
		mes "^0000FFWeek1^000000: Prontera Castle";
		mes "^0000FFWeek2^000000: Payon Castle";
		mes "^0000FFWeek3^000000: Aldebaran Castle";
		mes "^0000FFWeek4^000000: Geffen Castle";
		next;
	
		//mes "By : ^0000FF"+ .@creator$ +"^000000";
		//mes "Since : ^0000FF"+ .@time$ +"^000000";
		//mes " ";
		mes "Question : ^0000FF"+ .@title$ +"^000000";
		.@nb = query_sql( "select poll_option_name from poll_options where poll_id = "+ .@id, .@options$ );
		for ( .@i = 0; .@i < .@nb; ++.@i )
			.@menu$ = .@menu$ +"^0000FF"+ .@options$[.@i] +":";
		.@s = select( .@menu$ );
		query_sql "insert into poll_votes values ( "+ .@id +", "+ .@s +", "+ getcharid(3) +", '"+ escape_sql(getcharip()) +"', '"+ escape_sql( strcharinfo(0) )+"' )";
		query_sql "update poll_options set poll_votes = poll_votes +1 where poll_id = "+ .@id +" and poll_option = "+ .@s;
		next;
		mes "["+ strnpcinfo(1) +"]";
	}
		mes "We are about to run the most exciting feature of the game";
		mes "The War of Emperium with initial setting:";
		mes "- Only 1 castle will be opened for siege and will be rotated as follows:";
		mes "^0000FFWeek1^000000: Prontera Castle";
		mes "^0000FFWeek2^000000: Payon Castle";
		mes "^0000FFWeek3^000000: Aldebaran Castle";
		mes "^0000FFWeek4^000000: Geffen Castle";
		next;
	//mes "By : ^0000FF"+ .@creator$ +"^000000";
	//mes "Since : ^0000FF"+ .@time$ +"^000000";
	//mes " ";
	mes "Question : ^0000FF"+ .@title$ +"^000000";
	query_sql "select sum(poll_votes) from poll_options where poll_id = "+ .@id, .@total_votes;
	.@nb = query_sql( "select poll_option_name, poll_votes, poll_votes / "+ .@total_votes +" * 1000, poll_votes / "+ .@total_votes +" * 35 from poll_options where poll_id = "+ .@id +" order by poll_votes desc", .@options$, .@votes, .@percent, .@chart );
	mes " ";
	.@j = 0;
	while ( .@j < .@nb ) {
		mes ( .@j +1 )+". ^0000FF"+ .@options$[.@j] +"^000000";
		.@i = 0;
		while ( .@i < .@chart[.@j] ) {
			.@str$[.@j] = .@str$[.@j] +"#";
			++.@i;
		}
		if ( .@i < 35 ) {
			.@str$[.@j] = .@str$[.@j] +"^CCCCCC";
			while ( .@i < 35 ) {
				.@str$[.@j] = .@str$[.@j] +"#";
				++.@i;
			}
		}
		mes .@str$[.@j];
		mes "^999999"+ .@votes[.@j] +"(votes) / "+ .@total_votes +"(total) = ^CC6600"+( .@percent[.@j] / 10 ) +"."+( .@percent[.@j] % 10 )+"%^000000";
		++.@j;
		mes " ";
	}
	if ( .@aid == getcharid(3) )
		mes "You have already voted using this account.";
	else if ( !strcmp( .@ip$, getcharip() ) )
		mes "You have already voted using this IP address.";
	close;
OnInit:
	waitingroom "New Poll", 0;
	end;
}

-	script	poll	FAKE_NPC,{
OnWhisperGlobal:
	query_sql "select poll_id, poll_title, char_name, date_format( poll_time, '%W %e/%b/%y %l:%i:%p' ), poll_status from poll_title order by poll_id desc limit 1", .@id, .@title$, .@name$, .@time$, .@status;
	if ( .@status == 1 ) {
		mes "Close this poll ?";
		mes " ";
		mes "By : ^0000FF"+ .@name$ +"^000000";
		mes "Since : ^0000FF"+ .@time$ +"^000000";
		mes " ";
		mes "Question : ^0000FF"+ .@title$ +"^000000";
		next;
		if ( select ( "No:Yes" ) == 1 ) close;
		//announce strcharinfo(0) +" has closed the poll ["+ .@title$ +"]", 0;
		query_sql "update poll_title set poll_status = '0' where poll_id = "+ .@id;
		delwaitingroom "Poll NPC";
		disablenpc "Poll NPC";
		close;
	}
	.@title$ = ""; // hmm .... -.-"
	while (true) {
		mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
		.@size = getarraysize(.@options$);
		if ( !.@size )
			mes "Options : ^FF0000Incomplete^000000";
		else
			for ( .@i = 0; .@i < .@size; ++.@i )
				mes "Option "+( .@i+1 )+" : ^0000FF"+ .@options$[.@i] +"^000000";
		if ( .@baselevel ) {
			mes " ";
			mes "Base Level Requirement : "+ .@baselevel;
		}
		next;
		switch ( select( "Question"+( ( getstrlen(.@title$) )? "" : " - ^FF0000Incomplete^000000" ),
			"Options "+( ( .@size >= 2 )? "^00CC00["+ .@size +"]" : "^FF0000["+ .@size +"] - Incomplete" )+"^000000",
			"Base Level Requirement",
			( ( getstrlen(.@title$) && .@size >= 2 )? "^00CC00":"^FF0000" )+"Complete Setup" ) ) {
			case 1:
				mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
				mes " ";
				mes "Input the poll question";
				next;
				if ( input( .@tmp$, 2, 70 ) ) {
					mes "Poll title must between 2~70 characters";
					next;
					break;
				}
				else if ( compare ( .@tmp$, ":" ) || compare ( .@tmp$, "^" ) ) {
					mes "Poll Question must not allow to use these symbols";
					mes "-> : <- colon";
					mes "-> ^ <- caret";
					next;
					break;
				}
				mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
				mes " ";
				mes "Confirming this title ?";
				mes "^0000FF"+ .@tmp$ +"^000000";
				next;
				if ( select ( "Yes", "No" ) == 2 ) break;
				.@title$ = .@tmp$;
				break;
			case 2:
				while (1) {
					mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
					.@size = getarraysize(.@options$);
					if ( !.@size )
						mes "Options : ^FF0000Incomplete^000000";
					else
						for ( .@i = 0; .@i < .@size; ++.@i )
							mes "Option "+( .@i+1 )+" : ^0000FF"+ .@options$[.@i] +"^000000";
					next;
					switch ( select( "Add new Option", "Delete an Option", "- back" ) ) {
						case 1:
							mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
							.@size = getarraysize(.@options$);
							if ( !.@size )
								mes "Options : ^FF0000Incomplete^000000";
							else
								for ( .@i = 0; .@i < .@size; ++.@i )
									mes "Option "+( .@i+1 )+" : ^0000FF"+ .@options$[.@i] +"^000000";
							mes " ";
							if ( .@size == 30 ) {
								mes "The Poll only allow up to 30 different options";
								next;
								break;
							}
							mes "Input Option No."+( .@size +1 );
							next;
							if ( input( .@tmp$, 2, 70 ) ) {
								mes "Poll options must between 2~70 characters";
								next;
								break;
							}
							else if ( compare ( .@tmp$, ":" ) || compare ( .@tmp$, "^" ) ) {
								mes "Poll title must not allow to use these symbols";
								mes "-> : <- colon";
								mes "-> ^ <- caret";
								next;
								break;
							}
							.@options$[ .@size ] = .@tmp$;
							break;
						case 2:
							mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
							.@size = getarraysize(.@options$);
							if ( !.@size )
								mes "Options : ^FF0000Incomplete^000000";
							else
								for ( .@i = 0; .@i < .@size; ++.@i )
									mes "Option "+( .@i+1 )+" : ^0000FF"+ .@options$[.@i] +"^000000";
							mes " ";
							if ( !.@size ) {
								mes "There are no Options to remove";
								next;
								break;
							}
							.@menu$ = "";
							for ( .@i = 0; .@i < .@size; ++.@i )
								.@menu$ = .@menu$ +"^0000FF"+ .@options$[.@i] +":";
							.@menu$ = .@menu$ +"^000000- back";
							next;
							.@s = select( .@menu$ ) -1;
							if ( .@s == .@size ) break;
							deletearray .@options$[ .@s ], 1;
							break;
						case 3:
							break;
					}
					if ( @menu == 3 ) break;
				}
				break;
			case 3:
				mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
				mes " ";
				mes "Base Level Requirement : "+ .@baselevel;
				next;
				if ( input( .@tmp, 0, MAX_LEVEL ) ) {
					mes "Question : "+( ( getstrlen(.@title$) )? "^0000FF"+ .@title$ : "^FF0000Incomplete" )+"^000000";
					mes " ";
					mes "Invalid range";
					next;
				}
				else
					.@baselevel = .@tmp;
				break;
			case 4:
				if ( !getstrlen(.@title$) ) {
					mes "Question must not be leaving blank !";
					next;
					break;
				}
				else if ( .@size < 2 ) {
					mes "Must setup at least 2 Options !";
					next;
					break;
				}
				query_sql "insert into poll_title values ( null, '"+ escape_sql(.@title$) +"', '"+ escape_sql( strcharinfo(0) ) +"', now(), "+ .@base_level +", '1' )";
				query_sql "select max(poll_id) from poll_title", .@poll_id;
				.@poll_options$ = "insert into poll_options values ";
				for ( .@i = 0; .@i < .@size; ++.@i )
					.@poll_options$ = .@poll_options$ +"( "+ .@poll_id +", "+( .@i +1 )+", '"+ escape_sql(.@options$[.@i]) +"', 0 )"+( ( .@i < .@size -1 )? ",":";" );
				query_sql .@poll_options$;
				announce strcharinfo(0) +" has open a New Poll for Guild Masters !!! ["+ .@title$ +"]  visit the Office to cast your vote.", 0;
				enablenpc "Poll NPC";
				donpcevent "Poll NPC::OnInit";
				close;
		}
	}
	close; // shouldn't reach
OnInit:
	query_sql "select poll_status from poll_title order by poll_id desc limit 1", .@status;
	if ( !.@status ) {
		sleep 1;
		delwaitingroom "Poll NPC";
		disablenpc "Poll NPC";
	}
	bindatcmd "poll", strnpcinfo(3)+"::OnWhisperGlobal";
	end;
}