Code:
#include <amxmodx>
#include <sqlx>
#include <fakemeta>
// Max players possible, keep at 32 unless VALVe somehow makes more than 32 players
#define MAX_PLAYERS 32
// SQL tuple for the connections
new Handle:gSqlTuple = Empty_Handle;
// The current threaded query index so we don't overlap threaded queries for a player
new gThreadIndex[MAX_PLAYERS + 1];
// Use plugin_cfg() since it is after the sql.cfg is executed
public plugin_cfg()
{
// Create the SQL tuple from amx_sql_* cvars
gSqlTuple = SQL_MakeStdTuple();
// Grab any players that have connected
new players[32], pnum;
get_players(players, pnum);
// Check their names
while(pnum)
{
CheckName(players[--pnum]);
}
}
public client_putinserver(id)
{
// Check if already made the tuple
if(gSqlTuple != Empty_Handle)
{
// Query player for name
CheckName(id);
}
}
public client_infochanged(id)
{
// Check if the SQL tuple has been made and if player is connected
if(gSqlTuple != Empty_Handle && is_user_connected(id))
{
// Grab old and new names
new oldName[32], newName[32];
get_user_name(id, oldName, charsmax(oldName));
get_user_info(id, "name", newName, charsmax(newName));
// Check if name changed
if(!equal(oldName, newName))
{
// Check new name against the database
CheckName(id, newName);
}
}
}
CheckName(id, name[32] = "")
{
// Check if name was passed to function
if(!name[0])
{
// Grab current name instead
get_user_name(id, name, charsmax(name));
}
// Get player's SteamID for the query
new steamID[35];
get_user_authid(id, steamID, charsmax(steamID));
// Format the query
new query[128];
formatex(query, charsmax(query), "SELECT name FROM mytable WHERE steamid = '%s';", steamID);
// Prepare data for the query
new data[4];
data[0] = id;
data[1] = get_user_userid(id);
data[2] = ++gThreadIndex[id];
data[3] = EncodeText(name);
// Query the MySQL server for data
SQL_ThreadQuery(gSqlTuple, "QuerySelectData", query, data, sizeof(data));
}
public QuerySelectData(failstate, Handle:query, errcode, error[], data[], dataSize, Float:queueTime)
{
// Check if the query failed at all
switch(failstate)
{
case TQUERY_CONNECT_FAILED: log_amx("Failed to connect for query: %s", error);
case TQUERY_QUERY_FAILED : log_amx("Failed query: %s", error);
default:
{
// Grab player ID from data
new id = data[0];
// Check if player is connected and matches user id from data
if(is_user_connected(id) && get_user_userid(id) == data[1])
{
// Check if threaded query index matches
if(data[2] == gThreadIndex[id])
{
// Check if the query has results
if(SQL_NumResults(query))
{
// Grab the player's name that we are checking
new name[32];
DecodeText(data[3], name, charsmax(name));
// Grab the name from the database
new found[32];
SQL_ReadResult(query, 0, found, charsmax(found));
// Check if names match
if(!equal(name, found))
{
// Change name back
set_user_info(id, "name", found);
}
}
}
}
}
}
}
// Functions for encoding/decoding strings to integers
EncodeText(const string[])
{
return engfunc(EngFunc_AllocString, string);
}
DecodeText(const string, output[], output_len)
{
return global_get(glb_pStringBase, string, output, output_len);
}