#include <amxmodx>
#include <sqlx>
#include <chatmanager>
#pragma semicolon 1
#define PLUGIN_NAME "Chat Manager: Prefixes MySQL"
#define PLUGIN_VERSION "0.3b"
#define PLUGIN_AUTHOR "Denzer"
enum _:CVARS
{
HOST[32],
USER[16],
PASS[32],
DB[16],
TABLE[32],
ACCESS[6]
};
enum _:SQL
{
SQL_TABLE,
SQL_CLEAR,
SQL_LOAD,
SQL_INSERT,
SQL_DELETE
};
enum _:FIELDS
{
FIELD_ID,
FIELD_PLAYER_NAME,
FIELD_PLAYER_STEAMID,
FIELD_PREFIX,
FIELD_EXPIRED
};
new g_Cvars[CVARS], g_sPrefix[MAX_PLAYERS + 1][32];
new Handle:g_hSqlTuple, Handle:g_hSqlConnection;
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
InitCvars();
InitCmds();
InitSQL();
}
public client_putinserver(id)
{
SQL_Load(id);
}
public plugin_end()
{
SQL_FreeHandle(g_hSqlTuple);
if(g_hSqlConnection) SQL_FreeHandle(g_hSqlConnection);
}
public InitCvars()
{
new pCvar;
pCvar = create_cvar("cm_host", "ip", FCVAR_PROTECTED, "Host");
bind_pcvar_string(pCvar, g_Cvars[HOST], charsmax(g_Cvars[HOST]));
pCvar = create_cvar("cm_user", "name", FCVAR_PROTECTED, "User");
bind_pcvar_string(pCvar, g_Cvars[USER], charsmax(g_Cvars[USER]));
pCvar = create_cvar("cm_pass", "pass", FCVAR_PROTECTED, "Pass");
bind_pcvar_string(pCvar, g_Cvars[PASS], charsmax(g_Cvars[PASS]));
pCvar = create_cvar("cm_db", "name_db", FCVAR_PROTECTED, "DB");
bind_pcvar_string(pCvar, g_Cvars[DB], charsmax(g_Cvars[DB]));
pCvar = create_cvar("cm_table", "cm_prefixes", FCVAR_PROTECTED, "Table");
bind_pcvar_string(pCvar, g_Cvars[TABLE], charsmax(g_Cvars[TABLE]));
pCvar = create_cvar("cm_access", "a", FCVAR_PROTECTED, "Access");
bind_pcvar_string(pCvar, g_Cvars[ACCESS], charsmax(g_Cvars[ACCESS]));
AutoExecConfig();
}
public InitCmds()
{
// "player_name" "player_steamid" "prefix" "days"
register_concmd("cm_set_prefix_sql", "CmdSetPrefix");
// "player_steamid"
register_concmd("cm_reset_prefix_sql", "CmdResetPrefix");
register_concmd("cm_refresh_prefix", "SQL_Load"); //добавлена кмд для обновления
}
public CmdSetPrefix(id)
{
if(!(get_user_flags(id) & read_flags(g_Cvars[ACCESS])))
return PLUGIN_HANDLED;
new szArgs[256], szName[MAX_NAME_LENGTH], szAuth[24], szPrefix[32], szDays[6];
read_args(szArgs, charsmax(szArgs));
remove_quotes(szArgs);
trim(szArgs);
parse(szArgs,
szName, charsmax(szName),
szAuth, charsmax(szAuth),
szPrefix, charsmax(szPrefix),
szDays, charsmax(szDays));
new iDays = str_to_num(szDays);
if(!szAuth[0] || !szPrefix[0])
{
console_print(id, "[%s] Error. Syntax: cm_set_prefix_sql ^"player_name^" ^"player_steamid^" ^"prefix^" ^"days^"", PLUGIN_NAME);
return PLUGIN_HANDLED;
}
new pTarget = find_player_ex(FindPlayer_MatchAuthId,szAuth);
SQL_Delete(szAuth); //удаляем старую запись если есть
SQL_Insert(szName, szAuth, szPrefix, iDays);
SQL_Load(pTarget); //обновляем преф
console_print(id, "[%s] Prefix set: %s | %s | %s | %d day(s)", PLUGIN_NAME, szName, szAuth, szPrefix, iDays);
return PLUGIN_HANDLED;
}
public CmdResetPrefix(id)
{
if(!(get_user_flags(id) & read_flags(g_Cvars[ACCESS])))
return PLUGIN_HANDLED;
new szAuth[24];
read_args(szAuth, charsmax(szAuth));
remove_quotes(szAuth);
trim(szAuth);
if(!szAuth[0])
{
console_print(id, "[%s] Error. Syntax: cm_reset_prefix_sql ^"player_steamid^"", PLUGIN_NAME);
return PLUGIN_HANDLED;
}
new pTarget = find_player_ex(FindPlayer_MatchAuthId,szAuth);
SQL_Delete(szAuth);
g_sPrefix[pTarget] = "";
cm_set_prefix(pTarget, g_sPrefix[pTarget]); //обнуляем преф
console_print(id, "[%s] Data is deleted: %s", PLUGIN_NAME, szAuth);
return PLUGIN_HANDLED;
}
public InitSQL()
{
g_hSqlTuple = SQL_MakeDbTuple(g_Cvars[HOST], g_Cvars[USER], g_Cvars[PASS], g_Cvars[DB]);
SQL_SetCharset(g_hSqlTuple, "utf8");
new iError, szError[128];
g_hSqlConnection = SQL_Connect(g_hSqlTuple, iError, szError, charsmax(szError));
if(g_hSqlConnection == Empty_Handle)
set_fail_state("%s %d", szError, iError);
server_print("[%s] SQL connection was successfully established with the server.", PLUGIN_NAME);
new szQuery[512];
new cData[1]; cData[0] = SQL_TABLE;
formatex(szQuery, charsmax(szQuery), "\
CREATE TABLE IF NOT EXISTS `%s` \
( \
`id` INT(11) NOT NULL auto_increment PRIMARY KEY, \
`player_name` VARCHAR(32) DEFAULT 'N/A', \
`player_steamid` VARCHAR(24) DEFAULT 'N/A', \
`prefix` VARCHAR(32) DEFAULT 'N/A', \
`expired` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP \
);", g_Cvars[TABLE]);
SQL_ThreadQuery(g_hSqlTuple, "QueryHandler", szQuery, cData, sizeof(cData));
SQL_Clear();
}
public QueryHandler(iFailState, Handle:hQuery, szError[], iErrnum, cData[], iSize, Float:fQueueTime)
{
if(iFailState != TQUERY_SUCCESS)
{
log_amx("SQL Error #%d - %s", iErrnum, szError);
return;
}
switch(cData[0])
{
case SQL_TABLE, SQL_CLEAR, SQL_INSERT, SQL_DELETE: {}
case SQL_LOAD:
{
new id = cData[1];
if(!is_user_connected(id))
return;
if(SQL_NumResults(hQuery))
{
SQL_ReadResult(hQuery, FIELD_PREFIX, g_sPrefix[id], charsmax(g_sPrefix[]));
replace_color_tag(g_sPrefix[id]);
cm_set_prefix(id, g_sPrefix[id]);
}
else
g_sPrefix[id] = "";
}
}
}
public SQL_Clear()
{
new szQuery[256];
new cData[1]; cData[0] = SQL_CLEAR;
formatex(szQuery, charsmax(szQuery), "\
DELETE \
FROM `%s` \
WHERE `expired` <= now()", g_Cvars[TABLE]);
SQL_ThreadQuery(g_hSqlTuple, "QueryHandler", szQuery, cData, sizeof(cData));
}
public SQL_Load(id)
{
new szQuery[256];
new cData[2]; cData[0] = SQL_LOAD, cData[1] = id;
new szAuth[24]; get_user_authid(id, szAuth, charsmax(szAuth));
formatex(szQuery, charsmax(szQuery), "\
SELECT * \
FROM `%s` \
WHERE `player_steamid` = '%s'", g_Cvars[TABLE], szAuth);
SQL_ThreadQuery(g_hSqlTuple, "QueryHandler", szQuery, cData, sizeof(cData));
}
SQL_Insert(szName[] = "", szAuth[], szPrefix[], iDays)
{
new szQuery[512], szDays[32];
new cData[1]; cData[0] = SQL_INSERT;
if(iDays)
formatex(szDays, charsmax(szDays), "now() + interval %d day", iDays);
formatex(szQuery, charsmax(szQuery), "\
INSERT INTO `%s` \
( \
`player_name`, \
`player_steamid`, \
`prefix`, \
`expired` \
) \
VALUES \
( \
'%s', \
'%s', \
'%s', \
%s \
)", g_Cvars[TABLE], szName, szAuth, szPrefix, !iDays ? "NULL" : szDays);
SQL_ThreadQuery(g_hSqlTuple, "QueryHandler", szQuery, cData, sizeof(cData));
}
public SQL_Delete(szAuth[])
{
new szQuery[256];
new cData[1]; cData[0] = SQL_DELETE;
formatex(szQuery, charsmax(szQuery), "\
DELETE \
FROM `%s` \
WHERE `player_steamid` = '%s'", g_Cvars[TABLE], szAuth);
SQL_ThreadQuery(g_hSqlTuple, "QueryHandler", szQuery, cData, sizeof(cData));
}
replace_color_tag(string[])
{
new len = 0;
for (new i; string[i] != EOS; i++) {
if (string[i] == '!') {
switch (string[++i]) {
case 'd': string[len++] = 0x01;
case 't': string[len++] = 0x03;
case 'g': string[len++] = 0x04;
case EOS: break;
default: string[len++] = string[i];
}
} else {
string[len++] = string[i];
}
}
string[len] = EOS;
}