//@unloadnpc Top Ranking
//@loadnpc npc/thor/ranking.txt

prontera,137,94,6	script	Top Ranking	4_M_05,{

	.@cid = getcharid(0);
	.@gm_level = getgmlevel();

	mes "^0055FF[ ::: Top Ranking ::: ]^000000";
	mes "What can I do for you today?";
	next;
	switch( select(
		((getgmlevel() < 99)?":":"^FF0000Reset PvP Ranking^000000:")+
		"Top PvP Ranking:"+
		"Top MvP Ranking:"+
		"Close"
	 )){
	case 1: // Reset Global Ranking 
		mes "Are you sure you want to reset PvP Ladder?";
		if(select("No:Yes")!=2) close;
			freeloop 1;
			dispbottom "Clearing PvP Rank Table";
			query_sql "TRUNCATE TABLE `pvp_rank`";
			dispbottom "Clearing PvP Record for Online Players";
			query_sql( "SELECT COUNT(`account_id`) FROM `char` WHERE `online` = 1 ", .@total );
			while( .@count < .@total ){
				query_sql( "SELECT `account_id`,`name` FROM `char` WHERE `online` = 1 ORDER BY `account_id` LIMIT 128 OFFSET "+.@offset, .@aid,.@name$ );
				set .@i,0;
				set .@size,getarraysize( .@aid );	
				while( .@i < .@size ){
					if (isloggedin(.@aid[.@i])) {
						set Kill, 0;
						set Death, 0;
						set KillingStreak, 0; 
						set MultiKill, 0;
						set KillingSpree, 0;
						set Dominating, 0;
						set MegaKill, 0;
						set Unstoppable, 0;
						set WickedSick, 0;
						set MonsterKill, 0;
						set Godlike, 0;
						set BeyondGodlike, 0;
						set DoubleKill, 0;
						set TripleKill, 0;
						set UltraKill, 0;
						set Rampage, 0;
						set Ownage, 0;
						set NemesisKill, 0;
						set FeedCount, 0;
						dispbottom "Clearing Player "+.@aid[.@i]+" Name: "+.@name$[.@i];
					}	
					set .@count,.@count + 1;
					set .@i,.@i + 1;
				}
				set .@offset,.@offset + .@size;
				deletearray .@aid,.@size;
				deletearray .@name$,.@size;
			}
			dispbottom "Clearing PvP Record for Offline Players";
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'Kill'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'Death'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'KillingStreak'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'MultiKill'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'KillingSpree'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'Dominating'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'MegaKill'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'Unstoppable'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'WickedSick'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'MonsterKill'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'Godlike'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'BeyondGodlike'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'DoubleKill'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'TripleKill'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'UltraKill'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'Rampage'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'Ownage'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'NemesisKill'");
			query_sql ("DELETE FROM `char_reg_num_db` WHERE `key` = 'FeedCount'");
			announce "[PvP Manager] PvP Statistic has been reset.",bc_self,0xFF0000;
		close;
		case 2: //Player Ranking
		next; 
		if( getvariableofnpc(.ServerType, "Versus") ) {
			mes "[Top "+getvariableofnpc(.Top, "Versus")+" PvP Players]";
			mes "^0000FFKill^000000 | ^FF0000Death^000000 | ^FF22FFKDR^000000 | ^00FF00SK^000000 | ^666666MK^000000";
			query_sql "SELECT `char`, `kill`, `death`, `kdr`, `killingstreak`, `multikill` FROM `pvp_rank` ORDER BY `pvp_rank`.`kill` DESC LIMIT "+getvariableofnpc(.Top, "Versus")+"", .@char$, .@kill, .@death, .@kdr$, .@sk, .@mk;
			for(set .@i, 0; .@i < getarraysize(.@char$); set .@i, .@i + 1){
				mes .@i+1+". "+.@char$[.@i]+" ^0000FF"+.@kill[.@i]+"^000000 ^FF0000"+.@death[.@i]+"^000000 ^FF22FF"+.@kdr$[.@i]+"^000000 ^00FF00"+.@sk[.@i]+"^000000 ^666666"+.@mk[.@i]+"^000000 ";
			}
			while(1) {
			switch(select("My PvP Statistics:Close")){
			case 1:
				next;
				mes strcharinfo(0)+"'s PvP Statistic"; mes "^666666Kill:^000000 "+Kill+" ^666666Death:^000000 "+Death+" ^666666KDR:^000000 "+callfunc("KDR_Calc", Kill, ( !Death ? 1 : Death ))+"^000000";
				mes "^666666Killing Streak:^000000 "+KillingStreak+" ^666666Multi-Kills:^000000 "+MultiKill+"";
				switch(select("Killing Streak:Multi-Kill Streak:Nemesis Streak:"+(getvariableofnpc(.reset, "Versus")?"Reset":"")+"")){
				case 1: //Killing Streak
					next;
					setarray .LSDotA$[0], ""+KillingSpree+"", ""+Dominating+"", ""+MegaKill+"", ""+Unstoppable+"", ""+WickedSick+"", ""+MonsterKill+"", ""+Godlike+"", ""+BeyondGodlike+"";
					mes "Killing Streak Stats";
					switch(#Announcer){
						case 0:
							for(set .@i, 0; .@i < getarraysize(.LSDotA$); set .@i, .@i + 1){
								mes "^666666"+getvariableofnpc(.ESDotA$[.@i], "Versus")+"^000000: "+ .LSDotA$[.@i];
							 }
							break;
						default:
							for(set .@i, 0; .@i < getarraysize(.LSDotA$); set .@i, .@i + 1){
								mes "^666666"+getvariableofnpc(.ESHoN$[.@i], "Versus")+"^000000: "+ .LSDotA$[.@i];
							 }
							break;
						}
					break;
			case 2: //Multi-Kill
				next;
				setarray .LMDotA$[0], ""+DoubleKill+"", ""+TripleKill+"", ""+UltraKill+"", ""+Rampage+"", ""+Ownage+"";
				mes "Multi-Kill Stats";
				switch(#Announcer){
					case 0:
						for(set .@i, 0; .@i < getarraysize(.LMDotA$); set .@i, .@i + 1){
							mes "^666666"+getvariableofnpc(.EMDotA$[.@i], "Versus")+"^000000: "+ .LMDotA$[.@i];
						 }
						break;
					default:
						for(set .@i, 0; .@i < getarraysize(.LMDotA$); set .@i, .@i + 1){
							mes "^666666"+getvariableofnpc(.EMHoN$[.@i], "Versus")+"^000000: "+ .LMDotA$[.@i];
						 }
						break;
				}
			case 3:	//Nemesis
				next;
				mes "Nemesis Stat";
				mes "^666666Nemesis Kill^000000: "+NemesisKill;
				mes "^666666Feed Count^000000: "+FeedCount;
				break;
			case 4: //Reset Stat
				next;
				set .reset, getvariableofnpc(.reset, "Versus");
				set .ZenyFee, getvariableofnpc(.ZenyFee, "Versus");
				set .CashFee, getvariableofnpc(.CashFee, "Versus");
				set .SkullFee, getvariableofnpc(.SkullFee, "Versus");
				mes "^0055FF[ ::: Top Ranking ::: ]^000000"; 
				mes "Are you sure you want to reset your PvP Statistics?";
				if(.reset == 1){ mes "^FF0000Reset Fee:^000000 "+.ZenyFee+" Zeny."; }
				if(.reset == 2){ mes "^FF0000Reset Fee:^000000 "+.CashFee+" Cashpoints."; }
				if(.reset == 3){ mes "^FF0000Reset Fee:^000000 "+.SkullFee+" Skulls."; }
				switch(select("No:Yes")) {
					case 1: break;
					case 2:
						switch(.reset) {
							case 0: break;
							case 1:
									if(Zeny < .ZenyFee) { next; mes "^0055FF[ ::: Top Ranking ::: ]^000000"; mes "You don't have enough Zeny."; close; }
									set Zeny, Zeny - .ZenyFee; break;
								case 2:
									if(#CASHPOINTS < .CashFee) { next; mes "^0055FF[ ::: Top Ranking ::: ]^000000"; mes "You don't have enough Cashpoints."; close; }
									set #CASHPOINTS, #CASHPOINTS - .CashFee; break;
								case 3:
									if(countitem(7420) < .SkullFee) { next; mes "^0055FF[ ::: Top Ranking ::: ]^000000"; mes "You don't have enough Skull."; close; }
									delitem 7420, .SkullFee; break;
								default: break;
							}
							if( getvariableofnpc(.ServerType, "Versus") ) { query_sql "DELETE FROM `pvp_rank` WHERE `pvp_rank`.`char_id` = "+getcharid(0)+""; }
							callsub OnPvPReset; announce "[PvP Manager] Your PvP Statistic has been reset.",bc_self,0xFF0000; close;
					}
					break;
				}
				break;
			default:
				close;
			}
			}
			} else { 
				mes "Top "+getvariableofnpc(.Top, "Versus")+" ranking is not yet implemented for this server type."; 
		}
		case 3: // Top MvP Ranking
			while(1) {
			next;
			switch( select( 
				"^FF0000Get Rewards^000000",
				"This Week's Record",
				"Last Week's Record",
				"All MvP Record",
				"View Personal Record",
				"Filter by MVP ^FF0000["+F_InsertComma( .@total_kill )+"]^000000",
				( .@gm_level < .gm_level )?"":"^FF0000[ GM ]^000000 Filter by TOP Player^000000",
				( .@gm_level < .gm_level )?"":"^FF0000[ GM ]^000000 Setup Week^000000",
				"Information"
			)){
			case 1:
				mes "[ ^FF0000MVP Rank (Reward)^000000 ]";
				mes "Top 10 Weekly MvP Rankers will receive rewards every Monday (00:00:01).  Player should use @gmreward to claim.";
				mes "Top 1:";
				mes "^0055FF7d Rental "+getitemname(20086)+"^000000";
				mes "^0055FF2x "+getitemname(616)+"^000000";
				mes "Top 2";
				mes "^0055FF7d Rental "+getitemname(20087)+"^000000";
				mes "^0055FF1x "+getitemname(616)+"^000000";
				mes "Top 3";
				mes "^0055FF7d Rental "+getitemname(20088)+"^000000";
				mes "^0055FF3x "+getitemname(617)+"^000000";
				mes "Top 4";
				mes "^0055FF7d Rental "+getitemname(20089)+"^000000";
				mes "^0055FF2x "+getitemname(617)+"^000000";
				mes "Top 5";
				mes "^0055FF7d Rental "+getitemname(5421)+"^000000";
				mes "^0055FF1x "+getitemname(617)+"^000000";
				mes "Top 6";
				mes "^0055FF5x "+getitemname(617)+"^000000";
				mes "Top 7";
				mes "^0055FF4x "+getitemname(617)+"^000000";
				mes "Top 8";
				mes "^0055FF3x "+getitemname(617)+"^000000";
				mes "Top 9";
				mes "^0055FF2x "+getitemname(617)+"^000000";
				mes "Top 10";
				mes "^0055FF1x "+getitemname(617)+"^000000";
				break;
			case 2: 
				mes "[ ^FF0000MVP Rank (This Week)^000000 ]";
				query_sql    "SELECT DATABASE()", .@rag_db$;
				query_logsql "SELECT DATABASE()", .@log_db$;
				query_sql( "SELECT COUNT(`monster_id`) AS `total`,`char`.`name`,`kill_char_id` FROM "+.@log_db$+".`mvplog`"+" LEFT JOIN "+.@rag_db$+".`char` ON `char`.`char_id` = `mvplog`.`kill_char_id` WHERE `mvp_date` > FROM_UNIXTIME("+atoi($lastweek2$)+", '%Y-%m-%d %H:%M:%S') AND `kill_char_id` NOT IN ( "+.gm_char$+" ) GROUP BY `kill_char_id` ORDER BY `total` DESC LIMIT "+.top_rank,.@total,.@name$,.@kill_char_id );
				.@size = getarraysize( .@name$ );
				if( !.@size ){
					mes "No record found.";
					} else {
				for( .@i = 0; .@i < .@size; .@i++ ){
						mes ( .@i+1 )+". "+.@name$[.@i]+" ^FF0000["+.@total[.@i]+"]^000000";
						.@player_menu$ = .@player_menu$ + ( .@i+1 )+". " + .@name$[.@i] + ":";
						.@total_kill += .@total[.@i];
					}
				}
				break;
			case 3:
				mes "[ ^FF0000MVP Rank (Last Week)^000000 ]";
				query_sql    "SELECT DATABASE()", .@rag_db$;
				query_logsql "SELECT DATABASE()", .@log_db$;
				query_sql( "SELECT COUNT(`monster_id`) AS `total`,`char`.`name`,`kill_char_id` FROM "+.@log_db$+".`mvplog`"+" LEFT JOIN "+.@rag_db$+".`char` ON `char`.`char_id` = `mvplog`.`kill_char_id` WHERE `mvp_date` >= FROM_UNIXTIME("+atoi($lastweek1$)+", '%Y-%m-%d %H:%M:%S') AND   `mvp_date` <= FROM_UNIXTIME("+atoi($lastweek2$)+", '%Y-%m-%d %H:%M:%S') AND `kill_char_id` NOT IN ( "+.gm_char$+" ) GROUP BY `kill_char_id` ORDER BY `total` DESC LIMIT "+.top_rank,.@ltotal,.@lname$,.@lkill_char_id );
				.@lsize = getarraysize( .@lname$ );
				if( !.@lsize ){
					mes "No record found.";
				} else {
					for( .@i = 0; .@i < .@lsize; .@i++ ){
						mes ( .@i+1 )+". "+.@lname$[.@i]+" ^FF0000["+.@ltotal[.@i]+"]^000000";
						.@lplayer_menu$ = .@lplayer_menu$ + ( .@i+1 )+". " + .@lname$[.@i] + ":";
						.@ltotal_kill += .@ltotal[.@i];
					}
				}
				break;
			case 4:
				mes "[ ^FF0000MVP Rank (All Time)^000000 ]";
				query_sql    "SELECT DATABASE()", .@rag_db$;
				query_logsql "SELECT DATABASE()", .@log_db$;
				query_sql( "SELECT COUNT(`monster_id`) AS `total`,`char`.`name`,`kill_char_id` FROM "+.@log_db$+".`mvplog`"+" LEFT JOIN "+.@rag_db$+".`char` ON `char`.`char_id` = `mvplog`.`kill_char_id` WHERE `kill_char_id` NOT IN ( "+.gm_char$+" ) GROUP BY `kill_char_id` ORDER BY `total` DESC LIMIT "+.top_rank,.@atotal,.@aname$,.@akill_char_id );
				.@asize = getarraysize( .@aname$ );
				if( !.@asize ){
					mes "No record found.";
					} else {
				for( .@i = 0; .@i < .@asize; .@i++ ){
						mes ( .@i+1 )+". "+.@aname$[.@i]+" ^FF0000["+.@atotal[.@i]+"]^000000";
						.@aplayer_menu$ = .@aplayer_menu$ + ( .@i+1 )+". " + .@aname$[.@i] + ":";
						.@atotal_kill += .@atotal[.@i];
					}
				}
				break;

			case 5:
				mes "^FF0000[ ::: "+( ( @menu == 1 )?"Personal":.@name$[.@i]+"'s" )+" Record ::: ]^000000";
				query_sql    "SELECT DATABASE()", .@rag_db$;
				query_logsql "SELECT DATABASE()", .@log_db$;
				query_sql( "SELECT COUNT(`monster_id`) AS `total`,`monster_id` FROM "+.@log_db$+".mvplog"+" WHERE `kill_char_id` = "+.@cid+" GROUP BY `monster_id` ORDER BY `total` DESC,`mvp_date` DESC",.@personal_total,.@personal_mvp_id );
				.@personal_size = getarraysize( .@personal_total );
				if( !.@personal_size ){
					mes "You haven't kill any MVP so far.";
				} else {
					.@total_kill = 0;
					for( .@i = 0; .@i < .@personal_size; .@i++ ){
						mes ( .@i+1 )+". "+getmonsterinfo( .@personal_mvp_id[.@i],MOB_NAME )+" ^FF0000["+.@personal_total[.@i]+"]^000000";
						.@total_kill += .@personal_total[.@i];
					}
					message strcharinfo(0),"Total "+F_InsertComma( .@total_kill )+" MVP(s)";
				}
				break;
			case 6:
				query_sql    "SELECT DATABASE()", .@rag_db$;
				query_logsql "SELECT DATABASE()", .@log_db$;
				query_sql( "SELECT COUNT(`monster_id`) AS `total`,`monster_id` FROM "+.@log_db$+".mvplog"+" WHERE `kill_char_id` NOT IN ( "+.gm_char$+" )  GROUP BY `monster_id` ORDER BY `total` DESC,`monster_id` ",.@mvp_total,.@mvp_id );
				.@mvp_size = getarraysize( .@mvp_id );
				if( !.@size ){
					mes "^FF0000[ ::: Filter by MVP ::: ]^000000";
					mes "Nobody killed any MVP so far.";
				} else {
					for( .@i = 0; .@i < .@mvp_size; .@i++ )
						.@mvp_menu$ = .@mvp_menu$ + ( .@i+1 ) + ". " + getmonsterinfo( .@mvp_id[.@i],MOB_NAME ) + "^FF0000 ["+.@mvp_total[.@i]+"]^000000" + ":";
						do{
							.@i = select( .@mvp_menu$ ) - 1;
							mes "^FF0000[ MVP - "+getmonsterinfo( .@mvp_id[.@i],MOB_NAME )+" ]^000000";
							deletearray .@filter_name$;
							query_sql    "SELECT DATABASE()", .@rag_db$;
							query_logsql "SELECT DATABASE()", .@log_db$;
							query_sql( "SELECT COUNT(`monster_id`) AS `total`,`char`.`name` FROM "+.@log_db$+".mvplog"+" LEFT JOIN `char` ON `char`.`char_id` = mvplog.`kill_char_id` WHERE `monster_id` = "+.@mvp_id[.@i]+" GROUP BY `kill_char_id` ORDER BY `total` DESC,`mvp_date` DESC LIMIT "+.top_rank,.@filter_total,.@filter_name$ );
							.@filter_size = getarraysize( .@filter_name$ );
							if( !.@size ){
								mes "No record found.";
							} else {
								for( .@i = 0; .@i < .@filter_size; .@i++ )
								mes ( .@i+1 )+". "+.@filter_name$[.@i]+" ^FF0000["+.@filter_total[.@i]+"]^000000";
							}
							next;
						} while( 1 );
				}
				break;
			case 7:
				if( .@total_kill == 0 ) break;
				.@i = select( .@player_menu$ ) - 1;
				.@cid = .@kill_char_id[.@i];
				break;

			case 8:
				mes "[^FF0000Setup MvP Ranking^000000]";
				mes "What would you like to do?";
				while(1) {
					next;
						switch(select(
						 " > Week Start [^0000ff"+$lastweek1$+"^000000 ]:"+
 						 " > Week End [^0000ff"+$lastweek2$+"^000000 ]:"+
	 					 " > ^777777Close^000000"
					)) {
					case 1:
						input $lastweek1$;
						break;
					case 2:
						input $lastweek2$;
						break;
					default:
						close;
					}
				}
				break;
			default: 
				next;
				mes "^FF0000[ ::: Top Ranking ::: ]^000000";
				mes "I am incharge of record down whoever killed a MVP in this server. It's my duty to show players the statistic of MVP killing in this server.";
				next;
				mes "^0055FF[ ::: Top Ranking ::: ]^000000";
				mes "This is what I record:";
				mes "^777777  ~ Most killed MVP^000000";
				mes "^777777  ~ Most MVP Slayer^000000";
				mes "^777777  ~ Personal MVP Record^000000";
				next;
				mes "^0055FF[ ::: Top Ranking ::: ]^000000";
				mes "However, GM team remain the right to bar you from the MVP listing if you ever abuse the ranking.";
				mes " ";
				mes "Ranking are not meant to be permanent too, I might clear the Ranking monthly. It's all depend on me.";
				mes "Thank you.";
				close;
			}
			}
		default:
			close;
		}
	OnPvPReset:
		set Kill, 0;
		set Death, 0;
		set KillingStreak, 0; 
		set MultiKill, 0;
		set KillingSpree, 0;
		set Dominating, 0;
		set MegaKill, 0;
		set Unstoppable, 0;
		set WickedSick, 0;
		set MonsterKill, 0;
		set Godlike, 0;
		set BeyondGodlike, 0;
		set DoubleKill, 0;
		set TripleKill, 0;
		set UltraKill, 0;
		set Rampage, 0;
		set Ownage, 0;
		set NemesisKill, 0;
		set FeedCount, 0;
		return;

	OnInit:
		.top_rank = 10;
		.gm_level = 99;
		.gm_char$ = "150017,150000,150011";
		//.gm_char$ = "1520000,1520157";
		end;


}

