Last question
Is it possible not to make new handle and not to open new mysql connection in CheckSteamID()? I don't know how to use existing connection.
PHP Code:
#include <sourcemod>
#define DEBUG 1
new Handle:hDatabase = INVALID_HANDLE;
public Plugin:myinfo =
{
name = "CGC",
author = "3D-GRAF",
description = "CGC SteamID Check",
url = "http://cgaming.ru/"
};
public OnPluginStart()
{
hDatabase = MySQL_Connect();
#if DEBUG
PrintToServer("[CGC] Plugin loaded");
#endif
}
public OnClientPostAdminCheck(client)
{
decl String:steam_id[32];
GetClientAuthString(client, steam_id, sizeof(steam_id));
decl String:username[64];
GetClientName(client, username, sizeof(username));
if (StrEqual(steam_id[0], "BOT")) {
return;
}
#if DEBUG
PrintToServer("[CGC] %s - %s Authorized", username, steam_id[0]);
#endif
if (hDatabase == INVALID_HANDLE) {
return;
}
PrintToChatAll("\x04[CGC]\x03 Checking [ \x04%s \x03- \x04%s \x03]", username, steam_id[0]);
new String:query[255];
Format(query, sizeof(query), "SELECT team FROM cgc_players WHERE steam_id = '%s'", steam_id[0]);
SQL_TQuery(hDatabase, CheckSteamID, query, client);
}
Handle:MySQL_Connect() {
new String:error[256];
new Handle:db = INVALID_HANDLE;
db = SQL_Connect("cgc", true, error, sizeof(error));
if (db == INVALID_HANDLE) {
#if DEBUG
PrintToServer("[CGC] Can't connect MySQL: %s", error);
#endif
return db;
} else {
#if DEBUG
PrintToServer("[CGC] MySQL Connected");
#endif
return db;
}
}
public CheckSteamID(Handle:owner, Handle:hndl, const String:error[], any:client)
{
if (hndl == INVALID_HANDLE)
{
KickClient(client, "Ошибка авторизации");
#if DEBUG
PrintToServer("[CGC] Query failed");
#endif
return true;
}
decl String:steam_id[32];
GetClientAuthString(client, steam_id, sizeof(steam_id));
decl String:username[64];
GetClientName(client, username, sizeof(username));
new String:teamid[255];
new String:team[255];
while (SQL_FetchRow(hndl))
{
SQL_FetchString(hndl, 0, teamid, sizeof(teamid));
teamid[0] = teamid[0] - 48;
if (teamid[0] != 0) {
new String:queryt[255];
Format(queryt, sizeof(queryt), "SELECT name FROM cgc_teams WHERE tid = %i", teamid[0]);
new Handle:hQuery = INVALID_HANDLE;
hQuery = MySQL_Connect();
if ((hQuery = SQL_Query(hQuery, queryt)) == INVALID_HANDLE) {
return true;
}
while (SQL_FetchRow(hQuery))
{
SQL_FetchString(hQuery, 0, team, sizeof(team));
}
CloseHandle(hQuery);
} else {
team = "Not in team";
}
}
if (!SQL_GetRowCount(hndl)) {
KickClient(client, "Вы не заявлены на участие");
PrintToChatAll("\x04[CGC]\x03 Kicked [ \x04%s \x03- \x04%s \x03]", username, steam_id[0]);
#if DEBUG
PrintToServer("[CGC] %i not regged - kicked", client);
#endif
} else if (SQL_GetRowCount(hndl)) {
PrintToChatAll("\x04[CGC]\x03 Authenticated: \x04%s \x03Team: \x04%s", username, team[0]);
#if DEBUG
PrintToServer("[CGC] %s - %s Authenticated", steam_id[0], team[0]);
#endif
}
return true;
}