// plugin_knight_slothound.sma
// Author: KnightHawk
// License: Free to use, of course I'd love it if you gave me some credit if you use alot of the code.
// We don't want inactive players suking up slots so if dont move or are dead for x amount of time, then kick them.
// You need to add MaxDead, and MaxInactive settings to your vault.ini file.
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
#include <sound>
 
#define	IA_Count 0
#define	LastOrigin 2
#define IA_Dead 1
#define ACCESS_GODMODE 8192
 
new STRING_VERSION[MAX_DATA_LENGTH] = "1.10.3";
new UserTracker[MAX_PLAYERS][3];		// keep track of sounds per second
new MAX_IA = 5;  
new MAX_Dead = 5;
new TimerSeconds = 60;
 
 
public plugin_init() 
{
	plugin_registerinfo("Knight Slot Hound", "Auto kicks inactive people", STRING_VERSION);
	plugin_registercmd("admin_slotinactive","admin_slothound1",ACCESS_GODMODE,"");
	plugin_registercmd("admin_slotdead","admin_slothound2",ACCESS_GODMODE,"");
	plugin_registercmd("admin_slottest","admin_slottest",ACCESS_ALL,"");
 
	new Data[MAX_DATA_LENGTH];
	get_vaultdata("MaxDead",Data,MAX_DATA_LENGTH);
	MAX_Dead = strtonum(Data);
	get_vaultdata("MaxInactive",Data,MAX_DATA_LENGTH);
	MAX_IA = strtonum(Data);
	if((MAX_IA < 3) || (MAX_IA > 25)){
		MAX_IA = 5;
	} 
	if((MAX_Dead < 3) ||(MAX_Dead > 25)){
		MAX_Dead = 5;
	} 
	set_timer("slotcheck_timer",TimerSeconds, 99999,"");	
 
	return PLUGIN_CONTINUE;
}
 
public plugin_connect(HLUserName, HLIP, UserIndex) 
{
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS)
	{
		UserTracker[UserIndex][IA_Count] = 0;
		UserTracker[UserIndex][IA_Dead] = 0;
		UserTracker[UserIndex][LastOrigin] = 0;
	}
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName, UserIndex) 
{
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS)
	{
		UserTracker[UserIndex][IA_Count] = 0;
		UserTracker[UserIndex][IA_Dead] = 0;
		UserTracker[UserIndex][LastOrigin] = 0;
	}
	return PLUGIN_CONTINUE;
}
 
//sets the max inactivity
public admin_slothound1(HLCommand,HLData,HLUserName,UserIndex) 
{
	new Data[MAX_DATA_LENGTH];
	new i=0;
	new Cmd[MAX_TEXT_LENGTH];
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	i = strtonum(Data);
	if( i > 25 || i < 3 ) {
		i = 5;
	}
	MAX_IA = i;
	snprintf(Cmd,MAX_TEXT_LENGTH, "%i",MAX_IA);
	set_vaultdata("MaxInactive",Cmd);
	get_vaultdata("MaxInactive", Data, MAX_TEXT_LENGTH);
	snprintf(Cmd, MAX_TEXT_LENGTH, "The MaxInactivity is now:  %s ", Data);
	log(Cmd);
	return PLUGIN_HANDLED;
}
 
//sets the max is dead count
public admin_slothound2(HLCommand,HLData,HLUserName,UserIndex) 
{
	new Data[MAX_DATA_LENGTH];
	new i=0;
	new Cmd[MAX_TEXT_LENGTH];
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	i = strtonum(Data);
	if( i > 25 || i < 3 ) {
		i = 5;
	}
	MAX_Dead = i;
	snprintf(Cmd,MAX_TEXT_LENGTH, "%i",MAX_Dead);
	set_vaultdata("MaxDead",Cmd);
	get_vaultdata("MaxDead", Data, MAX_TEXT_LENGTH);
	snprintf(Cmd, MAX_TEXT_LENGTH, "The Max Death count is now:  %s ", Data);
	log(Cmd);
	return PLUGIN_HANDLED;
}
 
public admin_slottest(HLCommand,HLData,HLUserName,UserIndex) 
{
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Index = 0;
	new Text[MAX_TEXT_LENGTH];
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	get_userindex(Data,Index);
	snprintf(Text, MAX_TEXT_LENGTH, "%s's activity status is: deathcount: %i locationcount: %i  origin: %i",Data,UserTracker[Index][IA_Dead],UserTracker[Index][IA_Count],UserTracker[Index][LastOrigin]);
	messageex(User,Text,print_chat);
 
	return PLUGIN_HANDLED;
}
 
 
 
public slotcheck_timer(Timer,Repeat,HLName,HLParam) {
  CheckTheSlots();
}
 
 
 
CheckTheSlots(){
	new i = 1;
	new maxplayers = maxplayercount();
	new X = 0; 
	new Y = 0; 
	new Z = 0; 
	new Text[MAX_TEXT_LENGTH];
	new TargetName[MAX_NAME_LENGTH];
	new SessionID;
	new Team;
	new WONID;
	new Dead;
 
 
	for(i=1; i<=maxplayers; i++) {
 
		if(playerinfo(i,TargetName,MAX_NAME_LENGTH,SessionID,WONID,Team,Dead)==1) {
 
			if (check_immunity(TargetName)==1){
				//dont do anything if admin;
			}
			else{
				if(Team == 0){
					UserTracker[i][IA_Dead] = (UserTracker[i][IA_Dead] + 1);
				}
				if(Dead == 1){
					UserTracker[i][IA_Dead] = (UserTracker[i][IA_Dead] + 1);
				}else{
					if(UserTracker[i][IA_Dead] > 0){
						UserTracker[i][IA_Dead] = 0;
					}	
				}
 
				if( get_userorigin(TargetName, X, Y, Z)==1 ) {
					if(((X+Y)+Z) == UserTracker[i][LastOrigin]){
						UserTracker[i][IA_Count] = UserTracker[i][IA_Count] + 1;	
					}
					else{
						UserTracker[i][LastOrigin] = (X+Y+Z);
						if(UserTracker[i][IA_Count] > 0){
							UserTracker[i][IA_Count] = 0; 
						}		
					}	
                        	}
 
			//now check the amounts and warn or boot the s*ckers
				if((UserTracker[i][IA_Dead] >= (MAX_Dead-2)) || (UserTracker[i][IA_Count] >= (MAX_IA - 2))){
					messageex(TargetName, "attention!!: The server has logged you as inactive", print_chat);
					messageex(TargetName, "attention!!: Please either spawn or move in the next 60 seconds to prevent being kicked.", print_chat);
				}
				if((UserTracker[i][IA_Dead] > MAX_Dead) && (UserTracker[i][IA_Count] >= (MAX_IA - 2))){
					messageex(TargetName, "The server has logged you off due to inactivity!", print_console);
					messageex(TargetName, "The server has logged you off due to inactivity!", print_chat);
					playsoundall("knight\door.wav");
					kick(TargetName);
					snprintf(Text, MAX_TEXT_LENGTH, "Auto kicked %s for inactivity.", TargetName);
					say(Text);
					log(Text);
 
				}
				if(UserTracker[i][IA_Count] > MAX_IA){
					messageex(TargetName, "The server has logged you off due to inactivity!", print_console);
					messageex(TargetName, "The server has logged you off due to inactivity!", print_chat);
					playsoundall("knight\door.wav");
					kick(TargetName);
					snprintf(Text, MAX_TEXT_LENGTH, "Auto kicked %s for Inactivity.", TargetName);
					log(Text);
				}
			}		
		}
	}
	return PLUGIN_CONTINUE;
}