#include <amxmodx>
#include <sqlx>
#include <json>
#pragma dynamic 524288
public stock const PluginName[] = "Save Data Registration Player preferences";
public stock const PluginVersion[] = "1.0.9";
public stock const PluginAuthor[] = "GM-X Team, cpctrl, Limbooc";
public stock const PluginURL[] = "";
const MAX_KEY_LENGTH = 32;
const MAX_VALUE_STRING_LENGTH = 32;
const MAX_DATA_BUFFER = 102400; // 100KB
const QUERY_LENGTH = 102400; //1472; // размер переменной sql запроса
new g_iWarnPass[MAX_PLAYERS + 1];
new bool: DEBUG;
new bool:g_iUserConnected[MAX_PLAYERS + 1];
new Handle:g_hDBHandle, Handle:g_ConnectHandle;
new Trie: g_tPlayerPreferences[MAX_PLAYERS + 1];
new JSON: g_jObject[MAX_PLAYERS + 1];
static Trie:g_RegsStats;
#define CHECK_NATIVE_ARGS_NUM(%1,%2,%3) \
if (%1 < %2) { \
DEBUG && log_error(AMX_ERR_NATIVE, "Invalid num of arguments %d. Expected %d", %1, %2); \
return %3; \
}
#define FormatMain(%0) (iLen = formatex(szMenu, charsmax(szMenu), %0))
#define FormatItem(%0) (iLen += formatex(szMenu[iLen], charsmax(szMenu) - iLen, %0))
enum fwdStruct {
Fwd_Loaded,
Fwd_KeyChanged,
Fwd_LogOut
};
enum sqlx_e {
dbtable[32],
dbsecondtable[32],
dbhost[32],
dbuser[64],
dbpass[64],
db[32],
dbtype,
warnlimit,
chatprefix[32],
method_save,
salt[32]
};
enum _:edate
{
bool:Active,
Player_Id,
Login[32],
Password[32],
Email[64],
Clcmd_Type,
Clcmd_MenuType,
AutoLogin,
RegDate[32],
LastSign[32],
rAuth[35],
TempLogin[32]
}
enum _:db_type
{
NONGAMECMS = 0,
GAMECMS
}
enum _:TableInf
{
Player_Login[32],
bool:Player_Active
}
enum _:sql_que_type
{
SQL_IGNORE = 0,
SQL_REGS_PLAYERS,
SQL_JOIN_PLAYERS,
SQL_PRE_CONNECT,
SQL_JSON_CONNECT,
SQL_POST_CONNECT,
SQL_DISCONNECY_PLAYER,
SQL_AUTOLOGIN
}
enum _:EXT_DATA_STRUCT
{
EXT_DATA__SQL = 0,
EXT_DATA__INDEX,
EXT_DATA__REGS_ID,
EXT_DATA__USERID
}
new g_iUserDate[MAX_PLAYERS + 1][edate], dbdata[sqlx_e], Salt[32], g_eForwards[fwdStruct];
public plugin_init()
{
register_plugin(PluginName, PluginVersion, PluginAuthor);
register_menucmd(register_menuid("Show_MainMenu"), 1023, "Handle_MainMenu");
register_menucmd(register_menuid("Show_RegMenu"), 1023, "Handle_RegMenu");
register_menucmd(register_menuid("Show_SignMenu"), 1023, "Handle_SignMenu");
g_eForwards[Fwd_Loaded] = CreateMultiForward("player_loaded", ET_IGNORE, FP_CELL);
g_eForwards[Fwd_LogOut] = CreateMultiForward("player_logout", ET_IGNORE, FP_CELL);
g_eForwards[Fwd_KeyChanged] = CreateMultiForward("player_key_changed", ET_IGNORE, FP_CELL, FP_STRING);
register_logevent("LogEventHooK_RoundEnd", 2, "1=Round_End")
register_clcmd("say /regmenu", "regmenu");
register_clcmd("SingReg" , "clcmd_SingReg");
DEBUG = bool: (plugin_flags() & AMX_FLAG_DEBUG);
g_RegsStats = TrieCreate();
read_json();
}
public read_json()
{
new filePath[PLATFORM_MAX_PATH];
get_localinfo("amxx_configsdir", filePath, PLATFORM_MAX_PATH - 1);
add(filePath, PLATFORM_MAX_PATH - 1, "/preferences.json");
if (!file_exists(filePath)) {
set_fail_state("Configuration file '%s' not found", filePath);
return;
}
new JSON: config = json_parse(filePath, true);
if (config == Invalid_JSON) {
set_fail_state("Configuration file '%s' read error", filePath);
return;
}
json_object_get_string(config, "sql_table", dbdata[dbtable], charsmax(dbdata[dbtable]));
json_object_get_string(config, "sql_second_table", dbdata[dbsecondtable], charsmax(dbdata[dbsecondtable]));
json_object_get_string(config, "sql_host", dbdata[dbhost], charsmax(dbdata[dbhost]));
json_object_get_string(config, "sql_user", dbdata[dbuser], charsmax(dbdata[dbuser]));
json_object_get_string(config, "sql_password", dbdata[dbpass], charsmax(dbdata[dbpass]));
json_object_get_string(config, "sql_db", dbdata[db], charsmax(dbdata[db]));
json_object_get_string(config, "chat_prefix", dbdata[chatprefix], charsmax(dbdata[chatprefix]));
json_object_get_string(config, "sql_salt", dbdata[salt], charsmax(dbdata[salt]));
dbdata[method_save] = json_object_get_number(config, "method_save"); // 1 - Disconnected | 2 - Round End
dbdata[dbtype] = json_object_get_number(config, "sql_typeapi");
dbdata[warnlimit] = json_object_get_number(config, "sql_warnlimit");
DEBUG && log_amx("[PP] JSON: %d", dbdata[dbtype]);
json_free(config);
server_print("Preferences config has been loaded %s", dbdata[dbsecondtable]);
if(dbdata[dbtype] == NONGAMECMS)
{
sql_test_init();
}
}
public sql_test_init() {
g_hDBHandle = SQL_MakeDbTuple(dbdata[dbhost],dbdata[dbuser],dbdata[dbpass],dbdata[db], 1);
new errCode, error[512];
g_ConnectHandle = SQL_Connect(g_hDBHandle, errCode, error, charsmax(error));
if (g_ConnectHandle == Empty_Handle) {
SQL_FreeHandle(g_hDBHandle);
g_hDBHandle = Empty_Handle;
DEBUG && log_amx("[PP] Error connecting to db '%s': #%d: %s", dbdata[db], errCode, error);
return;
}
copy(Salt, charsmax(Salt), dbdata[salt]);
server_print("[PP] Connection to '%s' database success Mode: Without GameCMS", dbdata[db]);
}
public plugin_natives() {
register_native("pp_has_key", "native_has_key");
register_native("pp_get_number", "native_get_number");
register_native("pp_get_float", "native_get_float");
register_native("pp_get_bool", "native_get_bool");
register_native("pp_get_string", "native_get_string");
register_native("pp_set_number", "native_set_number");
register_native("pp_set_float", "native_set_float");
register_native("pp_set_bool", "native_set_bool");
register_native("pp_set_string", "native_set_string");
register_native("pp_is_login_registered", "native_is_login_registered");
register_native("pp_is_player_joined", "native_is_player_joined");
register_native("pp_is_login_already_joined", "native_is_login_already_joined");
register_native("pp_set_user_date", "native_set_user_date");
register_native("pp_get_user_login", "native_get_user_login");
}
public bool: native_is_login_registered(plugin, argc) {
enum {
arg_string = 1
};
CHECK_NATIVE_ARGS_NUM(argc, 1, false)
new iValue = 0;
new login[MAX_KEY_LENGTH];
get_string(arg_string, login, MAX_KEY_LENGTH - 1);
new Handle:hGetSrvId;
if(dbdata[dbtype] == GAMECMS)
{
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `login` FROM `users` WHERE `login` = '%s'", login);
}
else
{
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `login` FROM `%s` WHERE `login` = '%s'", dbdata[dbtable], login);
}
if(!SQL_Execute(hGetSrvId) || SQL_NumResults(hGetSrvId))
{
SQL_FreeHandle(hGetSrvId);
iValue = 1;
}
SQL_FreeHandle(hGetSrvId);
return bool:iValue;
}
public LogEventHooK_RoundEnd()
{
if(dbdata[method_save] == 2)
{
new players[32],pnum
get_players(players,pnum)
for(new i ; i < pnum ; i++)
{
SavePlayerStats(players[i])
}
}
}
public bool: native_is_player_joined(plugin, argc) {
enum {
arg_player = 1
};
CHECK_NATIVE_ARGS_NUM(argc, 1, false)
new id = get_param(arg_player);
return bool:g_iUserDate[id][Active];
}
public bool: native_is_login_already_joined(plugin, argc) {
enum {
arg_string = 1
};
CHECK_NATIVE_ARGS_NUM(argc, 1, false)
new login[MAX_KEY_LENGTH];
get_string(arg_string, login, MAX_KEY_LENGTH - 1);
new iValue = 0;
if(TrieKeyExists(g_RegsStats, login))
{
iValue = 1;
}
return bool:iValue;
}
public native_set_user_date(plugin, argc) {
enum {
arg_player = 1,
arg_login,
arg_password,
arg_email
};
new login[MAX_KEY_LENGTH],password[MAX_KEY_LENGTH],email[MAX_KEY_LENGTH];
new pId = get_param(arg_player);
get_string(arg_login, login, MAX_KEY_LENGTH - 1);
get_string(arg_password, password, MAX_KEY_LENGTH - 1);
get_string(arg_email, email, MAX_KEY_LENGTH - 1);
new booleanreturn = 1;
if(!strlen(login) || !strlen(password) || !strlen(email) || !str_contains(email, "@", false, 2 ) || !is_user_connected(pId) || g_iUserDate[pId][Active])
{
booleanreturn = 0;
return booleanreturn;
}
CHECK_NATIVE_ARGS_NUM(argc, 3, false)
new iValue = 0
new Handle:hGetSrvId;
if(dbdata[dbtype] == GAMECMS)
{
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `login` FROM `users` WHERE `login` = '%s'", login);
}
else
{
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `login` FROM `%s` WHERE `login` = '%s'", dbdata[dbtable], login);
}
if(!SQL_Execute(hGetSrvId) || SQL_NumResults(hGetSrvId))
{
iValue = 1;
}
SQL_FreeHandle(hGetSrvId);
if(iValue || g_iUserDate[pId][Active])
{
booleanreturn = 0;
return booleanreturn;
}
new query[QUERY_LENGTH];
new szName[32], szAuth[32], pass[64];
get_user_name(pId, szName, charsmax(szName));
get_user_authid(pId, szAuth, charsmax(szAuth));
password_convert(password, MAX_KEY_LENGTH - 1, pass, charsmax(pass));
if(dbdata[dbtype] == GAMECMS)
{
formatex(query,charsmax(query), "INSERT INTO `users` (`login`, `password`, `email`, `regdate`, `avatar`, `rights`, `nick`, `birth`, `last_activity`, `steam_id`, `signature`,`shilings`) \
VALUES ('%s', '%s', '%s', NOW(), 'files/avatars/no_avatars.jpg', '2', '%s', '1960-01-01', NOW(), '%s', '', 0)", login, pass, email, szName, szAuth);
}
else
{
formatex(query,charsmax(query), "INSERT INTO `%s` (`login`, `password`, `regdate`, `steam_id`) VALUES ('%s', '%s', NOW(), '%s')", dbdata[dbtable], login, pass, szAuth);
}
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_REGS_PLAYERS;
sData[EXT_DATA__INDEX] = pId;
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
return booleanreturn;
}
public native_get_user_login(plugin, argc) {
enum {
arg_player = 1,
arg_login,
arg_length
};
new id = get_param(arg_player);
if(!g_iUserDate[id][Active])
return 0;
set_string(arg_login, g_iUserDate[id][Login], get_param(arg_length));
return g_iUserDate[id][Player_Id]
}
public bool: native_has_key(plugin, argc) {
enum {
arg_player = 1,
arg_key
};
CHECK_NATIVE_ARGS_NUM(argc, 2, false)
new id = get_param(arg_player);
if(!g_iUserDate[id][Active])
return false;
new key[MAX_KEY_LENGTH];
get_string(arg_key, key, MAX_KEY_LENGTH - 1);
return TrieKeyExists(g_tPlayerPreferences[id], key);
}
public native_get_number(plugin, argc) {
enum {
arg_player = 1,
arg_key,
arg_default
};
CHECK_NATIVE_ARGS_NUM(argc, 2, 0)
new id = get_param(arg_player);
if(!g_iUserDate[id][Active])
return 0;
new key[MAX_KEY_LENGTH];
get_string(arg_key, key, MAX_KEY_LENGTH - 1);
if (!TrieKeyExists(g_tPlayerPreferences[id], key)) {
return argc >= arg_default ? get_param(arg_default) : 0;
}
new value;
TrieGetCell(g_tPlayerPreferences[id], key, value);
return value;
}
public Float: native_get_float(plugin, argc) {
enum {
arg_player = 1,
arg_key,
arg_default
};
CHECK_NATIVE_ARGS_NUM(argc, 2, 0.0)
new id = get_param(arg_player);
if(!g_iUserDate[id][Active])
return 0.0;
new key[MAX_KEY_LENGTH];
get_string(arg_key, key, MAX_KEY_LENGTH - 1);
if (!TrieKeyExists(g_tPlayerPreferences[id], key)) {
return argc >= arg_default ? get_param_f(arg_default) : 0.0;
}
new value;
TrieGetCell(g_tPlayerPreferences[id], key, value);
return float(value);
}
public bool: native_get_bool(plugin, argc) {
enum {
arg_player = 1,
arg_key,
arg_default
};
CHECK_NATIVE_ARGS_NUM(argc, 2, false)
new id = get_param(arg_player);
if(!g_iUserDate[id][Active])
return false;
new key[MAX_KEY_LENGTH];
get_string(arg_key, key, MAX_KEY_LENGTH - 1);
if (!TrieKeyExists(g_tPlayerPreferences[id], key)) {
return bool: (argc >= arg_default ? get_param(arg_default) : 0);
}
new value;
TrieGetCell(g_tPlayerPreferences[id], key, value);
return bool: value;
}
public native_get_string(plugin, argc) {
enum {
arg_player = 1,
arg_key,
arg_dest,
arg_length,
arg_default
};
CHECK_NATIVE_ARGS_NUM(argc, 2, 0)
new id = get_param(arg_player);
if(!g_iUserDate[id][Active])
return 0;
new key[MAX_KEY_LENGTH], value[MAX_VALUE_STRING_LENGTH];
get_string(arg_key, key, MAX_KEY_LENGTH - 1);
if (TrieKeyExists(g_tPlayerPreferences[id], key)) {
TrieGetString(g_tPlayerPreferences[id], key, value, MAX_VALUE_STRING_LENGTH - 1);
}
else if (argc >= arg_default) {
get_string(arg_default, value, MAX_VALUE_STRING_LENGTH - 1);
}
return set_string(arg_dest, value, get_param(arg_length));
}
public native_set_number(plugin, argc) {
enum {
arg_player = 1,
arg_key,
arg_value
};
CHECK_NATIVE_ARGS_NUM(argc, 3, 0)
new id = get_param(arg_player);
if(!g_iUserDate[id][Active])
{
return 0;
}
new key[MAX_KEY_LENGTH];
get_string(arg_key, key, charsmax(key));
new value = get_param(arg_value);
TrieSetCell(g_tPlayerPreferences[id], key, value);
return setValue(id, key, json_init_number(value));
}
public native_set_bool(plugin, argc) {
enum {
arg_player = 1,
arg_key,
arg_value
};
CHECK_NATIVE_ARGS_NUM(argc, 3, 0)
new id = get_param(arg_player);
if(!g_iUserDate[id][Active])
return 0;
new key[MAX_KEY_LENGTH];
get_string(arg_key, key, charsmax(key));
new bool: value = bool: get_param(arg_value);
TrieSetCell(g_tPlayerPreferences[id], key, value ? 1 : 0);
return setValue(id, key, json_init_bool(value));
}
public native_set_float(plugin, argc) {
enum {
arg_player = 1,
arg_key,
arg_value
};
CHECK_NATIVE_ARGS_NUM(argc, 3, 0)
new id = get_param(arg_player);
if(!g_iUserDate[id][Active])
return 0;
new key[MAX_KEY_LENGTH];
get_string(arg_key, key, charsmax(key));
new Float: value = get_param_f(arg_value);
TrieSetCell(g_tPlayerPreferences[id], key, value);
return setValue(id, key, json_init_number(cell: value));
}
public native_set_string(plugin, argc) {
enum {
arg_player = 1,
arg_key,
arg_value
};
CHECK_NATIVE_ARGS_NUM(argc, 3, 0)
new id = get_param(arg_player);
if(!g_iUserDate[id][Active])
return 0
new key[MAX_KEY_LENGTH], value[MAX_VALUE_STRING_LENGTH];
get_string(arg_key, key, charsmax(key));
get_string(arg_value, value, charsmax(value));
TrieSetString(g_tPlayerPreferences[id], key, value);
return setValue(id, key, json_init_string(value));
}
stock setValue(const id, const key[], JSON: value) {
new fwReturn;
ExecuteForward(g_eForwards[Fwd_KeyChanged], fwReturn, id, key);
if (g_hDBHandle == Empty_Handle || fwReturn == PLUGIN_HANDLED) {
return -1;
}
json_object_set_value(g_jObject[id], key, value);
json_free(value);
return 1;
}
public ChallengeClear(id) {
json_free(g_jObject[id]);
TrieDestroy(g_tPlayerPreferences[id]);
}
public client_putinserver(pId)
{
if(g_hDBHandle == Empty_Handle)
return;
new query[QUERY_LENGTH], szAuth[35];
get_user_authid(pId, szAuth, charsmax(szAuth));
if(dbdata[dbtype] == GAMECMS)
{
formatex(query, charsmax(query), "SELECT * FROM `users` WHERE steam_id = '%s' LIMIT 1", szAuth);
}
else
{
formatex(query, charsmax(query), "SELECT * FROM `%s` WHERE `autologin` = '1' AND steam_id = '%s' LIMIT 1", dbdata[dbtable], szAuth);
}
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_AUTOLOGIN;
sData[EXT_DATA__INDEX] = pId;
sData[EXT_DATA__USERID] = get_user_userid(pId);
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
}
public client_disconnected(pId)
{
if(g_iUserDate[pId][Active] && dbdata[method_save] == 1)
logout_user(pId);
//sql_handle(pId, SQL_DISCONNECY_PLAYER);
if( dbdata[method_save] == 2)
{
if(TrieKeyExists(g_RegsStats,g_iUserDate[pId][Login]))
TrieDeleteKey (g_RegsStats, g_iUserDate[pId][Login]);
g_iUserDate[pId][Active] = false;
ChallengeClear(pId);
g_iUserConnected[pId] = false;
}
g_iWarnPass[pId] = 0;
}
/*
SQL_IGNORE = 0,
SQL_REGS_PLAYERS,
SQL_JOIN_PLAYERS,
SQL_PRE_CONNECT,
SQL_JSON_CONNECT,
SQL_POST_CONNECT,
SQL_DISCONNECY_PLAYER,
SQL_AUTOLOGIN
*/
stock sql_handle(pId, const SQLType = 0)
{
switch(SQLType)
{
case SQL_DISCONNECY_PLAYER:
{
logout_user(pId);
}
case SQL_REGS_PLAYERS:
{
}
default:
{
}
}
}
public logout_user(pId)
{
if(g_hDBHandle == Empty_Handle)
return;
if(!g_iUserConnected[pId])
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 У вас обрабатывается запрос, ожидаете");
return;
}
if(TrieKeyExists(g_RegsStats,g_iUserDate[pId][Login]))
TrieDeleteKey (g_RegsStats, g_iUserDate[pId][Login]);
g_iUserDate[pId][Active] = false;
new query[QUERY_LENGTH],serial[MAX_DATA_BUFFER];
DEBUG && log_amx("logout_user | %d | %s", g_jObject[pId], serial);
json_serial_to_string(g_jObject[pId], serial, charsmax(serial));
formatex(query, charsmax(query), "UPDATE `%s` SET `text`='%s', `last_activity` = NOW() WHERE `user_id`= '%d' AND `api_type` = '%d'", dbdata[dbsecondtable], serial, g_iUserDate[pId][Player_Id] , dbdata[dbtype]);
DEBUG && log_amx(query);
new sData[EXT_DATA_STRUCT];
arrayset(g_iUserDate[pId], false, edate);
sData[EXT_DATA__SQL] = SQL_IGNORE;
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
ChallengeClear(pId);
g_iUserConnected[pId] = false;
ExecuteForward(g_eForwards[Fwd_LogOut], _, pId);
}
public SavePlayerStats(pId)
{
if(!g_iUserDate[pId][Active] || g_hDBHandle == Empty_Handle)
return;
new query[QUERY_LENGTH],serial[MAX_DATA_BUFFER], sData[EXT_DATA_STRUCT];
json_serial_to_string(g_jObject[pId], serial, charsmax(serial));
formatex(query, charsmax(query), "UPDATE `%s` SET `text`='%s', `last_activity` = NOW() WHERE `user_id`= '%d' AND `api_type` = '%d'", dbdata[dbsecondtable], serial, g_iUserDate[pId][Player_Id] , dbdata[dbtype]);
sData[EXT_DATA__SQL] = SQL_IGNORE;
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
}
public regmenu(pId)
{
return Show_MainMenu(pId);
}
public OnAPIPluginLoaded(Handle:SqlBans, version[], serverID)
{
if(dbdata[dbtype] == GAMECMS)
{
g_hDBHandle = SqlBans;
new error[32], errnum
g_ConnectHandle = SQL_Connect(g_hDBHandle, errnum, error, charsmax(error))
new Handle:hGetSrvId;
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `salt` FROM `config`");
if(!SQL_Execute(hGetSrvId))
{
new Error[128]
SQL_QueryError(hGetSrvId, Error, charsmax(Error))
DEBUG && log_amx("[PP] Error connecting to db '%s': #%d: %s", dbdata[db], errnum, error);
}
else
{
SQL_ReadResult(hGetSrvId, SQL_FieldNameToNum(hGetSrvId,"salt"), Salt, charsmax(Salt));
}
SQL_FreeHandle(hGetSrvId);
server_print("[PP] Connection to database success Mode: with GameCMS", dbdata[db]);
}
}
/*public plugin_end()
{
if(g_hDBHandle != Empty_Handle)
SQL_FreeHandle(g_hDBHandle);
if(g_ConnectHandle != Empty_Handle)
SQL_FreeHandle(g_ConnectHandle);
}*/
Show_MainMenu(pId)
{
new szMenu[512], iKeys = (1<<9), iLen;
if(!g_iUserDate[pId][Active])
{
FormatMain("\yМеню Личного кабинета^n^n");
FormatItem("\y1. \wВойти^n"), iKeys |= (1<<0);
if(!g_iUserDate[pId][Active])
FormatItem("\y2. \wЗарегистрироваться^n"), iKeys |= (1<<1);
else FormatItem("\y2. \dЗарегистрироваться^n");
}
else
{
FormatMain("\yМеню Личного кабинета^n\yВаш ID: \r%d^n\yДата регистрации: \r%s^n\yПоследний вход: \r%s^n^n", g_iUserDate[pId][Player_Id], g_iUserDate[pId][RegDate], g_iUserDate[pId][LastSign]);
if(dbdata[dbtype] == NONGAMECMS)
FormatItem("\y1. \wАвтологин \r[%s]^n", g_iUserDate[pId][AutoLogin] ? "Включен" : "Выключен"), iKeys |= (1<<0);
else
FormatItem("\y1. \dАвтологин \r[Не поддерживается]^n");
FormatItem("\y2. \wПерепривязать \rSteamID^n"), iKeys |= (1<<1);
FormatItem("\y3. \wОтвезать \rSteamID^n"), iKeys |= (1<<2);
FormatItem("^n^n^n\y5. \wВыйти с \rаккаунта^n"), iKeys |= (1<<4);
}
FormatItem("^n^n\y0. \wВыход^n"), iKeys |= (1<<9);
return show_menu(pId, iKeys, szMenu, -1, "Show_MainMenu");
}
public Handle_MainMenu(pId, iKey)
{
if(g_hDBHandle == Empty_Handle)
return PLUGIN_HANDLED;
if(!g_iUserDate[pId][Active])
{
switch(iKey)
{
case 0: return Show_SignMenu(pId);
case 1:
{
if(!g_iUserDate[pId][Active])
return Show_RegMenu(pId);
}
case 9: return PLUGIN_HANDLED;
}
}
else
{
switch(iKey)
{
case 0:
{
if(dbdata[dbtype] == NONGAMECMS)
{
g_iUserDate[pId][AutoLogin] = !g_iUserDate[pId][AutoLogin];
new query[QUERY_LENGTH];
formatex(query, charsmax(query), "UPDATE `%s` SET `autologin`='%d' WHERE `login`= '%s' AND `api_type` = '%d'", dbdata[dbtable], g_iUserDate[pId][AutoLogin], g_iUserDate[pId][Login], dbdata[dbtype]);
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_IGNORE;
sData[EXT_DATA__INDEX] = pId;
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
client_print_color(pId, print_team_default, "^x04[%s]^x01 Вы успешно %s автологин", dbdata[chatprefix], g_iUserDate[pId][AutoLogin] ? "включили" : "выключили");
}
}
case 1:
{
new szAuth[35];
get_user_authid(pId, szAuth, charsmax(szAuth));
if(equal(g_iUserDate[pId][rAuth], szAuth) )
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Вы уже привязаны к данному аккаунту", dbdata[chatprefix]);
return Show_MainMenu(pId);
}
if(strlen(g_iUserDate[pId][TempLogin]))
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Данный ^x04SteamID ^x01уже кем-то используется, логин: ^x04%s", dbdata[chatprefix], g_iUserDate[pId][TempLogin]);
return Show_MainMenu(pId);
}
new login[32];
if(is_auth_existing(pId, login, charsmax(login)))
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Данный ^x04SteamID ^x01уже кем-то используется, логин: ^x04%s", dbdata[chatprefix], login);
copy(g_iUserDate[pId][TempLogin] , charsmax(g_iUserDate[][TempLogin]), login);
return Show_MainMenu(pId);
}
new query[QUERY_LENGTH];
if(dbdata[dbtype] == NONGAMECMS)
formatex(query, charsmax(query), "UPDATE `%s` SET `steam_id`='%s' WHERE `login`= '%s'", dbdata[dbtable], szAuth, g_iUserDate[pId][Login]);
else formatex(query, charsmax(query), "UPDATE `users` SET `steam_id`='%s' WHERE `login`= '%s'", szAuth, g_iUserDate[pId][Login], dbdata[dbtype]);
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_IGNORE;
sData[EXT_DATA__INDEX] = pId;
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
client_print_color(pId, print_team_default, "^x04[%s]^x01 Вы успешно перепривязали ^x04SteamID", dbdata[chatprefix]);
get_user_authid(pId, g_iUserDate[pId][rAuth], charsmax(g_iUserDate[][rAuth]));
}
case 2:
{
if(!strlen(g_iUserDate[pId][rAuth]))
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 У вас не привязано ^x04SteamID, ^x01сначало ^x04перепривяжите", dbdata[chatprefix]);
return Show_MainMenu(pId);
}
new query[QUERY_LENGTH];
new szAuth[35];
get_user_authid(pId, szAuth, charsmax(szAuth));
if(dbdata[dbtype] == NONGAMECMS)
formatex(query, charsmax(query), "UPDATE `%s` SET `steam_id`= '' WHERE `login`= '%s'", dbdata[dbtable], g_iUserDate[pId][Login]);
else formatex(query, charsmax(query), "UPDATE `users` SET `steam_id`= '' WHERE `login`= '%s'", g_iUserDate[pId][Login], dbdata[dbtype]);
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_IGNORE;
sData[EXT_DATA__INDEX] = pId;
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
client_print_color(pId, print_team_default, "^x04[%s]^x01 Вы успешно отвезали ^x04SteamID", dbdata[chatprefix]);
formatex(g_iUserDate[pId][rAuth], charsmax(g_iUserDate[][rAuth]), "");
}
case 4:
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Вы успешно вышли с системы", dbdata[chatprefix]);
logout_user(pId);
}
case 9: return PLUGIN_HANDLED;
}
}
return Show_MainMenu(pId);
}
Show_SignMenu(pId)
{
new szMenu[512], iKeys = (1<<9), iLen;
FormatMain("\yМеню входа^n^n");
if(strlen(g_iUserDate[pId][Login]))
FormatItem("\y1. \wЛогин: \r%s^n", g_iUserDate[pId][Login]), iKeys |= (1<<0);
else FormatItem("\y1. \wЛогин: \rВвод^n"), iKeys |= (1<<0);
if(strlen(g_iUserDate[pId][Password]))
FormatItem("\y2. \wПароль: \r%s^n", g_iUserDate[pId][Password]), iKeys |= (1<<1);
else FormatItem("\y2. \wПароль: \rВвод^n"), iKeys |= (1<<1);
if(strlen(g_iUserDate[pId][Login]) && strlen(g_iUserDate[pId][Password]) && !g_iUserDate[pId][Active])
FormatItem("^n^n\y5. \rВойти^n"), iKeys |= (1<<4);
else FormatItem("^n^n\y5. \dВойти^n");
if(g_iWarnPass[pId])
{
FormatItem("\yВаши попытки: %d/%d^n", g_iWarnPass[pId],dbdata[warnlimit]);
}
FormatItem("^n^n^n^n\y0. \wВыход^n"), iKeys |= (1<<9);
return show_menu(pId, iKeys, szMenu, -1, "Show_SignMenu");
}
public Handle_SignMenu(pId, iKey)
{
if(g_hDBHandle == Empty_Handle)
return PLUGIN_HANDLED;
g_iUserDate[pId][Clcmd_Type] = iKey;
g_iUserDate[pId][Clcmd_MenuType] = 0;
switch(iKey)
{
case 0,1:
{
client_cmd(pId, "messagemode ^"SingReg^"")
}
case 4:
{
if(g_iUserDate[pId][Active])
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Вы уже вошли в систему", dbdata[chatprefix]);
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "");
return Show_SignMenu(pId);
}
if(is_login_connect(pId))
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Данным логином уже кто-то играет", dbdata[chatprefix]);
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "");
return Show_SignMenu(pId);
}
if(g_iWarnPass[pId] >= dbdata[warnlimit])
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Превышен лимит ввода логина", dbdata[chatprefix]);
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "");
return Show_SignMenu(pId);
}
new query[QUERY_LENGTH];
new szName[32], szAuth[32], pass[64];
get_user_name(pId, szName, charsmax(szName));
get_user_authid(pId, szAuth, charsmax(szAuth));
password_convert(g_iUserDate[pId][Password],charsmax(g_iUserDate[][Password]), pass, charsmax(pass));
if(dbdata[dbtype] == GAMECMS)
{
formatex(query,charsmax(query), "SELECT * FROM `users` WHERE `login` = '%s' AND `password` = '%s'", g_iUserDate[pId][Login], pass)
}
else
{
formatex(query,charsmax(query), "SELECT * FROM `%s` WHERE `login` = '%s' AND `password` = '%s'", dbdata[dbtable], g_iUserDate[pId][Login], pass)
}
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_JOIN_PLAYERS;
sData[EXT_DATA__INDEX] = pId;
return SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
}
case 9: return PLUGIN_HANDLED;
}
return Show_SignMenu(pId);
}
Show_RegMenu(pId)
{
new szMenu[512], iKeys = (1<<9), iLen;
FormatMain("\yМеню регистрации^n^n");
if(strlen(g_iUserDate[pId][Login]))
FormatItem("\y1. \wЛогин: \r%s^n", g_iUserDate[pId][Login]), iKeys |= (1<<0);
else FormatItem("\y1. \wЛогин: \rВвод^n"), iKeys |= (1<<0);
if(strlen(g_iUserDate[pId][Password]))
FormatItem("\y2. \wПароль: \r%s^n", g_iUserDate[pId][Password]), iKeys |= (1<<1);
else FormatItem("\y2. \wПароль: \rВвод^n"), iKeys |= (1<<1);
if(strlen(g_iUserDate[pId][Email]))
FormatItem("^n\y3. \wПочта: \r%s^n", g_iUserDate[pId][Email]), iKeys |= (1<<2);
else FormatItem("^n\y3. \wПочта: \rВвод^n"), iKeys |= (1<<2);
if(strlen(g_iUserDate[pId][Login]) && strlen(g_iUserDate[pId][Password]) && strlen(g_iUserDate[pId][Email]) && !g_iUserDate[pId][Active])
FormatItem("^n\y5. \rЗарегистрироваться^n"), iKeys |= (1<<4);
else FormatItem("^n\y5. \dЗарегистрироваться^n");
FormatItem("^n^n^n^n\y0. \wВыход^n"), iKeys |= (1<<9);
return show_menu(pId, iKeys, szMenu, -1, "Show_RegMenu");
}
public Handle_RegMenu(pId, iKey)
{
if(g_hDBHandle == Empty_Handle)
return PLUGIN_HANDLED;
g_iUserDate[pId][Clcmd_Type] = iKey;
g_iUserDate[pId][Clcmd_MenuType] = 1;
switch(iKey)
{
case 0,1,2:
{
client_cmd(pId, "messagemode ^"SingReg^"")
}
case 4:
{
if(g_iUserDate[pId][Active])
{
return PLUGIN_HANDLED;
}
if(is_login_existing(pId))
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Данный логин уже занят кем-то", dbdata[chatprefix]);
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "");
return Show_RegMenu(pId);
}
new login[32];
if(is_auth_existing(pId, login, charsmax(login)))
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Данный ^x04SteamID ^x01уже кем-то используется, логин: ^x04%s", dbdata[chatprefix], login);
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "");
return Show_RegMenu(pId);
}
new query[QUERY_LENGTH];
new szName[32], szAuth[32], pass[64];
get_user_name(pId, szName, charsmax(szName));
get_user_authid(pId, szAuth, charsmax(szAuth));
password_convert(g_iUserDate[pId][Password],charsmax(g_iUserDate[][Password]), pass, charsmax(pass));
if(dbdata[dbtype] == GAMECMS)
{
formatex(query,charsmax(query), "INSERT INTO `users` (`login`, `password`, `email`, `regdate`, `avatar`, `rights`, `nick`, `birth`, `last_activity`, `steam_id`, `signature`,`shilings`) \
VALUES ('%s', '%s', '%s', NOW(), 'files/avatars/no_avatars.jpg', '2', '%s', '1960-01-01', NOW(), '%s', '', 0)", g_iUserDate[pId][Login], pass, g_iUserDate[pId][Email], szName, szAuth);
}
else
{
formatex(query,charsmax(query), "INSERT INTO `%s` (`login`, `password`, `regdate`, `steam_id`) VALUES ('%s', '%s', NOW(), '%s')", dbdata[dbtable], g_iUserDate[pId][Login], pass, szAuth);
}
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_REGS_PLAYERS;
sData[EXT_DATA__INDEX] = pId;
return SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
}
case 9: return PLUGIN_HANDLED;
}
return Show_RegMenu(pId);
}
public clcmd_SingReg(pId)
{
switch(g_iUserDate[pId][Clcmd_MenuType])
{
case 0:
{
/*if(g_iUserDate[pId][Clcmd_Type] != 0 || g_iUserDate[pId][Clcmd_Type] != 1)
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Ошибка выбора меню", dbdata[chatprefix]);
return Show_SignMenu(pId);
}
*/
switch(g_iUserDate[pId][Clcmd_Type])
{
case 0:
{
read_argv(1, g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]))
if(is_login_connect(pId))
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Данным логином уже кто-то играет", dbdata[chatprefix]);
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "");
return Show_SignMenu(pId);
}
if(strlen(g_iUserDate[pId][Login]) > charsmax(g_iUserDate[][Login]) || strlen(g_iUserDate[pId][Login]) < 1)
{
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "")
client_print_color(pId, print_team_default, "^x04[%s]^x01 Некоректные данные, введите от 1 до 31 символов!", dbdata[chatprefix]);
return Show_SignMenu(pId);
}
re_mysql_escape_string(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]));
}
case 1:
{
read_argv(1, g_iUserDate[pId][Password], charsmax(g_iUserDate[][Password]))
if(strlen(g_iUserDate[pId][Password]) > charsmax(g_iUserDate[][Password]) || strlen(g_iUserDate[pId][Password]) < 1)
{
formatex(g_iUserDate[pId][Password], charsmax(g_iUserDate[][Password]), "")
client_print_color(pId, print_team_default, "^x04[%s]^x01 Некоректные данные, введите от 1 до 31 символов!", dbdata[chatprefix]);
}
re_mysql_escape_string(g_iUserDate[pId][Password], charsmax(g_iUserDate[][Password]));
}
}
return Show_SignMenu(pId);
}
case 1:
{
/*if(g_iUserDate[pId][Clcmd_Type] != 0 || g_iUserDate[pId][Clcmd_Type] != 1 || g_iUserDate[pId][Clcmd_Type] != 2)
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Ошибка выбора меню", dbdata[chatprefix]);
return Show_RegMenu(pId);
}*/
switch(g_iUserDate[pId][Clcmd_Type])
{
case 0:
{
read_argv(1, g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]))
if(strlen(g_iUserDate[pId][Login]) > charsmax(g_iUserDate[][Login]) || strlen(g_iUserDate[pId][Login]) < 1)
{
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "")
client_print_color(pId, print_team_default, "^x04[%s]^x01 Некоректные данные, введите от 1 до 31 символов!", dbdata[chatprefix]);
return Show_RegMenu(pId);
}
if(is_login_existing(pId))
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Данный логин уже занят кем-то", dbdata[chatprefix]);
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "");
return Show_RegMenu(pId);
}
re_mysql_escape_string(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]));
}
case 1:
{
read_argv(1, g_iUserDate[pId][Password], charsmax(g_iUserDate[][Password]))
if(strlen(g_iUserDate[pId][Password]) > charsmax(g_iUserDate[][Password]) || strlen(g_iUserDate[pId][Password]) < 1)
{
formatex(g_iUserDate[pId][Password], charsmax(g_iUserDate[][Password]), "")
client_print_color(pId, print_team_default, "^x04[%s]^x01 Некоректные данные, введите от 1 до 31 символов!", dbdata[chatprefix]);
}
re_mysql_escape_string(g_iUserDate[pId][Password], charsmax(g_iUserDate[][Password]));
}
case 2:
{
read_argv(1, g_iUserDate[pId][Email], charsmax(g_iUserDate[][Email]))
if(strlen(g_iUserDate[pId][Email]) > charsmax(g_iUserDate[][Email]) || strlen(g_iUserDate[pId][Email]) < 1 || !str_contains(g_iUserDate[pId][Email], "@", false, 2 ))
{
formatex(g_iUserDate[pId][Email], charsmax(g_iUserDate[][Email]), "")
client_print_color(pId, print_team_default, "^x04[%s]^x01 Некоректные данные, введите от 1 до 31 символов!", dbdata[chatprefix]);
}
re_mysql_escape_string(g_iUserDate[pId][Email], charsmax(g_iUserDate[][Email]));
}
}
return Show_RegMenu(pId);
}
}
return PLUGIN_HANDLED;
}
public selectQueryHandler(failstate, Handle:query, const err[], errNum, const data[], datalen, Float:queuetime)
{
switch(failstate)
{
case TQUERY_CONNECT_FAILED, TQUERY_QUERY_FAILED: // ошибка соединения с mysql сервером
{
new szText[128];
formatex(szText, charsmax(szText), "[Проблемы с БД. Код ошибки: #%d]", errNum);
if(datalen) log_to_file("mysqlt.log", "Query state: %d", data[0]);
log_to_file("mysqlt.log","%s", szText)
log_to_file("mysqlt.log","%s",err)
if(failstate == TQUERY_QUERY_FAILED)
{
new lastQue[QUERY_LENGTH], szText2[128];
formatex(szText2, charsmax(szText2), "======================================================");
SQL_GetQueryString(query, lastQue, charsmax(lastQue)) // узнаем последний SQL запрос
log_amx("%s",szText2)
log_amx("[ SQL ] %s",lastQue)
}
return PLUGIN_HANDLED;
}
}
new pId = data[EXT_DATA__INDEX];
switch(data[EXT_DATA__SQL])
{
case SQL_REGS_PLAYERS:
{
g_iUserDate[pId][Active] = true;
g_iUserDate[pId][AutoLogin] = 1;
client_print_color(pId, print_team_default, "^x04[%s]^x01 Вы успешно зарегистрировались, Логин: ^x04%s^x01, Почта: ^x04%s", dbdata[chatprefix], g_iUserDate[pId][Login],g_iUserDate[pId][Email]);
formatex(g_iUserDate[pId][LastSign], charsmax(g_iUserDate[][LastSign]), "Сегодня")
formatex(g_iUserDate[pId][RegDate], charsmax(g_iUserDate[][RegDate]), "Сегодня")
TrieSetCell(g_RegsStats, g_iUserDate[pId][Login], 1);
new query[QUERY_LENGTH];
if(dbdata[dbtype] == GAMECMS)
{
formatex(query,charsmax(query), "SELECT id FROM `users` WHERE `login` = '%s'", g_iUserDate[pId][Login]);
}
else
{
formatex(query,charsmax(query), "SELECT id FROM `%s` WHERE `login` = '%s'", dbdata[dbtable], g_iUserDate[pId][Login]);
}
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_PRE_CONNECT;
sData[EXT_DATA__INDEX] = pId;
return SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
}
case SQL_JOIN_PLAYERS:
{
if(SQL_NumResults(query))
{
g_iUserDate[pId][Active] = true;
TrieSetCell(g_RegsStats, g_iUserDate[pId][Login], 1);
client_print_color(pId, print_team_default, "^x04[%s]^x01 Вы успешно зашли в систему", dbdata[chatprefix]);
g_iUserDate[pId][Player_Id] = SQL_ReadResult( query, SQL_FieldNameToNum(query,"id") );
if(dbdata[dbtype] == NONGAMECMS)
g_iUserDate[pId][AutoLogin] = SQL_ReadResult( query, SQL_FieldNameToNum(query,"autologin") );
SQL_ReadResult( query, SQL_FieldNameToNum(query,"steam_id"), g_iUserDate[pId][rAuth], charsmax(g_iUserDate[][rAuth]));
SQL_ReadResult( query, SQL_FieldNameToNum(query,"regdate"), g_iUserDate[pId][RegDate], charsmax(g_iUserDate[][RegDate]));
g_jObject[pId] = json_init_object();
g_tPlayerPreferences[pId] = TrieCreate();
new query[QUERY_LENGTH];
formatex(query, charsmax(query), "SELECT * FROM `%s` WHERE `user_id` = '%d' AND `api_type` = '%d'", dbdata[dbsecondtable], g_iUserDate[pId][Player_Id], dbdata[dbtype]);
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_JSON_CONNECT;
sData[EXT_DATA__INDEX] = pId;
sData[EXT_DATA__USERID] = get_user_userid(pId);
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
return PLUGIN_HANDLED;
}
else
{
client_print_color(pId, print_team_default, "^x04[%s]^x01 Вы ввели некоректный логин или пароль", dbdata[chatprefix]);
g_iWarnPass[pId]++;
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "");
formatex(g_iUserDate[pId][Password], charsmax(g_iUserDate[][Password]), "");
return Show_SignMenu(pId);
}
}
case SQL_PRE_CONNECT:
{
if(SQL_NumResults(query))
{
g_iUserDate[pId][Player_Id] = SQL_ReadResult( query, SQL_FieldNameToNum(query,"id") );
g_jObject[pId] = json_init_object();
g_tPlayerPreferences[pId] = TrieCreate();
new query[QUERY_LENGTH],serial[MAX_DATA_BUFFER];
json_serial_to_string(g_jObject[pId], serial, charsmax(serial));
/*if (json_serial_size(g_jObject[pId]) < 3) {
DEBUG && log_amx("SQL_PRE_CONNECT | %d | %s", g_jObject[pId], serial);
ChallengeClear(pId);
return PLUGIN_HANDLED;
}*/
new szAuth[35];
get_user_authid(pId, szAuth, charsmax(szAuth));
formatex(query, charsmax(query), "INSERT INTO `%s` (`user_id`, `text`, `api_type`) VALUES ('%d', '%s', '%d')", dbdata[dbsecondtable], g_iUserDate[pId][Player_Id], serial, dbdata[dbtype] );
DEBUG && log_amx(query);
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_IGNORE;
sData[EXT_DATA__INDEX] = pId;
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
}
}
case SQL_AUTOLOGIN:
{
if (get_user_userid(pId) != data[EXT_DATA__USERID]) {
DEBUG && log_amx("[PP] Userid %d != Pushed userid %d", get_user_userid(pId), data[EXT_DATA__USERID]);
return PLUGIN_HANDLED;
}
if (SQL_NumResults(query))
{
SQL_ReadResult(query, SQL_FieldNameToNum(query, "login"), g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]));
if(g_iUserDate[pId][Active])
{
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "");
return 0;
}
if(is_login_connect(pId))
{
formatex(g_iUserDate[pId][Login], charsmax(g_iUserDate[][Login]), "");
return 0;
}
new query[QUERY_LENGTH];
new szName[32], szAuth[32];
get_user_name(pId, szName, charsmax(szName));
get_user_authid(pId, szAuth, charsmax(szAuth));
if(dbdata[dbtype] == GAMECMS)
{
formatex(query,charsmax(query), "SELECT * FROM `users` WHERE `login` = '%s'", g_iUserDate[pId][Login])
}
else
{
formatex(query,charsmax(query), "SELECT * FROM `%s` WHERE `login` = '%s'", dbdata[dbtable], g_iUserDate[pId][Login])
}
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_JOIN_PLAYERS;
sData[EXT_DATA__INDEX] = pId;
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
}
}
case SQL_JSON_CONNECT:
{
if (get_user_userid(pId) != data[EXT_DATA__USERID]) {
DEBUG && log_amx("[PP] Userid %d != Pushed userid %d", get_user_userid(pId), data[EXT_DATA__USERID]);
return PLUGIN_HANDLED;
}
if (SQL_NumResults(query))
{
new preferences[612];
SQL_ReadResult(query, SQL_FieldNameToNum(query, "text"), preferences, charsmax(preferences));
SQL_ReadResult( query, SQL_FieldNameToNum(query,"last_activity"), g_iUserDate[pId][LastSign], charsmax(g_iUserDate[][LastSign]));
new JSON: jsonValue = json_parse(preferences);
if (jsonValue == Invalid_JSON || preferences[0] != '{' || preferences[strlen(preferences) - 1] != '}') {
json_free(jsonValue);
if (DEBUG) {
log_error(AMX_ERR_NATIVE, "[PP] Skipped load from bad format json `login` = %s", g_iUserDate[pId][Login]);
}
ExecuteForward(g_eForwards[Fwd_Loaded], _, pId);
return PLUGIN_HANDLED;
}
new bool: bSomeBoolean, iSomeNumber;
for (new i = 0, n = json_object_get_count(jsonValue), JSON: element, key[MAX_KEY_LENGTH], value[MAX_VALUE_STRING_LENGTH]; i < n; i++) {
json_object_get_name(jsonValue, i, key, charsmax(key));
element = json_object_get_value_at(jsonValue, i);
switch (json_get_type(element)) {
case JSONString: {
json_get_string(element, value, MAX_VALUE_STRING_LENGTH - 1);
TrieSetString(g_tPlayerPreferences[pId], key, value);
json_object_set_string(g_jObject[pId], key, value);
}
case JSONNumber: {
iSomeNumber = json_get_number(element);
TrieSetCell(g_tPlayerPreferences[pId], key, iSomeNumber);
json_object_set_number(g_jObject[pId], key, iSomeNumber);
}
case JSONBoolean: {
bSomeBoolean = json_get_bool(element);
TrieSetCell(g_tPlayerPreferences[pId], key, bSomeBoolean ? 1 : 0);
json_object_set_bool(g_jObject[pId], key, bSomeBoolean);
}
}
json_free(element);
}
json_free(jsonValue);
DEBUG && server_print("[PP] Player Loaded %s", preferences);
}
else
{
new Handle:hGetSrvId;
if(dbdata[dbtype] == GAMECMS)
{
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `id` FROM `users` WHERE `login` = '%s'", g_iUserDate[pId][Login]);
}
else
{
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `id` FROM `%s` WHERE `login` = '%s'", dbdata[dbtable], g_iUserDate[pId][Login]);
}
if(!SQL_Execute(hGetSrvId) || SQL_NumResults(hGetSrvId))
{
g_iUserDate[pId][Player_Id] = SQL_ReadResult( hGetSrvId, SQL_FieldNameToNum(hGetSrvId,"id") );
}
SQL_FreeHandle(hGetSrvId);
g_jObject[pId] = json_init_object();
g_tPlayerPreferences[pId] = TrieCreate();
new query[QUERY_LENGTH],serial[MAX_DATA_BUFFER];
json_serial_to_string(g_jObject[pId], serial, charsmax(serial));
new szAuth[35];
get_user_authid(pId, szAuth, charsmax(szAuth));
formatex(query, charsmax(query), "INSERT INTO `%s` (`user_id`, `text`, `api_type`) VALUES ('%d', '%s', '%d')", dbdata[dbsecondtable], g_iUserDate[pId][Player_Id], serial, dbdata[dbtype]);
DEBUG && log_amx(query);
new sData[EXT_DATA_STRUCT];
sData[EXT_DATA__SQL] = SQL_IGNORE;
sData[EXT_DATA__INDEX] = pId;
SQL_ThreadQuery(g_hDBHandle, "selectQueryHandler", query, sData, sizeof sData);
}
g_iUserConnected[pId] = true;
ExecuteForward(g_eForwards[Fwd_Loaded], _, pId);
}
case SQL_IGNORE: {}
}
return PLUGIN_HANDLED;
}
stock password_convert(in_password[],iniLen, out_password[], outiLen)
{
new md5Hash[64];
add(in_password, iniLen, Salt);
hash_string(in_password, Hash_Md5, md5Hash, 64);
ReverseString( md5Hash, out_password );
add(out_password, outiLen, "a");
}
stock ReverseString( input[ ], output[ ] )
{
new i, j;
for( i = strlen( input ) - 1, j = 0 ; i >= 0 ; i--, j++ )
{
output[ j ] = input[ i ];
}
output[ j ] = '^0';
}
stock bool:str_contains(const string[], const substr[], const bool:ignorecase = false, start = 0, const end = 0) {
new pos = strfind(string, substr, ignorecase, start);
if (pos == -1 || end > 0 && pos > end) {
return false;
}
return true;
}
stock bool:is_login_existing(pId)
{
new iValue = 0;
if(g_ConnectHandle == Empty_Handle)
return bool:iValue;
new Handle:hGetSrvId;
if(dbdata[dbtype] == GAMECMS)
{
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `login` FROM `users` WHERE `login` = '%s'", g_iUserDate[pId][Login]);
}
else
{
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `login` FROM `%s` WHERE `login` = '%s'", dbdata[dbtable], g_iUserDate[pId][Login]);
}
if(!SQL_Execute(hGetSrvId) || SQL_NumResults(hGetSrvId))
{
iValue = 1
}
SQL_FreeHandle(hGetSrvId);
return bool:iValue;
}
stock bool:is_auth_existing(pId, login[], iLen)
{
new iValue = 0;
if(g_ConnectHandle == Empty_Handle)
return bool:iValue;
new szAuth[35];
get_user_authid(pId, szAuth, charsmax(szAuth));
new Handle:hGetSrvId;
if(dbdata[dbtype] == GAMECMS)
{
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `login` , `steam_id` FROM `users` WHERE `steam_id` = '%s' LIMIT 1", szAuth);
}
else
{
hGetSrvId = SQL_PrepareQuery(g_ConnectHandle, "SELECT `login` , `steam_id` FROM `%s` WHERE `steam_id` = '%s' LIMIT 1", dbdata[dbtable], szAuth);
}
if(!SQL_Execute(hGetSrvId) || SQL_NumResults(hGetSrvId))
{
SQL_ReadResult(hGetSrvId, SQL_FieldNameToNum(hGetSrvId,"login"), login, iLen);
iValue = 1
}
SQL_FreeHandle(hGetSrvId);
return bool:iValue;
}
stock re_mysql_escape_string( sString[ ], iLen )
{
replace_string( sString, iLen, "\", "\\" );
replace_string( sString, iLen, "\\x00", "\\0" );
replace_string( sString, iLen, "\\x1a", "\\Z" );
replace_string( sString, iLen, "'", "\'" );
replace_string( sString, iLen, "^"", "\^"" );
}
stock bool:is_login_connect(pId)
{
if(TrieKeyExists(g_RegsStats,g_iUserDate[pId][Login]))
{
return true;
}
return false;
}