VIP_Test: rewrote with threaded sql-queries

This commit is contained in:
neon 2018-09-12 11:50:14 +02:00
parent 6276a6cf88
commit ac796b675a

View File

@ -1,18 +1,33 @@
#pragma semicolon 1
#include <sourcemod>
char g_sAdmGroup[32] = "Game-Donator";
#pragma newdecls required
/* STRINGS */
char g_sAdmGroup[32] = "Game-Donator";
char g_sGroup[MAXPLAYERS+1][64];
/* CONVARS */
ConVar g_cvFreeVIPDuration;
/* DATABASE */
Database g_hDatabase;
/* BOOLS */
bool g_bPreAdminChecked[MAXPLAYERS+1];
bool g_bResponseFailed[MAXPLAYERS+1];
bool g_bResponsePassed[MAXPLAYERS+1];
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
name = "Unloze_VIP_Test",
name = "UNLOZE_VIP_Test",
author = "Neon",
description = "",
version = "1.0",
version = "2.0",
url = "https://steamcommunity.com/id/n3ontm"
}
@ -26,18 +41,15 @@ public void OnPluginStart()
RegConsoleCmd("sm_viptest", Command_VIP, "Activate free VIP period");
RegConsoleCmd("sm_testvip", Command_VIP, "Activate free VIP period");
RegConsoleCmd("sm_vip", Command_VIP, "Activate free VIP period");
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_VIP(int iClient, int iArgs)
{
if (!IsValidClient(iClient))
return Plugin_Handled;
AutoExecConfig();
CheckMYSQL(iClient, true);
return Plugin_Handled;
char sError[256];
if (SQL_CheckConfig("testvip"))
g_hDatabase = SQL_Connect("testvip", true, sError, sizeof(sError));
if (g_hDatabase == null)
LogError("Could not connect to database: %s", sError);
}
//----------------------------------------------------------------------------------------------------
@ -54,120 +66,169 @@ public void OnRebuildAdminCache(AdminCachePart part)
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientPostAdminFilter(int client)
public Action OnRebuildAdminCachePost(Handle hTimer)
{
CheckMYSQL(client);
for (int client = 1; client <= MaxClients; client++)
{
if(g_bResponsePassed[client] && g_bPreAdminChecked[client])
ApplyGroupFlags(client);
}
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
bool CheckMYSQL(int client, bool add = false)
public void OnClientConnected(int client)
{
if (client == 0)
return false;
g_bPreAdminChecked[client] = false;
g_bResponseFailed[client] = false;
g_bResponsePassed[client] = false;
char error[255];
Database db;
g_sGroup[client][0] = 0;
}
if (SQL_CheckConfig("testvip"))
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
g_bPreAdminChecked[client] = false;
g_bResponseFailed[client] = false;
g_bResponsePassed[client] = false;
g_sGroup[client][0] = 0;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientAuthorized(int client, const char[] sSteamID32)
{
if (IsFakeClient(client))
return;
char sSteamID[32];
GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID));
char sQuery[256];
Format(sQuery, sizeof(sQuery), "SELECT * FROM testvip_table WHERE steam_auth = '%s'", sSteamID);
SQL_TQuery(g_hDatabase, TQueryCBConnect, sQuery, GetClientUserId(client));
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void TQueryCBConnect(Handle owner, Handle rs, const char[] error, any data)
{
int client = 0;
if ((client = GetClientOfUserId(data)) == 0)
return;
if (SQL_GetRowCount(rs) > 0)
{
db = SQL_Connect("testvip", true, error, sizeof(error));
}
if (db == null)
{
LogError("Could not connect to database: %s", error);
delete db;
return false;
}
if (!IsFakeClient(client) && !IsClientSourceTV(client))
{
char sSID[64];
GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
char sQuery[255];
Format(sQuery, sizeof(sQuery), "SELECT * FROM testvip_table WHERE steam_auth = '%s'", sSID);
DBResultSet rs;
if ((rs = SQL_Query(db, sQuery)) == null)
{
delete db;
delete rs;
LogError("Database Error: %s", error);
return false;
}
int iField;
SQL_FetchRow(rs);
SQL_FieldNameToNum(rs, "activated", iField);
int iTimestampActivated = SQL_FetchInt(rs, iField);
int iTimestamp = GetTime();
int iTimestampActivated = GetTime();
bool bFound = false;
if (!(rs.RowCount > 0))
{
if (add)
{
bool bHasVIP = false;
AdminId AdmID;
if ((AdmID = GetUserAdmin(client)) == INVALID_ADMIN_ID)
bHasVIP = false;
else
{
for (int i = 0; i <= GetAdminGroupCount(AdmID); i++)
{
char sGroup[32];
if ((GetAdminGroup(AdmID, i, sGroup, sizeof(sGroup)) != INVALID_GROUP_ID))
{
if (StrEqual(sGroup, g_sAdmGroup))
bHasVIP = true;
}
}
}
if (!bHasVIP)
{
Format(sQuery, sizeof(sQuery), "INSERT INTO testvip_table (steam_auth, activated) VALUES ('%s', '%d')", sSID, iTimestamp);
SQL_FastQuery(db, sQuery);
ApplyGroupFlags(client);
PrintToChat(client, "[Unloze] Your TEST VIP will expire in %d minutes!", g_cvFreeVIPDuration.IntValue - (iTimestamp - iTimestampActivated) / 60);
PrintToChat(client, "[Unloze] You have now access to !zclass, !tag and !glow.");
}
else
{
PrintToChat(client, "[Unloze] You already have VIP activated!");
delete db;
delete rs;
return false;
}
}
}
else
{
int iField;
rs.FetchRow();
rs.FieldNameToNum("activated", iField);
iTimestampActivated = rs.FetchInt(iField);
bFound = true;
}
delete rs;
if (bFound)
if ((iTimestamp - iTimestampActivated) < g_cvFreeVIPDuration.IntValue *60)
{
if ((iTimestamp - iTimestampActivated) < g_cvFreeVIPDuration.IntValue *60)
{
ApplyGroupFlags(client);
PrintToChat(client, "[Unloze] Your TEST VIP will expire in %d minutes!", g_cvFreeVIPDuration.IntValue - (iTimestamp - iTimestampActivated) / 60);
}
else
{
if (add)
PrintToChat(client, "[Unloze] Your TEST VIP expired already!", g_cvFreeVIPDuration.IntValue - (iTimestamp - iTimestampActivated) / 60);
}
strcopy(g_sGroup[client], sizeof(g_sGroup[]), g_sAdmGroup);
}
}
delete db;
return true;
g_bResponsePassed[client] = true;
if (g_bPreAdminChecked[client])
NotifyPostAdminCheck(client);
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action OnClientPreAdminCheck(int client)
{
g_bPreAdminChecked[client] = true;
if (g_bResponsePassed[client] || g_bResponseFailed[client])
return Plugin_Continue;
RunAdminCacheChecks(client);
return Plugin_Handled;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientPostAdminFilter(int client)
{
ApplyGroupFlags(client);
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_VIP(int client, int iArgs)
{
if (!IsValidClient(client))
return Plugin_Handled;
char sSteamID[32];
GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID));
char sQuery[255];
Format(sQuery, sizeof(sQuery), "SELECT * FROM testvip_table WHERE steam_auth = '%s'", sSteamID);
SQL_TQuery(g_hDatabase, TQueryCBCommand, sQuery, GetClientUserId(client));
return Plugin_Handled;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void TQueryCBCommand(Handle owner, Handle rs, const char[] error, any data)
{
int client = 0;
if ((client = GetClientOfUserId(data)) == 0)
return;
int iTimestamp = GetTime();
if (SQL_GetRowCount(rs) > 0)
{
int iField;
SQL_FetchRow(rs);
SQL_FieldNameToNum(rs, "activated", iField);
int iTimestampActivated = SQL_FetchInt(rs, iField);
delete rs;
if ((iTimestamp - iTimestampActivated) < g_cvFreeVIPDuration.IntValue *60)
PrintToChat(client, "[UNLOZE] Your TEST VIP will expire in %d minutes!", g_cvFreeVIPDuration.IntValue - (iTimestamp - iTimestampActivated) / 60);
else
PrintToChat(client, "[UNLOZE] Your TEST VIP expired already!");
}
else
{
if (IsVIP(client))
{
PrintToChat(client, "[UNLOZE] You already have VIP activated!");
return;
}
char sSteamID[32];
GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID));
char sQuery[255];
Format(sQuery, sizeof(sQuery), "INSERT INTO testvip_table (steam_auth, activated) VALUES ('%s', '%d')", sSteamID, iTimestamp);
SQL_FastQuery(g_hDatabase, sQuery);
strcopy(g_sGroup[client], sizeof(g_sGroup[]), g_sAdmGroup);
ApplyGroupFlags(client);
PrintToChat(client, "[UNLOZE] You have now access to !zclass, !tag and !glow and other VIP-Perks.");
PrintToChat(client, "[UNLOZE] Your TEST VIP will expire in %d minutes!", g_cvFreeVIPDuration.IntValue);
}
}
//----------------------------------------------------------------------------------------------------
@ -175,6 +236,9 @@ bool CheckMYSQL(int client, bool add = false)
//----------------------------------------------------------------------------------------------------
stock void ApplyGroupFlags(int client)
{
if (!g_bResponsePassed[client])
return;
AdminId AdmID;
GroupId GrpID;
@ -186,27 +250,13 @@ stock void ApplyGroupFlags(int client)
SetUserAdmin(client, AdmID, true);
}
if ((GrpID = FindAdmGroup(g_sAdmGroup)) != INVALID_GROUP_ID)
if ((GrpID = FindAdmGroup(g_sGroup[client])) != INVALID_GROUP_ID)
{
if (AdminInheritGroup(AdmID, GrpID))
LogMessage("%L added to group %s", client, g_sAdmGroup);
LogMessage("%L added to group %s", client, g_sGroup[client]);
}
else
{
LogMessage("%L group not found %s", client, g_sAdmGroup);
}
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action OnRebuildAdminCachePost(Handle hTimer)
{
for (int client = 1; client <= MaxClients; client++)
{
if (IsValidClient(client))
CheckMYSQL(client);
}
LogMessage("%L group not found %s", client, g_sGroup[client]);
}
//----------------------------------------------------------------------------------------------------
@ -215,8 +265,31 @@ public Action OnRebuildAdminCachePost(Handle hTimer)
stock int IsValidClient(int client, bool nobots = true)
{
if (client <= 0 || client > MaxClients || !IsClientConnected(client) || (nobots && IsFakeClient(client)))
{
return false;
}
return IsClientInGame(client);
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public bool IsVIP(int client)
{
AdminId AdmID;
if ((AdmID = GetUserAdmin(client)) == INVALID_ADMIN_ID)
return false;
else
{
for (int i = 0; i <= GetAdminGroupCount(AdmID); i++)
{
char sGroup[32];
if ((GetAdminGroup(AdmID, i, sGroup, sizeof(sGroup)) != INVALID_GROUP_ID))
{
if (StrEqual(sGroup, g_sAdmGroup))
return true;
}
}
}
return false;
}