/* Plugin generated by AMXX-Studio */

/*
AMX Mod X Plugin

Last Visited by GordonFreeman

Plugin made:
- Remembers the last time a player visits the server

Features:
- Greet messages
- Greet sounds
- Store configuration in nvault file

Type in chat:
/lastvisited or /lv for last visited info :)


I REAL LIKE nVault system!!!
*/
//-----------------------------------------------------------------------
#include <amxmodx>
#include <nvault>
//-----------------------------------------------------------------------
#define PLUGIN "Last Visited"
#define VERSION "1.1"
#define AUTHOR "GordonFreeman"
//-----------------------------------------------------------------------
new vault
new const tag[]="[LV]"
new chour[6],cmin[6],cday[6],cm[6],cyear[6],ctime[32] //  TIME IS NOW
new bool:g_messages[33],bool:g_sounds[33] // Cvars :)
//-----------------------------------------------------------------------
public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	// Client commands
	register_clcmd("say /lastvisited","say_lastvisited")
	register_clcmd("say /lv","say_lastvisited")
	// Server commands
	register_srvcmd("amx_lv_messages","set_messages") // messages cvar
	register_srvcmd("amx_lv_sounds","set_sounds") // sounds cvar
	
	vault=nvault_open("last_visited") // the vault
	
	set_time() // Set current time
}
//-----------------------------------------------------------------------
public plugin_end(){
	nvault_close(vault)
}
//-----------------------------------------------------------------------
public plugin_cfg(){
	// Get plugin configuration
	new messages_status=nvault_get(vault,"set_messages")
	new sounds_status=nvault_get(vault,"set_sounds")
	
	// Set greet messages status
	if(messages_status==0){
		g_messages[32]=false
	}else{
		g_messages[32]=true
	}
	
	// Set greet sounds status
	if(sounds_status==0){
		g_sounds[32]=false
	}else{
		g_sounds[32]=true
	}
}
//-----------------------------------------------------------------------
public set_time(){
	get_time("%H",chour,5) 	// Hours
	get_time("%M",cmin,5) 	// Minutes
	get_time("%d",cday,5)	// Day
	get_time("%m",cm,5)	// Mounth
	get_time("%Y",cyear,5)	// Year
	get_time("%H:%M:%S - %m/%d/%Y",ctime,31) // the time
}
//-----------------------------------------------------------------------
public client_putinserver(id){
	set_time()
	set_task(5.0,"check_data",id) // it's begining :)
}
//-----------------------------------------------------------------------
public check_data(id){
	set_time()
	new user[32],nick[31],value[64]
	new shour[6],smin[6],sday[6],sm[6],syear[6] // stored time
	
	get_user_authid(id,user,31)
	get_user_name(id,nick,31)
	
	// Get stored time
	nvault_get(vault,user,value,63)
	parse(value,shour,5,smin,5,sday,5,sm,5,syear,5)
	
	if(g_messages[32]==true)greet_messages(id)
	if(g_sounds[32]==true)greet_sounds(id)
	
	if(nvault_get(vault,user)==0){
		client_print(id,print_chat,"%s You have not played here",tag)
	}else{
		client_print(id,print_chat,"%s The last time you played here at %s:%s - %s/%s/%s",tag,shour,smin,sm,sday,syear)
	}
	
	save_data(id)
}
//------------------------------------------------------------------------------	
public greet_messages(id){
	new user[32],hostname[64]
	
	new hrs=str_to_num(chour)
	
	get_user_name(id,user,31)
	
	get_cvar_string("hostname",hostname,63)
	
	set_time()
	
	if(hrs<12){
		client_print(id,print_chat,"%s Good Morning %s! Welcome to %s",tag,user,hostname)
		client_print(id,print_chat,"%s The time is now - %s",tag,ctime)
	}else if(hrs<=16){
		client_print(id,print_chat,"%s Good Afternoon %s! Welcome to %s",tag,user,hostname)
		client_print(id,print_chat,"%s The time is now - %s",tag,ctime)
	}else if(hrs<=23){
		client_print(id,print_chat,"%s Good Evening %s! Welcome to %s",tag,user,hostname)
		client_print(id,print_chat,"%s The time is now - %s",tag,ctime)
	}else if(hrs<=03){
		client_print(id,print_chat,"%s Good Night %s! Welcome to %s",tag,user,hostname)
		client_print(id,print_chat,"%s The time is now - %s",tag,ctime)
	}else{
		client_print(id,print_chat,"%s Hello %s! Welocme to %s",tag,user,hostname)
		client_print(id,print_chat,"%s The time is now - %s",tag,ctime)
	}
}
//-------------------------------------------------------------------------------
public greet_sounds(id){
	new hrs=str_to_num(chour)
	
	new morningsounds[2][]={
		"scientist/c1a0_sci_gm(e55)",
		"scientist/c1a0_sci_gm1(e60)"
	}
	new afternoonsounds[5][]={
		"scientist/goodtoseeyou",
		"scientist/greetings",
		"scientist/greetings2",
		"scientist/hellofreeman(e35)",
		"scientist/hellothere"
	}
	new evningsounds[3][]={
		"scientist/hellothere",
		"scientist/greetings",
		"scientist/c1a0_sci_itsyou"
	}
	if(hrs<=12){
		client_cmd(id,"spk %s",morningsounds[random_num(0,1)])
	}else if(hrs<=16){
		client_cmd(id,"spk %s",afternoonsounds[random_num(0,4)])
	}else if(hrs<=23){
		client_cmd(id,"spk %s",evningsounds[random_num(0,2)])
	}
}
//--------------------------------------------------------------------------------	
public save_data(id){
	new user[32],value[64]
	
	get_user_authid(id,user,31)
	
	set_time()
	formatex(value,63,"%s %s %s %s %s",chour,cmin,cday,cm,cyear)
	nvault_set(vault,user,value) // save
}
//--------------------------------------------------------------------------------
public set_messages(){
	// Set greet messages option
	if(g_messages[32]==false){
		g_messages[32]=true
		nvault_set(vault,"set_messages","1")
		log_amx(">>> Greet messages is enabled")
		server_print("%s Greet messages is enabled",tag)
	}else{
		g_messages[32]=false
		nvault_set(vault,"set_messages","0")
		log_amx(">>> Greet messages is disabled")
		server_print("%s Greet messages is disabled",tag)
	}
}
public set_sounds(){
	// Set greet sounds option
	if(g_sounds[32]==false){
		g_sounds[32]=true
		nvault_set(vault,"set_sounds","1")
		log_amx(">>> Greet sounds is enabled")
		server_print("%s Greet sounds is enabled",tag)
	}else{
		g_sounds[32]=false
		nvault_set(vault,"set_sounds","0")
		log_amx(">>> Greet sounds is disabled")
		server_print("%s Greet sounds is disabled",tag)
	}
}
//--------------------------------------------------------------------------------
public say_lastvisited(id){
	// say /lv
	new shour[6],smin[6],sday[6],sm[6],syear[6],value[64],user[32]
	
	get_user_authid(id,user,31)
	
	nvault_get(vault,user,value,63)
	parse(value,shour,5,smin,5,sday,5,sm,5,syear,5)
	
	client_print(id,print_chat,"%s The last time you played here at %s:%s - %s/%s/%s",tag,shour,smin,sm,sday,syear)
}
