//@unloadnpc Top3
//@loadnpc npc/amatsu/event/raceto99.txt

-	script	Top3	-1,{
function ChkTop3; function LeaderBoard; 
	mes "Cool reward for the ^4B29D5Top 3^000000 level 99/50 of each ^4B29D5Super Novice^000000 and ^4B29D52nd Job^000000. The reward will be sent automatically to your inventory when you hit level 99/50. Only NOVICE created from April 25 (6PM) onwards may join and this event will end on May 25.";
	mes "Top 3 Per Class";
	if (#mobile) {
		mes "^4B29D51x Brilliant Golden Wings^000000";
		mes "A radiant wing. Enable the use of Lvl 1 Teleport when equip.";
		mes "Costume Garmet (Account Bound)";	
	} else {		
		mes Itemlink(20727);
		mes "Costume Garmet (Account Bound)";	
		//mes"^4B29D510k Credit Points^000000";
	}
	mes "- - - - -";
		next;
		.@menu$ = "";
		.@menu$ += "Leaderboard:";
		for (.@i = 1; .@i < getarraysize(.jobs); ++.@i) {
			query_sql "SELECT count(`char_id`) AS total FROM `top3_event` WHERE `class`="+.jobs[.@i], .@total;
			.@menu$ += sprintf("%s:", jobname(.jobs[.@i])+" [ "+.@total+"/"+.topcount+" ]"); 
		}
		.@menu$ += "Cancel";
		.@s = select(.@menu$) -1;
		if (.@s == 0) {
			 LeaderBoard();
			 close();
		}
		if (.@s == getarraysize(.jobs)) {
			 close();
		}
		ChkTop3(.jobs[.@s]);
		close;
	
OnInit:
	set .MaxLvl, 99;
	set .MaxJob, 50;
	set .topcount, 3;
	waitingroom "Race-To-99 Event 2",0;
	setarray(.jobs[0],0,7,8,9,10,11,12,14,15,16,17,18,19,20,23);
	end;

OnPCLoginEvent:
	if (Class == Novice && JobLevel == 1 && BaseLevel == 1){
		if (!race2) {
			race2 = 1;
			dispbottom "Congratulation, you are entitled to join Race-to-99/50 Event. Visit our FB Page/Group for more info.";
			end;
		}
	}
	end;

OnPCBaseLvUpEvent:
OnPCJobLvUpEvent:
	if (!race2) end;
	set .@myClass,Class;
	if ( Class == Job_Knight || Class == Job_SuperNovice || Class == Job_Priest || Class == Job_Wizard || Class == Job_Blacksmith || Class == Job_Hunter || Class == Job_Assassin || Class == Job_Crusader || Class == Job_Monk || Class == Job_Sage || Class == Job_Rogue || Class == Job_Alchemist || Class == Job_Bard || Class == Job_Dancer ) {
		if ((BaseLevel==.MaxLvl) && (JobLevel==.MaxJob)){
			query_sql "SELECT count(class) FROM `top3_event` WHERE `class` = "+.@myClass,.@top3;
			if (.@top3 < .topcount ) {
				if (!race2claimed) {				
					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 20727,1; // Brilliant Golden Wing
					//callfunc "F_UpdateCredit",+10000;
					dispbottom "You received: 1x "+getitemname(20727);
					//dispbottom "You received: 10k Credits";
					chanmsg("#system","Congratulation to [ "+strcharinfo(0)+" ] for one of being the Top "+jobname(Class)+" in the server."); 
					dispbottom "Congratulation for one of being the Top "+jobname(Class);
					race2claimed=1;
				}
			}
		}
	}
	end;

function LeaderBoard {
	mes "Who is currently on the race?";
	mes "- - - - - - - - - - - - - -";	
	query_sql ("SELECT c.name, c.class,c.base_level, c.job_level FROM `char` AS c INNER JOIN `char_reg_num_db` AS crnd ON c.char_id = crnd.char_id WHERE crnd.key= 'race2' AND c.base_level < 99 ORDER BY c.base_level DESC LIMIT 30"), .@name$, .@class, .@blevel, .@jlevel;
	if (getarraysize(.@name$)){
		for (.@i=0; .@i<getarraysize(.@name$); .@i++) {
			mes (.@i+1) +". [ ^4B29D5"+.@name$[.@i]+"^000000 ]";
			mes "     [ "+jobname(.@class[.@i])+" ] "+.@blevel[.@i]+"/"+.@jlevel[.@i];
		}
	} else {
		mes "No entry recorded";
	}
	return;
}	

function ChkTop3 {
	mes "1st 3 lvl 99/50 ^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"+.@name$[.@i]+"^000000 ]";
		}
	}
	return;
}

}

prontera,141,180,0	duplicate(Top3)	Top3#prt	4_M_MINSTREL1