/* AMX Mod script. 
* 
* (c) Copyright 2002, Noplay
* This file is provided as is (no warranties). 
* 
* Warning don't remove my connexion copyright
* 
*/ 

#include <amxmod>



new Float:private[33]
new pg_player[64][32]
new player_name[32],refuse[3025],accept[3025],msg_kick[3025],passfield[11],userauthid[32]
new user_nb = 1
new maxplayers,nb_players


kick(id,level)
{	
	new who = 0, connection_time, longest = 0
	new l=0
	while (who==0)
	{
		for(new i = 1; i <= maxplayers; ++i)
		{
			if ( !is_user_connected(i) && !is_user_connecting(i) )	continue // not used slot	
			if (private[i]>l) continue // has reservation (don't touch)
			connection_time = get_user_time(i) // get user playing time with connection duration	
			if ( longest < connection_time )	
			{
				longest = connection_time
				who = i
			}
		}
		l++
		
		if (l==level)
		{
			client_cmd(id,"echo [PG] %s;disconnect",msg_kick)
			break
		}
	}
	
	if(who!=0)
	{
		get_user_name(who,player_name,31)
		client_cmd(who,"echo [PG] %s;disconnect",msg_kick)
		server_print("[PG] Kick %s by Private Gamers",player_name)
		log_message("[PG] Kick %s by Private Gamers",player_name)
	}
}

public pg_add()
{
	read_argv(1,pg_player[user_nb],31)	
	user_nb++
	return PLUGIN_CONTINUE 
}

public client_disconnect(id)
{
	private[id]=0.0
	return PLUGIN_CONTINUE 
}


public client_connect(id)
{
	new password[32]
	new server_password[32]
	get_user_info(id,passfield,password,31)
	get_user_authid(id,userauthid,31)
	get_user_name(id,player_name,31) 
	nb_players=get_playersnum()+1
	new ok
	
	
	ok=0
	
	client_cmd(id, "echo;echo ======================================================================")
	client_cmd(id,"echo;echo This server use the Private Gamers plugin;echo Write by Noplay (noplay@noplay.net) for QuadParty (www.quadparty.com);echo More informations on privategamers.noplay.net;echo;echo;")
	
	for(new i = 1; i <= user_nb; ++i)
	{
		if(equal(pg_player[i],userauthid))
		{
			private[id]=3.0
			ok=1
			
			break
		}
	}
	
	if (ok!=1)
	{
		new pass
		new p[32]
		server_print("[PG] Password: %s & wonid: %s",password,userauthid)


 		pass = strtonum(userauthid)

		pass=pass*2/*EDIT THIS LINE FOR CHANGE PASSWORD ENCRYPTION*/
		
		set_cvar_num("pg_user_password",pass)		
		get_cvar_string("pg_user_password",p,31)
		
		if(equal(password,p))
		{
			private[id]=2
			ok=1		
		}
	}
	else if (ok!=1)
	{
		server_print("[PG] Password: %s & wonid: %s",password,userauthid)
		get_cvar_string("pg_password",server_password,31)

		if(server_password[1])
		{
			if(equal(password,server_password))
			{
				private[id]=1
				ok=1	
			}
		}
	}
	
	if (ok==1)
	{
		server_print("[PG] %s is a register user with level %d",player_name,private[id])
		log_message("[PG] %s is a register user with level %d",player_name,private[id])
		client_cmd(id,"echo [PG] Your level is %d",private[id])
		client_cmd(id,"echo [PG] %s",accept)
		
		if(nb_players==maxplayers)
		{			
			kick(id,private[id])
		}
	}
	else
	{
		server_print("[PG] %s is not a register user",player_name)
		log_message("[PG] %s is not a register user",player_name)
		
		if(nb_players==maxplayers)
		{
			client_cmd(id,"echo [PG] %s;disconnect",refuse)
			server_print("[PG] No slot available (%d/%d) for %s",nb_players,maxplayers,player_name)			
			log_message("[PG] No slot available (%d/%d) for %s",nb_players,maxplayers,player_name)

			return PLUGIN_HANDLED
		}	
	}
	
	client_cmd(id, "echo ======================================================================")
	
	return PLUGIN_CONTINUE
}

public init_minuterie()
{

	server_print("[PG] Private Gamers by Noplay is start")

	get_cvar_string("pg_refuse",refuse,3024)
	get_cvar_string("pg_accept",accept,3024)
	get_cvar_string("pg_msg_kick",msg_kick,3024)
	get_cvar_string("pg_passfield",passfield,10)

	
	return PLUGIN_CONTINUE
}

public pg_list()
{
	server_print("[PG] List players")
	nb_players=get_playersnum()
	for(new i = 1; i <= nb_players; ++i)
	{
		get_user_name(i,player_name,31)
		server_print("[PG] #%d %s %d",i,player_name,private[i])
	}
	return PLUGIN_CONTINUE 
}


public plugin_init()
{ 
	register_plugin("Private gamers","0.1","Noplay")

	register_cvar("private_gamers","1")	
	register_cvar("pg_refuse","No public place on this servs")
	register_cvar("pg_accept","Password accepted")
	register_cvar("pg_msg_kick","Sorry but not public place on the server")
	register_cvar("pg_passfield","pr_ga")
	register_cvar("pg_password","")
	register_cvar("pg_user_password","")	

	register_srvcmd("pg_add","pg_add")
	register_srvcmd("pg_list","pg_list")

	
	server_cmd("exec addons/amx/private_gamers.cfg")
	server_cmd("exec addons/amx/players.cfg")

	get_cvar_string("pg_refuse",refuse,3024)
	get_cvar_string("pg_accept",accept,3024)
	get_cvar_string("pg_msg_kick",msg_kick,3024)

	set_task(0.25,"init_minuterie")
 

	maxplayers=get_maxplayers()	

	return PLUGIN_CONTINUE 
}

