/* AMX Mod script.
*
* Lastname Plugin
*  by JustinHoMi
*
*/

/* Database schema
*
* CREATE TABLE amx_lastname (
*   player_id varchar(16) NOT NULL default '',
*   player_name varchar(32) NOT NULL default '',
*   time timestamp(14) NOT NULL,
*   PRIMARY KEY  (player_id,player_name)
* ) TYPE=MyISAM;
*
*/

#include <amxmod>
#include <mysql>

new merror[32]
new mhost[64]
new muser[32]
new mpass[32]
new mdb[32]
new mysql = 0
new state = 0


public sql_init(){

	get_cvar_string("amx_mysql_host",mhost,64)
	get_cvar_string("amx_mysql_user",muser,32)
	get_cvar_string("amx_mysql_pass",mpass,32)
	get_cvar_string("amx_mysql_db",mdb,32)

	mysql = mysql_connect(mhost,muser,mpass,mdb,merror,32)

	if(mysql < 1){
		mysql_error(mysql,merror,32)
		server_print("[AMX] MySQL error : could not connect : '%s'",merror)
	}
	else
		server_print("[AMX] Lastname Plugin successfully connected.")

	// delete all records that are over 2 weeks old
	new query[256]
	format(query,256,"DELETE FROM amx_lastname where now()-time>1209600")
	mysql_query(mysql,query)

	if (merror[0]) server_print("[AMX] MYSQL error: %s",merror)

	state = 1

	return PLUGIN_CONTINUE
}


public client_connect(id){

	if (state == 0)
		sql_init()

	new authid[16],name[32]
	get_user_authid(id,authid,16)
	get_user_name(id,name,32)

	new query[256]
	format(query,256,"REPLACE into amx_lastname (player_id,player_name) values ('%s','%s')",authid,name)

	mysql_query(mysql,query)

	mysql_error(mysql,merror,32)
	if (merror[0]){
		server_print("[AMX] MYSQL error: %s",merror)
		return PLUGIN_HANDLED
	}

	return PLUGIN_CONTINUE
}


public client_infochanged(id){
	if (is_user_connected(id)){
		new newname[32], oldname[32]
		get_user_info(id, "name", newname,32)
		get_user_name(id,oldname,32)

		if (!equal(oldname,newname)){
			new authid[16]
			get_user_authid(id,authid,16)

			new query[256]
			format(query,256,"REPLACE into amx_lastname (player_id,player_name) values ('%s','%s')",authid,newname)
			mysql_query(mysql,query)
			mysql_error(mysql,merror,32)
			if (merror[0]) server_print("[AMX] MYSQL error: %s",merror)
     	}
	}
	return PLUGIN_CONTINUE
}


public sql_lastname(id){

	if (!(get_user_flags(id)&ADMIN_IMMUNITY)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}
	if (read_argc() < 2){
		client_print(id,print_console,"[AMX] Usage: amx_lastname < authid or part of nick >")
		return PLUGIN_HANDLED
	}

	new arg[32]
	read_argv(1,arg,32)

	new player = find_player("c",arg)
	if (!player) player = find_player("bl",arg)
	if (!player){
		client_print(id,print_console,"[AMX] Player not found")
		return PLUGIN_HANDLED
	}

	new authid[16]
	get_user_authid(player,authid,16)

	new query[256]
	format(query,256,"SELECT DISTINCT player_name FROM amx_lastname WHERE player_id='%s' ORDER BY time DESC",authid)

	mysql_query(mysql,query)
	mysql_error(mysql,merror,32)
	if (merror[0]){
		server_print("[AMX] MYSQL error: %s",merror)
		return PLUGIN_HANDLED
	}

	new last[96],last2[32],last3[32]

	mysql_nextrow(mysql)

	if(mysql_nextrow(mysql)>0)
		mysql_getfield(mysql,1,last,32)
	if(mysql_nextrow(mysql)>0)
	{
		mysql_getfield(mysql,1,last2,32)
		add(last,96,",")
		add(last,96,last2)
	}
	if(mysql_nextrow(mysql)>0)
	{
		mysql_getfield(mysql,1,last3,32)
		add(last,96,",")
		add(last,96,last3)
	}

	client_print(id,print_console,"[AMX] Last names: %s",last)

	return PLUGIN_HANDLED
}


public plugin_end()
{
	if (mysql > 0)
		mysql_close(mysql)
	return PLUGIN_CONTINUE
}


public plugin_init(){
	register_plugin("Lastname","0.1b","JustinHoMi")
	register_clcmd("amx_lastname","sql_lastname",ADMIN_IMMUNITY,"amx_lastname < wonid or nick >")
	return PLUGIN_CONTINUE
}

