//@unloadnpc ResetLadder
//@loadnpc npc/thor/resetladder.txt

-	script	ResetLadder	FAKE_NPC,{
	function func_item_name;
end;


OnClock0000:
		if (gettime(GETTIME_WEEKDAY)!=1) end;
OnReset:
		setarray .@mvpr, 20086, 20087, 20088, 20089, 617, 617, 617, 617, 617, 617;
		setarray .@mvpa, 1, 1, 1, 1, 6, 5,4, 3, 2, 1;

		setarray .@mvpr2, 616, 616, 617, 617, 617, 0, 0, 0, 0, 0;
		setarray .@mvpa2, 2, 1, 3, 4, 1, 0,0, 0, 0, 0;

		setarray .@mvpr3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
		setarray .@mvpa3, 1, 0, 0, 0, 0, 0,0, 0, 0, 0;

		setarray .@pvpr, 616, 684, 603, 12260, 607, 14541, 668, 12266, 985, 984;
		setarray .@pvpa, 3, 10, 5, 1, 10, 10, 10, 5, 3, 3;

		$lastweek1$ = gettimetick(2)-604800;
		$lastweek2$ = gettimetick(2);
	    
		// get database names, for join
		query_sql    "SELECT DATABASE()", .@rag_db$;
		query_logsql "SELECT DATABASE()", .@log_db$;

		query_sql "SELECT `name`,COUNT(mvp_id) , `kill_char_id` FROM "+.@log_db$+".mvplog " + 
			"LEFT JOIN "+.@rag_db$+".`char` ON mvplog.kill_char_id=`char`.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') " +
			"GROUP BY kill_char_id ORDER BY COUNT(mvp_id) DESC limit "+ .ladder_length, .@name$, .@killed, .@cid;

		if (getarraysize( .@name$ )) {
			for ( .@i = 0; .@i < getarraysize( .@name$ ); .@i++) {
				.@gm_aid = 1;
				.@gm_name$="Reward";
				.@r_refine = 0;
				.@sold_refine = 0;
				.@sold_card1 = 0;
				.@sold_card2 = 0;
				.@sold_card3 = 0;
				.@sold_card4 = 0;
				.@note$ = "Top "+(.@i+1)+" Weekly MVP Reward";
				if (.@mvpr[.@i]) {
					.@formatted_name$ = func_item_name( .@mvpr[.@i],.@mvpa[.@i],.@r_refine );
					query_sql( "INSERT INTO `gm_reward` ( `gm_aid`,`gm_name`,`nameid`,`amount`,`refine`,`format_name`,`card1`,`card2`,`card3`,`card4`,`reward_time`,`claim_aid`,`claim_name`,`note` ) VALUES ( "+.@gm_aid+",'"+escape_sql( .@gm_name$ )+"',"+.@mvpr[.@i]+","+.@mvpa[.@i]+","+.@sold_refine+",'"+escape_sql( .@formatted_name$ )+"',"+.@sold_card1+","+.@sold_card2+","+.@sold_card3+","+.@sold_card4+",NOW(),"+.@cid[(.@i)]+",'"+escape_sql( .@name$[(.@i)]  )+"','"+.@note$+"' )" );	
					rodex_sendmail(.@cid[.@i], "[ "+.@gm_name$+" ]", "[ Reward Notice ]", "You have a pending reward [ "+.@formatted_name$+" ] for [ "+.@note$+" ] to be claim.  Type @gmreward to claim.",0);				 
				}
				if (.@mvpr2[.@i]) {
					.@formatted_name2$ = func_item_name( .@mvpr2[.@i],.@mvpa2[.@i],.@r_refine );
					query_sql( "INSERT INTO `gm_reward` ( `gm_aid`,`gm_name`,`nameid`,`amount`,`refine`,`format_name`,`card1`,`card2`,`card3`,`card4`,`reward_time`,`claim_aid`,`claim_name`,`note` ) VALUES ( "+.@gm_aid+",'"+escape_sql( .@gm_name$ )+"',"+.@mvpr2[.@i]+","+.@mvpa2[.@i]+","+.@sold_refine+",'"+escape_sql( .@formatted_name2$ )+"',"+.@sold_card1+","+.@sold_card2+","+.@sold_card3+","+.@sold_card4+",NOW(),"+.@cid[(.@i)]+",'"+escape_sql( .@name$[(.@i)]  )+"','"+.@note$+"' )" );	
					rodex_sendmail(.@cid[.@i], "[ "+.@gm_name$+" ]", "[ Reward Notice ]", "You have a pending reward [ "+.@formatted_name2$+" ] for [ "+.@note$+" ] to be claim.  Type @reward to claim.",0);				 
				}
				if (.@mvpr3[.@i]) {
					.@formatted_name3$ = func_item_name( .@mvpr3[.@i],.@mvpa3[.@i],.@r_refine );
					query_sql( "INSERT INTO `gm_reward` ( `gm_aid`,`gm_name`,`nameid`,`amount`,`refine`,`format_name`,`card1`,`card2`,`card3`,`card4`,`reward_time`,`claim_aid`,`claim_name`,`note` ) VALUES ( "+.@gm_aid+",'"+escape_sql( .@gm_name$ )+"',"+.@mvpr3[.@i]+","+.@mvpa3[.@i]+","+.@sold_refine+",'"+escape_sql( .@formatted_name3$ )+"',"+.@sold_card1+","+.@sold_card2+","+.@sold_card3+","+.@sold_card4+",NOW(),"+.@cid[(.@i)]+",'"+escape_sql( .@name$[(.@i)]  )+"','"+.@note$+"' )" );	
					rodex_sendmail(.@cid[.@i], "[ "+.@gm_name$+" ]", "[ Reward Notice ]", "You have a pending reward [ "+.@formatted_name2$+" ] for [ "+.@note$+" ] to be claim.  Type @reward to claim.",0);				 
				}

			}
		}
		announce "[System] Last Week's Top MVP has been rewarded.  Congratulations to the Top Rankers for last week. Use @reward to claim..",0;
		end;

OnInit:
	.ladder_length = 10;
	.debug = 0;
	bindatcmd "resetmvp","ResetLadder::OnReset",99,99;
	end;

	function	func_item_name	{
		.@item_id = getarg(0);
		.@amount = getarg(1);
		.@refine = getarg(2);
		
		.@string$ = getitemname( .@item_id );
		.@type = getiteminfo( .@item_id,2 );
			
		if ( .@string$ == "null" )
			return "none";
			
		if ( .@refine ) 
			.@string$ = "+"+.@refine+" "+.@string$;
		if ( .@type == IT_WEAPON || .@type == IT_ARMOR )
			.@string$ = .@string$ + " ["+getitemslots( .@item_id )+"]";
		if ( .@amount > 1 )
			.@string$ = .@amount+" x "+.@string$;
			
		return .@string$;
	}

}