/*
*  AMX Mod script. 
* 
* (c) Copyright 2003, ST4life 
* This file is provided as is (no warranties). 
*/

#include <amxmod>
#include <mysql>

/*
* "DefinedNames" is a plugin to examine players names. It checks for player authid and its name.
* is it another name than in the list it will show a menu to him to choose one.
*
* There are to flags you can use:
*  a - user can only use the "original" name
*  b - user can use "original", "fake1" and "fake2" nick
*
* Define a player (f.e. in "admin.cfg"):
* amx_playernames "authid" "flag" "original" "fake1" "fake2"
*
* Example:
* amx_playernames "123456" "a" "Ernie" "Bert"  // user only can use "Ernie" as a nick
* amx_playernames "654321" "b" "Biene Maja" "Willi" "Rumpel" // user can use every of this three nicks
* 
*
* "DefinedNames" is a rewritten "Admin Base", "One name" plugin from OLO. So many many thanks to the
* AMX-god ;) !!! Ideas for flags comes from ToT|Viper! Thanks!
* 
* History:
*
* v0.2: - new: If someone in list type in a name to something he is
*              not allowed, there will be now a menu to choose a name
*              he can use.
*
* v0.1: - public release
*/ 

#define MAX_NAMES	64

new names_authid[MAX_NAMES][32]
new names_original[MAX_NAMES][32]
new names_fake1[MAX_NAMES][32]
new names_fake2[MAX_NAMES][32]
new names_flag[MAX_NAMES][32]
new names_num = 0
new dn_dbhost[32]
new dn_dbuser[32]
new dn_dbpass[32]
new dn_dbname[32]
new dn_dberror[32]
new mysql = 0 

public mysql_init(){
	server_cmd("exec addons/amx/mysql.cfg")

	get_cvar_string("amx_mysql_host",dn_dbhost,32) 
	get_cvar_string("amx_mysql_user",dn_dbuser,32) 
	get_cvar_string("amx_mysql_pass",dn_dbpass,32) 
	get_cvar_string("amx_mysql_db",dn_dbname,32)
	
	mysql=mysql_connect(dn_dbhost,dn_dbuser,dn_dbpass,dn_dbname,dn_dberror,32) 
  
	if(mysql < 1){
		server_print("[AMX] DefinedNames: ERROR, could not connect!!")
		server_print("[AMX] DefinedNames: %s",dn_dberror)
		return PLUGIN_HANDLED
	} else {
		set_task(0.1,"plyrnamesmysql_cmd")
	}
	return PLUGIN_CONTINUE
}

public plyrnamesmysql_cmd(){
	new query[400]
	format(query,390,"SELECT authid,flag,original,fake1,fake2 FROM amx_defnames")
	mysql_query(mysql,query)
	while(mysql_nextrow(mysql) > 0){
		if (names_num >= MAX_NAMES){
			server_print("[AMX] Names limit reached!")
			return PLUGIN_HANDLED
		}
		mysql_getfield(mysql,1,names_authid[names_num],31)
		mysql_getfield(mysql,2,names_flag[names_num],31)
		mysql_getfield(mysql,3,names_original[names_num],31)
		mysql_getfield(mysql,4,names_fake1[names_num],31)
		mysql_getfield(mysql,5,names_fake2[names_num],31)
		names_num++
	}
	return PLUGIN_CONTINUE
}

public act_menu(id, key) {
	new authid[32]
	get_user_authid(id,authid,31)
	new index = -1
	for(new i = 0; i < names_num; ++i) {
		if (equal(authid,names_authid[i])) {
			index = i
			break
		}
	}
	if (index != -1) {
		switch(key) {
			case 0: {
				set_user_info(id,"name",names_original[index])
				client_print(id,print_chat,"[AMX] Name changed to %s",names_original[index])
			}
			case 1: {
				set_user_info(id,"name",names_fake1[index])
				client_print(id,print_chat,"[AMX] Name changed to %s",names_fake1[index])
			}
			case 2: {
				set_user_info(id,"name",names_fake2[index])
				client_print(id,print_chat,"[AMX] Name changed to %s",names_fake2[index])
			}
			case 9: {
				client_print(id,print_chat,"[AMX] Name changed to your last nick")
			}
		}
	}
	return PLUGIN_HANDLED 
}

public check_connectname(id,username[],userauthid[]){
	new index = -1
	for(new i = 0; i < names_num; ++i) {
		if (equal(userauthid,names_authid[i])) {
			index = i
			break
		}
	}
	if (index != -1) {
		if (contain(names_flag[index],"a") != -1){
			if (!equal(username,names_original[index])) {
				set_user_info(id,"name",names_original[index])
			}
		}
		if (contain(names_flag[index],"b") != -1) {
			if ((!equal(username,names_original[index]))&&(!equal(username,names_fake1[index]))&&(!equal(username,names_fake2[index]))){
				set_user_info(id,"name",names_original[index])
			}
		}
	}
	return PLUGIN_CONTINUE
}

public check_namechanged(id,usernewname[],useroldname[],userauthid[]){
	new index = -1
	for(new i = 0; i < names_num; ++i) {
		if (equal(userauthid,names_authid[i])) {
			index = i
			break
		}
	}
	if (index != -1) {
		if (contain(names_flag[index],"a") != -1) {
			if (!equal(usernewname,names_original[index])) {
				set_user_info(id,"name",names_original[index])
				client_print(id,print_chat,"[AMX] You are not allowed to change your name!")
			}
		}
		if (contain(names_flag[index],"b") != -1) {
			if (!equal(usernewname,names_original[index]) && !equal(usernewname,names_fake1[index]) && !equal(usernewname,names_fake2[index])) {
				set_user_info(id,"name",useroldname)
				client_print(id,print_chat,"[AMX] You can change your name only to this available nicks:")
				client_print(id,print_chat,"[AMX] Original: %s, Fake 1: %s, Fake 2: %s",names_original[index],names_fake1[index],names_fake2[index])
				client_print(id,print_chat,"[AMX] Please choose one from the menu!")
				new keys 
				new text[513]
				if(equal(names_fake2[index],"")) {
					format(text, 512, "\yChoose Name: ^n\w1. %s^n2. %s^n^n0. Back (old nick)",names_original[index],names_fake1[index]) 
					keys = (1<<0)|(1<<1)
				} else {
					format(text, 512, "\yChoose Name: ^n\w1. %s^n2. %s^n3. %s^n^n0. Back (old nick)",names_original[index],names_fake1[index],names_fake2[index]) 
					keys = (1<<0)|(1<<1)|(1<<2)
				}
				keys |= (1<<9)
				show_menu(id, keys, text) 
			}
		}
	}
	return PLUGIN_CONTINUE
}

public client_putinserver(id){
	new name[33], authid[32]
	get_user_name(id,name,32)
	get_user_authid(id,authid,31)
	check_connectname(id,name,authid)
	return PLUGIN_CONTINUE 
}

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

public plugin_init(){
	register_plugin("DefinedNames MySQL","0.2","ST4life")
	register_menucmd(register_menuid("Choose Name"), 1023, "act_menu")
	set_task(1.0, "mysql_init")
	return PLUGIN_CONTINUE
}