//@unloadnpc Top3
//@loadnpc npc/amatsu/events/race.txt

-	script	Race Event	-1,{
function ChkTop3; function LeaderBoard; 
	mes "Cool reward for the ^4B29D5Top^000000 level 99/50 of each ^4B29D52nd Job^000000 and ^4B29D5Extended Job^000000.";
	mes "1st to reach 99/50:";	
	mes "^4B29D5 2x Body Armor Certifate (+7)^000000";
	mes "^4B29D5 1x Duneyrr Helm^000000";
	mes "^4B29D5 2x Old Card Album^000000";
	mes "^4B29D510k Cash Points^000000";
	mes "- - - - -";
	mes "Top 1 Per Class";
	mes "^4B29D5 1x Body Armor Certifate (+7)^000000";
	mes "^4B29D5 1x Baseball Cap^000000";
	mes "^4B29D5 1x Old Card Album^000000";
	mes "^4B29D5 5k Cash Points^000000";	
	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;
		if(.@total == .topcount){
			.@menu$ += sprintf("^ff0000%s:", jobname(.jobs[.@i])+" [ "+.@total+"/"+.topcount+" ]^000000"); 
		} else {
			.@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, 1;
	waitingroom "Race-To-99 Event",0;
	setarray(.jobs[1],7,8,9,10,11,12,14,15,16,17,18,19,20);
	.gm_accounts$ = "2000000,2000003,2000004,2000033";
	end;

OnPCBaseLvUpEvent:
OnPCJobLvUpEvent:
	set .@myClass,Class;
	if ( Class == Job_Knight || 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,.@top;
			query_sql "SELECT count(class) FROM `top3_event`",.@hasrecord;	
			if (!.@hasrecord){
				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 6234,2; // Safe Refine
					getitem 5762,1; // Dunner Cap
					getitem 616,2;  // Old Card Album
					#CASHPOINTS += 10000;
					dispbottom "You received: 2x "+getitemname(6234)+" | 1x "+getitemname(5762)+" | 2x "+getitemname(616)+" | 10k Cash Points";
					chanmsg("#system","Congratulation to [ "+strcharinfo(0)+" ] our 1st 99/50 "+jobname(Class)+" in the server."); 
					dispbottom "Congratulation you are the 1st 99/50 "+jobname(Class);
					race2claimed=1;
					end;
				}	
			}		
			if (!.@top ) {
				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 6234,1; // Safe Refine
					getitem 5147,1; // Baseball Cap
					getitem 616,1;  // Old Card Album
					#CASHPOINTS += 5000;
					dispbottom "You received: 2x "+getitemname(6234)+" | 1x "+getitemname(5147)+" | 1x "+getitemname(616)+" | 5k Cash Points";
					chanmsg("#system","Congratulation to [ "+strcharinfo(0)+" ] our 1st 99/50 "+jobname(Class)+" in the server."); 
					dispbottom "Congratulation you are the 1st 99/50 "+jobname(Class);
					race2claimed=1;
					end;
				}
			}
		}
	}
	end;

function LeaderBoard {
	mes "Who is currently on the race?";
	mes "- - - - - - - - - - - - - -";	
	query_sql ("SELECT account_id,char_id,name, class,base_level, job_level FROM `char` WHERE base_level < 100 AND account_id NOT IN ( "+.gm_accounts$+" ) ORDER BY base_level DESC LIMIT 30"), .@account_id,.@char_id,.@name$, .@class, .@blevel, .@jlevel;
	if (getarraysize(.@name$)){
		for (.@i=0; .@i<getarraysize(.@name$); .@i++) {
			query_sql "SELECT count(class) FROM `top3_event` WHERE `class` = "+.@class[.@i],.@completed;
			if(!.@completed && .@class[.@i] > 6){
				mes "[ ^4B29D5"+.@name$[.@i]+"^000000 ]";
				mes jobname(.@class[.@i])+"  "+.@blevel[.@i]+"/"+.@jlevel[.@i];
			}
		}
	} else {
		mes "No entry recorded";
	}
	return;
}	

function ChkTop3 {
	mes "Top 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;
}

}

payon,74,121,6	duplicate(Race Event)	Race Event#prt	4_M_ORIENT02