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

Solved [CS:GO] Help with keyvalue-based ctban plugin


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
potatoz
AlliedModders Donor
Join Date: Nov 2016
Old 08-16-2018 , 14:37   [CS:GO] Help with keyvalue-based ctban plugin
Reply With Quote #1

Hi,

A server i used to develop and administrate for, which i still help out with plugins and such in a few cases, has recently been having major problems finding a CT-Ban plugin that works, trying CT-Ban by databomb, the edit by Addicted, Teambans by Bara and more to no success. I went to work on a simple ct-ban plugin based on keyvalues partly considering knowledge of coding with SQL is limited but also since i know more about how to work with keyvalues having used it before.

Now, whilst the plugin works in ct-banning players and such, players also seem to get invalid ct-bans in a way which seems to give the player someone elses ct-ban on the postadmincheck where it gets a clients ct-ban info from the local file. The file is formatted sort of like the example below, considering i don't have an actual example myself this may not be exact but it should be about right;

PHP Code:
"ctban_database"
{
   
"STEAM_0:1:57886172"
   
{
       
"ctban_time"    "582"
   
}

Any help to as of why this could be happening and how to fix it is really appreciated, i will leave the source-code here as well, keep in mind that the code might not be perfect in syntax rules and does contain a lot of possibly unnecessary code partly in desperate hope to fix the issue; feel free to point these things out or even just rewrite parts of code

PHP Code:
#include <sourcemod>
#include <sdktools>
#include <cstrike>
#pragma semicolon 1 
 
int ctban[MAXPLAYERS+1];
Handle ctbantimer[MAXPLAYERS+1];
 
KeyValues kv

public 
Plugin myinfo =
{
    
name "Simple CT-Ban",
    
author "Potatoz",
    
description "Simple CT-Ban system for jailbreak servers",
    
version "1.0",
    
url "http://www.sourcemod.net/"
};


public 
void OnPluginStart()
{
    
LoadTranslations("common.phrases");
    
RegAdminCmd("sm_ctban"Command_CTBanADMFLAG_BAN);
    
RegAdminCmd("sm_ctunban"Command_CTUnBanADMFLAG_BAN);
    
RegAdminCmd("sm_isbanned"Command_IsBannedADMFLAG_BAN);
    
    
HookEvent("player_team"Event_PlayerTeam);
    
AddCommandListener(Event_OnJoinTeam"jointeam");
    
    
char buffer[PLATFORM_MAX_PATH];
    
BuildPath(Path_SMbuffersizeof(buffer), "configs/ctban_database.cfg");
    
kv = new KeyValues("ctban_database");
    
kv.ImportFromFile(buffer);
}

public 
Action Event_PlayerTeam(Handle event, const char[] namebool dontBroadcast)
{
    new 
client  GetClientOfUserId(GetEventInt(event"userid"));
    if(
IsValidClient(client))
        
CreateTimer(0.2CheckTeamclientTIMER_FLAG_NO_MAPCHANGE);
}

public 
Action CheckTeam(Handle timerint client)
{
    if(
IsValidClient(client) && GetClientTeam(client) == && ctban[client] > 0)
    {
        
int minutes ctban[client] / 60;
        
minutes++;
    
        
PrintToChat(client" \x07* You are banned from the CT team for %.1d more minute(s)"minutes);
        
ChangeClientTeam(client2);
    }
    
    return 
Plugin_Handled;
}

public 
Action Event_OnJoinTeam(int client, const char[] commandint args
{
    if (!
IsValidClient(client) || IsFakeClient(client))
        return 
Plugin_Continue;

    
char teamString[2];
    
GetCmdArg(1teamStringsizeof(teamString));
    
int team StringToInt(teamString);

    
int minutes ctban[client] / 60;
    
minutes++;
    
    if (
team == 0
    {
        
PrintToChat(client" \x07* You can't auto-assign to select team");
        return 
Plugin_Stop;
    }
    
    if (
team == && ctban[client] > 0
    {
        
PrintToChat(client" \x07* You are banned from the CT team for %.1d more minute(s)"minutes);
        return 
Plugin_Stop;
    }
    
    return 
Plugin_Continue;
}

public 
OnClientPostAdminCheck(int client
{
    
CreateTimer(1.0Timer_GetCTBanDataTIMER_FLAG_NO_MAPCHANGE);
}

public 
Action Timer_GetCTBanData(Handle timerint client)
{
    
GetCTBanData(client);
    
    if(
ctban[client] > 0)
        
ctbantimer[client] = CreateTimer(1.0Timer_CTBanclientTIMER_REPEAT TIMER_FLAG_NO_MAPCHANGE);
        
    return 
Plugin_Handled;
}

public 
Action Command_CTBan(int clientint args)
{
    if(
args 3
    {
        
ReplyToCommand(client"[SM] Usage: sm_ctban <name|#userid> [minutes] [reason]");
        return 
Plugin_Handled;
    }
    
    
char arg1[32], arg2[32], arg3[40];

    
GetCmdArg(1arg1sizeof(arg1));
    
GetCmdArg(2arg2sizeof(arg2));
    
GetCmdArg(3arg3sizeof(arg3));
    
    
int minutes StringToInt(arg2);
    
int target FindTarget(clientarg1);
    if (
target == -1) return Plugin_Handled;
    
    if(
ctban[target] > 0)
    {
        
PrintToChat(client" \x07* %N is already CT-Banned"target);
        return 
Plugin_Handled;
    }
    
    
ctban[target] = minutes 60;
    
ctbantimer[target] = CreateTimer(1.0Timer_CTBantargetTIMER_REPEAT TIMER_FLAG_NO_MAPCHANGE);
    
    
UpdateCTBanData(target);
    
    if(
GetClientTeam(target) == 3)
    {
        
ChangeClientTeam(target2);
        
ForcePlayerSuicide(target);
    }
    
    
PrintToChatAll(" \x07*\x01 ADMIN: \x07%N\x01 CT-Banned \x07%N \x01for %s minute(s) with reason: \x07%s"clienttargetarg2arg3);
    
    return 
Plugin_Handled;
}

public 
Action Command_CTUnBan(int clientint args)
{
    if(
args 1
    {
        
ReplyToCommand(client"[SM] Usage: sm_ctunban <name|#userid>");
        return 
Plugin_Handled;
    }
    
    
char arg1[32];

    
GetCmdArg(1arg1sizeof(arg1));
    
int target FindTarget(clientarg1);
    if (
target == -1) return Plugin_Handled;
    
    if(
ctban[target] < 1)
        
PrintToChat(client" \x07* %N is not CT-Banned"target);
        
    
ctbantimer[target] = null;
    
KillTimer(ctbantimer[target]);
    
    
ctban[target] = 0;
    
PrintToChatAll(" \x06*\x01 ADMIN: \x06%N\x01 has removed CT-Ban from \x06%N"clienttarget);
    
UpdateCTBanData(target);
    
    return 
Plugin_Handled;
}

public 
Action Timer_CTBan(Handle timerint client)
{
    if(
ctban[client] < 1)
    {
        
ctban[client] = 0;
        
        
ctbantimer[client] = null;
        
KillTimer(ctbantimer[client]);
        
        return 
Plugin_Stop;
    }
    
    
ctban[client]--;
    
UpdateCTBanData(client);
    
    if(
ctban[client] < 1)
    {
        
PrintToChat(client" \x06*\x01 Your CT-Ban has expired");
        
ctban[client] = 0;
        
        
ctbantimer[client] = null;
        
KillTimer(ctbantimer[client]);
        
        return 
Plugin_Stop;
    }
    
    return 
Plugin_Continue;
}

public 
void OnClientDisconnect(int client)
{
    if (
ctbantimer[client] != null)
    {
        
KillTimer(ctbantimer[client]);
        
ctbantimer[client] = null;
    }
}

public 
Action Command_IsBanned(int clientint args)
{
    if(
args 1
    {
        
ReplyToCommand(client"[SM] Usage: sm_isbanned <name|#userid>");
        return 
Plugin_Handled;
    }
    
    
char arg1[32];
    
GetCmdArg(1arg1sizeof(arg1));
    
int target FindTarget(clientarg1);
    if (
target == -1) return Plugin_Handled;
    
    
int minutes ctban[client] / 60;
    
minutes++;
    
    if(
ctban[target] < 1)
        
PrintToChat(client" \x07* %N is not CT-Banned"target);
    else 
PrintToChat(client" \x06* %N \x01is CT-Banned for %.1d more minute(s)"targetminutes);
    
    return 
Plugin_Handled;
}

UpdateCTBanData(int client)
{
    
char buffer[PLATFORM_MAX_PATH], steamauth[50];
    
GetClientAuthId(clientAuthId_Steam2steamauthsizeof(steamauth));
    
Format(buffersizeof(buffer), "%d"ctban[client]);
    
kv.JumpToKey(steamauthtrue);
    
kv.SetString("ctban_time"buffer);
    
    
int length StringToInt(buffer);
    if (
length == 0)
        
kv.DeleteThis();
    
    
kv.Rewind();
    
BuildPath(Path_SMbuffersizeof(buffer), "configs/ctban_database.cfg");
    
kv.ExportToFile(buffer);
}

GetCTBanData(int client)
{
    
char sConfig[PLATFORM_MAX_PATH], sUserID[50];
    
GetClientAuthId(clientAuthId_Steam2sUserIDsizeof(sUserID));
    
BuildPath(Path_SMsConfigsizeof(sConfig), "configs/ctban_database.cfg");

    
KeyValues hConfig = new KeyValues("ctban_database");
    
    if (
FileToKeyValues(hConfigsConfig))
    {
        if (
KvJumpToKey(hConfigsUserIDfalse))
        {
            
char sUserID2[50];
            
GetClientAuthId(clientAuthId_Steam2sUserID2sizeof(sUserID2));
            if(
StrEqual(sUserIDsUserID2))
            {
                
char sTimeLeft[20];
                
KvGetString(hConfig"ctban_time"sTimeLeftsizeof(sTimeLeft));
                
                if (!
sTimeLeft[0])
                    
ctban[client] = StringToInt(sTimeLeft);
                else
                    
ctban[client] = 0;
                
                if(
ctban[client] > 0)
                {
                    if(
ctbantimer[client] == null)
                        
ctbantimer[client] = CreateTimer(1.0Timer_CTBanclientTIMER_REPEAT TIMER_FLAG_NO_MAPCHANGE);
                }
                else
                {
                    
ctban[client] = 0;
                    
KillTimer(ctbantimer[client]);
                    
ctbantimer[client] = null;
                }
            }
            else
            {
                
ctban[client] = 0;
                
KillTimer(ctbantimer[client]);
                
ctbantimer[client] = null;
            }
        }
        else
        {
            
ctban[client] = 0;
            
KillTimer(ctbantimer[client]);
            
ctbantimer[client] = null;
        }
    }
    
    
CloseHandle(hConfig);
}

bool IsValidClient(int client
{
    if (!( 
<= client <= MaxClients ) || !IsClientInGame(client)) 
        return 
false
     
    return 
true

__________________

Last edited by potatoz; 08-19-2018 at 06:05.
potatoz is offline
Addicted.
AlliedModders Donor
Join Date: Dec 2013
Location: 0xA9D0DC
Old 08-16-2018 , 15:00   Re: [CS:GO] Help with keyvalue-based ctban plugin
Reply With Quote #2

Using baras plugin should be perfectly fine.

As long as you add the database to databases.cfg it will do all the work including making the tables

Maybe you can describe the issue you are having

Last edited by Addicted.; 08-16-2018 at 15:00.
Addicted. is offline
potatoz
AlliedModders Donor
Join Date: Nov 2016
Old 08-16-2018 , 15:13   Re: [CS:GO] Help with keyvalue-based ctban plugin
Reply With Quote #3

@Addicted i'm unsure as it is the server manager with the real issues but apparently the CT-Bans just don't stay correctly and either way they don't like that you need to be alive to work with most CT-Ban plugins. Teambans just flat-out doesn't load if i understand it correctly.

This is why i made, whilst sort of sloppy, my own ct-ban plugin but of course there are major issues in the way people are randomly getting invalid bans but my coding skill is not all that good in this area.
__________________

Last edited by potatoz; 08-16-2018 at 15:40.
potatoz is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 08-16-2018 , 15:58   Re: [CS:GO] Help with keyvalue-based ctban plugin
Reply With Quote #4

I noticed that you're getting the same client's Steam ID twice and comparing them to each other. If the kv.JumpToKey() already finds the client's Steam ID, you don't need to check for a match inside it.

Also, since you're checking for empty strings, change !sTimeLeft[0] to sTimeLeft[0] != '\0' because !sTimeLeft[0] looks more like you're checking sTimeLeft[0] == '\0' which is only triggering when the string is EMPTY.

Basically, this:

PHP Code:
// what you currently have...
if (sTimeLeft[0] == '\0'// if string is empty (!sTimeLeft[0])
{
     
ctban[client] = StringToInt(sTimeLeft); // converts the empty string to an int which is basically nothing...
}
else 
// if string is NOT empty
{
     
ctban[client] = 0// ban duration is set to 0.

TL;DR: You may have your checks reversed.
__________________

Last edited by Psyk0tik; 08-16-2018 at 15:59.
Psyk0tik is offline
potatoz
AlliedModders Donor
Join Date: Nov 2016
Old 08-16-2018 , 16:14   Re: [CS:GO] Help with keyvalue-based ctban plugin
Reply With Quote #5

@Crasher_3637 Thanks a bunch for your perspective, totally got blinded to that simple mistake, not 100% sure on empty string checking but i will send a new version of the plugin with double steamid-check and fixed string-check as seen below with my understanding of your text.

One thing i'm wondering about though is, the code shouldn't go past "if (KvJumpToKey(hConfig, sUserID, false))" since people are only added in the file if they've had a CT-Ban given to them in function "UpdateCTBanData", either way, i'm hoping for the best!

PHP Code:
if (sTimeLeft[0] != '\0')
    
ctban[client] = StringToInt(sTimeLeft);
else
    
ctban[client] = 0
__________________

Last edited by potatoz; 08-16-2018 at 16:23.
potatoz is offline
potatoz
AlliedModders Donor
Join Date: Nov 2016
Old 08-17-2018 , 13:47   Re: [CS:GO] Help with keyvalue-based ctban plugin
Reply With Quote #6

EDIT: Same issue so still open for help :7
__________________
potatoz is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 08-17-2018 , 15:39   Re: [CS:GO] Help with keyvalue-based ctban plugin
Reply With Quote #7

SQL Would be a better option for your case.
mug1wara is offline
potatoz
AlliedModders Donor
Join Date: Nov 2016
Old 08-17-2018 , 18:16   Re: [CS:GO] Help with keyvalue-based ctban plugin
Reply With Quote #8

Quote:
Originally Posted by mug1wara View Post
SQL Would be a better option for your case.
Would love to make it work with SQLite instead of keyvalues, however i don't know anything of how to work with it nor do i have enough of a motivation to learn all of it. Understandingly it would work better though
__________________

Last edited by potatoz; 08-17-2018 at 18:17.
potatoz is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 08-17-2018 , 18:19   Re: [CS:GO] Help with keyvalue-based ctban plugin
Reply With Quote #9

Show your updated code.
__________________
Psyk0tik is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 08-17-2018 , 18:29   Re: [CS:GO] Help with keyvalue-based ctban plugin
Reply With Quote #10

(OnClientPostAdminCheck)
PHP Code:
CreateTimer(1.0Timer_GetCTBanDataTIMER_FLAG_NO_MAPCHANGE); 
You pass TIMER_FLAG_NO_MAPCHANGE as param for client.

SQL version (not tested):
PHP Code:
#include <sourcemod>

#pragma semicolon 1

ConVar g_cvDatabaseName;

Database g_hDatabase;

char g_sSteamID[MAXPLAYERS 1][32];

int g_iTime[MAXPLAYERS 1];

public 
void OnPluginStart()
{
    
g_cvDatabaseName CreateConVar("sm_ctbans_database_name""CTBans");
    
    
RegConsoleCmd("sm_ctban"Cmd_CTBan);
    
    
HookEvent("player_team"Event_Team);
}

public 
void OnConfigsExecuted()
{
    
char sDatabaseName[256];
    
GetConVarString(g_cvDatabaseNamesDatabaseNamesizeof(sDatabaseName));
    
    
Database.Connect(SQL_ConnectionsDatabaseName);
}

public 
void SQL_Connection(Database hDatabase, const char[] sErrorint iData)
{
    if (
hDatabase == null)
        
ThrowError(sError);
    
    else
    {
        
g_hDatabase hDatabase;
        
        
g_hDatabase.Query(SQL_Error"CREATE TABLE IF NOT EXISTS CTBans (steam_id VARCHAR(32) NOT NULL, time INT(11) NOT NULL)");
    }
}

public 
void SQL_Error(Database hDatabaseDBResultSet hResults, const char[] sErrorint iData)
{
    if (
hResults == null)
        
ThrowError(sError);
}

public 
void OnClientPostAdminCheck(int iClient)
{
    if (
IsValidClient(iClient))
    {
        
GetClientAuthId(iClientAuthId_Steam2g_sSteamID[iClient], sizeof(g_sSteamID[]));
        
        
char sQuery[256];
        
Format(sQuerysizeof(sQuery), "SELECT * FROM CTBans WHERE steam_id = \"%s\""g_sSteamID[iClient]);
        
        
g_hDatabase.Query(SQL_AuthorizationsQueryGetClientUserId(iClient));
    }
}

public 
void SQL_Authorization(Database hDatabaseDBResultSet hResults, const char[] sErrorint iData)
{
    if (
hResults == null)
        
ThrowError(sError);
    
    
int iClient GetClientOfUserId(iClient);
    
    if (
IsValidClient(iClient))
    {
        if (
hResults.RowCount != 0)
        {
            
hResults.FetchRow();
            
            
g_iTime[iClient] = hResults.FetchInt(1);
            
            if (
g_iTime[iClient] != 0)
                
CreateTimer(1.0Timer_CountDownGetClientUserId(iClient), TIMER_REPEAT TIMER_FLAG_NO_MAPCHANGE);
        }
        
        else
        {
            
char sQuery[256];
            
Format(sQuerysizeof(sQuery), "INSERT INTO CTBans (steam_id, time) VALUES (\"%s\", '%d')"g_sSteamID[iClient], g_iTime[iClient]);
            
            
g_hDatabase.Query(SQL_ErrorsQuery);
        }
    }
}

public 
Action Timer_CountDown(Handle hTimerint iClient)
{
    if (
IsValidClient(iClient))
        if (
g_iTime[iClient] != 0)
            
g_iTime[iClient]--;
}

public 
Action Cmd_CTBan(int iClientint iArgs)
{
    if (
IsValidClient(iClient))
    {
        if (
iArgs != 2)
        {
            
ReplyToCommand(iClient"[SM] Usage: sm_ctban <player> <time>");
            
            return 
Plugin_Handled;
        }
        
        
char sTarget[MAX_NAME_LENGTH], sTime[32];
        
        
GetCmdArg(1sTargetsizeof(sTarget));
        
GetCmdArg(2sTimesizeof(sTime));
        
        
int iTarget FindTarget(iClientsTarget);
        
        
g_iTime[iTarget] = StringToInt(sTime);
        
        
char sQuery[256];
        
Format(sQuerysizeof(sQuery), "UPDATE CTBans SET time = '%d' WHERE steam_id = \"%s\""g_iTime[iTarget], g_sSteamID[iTarget]);
        
        
g_hDatabase.Query(SQL_ErrorsQuery);
    }
    
    return 
Plugin_Continue;
}

public 
Action Event_Team(Event hEvent, const char[] sNamebool bDontBroadcast)
{
    
int iClient GetClientOfUserId(hEvent.GetInt("userid"));
    
    if (
IsValidClient(iClient))
    {
        if (
g_iTime[iClient] != 0)
        {
            
int iTeam hEvent.GetInt("team");
            
            if (
iTeam == 3)
            {
                
ChangeClientTeam(iClient2);
                
                
PrintToChat(iClient"[SM] You're currently CTBanned.");
                
                return 
Plugin_Handled;
            }
        }
    }
    
    return 
Plugin_Continue;
}

public 
void OnClientDisconnect(int iClient)
{
    if (
IsValidClient(iClient))
    {
        
char sQuery[256];
        
Format(sQuerysizeof(sQuery), "UPDATE CTBans SET time = '%d' WHERE steam_id = \"%s\""g_iTime[iClient], g_sSteamID[iClient]);
        
        
g_hDatabase.Query(SQL_ErrorsQuery);
    }
}

public 
bool IsValidClient(int iClient)
{
    if (!(
iClient <= MaxClients) || !IsClientInGame(iClient) || IsFakeClient(iClient))
        return 
false;
    
    return 
true;

EDIT:

player_team event callback;

If the client is banned and changes team to ct, he'll be swapped to t, which means that the client would be respawned.

Remove that line and just return Plugin_Handled.

Last edited by mug1wara; 08-17-2018 at 19:26.
mug1wara is offline
Reply


Thread Tools
Display Modes

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 20:55.


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