Raised This Month: $32 Target: $400
 8% 

[ANY] Help with a script


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 05-18-2017 , 13:17   [ANY] Help with a script
Reply With Quote #1

Hey,
So I have just begun learning scripting in sourcepawn. I have been writing a plugin that basically I want users to be able to use VIP trials in-game for a period of time. I also have wanted to implement SourceBans support for the plugin. However, I don't know where to begin. Here is my code so far:
Code:
#include <sourcemod>
#include <dbi>

public Plugin myinfo()
{
      name = "[ANY] SourceBans VIP Trials",
      author = "Sgt. Gremulock",
      description = "Give players the ability to use VIP trials (with SourceBans support).",
      version = "1.0 (Alpha)",
      url = "grem-co.site.nfoservers.com"
};

public void OnPluginStart()
{
      RegConsoleCmd("sm_trial", Command_Trial);
      
      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!)");

      AutoExecConfig(true, "plugin.trials");
}

public Action Command_Trial(int client)
{
      return Plugin_Handled;
}
The thing that I really need help with is knowing how to make the command client only (can't use on other players), how to grab client's steam id, and how to query database to insert the user for VIP for a period of time then auto remove them. After I finish this plugin, I plan on releasing it publicly.
ThatKidWhoGames is offline
Bobakanoosh
Senior Member
Join Date: Sep 2015
Location: United States
Old 05-18-2017 , 14:52   Re: [ANY] Help with a script
Reply With Quote #2

https://sm.alliedmods.net/new-api/cl...etClientAuthId should suffice for getting a client's steam id, personally I would use AuthId_SteamID64 as the Authid type.

Code:
public Action Command_Trial(int client){} 
should be 
public Action Command_Trial(int client, int args){}
Quote:
knowing how to make the command client only (can't use on other players)
This is already done, really. As long as you're not dealing with any of the args that the client submitted (i.e /trial bobakanoosh), nothing will happen. The client you're dealing with is simply whoever issued the command.
Bobakanoosh is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 05-18-2017 , 19:14   Re: [ANY] Help with a script
Reply With Quote #3

Quote:
Originally Posted by Bobakanoosh View Post
https://sm.alliedmods.net/new-api/cl...etClientAuthId should suffice for getting a client's steam id, personally I would use AuthId_SteamID64 as the Authid type.

Code:
public Action Command_Trial(int client){} 
should be 
public Action Command_Trial(int client, int args){}

This is already done, really. As long as you're not dealing with any of the args that the client submitted (i.e /trial bobakanoosh), nothing will happen. The client you're dealing with is simply whoever issued the command.
Thank you very much for the help I appreciate it!
ThatKidWhoGames is offline
Pruppet
Junior Member
Join Date: Dec 2016
Old 05-19-2017 , 17:58   Re: [ANY] Help with a script
Reply With Quote #4

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;
	}
}
__________________
Contact me on Steam for private plugins.
Pruppet is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 05-22-2017 , 11:53   Re: [ANY] Help with a script
Reply With Quote #5

Quote:
Originally Posted by Pruppet View Post
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!
ThatKidWhoGames is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 23:22.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode