#include <amxmodx>
#include <amxmisc>
#include <nvault>
#include <colorchat>

#define PLUGIN "VisitCounter"
#define VERSION "1.1"
#define AUTHOR "Sebxx"

new c_tmp1, c_tmp2, c_tmp3, c_str1, c_str2, g_vault, visits, last_visit
new stamp[11], prefix[33], web_addr[33], szSteam[35], temp[19], tmp[7]

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	g_vault = nvault_open("visitcounter_db")

	register_clcmd("say /visits", "show_visits")														// komenda do sprawdzania liczby wizyt ( /viptest )
	c_str1 = register_cvar("visitcounter_prefix", "[NazwaForum?]")												// prefix przed komunikatami
	c_str2 = register_cvar("visitcounter_webaddr", "TwojaStrona.pl")											// adres sieci w komunikacie
	c_tmp1 = register_cvar("visitcounter_adson", "1")													// wlacz komunikat z liczba wizyt
	c_tmp2 = register_cvar("visitcounter_adstime", "300.0")													// co ile sekund wyswietlac komunikat?
	c_tmp3 = register_cvar("visitcounter_interval", "1800")													// po jakim czasie naliczyc nowe wejscie?
}

public client_authorized( id, const authid[] )
{
	if( !is_user_bot( id ) && !is_user_hltv( id ) )
	{
		set_task(10.0, "counter", id, _, _, "a", 1)
		if ( get_pcvar_num(c_tmp1) == 1 )
		{
			set_task(get_pcvar_float(c_tmp2), "show_visits", id, _, _, "b")
		}
	}
}

public counter(id)
{
	if ( is_user_connected( id ) )
	{
		get_user_authid( id, szSteam, charsmax( szSteam ) )
		get_pcvar_string( c_str1, prefix, charsmax(prefix) )
		get_pcvar_string( c_str2, web_addr, charsmax(web_addr) )

		if ( nvault_get( g_vault , szSteam , temp , charsmax( temp ) ) )
		{
			strtok(temp, tmp, charsmax( tmp ), stamp, charsmax( stamp ), ';');
			last_visit = str_to_num( stamp )

			if ( last_visit + get_pcvar_num( c_tmp3 ) <= get_systime() )
			{
				visits = str_to_num( tmp ) + 1
				ColorChat( id, TEAM_COLOR, "%s^x01 To juz Twoja ^x03%d^x01 wizyta na naszym serwerze! Zarejestruj sie na ^x03%s^x01, jesli jeszcze tego nie zrobiles :)", prefix, visits, web_addr )
			}
			else
			{	
				return PLUGIN_CONTINUE
			}
		}
		else
		{	
			visits = 1
			ColorChat( id, TEAM_COLOR, "%s^x01 To Twoja ^x03pierwsza^x01 wizyta na naszym serwerze. Zarejestruj sie na ^x03%s^x01 aby nic Cie nie omilelo! :)", prefix, web_addr )
		}

		num_to_str( visits, tmp, charsmax(tmp) )
		num_to_str( get_systime(), stamp, charsmax(stamp) )

		new iLen = 0, iMax = sizeof( temp ) - 1;
		iLen += formatex( temp[iLen], iMax - iLen, tmp )
		iLen += formatex( temp[iLen], iMax - iLen, ";" )
		iLen += formatex( temp[iLen], iMax - iLen, stamp )

		nvault_set( g_vault, szSteam, temp )
	}

	return PLUGIN_CONTINUE
}

public show_visits(id)
{
	if ( is_user_connected( id ) )
	{
		get_user_authid( id, szSteam, charsmax( szSteam ) )
		get_pcvar_string( c_str1, prefix, charsmax(prefix) )
		get_pcvar_string( c_str2, web_addr, charsmax(web_addr) )

		nvault_get( g_vault , szSteam , temp , charsmax( temp ) )
		
		strtok(temp, tmp, charsmax( tmp ), stamp, charsmax( stamp ), ';');

		ColorChat( id, TEAM_COLOR, "%s^x01 To juz Twoja ^x03%s^x01 wizyta na naszym serwerze. Zarejestruj sie na ^x03%s^x01, jesli jeszcze tego nie zrobiles :)", prefix, tmp, web_addr )
	}
}

public client_disconnected(id)
{
	if( task_exists(id) )
	{
		remove_task(id)
	}
}

public plugin_end()
{
	if( g_vault != INVALID_HANDLE )
	{
		nvault_close(g_vault)
	}
}