Участник
Пользователь
- Сообщения
- 368
- Реакции
- 106
- Помог
- 2 раз(а)
Vaqtincha, как я понимаю, это не совсем то, но тоже как вариант) https://github.com/alliedmodders/am...0d4d70e0a650074fd485d/amxmodx/format.cpp#L738
стоит добавить параметр without_portstock UTIL_GetUserIp(const iPlayer) { new szIp[MAX_IP_LENGTH]; get_user_ip(iPlayer, szIp, charsmax(szIp)); return szIp; }
stock parse_rgb(const value[], output[3]) {
for (new i, temp_color[8], pos; i < 3; i++) {
pos = argparse(value, pos, temp_color, charsmax(temp_color));
output[i] = str_to_num(temp_color);
}
}
stock parse_rgb_f(const value[], Float: output[3]) {
for (new i, temp_color[8], pos; i < 3; i++) {
pos = argparse(value, pos, temp_color, charsmax(temp_color));
output[i] = str_to_float(temp_color);
}
}
#if defined _sqlx_api_included
#endinput
#endif
#define _sqlx_api_included
// Подключение к базе данных
native bool:SQLx_Init(const host[], const user[], const pass[], const db[], timeout = 0);
native bool:SQLx_IsConnected();
native SQLx_Disconnect();
// Выполнение запросов
native bool:SQLx_Query(const query[], &result[], max_results = 128);
native bool:SQLx_QueryAsync(const query[], callback, data[] = "");
native bool:SQLx_GetAsyncResult(id, &result[], max_results = 128);
// Управление статистикой
native SQLx_GetTotalQueries();
native Float:SQLx_GetAverageQueryTime();
native SQLx_ResetStats();
// Логирование
native SQLx_LogInfo(const message[]);
native SQLx_LogError(const message[]);
// Конфигурация
native bool:SQLx_LoadConfig(const config_file[]);
native bool:SQLx_GetConfigValue(const key[], value[], max_len);
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
#include <reapi>
new Handle:g_sqlTuple;
new g_StatsInterval;
new bool:g_StatsEnabled;
new bool:g_dbEnabled;
new g_TotalQueries;
new Float:g_TotalTime;
new db_host[64];
new db_user[32];
new db_pass[32];
new db_name[32];
// Инициализация плагина
public plugin_init() {
register_plugin("SQLx Checkers", "2.0", "kreedzru.pro");
register_cvar("db_enabled", "true");
load_config();
if (g_StatsEnabled) {
set_task(float(g_StatsInterval), "SQLxCollectStats", 0, "", 0, "b");
}
}
load_config() {
new config_file[64];
get_configsdir(config_file, charsmax(config_file));
format(config_file, charsmax(config_file), "%s/sqlx_checkers.cfg", config_file);
if (!file_exists(config_file)) {
log_amx("[SQLx Errors] Config file not found: %s", config_file);
return;
}
new line[256], key[64], value[64];
new file = fopen(config_file, "rt");
while (fgets(file, line, charsmax(line))) {
trim(line);
if (!line[0] || line[0] == ';' || line[0] == '#') {
continue;
}
parse(line, key, charsmax(key), value, charsmax(value));
trim(key);
trim(value);
if (equali(key, "db_host")) {
copy(db_host, charsmax(db_host), value);
} else if (equali(key, "db_user")) {
copy(db_user, charsmax(db_user), value);
} else if (equali(key, "db_pass")) {
copy(db_pass, charsmax(db_pass), value);
} else if (equali(key, "db_name")) {
copy(db_name, charsmax(db_name), value);
} else if (equali(key, "stats_interval")) {
g_StatsInterval = str_to_num(value);
} else if (equali(key, "SQLxStatsOnOff")) {
g_StatsEnabled = equali(value, "true") || equali(value, "1");
} else if (equali(key, "db_enabled")) {
g_dbEnabled = equali(value, "true") || equali(value, "1");
}
}
fclose(file);
if (g_dbEnabled) {
connect_to_database(db_host, db_user, db_pass, db_name);
}
}
connect_to_database(const host[], const user[], const pass[], const db[], timeout = 0) {
if (!g_dbEnabled) {
return;
}
new start_time = get_systime();
g_sqlTuple = SQL_MakeDbTuple(host, user, pass, db, timeout);
if (!g_sqlTuple) {
log_amx("[SQLx Errors] Failed to create database tuple.");
return;
}
new error[256];
new errcode;
new Handle:connection = SQL_Connect(g_sqlTuple, errcode, error, charsmax(error));
if (connection) {
new end_time = get_systime();
log_amx("[SQLx Informers] Connected to the database. Connection time: %d ms", end_time - start_time);
SQL_FreeHandle(connection);
} else {
log_amx("[SQLx Errors] Failed to connect to the database. Code: %d, Message: %s", errcode, error);
}
}
public SQLxCollectStats() {
if (!g_StatsEnabled) {
remove_task(0);
return;
}
if (g_TotalQueries > 0) {
new avg_time = floatround(g_TotalTime / float(g_TotalQueries), floatround_floor);
log_amx("[SQLx Statistics] Total queries: %d, Average query time: %d ms", g_TotalQueries, avg_time);
} else {
log_amx("[SQLx Statistics] No queries executed during this interval.");
}
g_TotalQueries = 0;
g_TotalTime = 0.0;
}
// no release coming soon
public cmdResetQueries(id) {
client_print_color(id, print_chat, "!g[SQLx Checker] !yQuery counter reset command is disabled.");
}
db_host "localhost"
db_user "user"
db_pass "pass"
db_name "namebd"
check_interval 60 // Интервал проверки скорости подключения (в секундах)
query_limit 100 // Лимит запросов для отслеживания (за интервал)
logging_enable true // Включение/выключение логирования (true/false)
stats_interval 150 // Интервал сбора статистики (в секундах)
SQLxStatsOnOff true // Включение/выключение сбора статистики (true/false)
db_enabled true // Включение/Выключение использования бд (true/false)
#include <amxmodx>
#include <sqlx_api>
public plugin_init() {
register_plugin("Integration API SQLx Checker", "1.0", "kreedzru & midnightfury");
if (!SQLx_Init("localhost", "user", "password", "database")) {
SQLx_LogError("Failed to connect to the database.");
return;
}
new result[128];
if (SQLx_Query("SELECT * FROM users", result)) {
SQLx_LogInfo("Query executed successfully.");
} else {
SQLx_LogError("Query failed.");
}
SQLx_QueryAsync("INSERT INTO logs (message) VALUES ('Test')", "OnQueryComplete");
}
public OnQueryComplete(id, success) {
if (success) {
SQLx_LogInfo("Async query completed successfully.");
} else {
SQLx_LogError("Async query failed.");
}
}