/* 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>

#define NUM_NAMES 3

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,63)
    get_cvar_string("amx_mysql_user",muser,31)
    get_cvar_string("amx_mysql_pass",mpass,31)
    get_cvar_string("amx_mysql_db",mdb,31)

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

    if(mysql < 1)
        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,255,"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[17],name[33]
    get_user_authid(id,authid,16)
    get_user_name(id,name,32)

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

    mysql_query(mysql,query)

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

    return PLUGIN_CONTINUE
}


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

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

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


public sql_lastname(id,level){

    if (!(get_user_flags(id)&level)){
        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[33]
    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,15)

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

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

    new msg[196]
    mysql_nextrow(mysql)

    new i = 0
    new last[33] = ""
    while(mysql_nextrow(mysql)&&(i<NUM_NAMES)){
		if(i>0) add(msg,195,", ")
		mysql_getfield(mysql,1,last,32)
		add(msg,195,last)
		i++
	}

	new name[33]
	get_user_name(player,name,32)
	client_print(id,print_console,"[AMX] Last names for %s: %s",name,msg)

	return PLUGIN_HANDLED
}


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


public plugin_init(){
    register_plugin("Lastname","0.8","JustinHoMi")
    register_clcmd("amx_lastname","sql_lastname",ADMIN_IMMUNITY,"< authid or part of nick >")
    return PLUGIN_CONTINUE
}
