Всем привет. Подскажите пожалуйста, как лучше всего организовать файл конфигурации для этого плагина, в котором хранить данные для подключения к базе данных.
Код:
#include <amxmodx>
#include <reapi>
#include <sqlx>
enum color_e { R, G, B }
new const FADE_COLOR_BLUE[3] = { 0, 127, 255 };
#define MAXPLAYERS 32
new bool:g_bFadeEnabled[MAXPLAYERS + 1];
new Handle:g_dbTuple;
new Handle:g_dbConnection;
new DB_HOST[64] = "HOST";
new DB_USER[64] = "DBUSER";
new DB_PASS[64] = "DBPASS";
new DB_NAME[64] = "DBNAME";
new DB_TABLE[64] = "DBTABLE";
public plugin_init()
{
register_plugin("Killer ScreenFade", "1.0", "old.");
RegisterHookChain(RG_CSGameRules_PlayerKilled, "CSGameRules_PlayerKilled", .post = true);
register_clcmd("say /fade", "cmd_fade");
for (new i = 1; i <= MAXPLAYERS; i++)
{
g_bFadeEnabled[i] = true;
}
g_dbTuple = SQL_MakeDbTuple(DB_HOST, DB_USER, DB_PASS, DB_NAME, 0);
if (g_dbTuple == Empty_Handle)
{
return;
}
new errcode;
new error[256];
g_dbConnection = SQL_Connect(g_dbTuple, errcode, error, sizeof(error));
if (g_dbConnection == Empty_Handle)
{
return;
}
}
public client_putinserver(id)
{
new steamid[32];
get_user_authid(id, steamid, sizeof(steamid));
new query[256];
format(query, sizeof(query), "SELECT fade FROM %s WHERE authid = '%s'", DB_TABLE, steamid);
new Handle:preparedQuery = SQL_PrepareQuery(g_dbConnection, query);
if (preparedQuery == Empty_Handle)
{
return;
}
if (SQL_Execute(preparedQuery))
{
if (SQL_NumResults(preparedQuery) > 0)
{
new fadeValue = SQL_ReadResult(preparedQuery, 0);
g_bFadeEnabled[id] = fadeValue == 1;
}
else
{
g_bFadeEnabled[id] = false;
}
}
SQL_FreeHandle(preparedQuery);
}
public cmd_fade(id)
{
new steamid[32];
get_user_authid(id, steamid, sizeof(steamid));
new query[256];
format(query, sizeof(query), "SELECT fade FROM %s WHERE authid = '%s'", DB_TABLE, steamid);
new Handle:preparedQuery = SQL_PrepareQuery(g_dbConnection, query);
if (preparedQuery == Empty_Handle)
{
return;
}
if (SQL_Execute(preparedQuery))
{
if (SQL_NumResults(preparedQuery) > 0)
{
new fadeValue = SQL_ReadResult(preparedQuery, 0);
g_bFadeEnabled[id] = fadeValue == 1;
}
else
{
g_bFadeEnabled[id] = false;
}
g_bFadeEnabled[id] = !g_bFadeEnabled[id];
format(query, sizeof(query), "UPDATE %s SET fade = %d WHERE authid = '%s'", DB_TABLE, g_bFadeEnabled[id] ? 1 : 0, steamid);
new Handle:preparedUpdateQuery = SQL_PrepareQuery(g_dbConnection, query);
if (preparedUpdateQuery != Empty_Handle)
{
if (!SQL_Execute(preparedUpdateQuery))
{
new error[256];
SQL_QueryError(preparedUpdateQuery, error, sizeof(error));
}
SQL_FreeHandle(preparedUpdateQuery);
}
}
SQL_FreeHandle(preparedQuery);
}
public CSGameRules_PlayerKilled(const pPlayer, const pevKiller, const pevInflictor)
{
if (pPlayer == pevKiller || pevKiller != pevInflictor || !is_user_alive(pevKiller))
return HC_CONTINUE;
if (IsBlind(pevKiller))
return HC_CONTINUE;
if (g_bFadeEnabled[pevKiller])
{
__UTIL_ScreenFade(pevKiller, FADE_COLOR_BLUE, 30, 0.5, 0.8);
}
return HC_CONTINUE;
}
stock __UTIL_ScreenFade(const pPlayer, const iColor[3], iAlpha, Float:flFxTime = 1.0, Float:flHoldTime = 1.0)
{
const FFADE_IN = 0x0000;
static iMsgIdScreenFade;
if (iMsgIdScreenFade > 0 || (iMsgIdScreenFade = get_user_msgid("ScreenFade")))
{
message_begin(MSG_ONE_UNRELIABLE, iMsgIdScreenFade, .player = pPlayer);
write_short(FixedUnsigned16(flFxTime));
write_short(FixedUnsigned16(flHoldTime));
write_short(FFADE_IN);
write_byte(iColor[R]);
write_byte(iColor[G]);
write_byte(iColor[B]);
write_byte(iAlpha);
message_end();
}
}
stock FixedUnsigned16(Float:flValue, iScale = (1 << 12)) {
return clamp(floatround(flValue * iScale), 0, 0xFFFF);
}
stock bool:IsBlind(const pPlayer) {
return bool:(Float:get_member(pPlayer, m_blindStartTime) + Float:get_member(pPlayer, m_blindFadeTime) >= get_gametime());
}
public plugin_end()
{
if (g_dbConnection != Empty_Handle)
{
SQL_FreeHandle(g_dbConnection);
}
if (g_dbTuple != Empty_Handle)
{
SQL_FreeHandle(g_dbTuple);
}
}