#include <amxmodx>
#include <reapi>
#include <sqlx>
#define VERSION "1.0"
new const db_name[] = "hns_x"
new const table_name[] = "save_players"
#define mp MAX_PLAYERS + 1
new Handle:sql, g_szSteamID[mp][MAX_AUTHID_LENGTH];
new g_iKills[mp], g_iDeaths[mp];
enum _:TOTAL_SQL_TYPES {
SQL_TYPE_LOAD,
SQL_TYPE_IGNORE
};
public plugin_init() {
register_plugin("HnS X", VERSION, "MIRDOBRO");
// register_clcmd("say /s", "Test");
RegisterHookChain(RG_CBasePlayer_Killed, "on_player_killed_post", .post = true);
}
public OnConfigsExecuted() {
SQL_SetAffinity("sqlite");
sql = SQL_MakeDbTuple("", "", "", db_name); // Создание картежа данных для соединения с БД
SQL_SetCharset(sql, "utf8");
// запрос на создание таблицы
new szQuery[MAX_FMT_LENGTH], szData[1] = SQL_TYPE_IGNORE;
formatex(szQuery, charsmax(szQuery), "CREATE TABLE IF NOT EXISTS `%s` (\
`nick` TEXT NOT NULL,\
`steamid` TEXT NOT NULL,\
`kills` INTEGER NOT NULL DEFAULT 0,\
`deaths` INTEGER NOT NULL DEFAULT 0,\
PRIMARY KEY (steamid))", table_name);
SQL_ThreadQuery(sql, "Sql_Handler", szQuery, szData, sizeof szData);
}
public client_putinserver(id) {
if(is_user_bot(id) || is_user_hltv(id)) return;
get_user_authid(id, g_szSteamID[id], charsmax(g_szSteamID));
LoadData(id);
}
public on_player_killed_post(iVictim, iAttacker) {
if(iVictim != iAttacker) {
g_iKills[iAttacker]++;
g_iDeaths[iVictim]++;
UpdateData(iVictim);
}
}
public LoadData(id) {
new szQuery[MAX_FMT_LENGTH], szData[2];
szData[0] = SQL_TYPE_LOAD;
szData[1] = id;
formatex(szQuery, charsmax(szQuery), "SELECT * FROM `%s` WHERE `steamid` = '%s'", table_name, g_szSteamID[id]);
SQL_ThreadQuery(sql, "Sql_Handler", szQuery, szData, sizeof szData);
}
public UpdateData(id) {
new szQuery[MAX_FMT_LENGTH], szData[1] = SQL_TYPE_IGNORE;
formatex(szQuery, charsmax(szQuery),"\
UPDATE `%s` SET\
`kills` = '%d',\
`deaths` = '%d',\
WHERE `steamid` = '%s'\
", table_name, g_iKills[id], g_iDeaths[id], g_szSteamID[id]);
SQL_ThreadQuery(sql, "Sql_Handler", szQuery, szData, sizeof szData);
}
public Sql_Handler(iFailState, Handle:Query, szError[], iErrNum, szData[], size, Float:flQueryTime) {
switch(iFailState) {
case TQUERY_CONNECT_FAILED: {
log_to_file("fail_sql.txt" , "^nConnection failed^nError: #%d^n%s^n", iErrNum, szError);
return;
}
case TQUERY_QUERY_FAILED: {
log_to_file("fail_sql.txt" , "^nQuery failed^nError: #%d^n%s^n", iErrNum, szError);
new szLastQuery[96];
SQL_GetQueryString(sql, szLastQuery, charsmax(szLastQuery));
log_to_file("fail_sql.txt" , "Last Query: %s^n", szLastQuery);
return;
}
}
new iDataType = szData[0];
switch(iDataType) {
case SQL_TYPE_LOAD: {
new id = szData[1];
if(!is_user_connected(id)) return;
if(!SQL_NumResults(sql)) {
new szName[32]; get_user_info(id, "name", szName, charsmax(szName));
mysql_escape_string(szName, charsmax(szName));
new szQuery[MAX_FMT_LENGTH], szData[1] = SQL_TYPE_IGNORE;
formatex(szQuery, charsmax(szQuery), "INSERT INTO `%s` (`nick`, `steamid`, `kills`, `deaths`)\
VALUES ('%s', '%s', '%d', '%d')\
", table_name, szName, g_szSteamID[id], g_iKills[id], g_iDeaths[id]);
SQL_ThreadQuery(sql, "Sql_Handler", szQuery, szData, sizeof szData);
} else {
g_iKills[id] = SQL_ReadResult(sql, SQL_FieldNameToNum(sql, "kills"));
g_iDeaths[id] = SQL_ReadResult(sql, SQL_FieldNameToNum(sql, "deaths"));
}
log_to_file("db_sql.txt" , "^nKills: %d^nDeaths: %d^n", g_iKills[id], g_iDeaths[id]);
}
}
}
mysql_escape_string(dest[],len) {
replace_all(dest,len,"\\","\\\\");
replace_all(dest,len,"\0","\\0");
replace_all(dest,len,"\n","\\n");
replace_all(dest,len,"\r","\\r");
replace_all(dest,len,"\x1a","\Z");
replace_all(dest,len,"'","''");
replace_all(dest,len,"^"","^"^"");
}