#include <amxmodx>
#include <sqlx>
new const TASKID = 11575757;
new bool:g_isLoad[33];
native zp_get_user_ammo_packs(id);
native zp_set_user_ammo_packs(id, amount);
enum _:ques { INSTALL_PLUGIN, LOAD_AMMO, SAVE_AMMO, PRUNED };
new Handle:g_sql_tuple, szQuery[1024];
public plugin_init()
set_task(1.0, "PluginCfg");
public PluginCfg()
{
new szHost[64], szUser[32], szPasswd[32], szDb[32];
get_cvar_string("amx_sql_host", szHost, charsmax(szHost));
get_cvar_string("amx_sql_user", szUser, charsmax(szUser));
get_cvar_string("amx_sql_pass", szPasswd, charsmax(szPasswd));
get_cvar_string("amx_sql_db", szDb, charsmax(szDb));
g_sql_tuple = SQL_MakeDbTuple(szHost, szUser, szPasswd, szDb);
new data[3];
data[0] = INSTALL_PLUGIN;
formatex(szQuery, charsmax(szQuery), "CREATE TABLE IF NOT EXISTS `zp_ammo_manager` (`id` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(33) NOT NULL, `Ammo` int(10) NOT NULL, `SteamID` varchar(25) NOT NULL UNIQUE, `LastConnect` int(11) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
SQL_ThreadQuery(g_sql_tuple, "SQL_Handler", szQuery, data, sizeof(data));
SQL_SetCharset(g_sql_tuple, "utf8");
data[0] = PRUNED;
formatex(szQuery, charsmax(szQuery), "DELETE FROM `zp_ammo_manager` WHERE `LastConnect` < (UNIX_TIMESTAMP(NOW()) - %d)", (30 * 86400)); // delete players after 30 days
SQL_ThreadQuery(g_sql_tuple, "SQL_Handler", szQuery, data, sizeof(data));
}
public client_connect(id)
g_isLoad[id] = false;
public client_putinserver(id)
{
if(is_user_bot(id) || is_user_hltv(id)) return;
zp_set_user_ammo_packs(id, 0);
set_task(3.0, "LoadAmmo", id+TASKID);
}
public LoadAmmo(taskid)
{
new id = taskid - TASKID;
if(!is_user_connected(id) || g_isLoad[id]) return;
new data[3];
data[0] = LOAD_AMMO;
data[1] = id;
data[2] = get_user_userid(id);
new szName[32]; get_user_name(id, szName, charsmax(szName));
new szAuthid[25]; get_user_authid(id, szAuthid, charsmax(szAuthid));
log_amx("[Ammo Manager] Запрос на загрузку аммо для: %s | %s", szName, szAuthid);
formatex(szQuery, charsmax(szQuery), "SELECT * FROM `zp_ammo_manager` WHERE `SteamID` = '%s'", szAuthid);
SQL_ThreadQuery(g_sql_tuple, "SQL_Handler", szQuery, data, sizeof(data));
}
public client_disconnected(id)
{
if(is_user_bot(id) || is_user_hltv(id) || !g_isLoad[id]) return;
remove_task(id+TASKID);
new ammo = zp_get_user_ammo_packs(id);
new szName[32]; get_user_name(id, szName, charsmax(szName));
new szAuthid[25]; get_user_authid(id, szAuthid, charsmax(szAuthid));
new szNewName[64];
SQL_QuoteString(Empty_Handle, szNewName, charsmax(szNewName), szName);
formatex(szQuery, charsmax(szQuery), "INSERT INTO `zp_ammo_manager` (`Name`, `Ammo`, `SteamID`, `LastConnect`) \
VALUES ('%s', '%d', '%s', UNIX_TIMESTAMP(NOW())) \
ON DUPLICATE KEY UPDATE `Name` = '%s', `Ammo` = '%d', `LastConnect` = UNIX_TIMESTAMP(NOW())",
szNewName, ammo, szAuthid, szNewName, ammo);
SQL_ThreadQuery(g_sql_tuple, "SQL_Handler", szQuery);
g_isLoad[id] = false;
}
public SQL_Handler(failstate, Handle:query, err[], errcode, data[], datasize)
{
if(failstate == TQUERY_SUCCESS)
{
switch(data[0])
{
case LOAD_AMMO:
{
new id = data[1];
if(!is_user_connected(id) || g_isLoad[id]) return 0;
if(data[2] != get_user_userid(id))
{
log_amx("[Ammo Manager] Загрузка другому игроку");
return 0;
}
new szName[32]; get_user_name(id, szName, charsmax(szName));
if(SQL_NumResults(query) > 0)
{
zp_set_user_ammo_packs(id, SQL_ReadResult(query, 3));
log_amx("[Ammo Manager] Загружено %d аммо для игрока %s", SQL_ReadResult(query, 3), szName);
}
else // first connect
{
zp_set_user_ammo_packs(id, 50); // start bonus
log_amx("[Ammo Manager] Игрок %s получил стартовый бонус", szName);
}
g_isLoad[id] = true;
}
case PRUNED:
{
new pruned = SQL_AffectedRows(query);
if(pruned) log_amx("[Pruned] Removed %d players!", pruned);
}
case INSTALL_PLUGIN, SAVE_AMMO: { }
}
}
else log_amx("[State #%d] Error [#%d] %s", data[0], errcode, err);
return PLUGIN_CONTINUE;
}
public plugin_end()
SQL_FreeHandle(g_sql_tuple);