/*
CREATE TABLE top3_event (
  time datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  account_id int(11) NOT NULL DEFAULT 0,
  char_id int(11) UNSIGNED NOT NULL DEFAULT 0,
  name varchar(23) NOT NULL DEFAULT '',
  class smallint(6) UNSIGNED NOT NULL DEFAULT 0
)
ENGINE = MYISAM
AVG_ROW_LENGTH = 45
CHARACTER SET latin1
COLLATE latin1_swedish_ci;

-	script	maxlevel	-1,{
*/
office,132,37,4	script	Top3	4_M_MINSTREL1,{
function ChkTop3; 
	mes "Cool reward for the ^4B29D5Top 3^000000 level 99/70 of each ^4B29D5Transcendent Second Class^000000. The reward will be sent automatically to your inventory when you hit level 99/70.";
	mes "REWARD:";
	mes "1st to reach 99/70";
	mes "        ^4B29D51x Flapping Angel Wing^000000";
	mes "        ^4B29D510k Credit Points^000000";
	mes "Top 3 Per Trans Class";
	mes "1st";
	mes"         ^4B29D51x Drooping Ninte Tail^000000";
	mes"         ^4B29D53k Credit Points^000000";
	mes "2nd";
	mes"         ^4B29D51x Observer^000000";
	mes"         ^4B29D52k Credit Points^000000";
	mes "3rd";
	mes"         ^4B29D51x Four Leaf Clover in the Mouth^000000";
	mes"         ^4B29D51k Credit Points^000000";
	while(1) {
		next;
		switch(select("Lord Knight","High Priest","High Wizard","Mastersmith","Sniper","Assassin Cross","Paladin","Champion","Professor","Stalker","Creator","Clown","Gypsy","Close")){

			case 1: set .@99class,4008;
				ChkTop3(.@99class);
				break;
			case 2: set .@99class,4009;
				ChkTop3(.@99class);
				break;
			case 3: set .@99class,4010;
				ChkTop3(.@99class);
				break;
			case 4: set .@99class,4011;
				ChkTop3(.@99class);
				break;
			case 5: set .@99class,4012;
				ChkTop3(.@99class);
				break;
			case 6: set .@99class,4013;
				ChkTop3(.@99class);
				break;
			case 7: set .@99class,4015;
				ChkTop3(.@99class);
				break;
			case 8: set .@99class,4016;
				ChkTop3(.@99class);
				break;
			case 9: set .@99class,4017;
				ChkTop3(.@99class);
				break;
			case 10: set .@99class,4018;
				ChkTop3(.@99class);
				break;
			case 11: set .@99class,4019;
				ChkTop3(.@99class);
				break;
			case 12: set .@99class,4020;
				ChkTop3(.@99class);
				break;
			case 13: set .@99class,4021;
				ChkTop3(.@99class);
				break;
			default:
				close;
		}
	}
		
OnInit:
	set .MaxLvl, 99;
	set .MaxJob, 70;
	set .topcount, 3;
	waitingroom "Top 3 Trans Reward",0;
	end;

function ChkTop3 {
	mes "1st 3 lvl 99/70 ^4B29D5"+jobname(getarg(0))+"^000000";
	mes "- - - - - - - - - - - - - -";	
	query_sql "SELECT `char_id`,`name` FROM `top3_event` WHERE `class`="+getarg(0), .@char_id, .@name$;
	if (!getarraysize(.@char_id)){
		mes "No ^4B29D5"+jobname(getarg(0))+"^000000 yet recorded";
	} else {
		for (.@i=0; .@i<getarraysize(.@char_id); .@i++) {
			mes (.@i+1) +". ^4B29D5"+.@char_id[.@i]+"^000000 - ^49D529"+.@name$[.@i]+"^000000";
		}
	}
	return;
}

OnPCBaseLvUpEvent:
OnPCJobLvUpEvent:
	set .@myClass,Class;
	set .@eac,eaclass();
	if ((BaseLevel==.MaxLvl) && (JobLevel==.MaxJob) && ( .@eac&EAJL_UPPER )){
		query_sql "SELECT count(`account_id`) FROM `top3_event`",.@count	;
		if (!.@count){
			chanmsg("#system","Congratulation! to our 1st level 99/70 player: [ "+strcharinfo(0)+" ] on reaching the maximum level 99/70 as "+jobname(Class)); 
			query_sql "INSERT INTO `top3_event` (time,account_id,char_id,name,class) VALUES ('"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"',"+getcharid(3)+","+getcharid(0)+",'"+escape_sql(strcharinfo(0))+"',"+.@myClass+")"; 
			getitem 5269,1; // Flapping Angel Wings
			callfunc "F_UpdateCredit",+10000;
			dispbottom "You received: Flapping Angel Wing";
			dispbottom "You received: 10k Credits";
			dispbottom "Congratulation for being the 1st to reach level 99/70 in the server.";
			end;
		}
		query_sql "SELECT count(class) FROM `top3_event` WHERE `class` = "+.@myClass,.@top3;
		if (.@top3 < .topcount ) {
			if (!.@top3) {
				query_sql "INSERT INTO `top3_event` (time,account_id,char_id,name,class) VALUES ('"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"',"+getcharid(3)+","+getcharid(0)+",'"+escape_sql(strcharinfo(0))+"',"+.@myClass+")"; 
				getitem 5252,1; // Drooping Nine Tail
				callfunc "F_UpdateCredit",+3000;
				chanmsg("#system","Congratulation to [ "+strcharinfo(0)+" ] for being the 1st "+jobname(Class)+" in the server."); 
				dispbottom "You received:Drooping Nine Tail";
				dispbottom "You received: 3k Credits";
				dispbottom "Congratulation for being the #"+(.@top3+1)+" "+jobname(Class);
			}
			if (.@top3 == 1) {
				query_sql "INSERT INTO `top3_event` (time,account_id,char_id,name,class) VALUES ('"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"',"+getcharid(3)+","+getcharid(0)+",'"+escape_sql(strcharinfo(0))+"',"+.@myClass+")"; 
				getitem 5315,1; // Observer
				callfunc "F_UpdateCredit",+2000;
				dispbottom "You received: Observer";
				dispbottom "You received: 2k Credits";
				chanmsg("#system","Congratulation to [ "+strcharinfo(0)+" ] for being the 2nd "+jobname(Class)+" in the server."); 
				dispbottom "Congratulation for being the #"+(.@top3+1)+" "+jobname(Class);
			}
			if (.@top3 == 2) {
				query_sql "INSERT INTO `top3_event` (time,account_id,char_id,name,class) VALUES ('"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"',"+getcharid(3)+","+getcharid(0)+",'"+escape_sql(strcharinfo(0))+"',"+.@myClass+")"; 
				getitem 5596,1; // Four Leaf Clover
				callfunc "F_UpdateCredit",+1000;
				chanmsg("#system","Congratulation to [ "+strcharinfo(0)+" ] for being the 3rd "+jobname(Class)+" in the server."); 
				dispbottom "You received: Four Leaf Clover in Mouth";
				dispbottom "You received: 1k Credits";
				dispbottom "Congratulation for being the #"+(.@top3+1)+" "+jobname(Class);
			}
		}
	
	}
}

