// @unloadnpc abuse
// @loadnpc npc/thor/abuse.txt

-	script	abuse	-1,{
    OnInit:
    	set .jailmap$,"office";
    	set .jailx,89;
 	set .jaily,65;  			
    	bindatcmd "abuse","abuse::OnBadPoints", 1, 99;
    	end;


OnBadPoints:
	set .@gm_id,getcharid(3);
    	mes " Hello GM ^00CC00"+strcharinfo(0)+"^000000!";
	mes "Input Player Name to process BAD POINTS:";
	input .@chrnm$;
	query_sql "SELECT `char_id`, `account_id` FROM `char` WHERE `name` = '" + .@chrnm$ + "'",.@char_id,.@account_id;
	if (!.@char_id) { 
   
		mes " I'm Sorry but I couldn't find that character or account.";
		mes " Please verify if you dind't make a typo.";
		close;
	}
	mes "Please attach a reference link:";
	input .@ref$;
	if (.@ref$=="") { 
		mes "We need to add the reference link as evidence.  Please try again.";
		close;
	}
	// Display Category List
	set .@Menu$,"";	
	set .@vcat,query_sql(" SELECT `rcat_id`,`rcat_desc` FROM rules_cat",.@rcat_id, .@rcat_desc$);
	for( set .@i,0; .@i < .@vcat; set .@i,.@i + 1 )
		set .@Menu$,.@Menu$ + "^FF0000[ "+( .@i+1 )+". ]^0000FF "+.@rcat_desc$[.@i]+":";
	set .@Select,select( .@Menu$ ) - 1;
	set .@rcat_desc$,.@rcat_desc$[.@Select];
	set .@rcat_id,.@rcat_id[.@Select];

	// Check Counts & Penalty
	query_sql "SELECT count(rcat_id) FROM rules_log WHERE `rcat_id` = "+.@rcat_id+" AND `account_id` = "+.@account_id,.@count;
	sleep2 100;
	query_sql "SELECT `penalty1`,`penalty2`,`penalty3` FROM rules_cat WHERE `rcat_id` = "+.@rcat_id,.@penalty1,.@penalty2,.@penalty3;
	if(!.@count) 		{ 	set .@penalty, .@penalty1; set .@countxt$,"1st";
	} else if (.@count==1) 	{	set .@penalty, .@penalty2; set .@countxt$,"2nd";
	} else 			{ 	set .@penalty, .@penalty3; set .@countxt$,"3rd";
	}

	if(.@penalty==3) { set .@penalty$,"PERMA JAIL"; set .@penalty1,0;
	} else if(.@penalty >= 100 && .@penalty <=199) 	{ set .@penalty$,"H JAIL"; set .@penalty1,.@penalty-100;
	} else if(.@penalty >= 200 && .@penalty <=299) 	{ set .@penalty$,"D BAN"; set .@penalty1,.@penalty-200;
	} else { set .@penalty$,"BLOCK"; set .@penalty1,.@penalty-300;
	}
	
	mes "^FF0000BAD POINTS TRANSACTION^000000";
	mes "^FF0000Player^000000 : "+.@chrnm$;
	mes "^FF0000Remarks:^000000 : "+.@rcat_desc$;
	mes "^FF0000Count:^000000 : "+.@countxt$+" Offense";
	mes "^FF0000Penalty:^000000 : "+.@penalty1+" "+.@penalty$;
	mes "Do you wish to continue?";
	next;
	switch(select("No:Yes, Proceed with the badpoints.")) {
	case 1:
		mes "^FF0000BAD POINTS TRANSACTION^000000";
		mes "Okay, come back when you are ready!!!";
		close;
	case 2:
		if(.@penalty==3) { 
			atcommand "@kick "+.@chrnm$;
			query_sql "INSERT INTO rules_log (`account_id`,`char_id`, `name`,`rcat_id`,`vdate`,`count`, `status`, `ban_type`,`ban_until`, `ref`) VALUES ("+.@account_id+","+.@char_id+",'"+escape_sql(.@chrnm$)+"',"+.@rcat_id[.@Select]+",'"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"',"+(.@count+1)+",1,1,'0000-00-00 00:00:00','"+escape_sql(.@ref$)+"'"")";
			query_sql "UPDATE `char` SET `last_map`='"+escape_sql(.jailmap$)+"', `last_x`="+.jailx+", `last_y`="+.jaily+" WHERE `char_id`="+.@char_id;
			query_sql "UPDATE `char` SET `save_map`='"+escape_sql(.jailmap$)+"', `save_x`="+.jailx+", `save_y`="+.jaily+" WHERE `char_id`="+.@char_id;
			next;
			mes "^FF0000BAD POINTS TRANSACTION^000000";
			mes "^FF0000Status:^000000 : Completed"; 
			close;
		}

		if(.@penalty >= 100 && .@penalty <=199) { 
			atcommand "@kick "+.@chrnm$;
			query_sql "INSERT INTO rules_log (`account_id`,`char_id`, `name`,`rcat_id`,`vdate`,`count`, `status`, `ban_type`,`ban_until`, `ref`) VALUES ("+.@account_id+","+.@char_id+",'"+escape_sql(.@chrnm$)+"',"+.@rcat_id[.@Select]+",'"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"',"+(.@count+1)+",1,1,'0000-00-00 00:00:00','"+escape_sql(.@ref$)+"'"")";
			next;
			mes "^FF0000BAD POINTS TRANSACTION^000000";
			mes "^FF0000Status:^000000 : Completed"; 
			close;
		}
		if(.@penalty >= 200 && .@penalty <=299) { 
			set .@penalty,.@penalty-200; 	
			atcommand "@ban +"+.@penalty+"d "+.@chrnm$;
			query_sql "INSERT INTO rules_log (`account_id`,`char_id`,`name`,`rcat_id`,`vdate`,`count`, `status`, `ban_type`,`ban_until`, `ref`) VALUES ("+.@account_id+","+.@char_id+",'"+escape_sql(.@chrnm$)+"',"+.@rcat_id[.@Select]+",'"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"',"+(.@count+1)+",1,2,'"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"','"+escape_sql(.@ref$)+"'"")";
			sleep2 100;
			query_sql ("UPDATE rules_log SET status = 0, ban_until = DATE_ADD(vdate, INTERVAL "+.@penalty+" DAY) WHERE rcat_id = "+.@rcat_id+" AND status = 1 AND account_id= "+.@account_id+";");
		}
	
		if(.@penalty == 300 ) { 
			atcommand "@block "+.@chrnm$;
			query_sql "INSERT INTO rules_log (`account_id`,`char_id`,`name`,`rcat_id`,`vdate`,`count`, `status`, `ban_type`,`ban_until`, `ref`) VALUES ("+.@account_id+","+.@char_id+",'"+escape_sql(.@chrnm$)+"',"+.@rcat_id[.@Select]+",'"+gettimestr("%Y-%m-%d %H:%M:%S",21)+"',"+(.@count+1)+",0,3,'0000-00-00 00:00:00','"+escape_sql(.@ref$)+"'"")";
		}
		next;
		mes "^FF0000BAD POINTS TRANSACTION^000000";
		mes "^FF0000Status:^000000 : Completed";
	}
	close;

OnPCLoginEvent:
	//Bad Points
	query_sql "SELECT `rcat_id` FROM rules_log WHERE `status` = 1 AND `rcat_id`= 1 AND `char_id` = "+getcharid(0),.@rcat_id;
	if (.@rcat_id) { 
		savepoint "office",83,65;	
		warp "office",83,65;
		sleep2 100;
		query_sql ("UPDATE rules_log SET status = 0, ban_until = DATE_ADD(vdate, INTERVAL 3 YEAR) WHERE rcat_id = "+.@rcat_id+" AND status = 1 AND char_id= "+getcharid(0)+";");
		end;
	}
	query_sql "SELECT `rcat_id` FROM rules_log WHERE `status` = 1 AND `account_id` = "+getcharid(3),.@rcat_id;
	if (.@rcat_id) { 
		query_sql "SELECT count(rcat_id) FROM rules_log WHERE `rcat_id` = "+.@rcat_id+" AND `account_id` = "+getcharid(3),.@count;
		query_sql "SELECT `penalty1`,`penalty2`,`penalty3` FROM rules_cat WHERE `rcat_id` = "+.@rcat_id,.@penalty1,.@penalty2,.@penalty3;
		if(.@count==1) { set .@penalty, .@penalty1; } if(.@count==2) { set .@penalty, .@penalty2; } if(.@count==3) { set .@penalty, .@penalty3; }
		if(.@penalty >= 100 && .@penalty <=199) { set .@penalty,.@penalty-100;
			savepoint "office",83,65;	
			warp "office",83,65;
	 	}
		sleep2 100;
		query_sql ("UPDATE rules_log SET status = 0, ban_until = DATE_ADD(vdate, INTERVAL "+.@penalty+" HOUR) WHERE rcat_id = "+.@rcat_id+" AND status = 1 AND account_id= "+getcharid(3)+";");
	}
	end;
}

