Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
|
05-22-2017
, 11:53
Re: [ANY] Help with a script
|
#5
|
Quote:
Originally Posted by Pruppet
Untested, there might be errors here and there, also you should definitely modify this to your liking, it's not complete, but there's all the necessary stuff to continue on. Enjoy the snippet
Code:
Handle g_hDatabase;
bool g_hDatabaseReady;
bool g_bIsClientOnTrial[MAXPLAYERS + 1];
bool g_bIsTrialExpired[MAXPLAYERS + 1];
int g_iTimeLeft[MAXPLAYERS + 1];
public void OnPluginStart()
{
sm_trial_timer = CreateConVar("sm_trial_timer", "60", "How long should trials last (in minutes)?");
sm_trial_limit = CreateConVar("sm_trial_limit", "1", "How many trials should user's be allowed to use?");
sm_trial_interval = CreateConVar("sm_trial_interval", "0", "How long (in minutes) until a user can receive another trial? (Use only if sm_trial_limit is set to more than 1; leave it on 0 to disable)");
sm_trial_flags = CreateConVar("sm_trial_flags", "", "What SourceBans flag(s) should be given to players?");
sm_trial_groups = CreateConVar("sm_trial_groups", "", "What SourceBans server admin group should be given to players? (group id # in SourceBans; also, make sure you create a server group in SourceBans that includes all the servers, or the ones you want trials to be on)");
sm_trial_servers = CreateConVar("sm_trial_server_group", "", "What server group id # should the plugin use? (make sure that the server group you use is the server group that the admin group has access to!)");
sm_trial_database = CreateConVar("sm_trial_database", "sourcebans", "What is the database entry for SourceBans in databases.cfg? (you probably won't want to change this!)");
RegConsoleCmd("sm_trial", Command_Trial);
AutoExecConfig(true, "plugin.trials");
DatabaseConnect();
CreateTimer(60.0, Timer_UpdateTrialTimeLeft, _, TIMER_REPEAT);
}
public void OnPluginEnd()
{
DatabaseDisconnect();
}
public void OnClientConnected(int client)
{
g_bIsClientOnTrial[client] = false;
g_bIsTrialExpired[client] = false;
if (g_hDatabaseReady)
{
PerformTrialCheck(client);
}
}
public Action Command_Trial(int client, int args)
{
if (g_hDatabaseReady)
{
if (IsValidClient(client))
{
g_bIsClientOnTrial[client] = true;
g_iTimeLeft[client] = GetConVarInt(sm_trial_timer);
QueryClientTrial(client);
PrintToChat(client, "[SM] You have now started your trial period of %d minutes.", GetConVarInt(sm_trial_timer));
}
}
else
{
if (IsValidClient(client))
{
PrintToChat(client, "[SM] Error: Database not connected.");
}
}
return Plugin_Handled;
}
void PerformTrialCheck(int client)
{
if (g_hDatabaseReady)
{
if (IsValidClient(client))
{
char m_sSteamID[20];
GetClientAuthId(client, AuthId_Steam2, m_sSteamID, sizeof(m_sSteamID));
char sQuery[256];
FormatEx(sQuery, sizeof(sQuery), "SELECT timeleft, expired FROM database WHERE steamid LIKE '%s'", m_sSteamID);
SQL_TQuery(g_hDatabase, SQLCallback_PerformTrialCheck, sQuery, client);
}
}
}
void QueryClientTrial(int client)
{
if (g_hDatabaseReady)
{
if (IsValidClient(client))
{
char m_sSteamID[20];
GetClientAuthId(client, AuthId_Steam2, m_sSteamID, sizeof(m_sSteamID));
int m_iTime = GetConVarInt(sm_trial_timer);
char sQuery[256];
char sTemp[256];
char sEscapedName[128];
Format(sTemp, sizeof(sTemp), "%N", client);
SQL_EscapeString(g_hDatabase, sTemp, sEscapedName, sizeof(sEscapedName));
FormatEx(sQuery, sizeof(sQuery), "INSERT INTO database (username, steamid, time, timeleft, expired) VALUES ('%s', '%s', %d, %d, %d)", sEscapedName, m_sSteamID, m_iTime, m_iTime, 0);
SQL_TQuery(g_hDatabase, SQLCallback_QueryClientTrial, sQuery, _, DBPrio_Low);
}
}
}
public void SQLCallback_PerformTrialCheck(Handle owner, Handle hndl, const char[] error, int client)
{
if (hndl != null)
{
if (SQL_FetchRow(hndl))
{
int timeleft = SQL_FetchInt(hndl, 0);
int expired = SQL_FetchInt(hndl, 1);
g_bIsClientOnTrial[client] = ((timeleft > -1) ? true : false);
g_bIsTrialExpired[client] = ((expired == 1) ? true : false);
}
}
}
public void SQLCallback_QueryClientTrial(Handle owner, Handle hndl, const char[] error, any data)
{
if (hndl == null || hndl == INVALID_HANDLE)
{
LogError("[SM] Query error: %s.", error);
}
}
public Action Timer_UpdateTrialTimeLeft(Handle timer)
{
for (int i = 1; i <= MaxClients; i++)
{
if (IsValidClient(i))
{
if (g_bIsClientOnTrial[i])
{
g_iTimeLeft[i]--;
}
}
}
return Plugin_Handled;
}
static void DatabaseConnect()
{
char sError[64];
g_hDatabase = SQL_Connect("database", true, sError, sizeof(sError));
if (g_hDatabase == null)
{
delete g_hDatabase;
g_bDatabaseReady = false;
SetFailState("[Perms] Failed on connection to database: %s", sError);
}
else
{
g_bDatabaseReady = true;
}
}
static void DatabaseDisconnect()
{
if (g_hDatabase != null)
{
delete g_hDatabase;
g_hDatabase = null;
g_bDatabaseReady = false;
}
}
|
Thanks!
|
|