Here is the code maybe help someone.
I'm now with a problem every time change map my plugin say can't connect to database and i don't change anything in plugin.
PHP Code:
/* Plugin generated by AMXX-Studio */
#include <amxmodx>
#include <amxmisc>
#include <dbi>
#include <engine>
#include <csx>
#define PLUGIN "Change Name and Kick no registered steamID"
#define VERSION "1.0"
#define AUTHOR "spiider"
#define CVAR_CHHOST "reg_host"
#define CVAR_CHUSER "reg_user"
#define CVAR_CHPASS "reg_pass"
#define CVAR_CHDB "reg_db"
#define TABLE "users"
//new names[32] // id->playername
new g_regged[33]
// vars going here
new gate_matchID
//public pug_cvar_shield = 1;
public plugin_modules() {
require_module("engine")
require_module("dbi")
}
// here goes check client and change name :)
public client_authorized(id) {
if (is_user_bot(id))
return PLUGIN_CONTINUE
new authid[32]
get_user_authid(id,authid,31)
new Sql:sql
if (!connect(sql))
return false
const LONGER = 128
new query[LONGER + 1]
format(query, LONGER, "SELECT `name` FROM %s WHERE `steamid` = '%s' LIMIT 1", TABLE, authid)
new Result:result = dbi_query(sql, query)
//Loop through the result set
while (result && dbi_nextrow(result)>0) {
new qry[32]
//Get the column/field called "keyname" from the result set
dbi_result(result, "name", qry, 32)
set_user_info(id,"name",qry)
}
dbi_close(sql)
g_regged[id] = checkregister(authid)
if (g_regged[id]) {
} else {
server_cmd("kick #%d Your steamID %s is not registered for this game.", get_user_userid(id), authid)
}
return true
}
// now when change name in game
public client_infochanged(id) {
if (is_user_bot(id) || is_user_connecting(id))
return PLUGIN_CONTINUE
new nick[32]
new newnick[32]
new authid[32]
get_user_authid(id,authid,31)
new Sql:sql
if (!connect(sql))
return false
const LONGER = 128
new query[LONGER + 1]
format(query, LONGER, "SELECT `name` FROM %s WHERE `steamid` = '%s' LIMIT 1", TABLE, authid)
new Result:result = dbi_query(sql, query)
//Loop through the result set
while (result && dbi_nextrow(result)>0) {
new qry[32]
dbi_result(result, "name", qry, 32)
// set_user_info(id,"name",qry)
nick = qry;
}
format(newnick, 31, "%s", nick)
set_user_info(id, "name", newnick)
dbi_close(sql)
return true
}
// here goes check if is registered for this game
bool:checkregister(authid[]) {
new Sql:sql
if (!connect(sql))
return false
const LONGER = 128
new query[LONGER + 1]
gate_matchID = get_cvar_pointer ( "matchID" )
format(query, LONGER, "SELECT `playerID` FROM players WHERE `steamID` = ^"%s^" AND `matchID` = '%d' LIMIT 1", authid,get_pcvar_num(gate_matchID))
new Result:result = dbi_query(sql, query)
if (result <= RESULT_FAILED) {
new error[256]
dbi_error(sql, error, 255)
log_amx("Error while quering SQL server for %s, can't check registration: %s", authid, error)
dbi_close(sql)
return false
}
else if (result == RESULT_NONE || !dbi_nextrow(result)) {
dbi_close(sql)
return false
}
gate_matchID = get_cvar_pointer ( "matchID" )
format(query, LONGER, "UPDATE players SET `in`='1' WHERE `matchID`='%d' AND `steamid`=^"%s^"", get_pcvar_num(gate_matchID), authid)
dbi_query(sql, query)
dbi_close(sql)
return true
}
// here goes the database connection
bool:connect(&Sql:sql) {
const LEN = 128
new host[LEN], user[LEN], pass[LEN], db[LEN], error_msg[LEN]
get_cvar_string(CVAR_CHHOST, host, LEN - 2)
get_cvar_string(CVAR_CHUSER, user, LEN - 2)
get_cvar_string(CVAR_CHPASS, pass, LEN - 2)
get_cvar_string(CVAR_CHDB, db, LEN - 2)
sql = dbi_connect(host, user, pass, db, error_msg, LEN - 2)
if (!sql) {
log_amx("ERROR - Can't connect to SQL db: %s", error_msg)
return false
}
return true
}
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_cvar(CVAR_CHHOST, "ip")
register_cvar(CVAR_CHUSER, "user")
register_cvar(CVAR_CHPASS, "pw")
register_cvar(CVAR_CHDB, "db")
register_cvar("matchID", "1")
new Sql:sql
if (!connect(sql)) {
log_amx("Couldn't connect to SQL database at plugin_init! Pausing plugin.")
pause("a")
return
}
else
server_print("[%s] Connected successfully to SQL database.", PLUGIN)
dbi_close(sql)
}
And offcurse i know SQLx is better but i'm without time to change the script to SQLx. And this is my first script in AMXX
And the variable warmup is part of a project I'm developing and this script make part.