office,89,72,3	script	Guard#prison	8W_SOLDIER,{
	set .@char_id, getcharid(0);
	set .@account_id, getcharid(3);
	set .@charname$, strcharinfo(0);
	sleep2 500;
	query_sql "SELECT `vdate`,unix_timestamp(`ban_until`),`ban_until`,`rcat_id`, `ref` FROM rules_log WHERE `account_id` = "+.@account_id+" ORDER BY `vdate` DESC LIMIT 1",.@vdate$,.@unixban,.@bantime$,.@rcat_id,.@link$;
	if (!.@rcat_id) {  close; }
	
	//Get Violation Info
	query_sql "SELECT count(rcat_id) FROM rules_log WHERE `rcat_id` = "+.@rcat_id+" AND `account_id` = "+.@account_id,.@count;
	query_sql "SELECT `rcat_desc`,`penalty1`,`penalty2`,`penalty3` FROM rules_cat WHERE `rcat_id` = "+.@rcat_id,.@rcat_desc$,.@penalty1,.@penalty2,.@penalty3;

	mes " Hello ^00CC00"+strcharinfo(0)+"^000000!";
	mes " You have been held here because one of your character have violated certain rules and regulations:";
	next;
	mes " Violation: ^FF0000"+.@rcat_desc$+"^000000";
	mes " Offense Count: ^FF0000"+.@count+"^000000";
	mes " Date Filed: ^FF0000"+.@vdate$+"^000000";
	mes " Jail Until: ^FF0000"+.@bantime$+"^000000";
	
	if(.@penalty1 == 3) { set .@penalty1,.@penalty1; mes "Please request a character ^FF0000RENAME^000000 at ^FF0000fb.com/messages/classicro.net^000000 so that we can process your release";}
	if(.@penalty1 >= 100 && .@penalty1 <=199) { set .@penalty1,.@penalty1-100; 	mes "1st Offense: ^FF0000"+.@penalty1+" hour(s)^000000 JAIL"; }
	if(.@penalty1 >= 200 && .@penalty1 <=299) { set .@penalty1,.@penalty1-200; 	mes "1st Offense: ^FF0000"+.@penalty1+" day(s)^000000 BAN"; }
	if(.@penalty1 == 300 ) 			{ 					mes "1st Offense: ^FF0000Permanent Ban^000000";}

	if(.@penalty2 >= 100 && .@penalty2 <=199) { set .@penalty2,.@penalty2-100; 	mes "2nd Offense: ^FF0000"+.@penalty2+" hour(s)^000000 JAIL"; }
	if(.@penalty2 >= 200 && .@penalty2 <=299) { set .@penalty2,.@penalty2-200; 	mes "2nd Offense: ^FF0000"+.@penalty2+" day(s)^000000 BAN"; }
	if(.@penalty2 == 300 ) 			{ 					mes "2nd Offense: ^FF0000Permanent Ban^000000";}

	if(.@penalty3 >= 100 && .@penalty3 <=199) { set .@penalty3,.@penalty3-100; 	mes "3rd Offense: ^FF0000"+.@penalty3+" hour(s)^000000 JAIL"; }
	if(.@penalty3 >= 200 && .@penalty3 <=299) { set .@penalty3,.@penalty3-200; 	mes "3rd Offense: ^FF0000"+.@penalty3+" day(s)^000000 BAN"; }
	if(.@penalty3 == 300 ) 			{ 					mes "3rd Offense: ^FF0000Permanent Ban^000000";}
	mes "Reference: ^FF0000"+.@link$+"^000000";
	if (gettimetick(2) >= .@unixban)  {
		next;
		mes "Your jail time has ended.  Please read the server rules to avoid getting ban";
		next;
		switch(select(
			 "- ^777777Go back in town.^000000:"+
			 "- ^777777Close^000000"
			)) {

			case 1:
				savepoint "office",128,37;
				warp "office",128,37;
				close;
			case 2: 
				close;
			}
	}
	
	close;
	
}

office	mapflag	nomemo
office	mapflag	noreturn
office	mapflag	noteleport
office	mapflag	nowarp