[CS:GO] Help with keyvalue-based ctban plugin
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_CTBan, ADMFLAG_BAN);
RegAdminCmd("sm_ctunban", Command_CTUnBan, ADMFLAG_BAN);
RegAdminCmd("sm_isbanned", Command_IsBanned, ADMFLAG_BAN);
HookEvent("player_team", Event_PlayerTeam);
AddCommandListener(Event_OnJoinTeam, "jointeam");
char buffer[PLATFORM_MAX_PATH];
BuildPath(Path_SM, buffer, sizeof(buffer), "configs/ctban_database.cfg");
kv = new KeyValues("ctban_database");
kv.ImportFromFile(buffer);
}
public Action Event_PlayerTeam(Handle event, const char[] name, bool dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
if(IsValidClient(client))
CreateTimer(0.2, CheckTeam, client, TIMER_FLAG_NO_MAPCHANGE);
}
public Action CheckTeam(Handle timer, int client)
{
if(IsValidClient(client) && GetClientTeam(client) == 3 && 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(client, 2);
}
return Plugin_Handled;
}
public Action Event_OnJoinTeam(int client, const char[] command, int args)
{
if (!IsValidClient(client) || IsFakeClient(client))
return Plugin_Continue;
char teamString[2];
GetCmdArg(1, teamString, sizeof(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 == 3 && 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.0, Timer_GetCTBanData, TIMER_FLAG_NO_MAPCHANGE);
}
public Action Timer_GetCTBanData(Handle timer, int client)
{
GetCTBanData(client);
if(ctban[client] > 0)
ctbantimer[client] = CreateTimer(1.0, Timer_CTBan, client, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
return Plugin_Handled;
}
public Action Command_CTBan(int client, int 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(1, arg1, sizeof(arg1));
GetCmdArg(2, arg2, sizeof(arg2));
GetCmdArg(3, arg3, sizeof(arg3));
int minutes = StringToInt(arg2);
int target = FindTarget(client, arg1);
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.0, Timer_CTBan, target, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
UpdateCTBanData(target);
if(GetClientTeam(target) == 3)
{
ChangeClientTeam(target, 2);
ForcePlayerSuicide(target);
}
PrintToChatAll(" \x07*\x01 ADMIN: \x07%N\x01 CT-Banned \x07%N \x01for %s minute(s) with reason: \x07%s", client, target, arg2, arg3);
return Plugin_Handled;
}
public Action Command_CTUnBan(int client, int args)
{
if(args < 1)
{
ReplyToCommand(client, "[SM] Usage: sm_ctunban <name|#userid>");
return Plugin_Handled;
}
char arg1[32];
GetCmdArg(1, arg1, sizeof(arg1));
int target = FindTarget(client, arg1);
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", client, target);
UpdateCTBanData(target);
return Plugin_Handled;
}
public Action Timer_CTBan(Handle timer, int 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 client, int args)
{
if(args < 1)
{
ReplyToCommand(client, "[SM] Usage: sm_isbanned <name|#userid>");
return Plugin_Handled;
}
char arg1[32];
GetCmdArg(1, arg1, sizeof(arg1));
int target = FindTarget(client, arg1);
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)", target, minutes);
return Plugin_Handled;
}
UpdateCTBanData(int client)
{
char buffer[PLATFORM_MAX_PATH], steamauth[50];
GetClientAuthId(client, AuthId_Steam2, steamauth, sizeof(steamauth));
Format(buffer, sizeof(buffer), "%d", ctban[client]);
kv.JumpToKey(steamauth, true);
kv.SetString("ctban_time", buffer);
int length = StringToInt(buffer);
if (length == 0)
kv.DeleteThis();
kv.Rewind();
BuildPath(Path_SM, buffer, sizeof(buffer), "configs/ctban_database.cfg");
kv.ExportToFile(buffer);
}
GetCTBanData(int client)
{
char sConfig[PLATFORM_MAX_PATH], sUserID[50];
GetClientAuthId(client, AuthId_Steam2, sUserID, sizeof(sUserID));
BuildPath(Path_SM, sConfig, sizeof(sConfig), "configs/ctban_database.cfg");
KeyValues hConfig = new KeyValues("ctban_database");
if (FileToKeyValues(hConfig, sConfig))
{
if (KvJumpToKey(hConfig, sUserID, false))
{
char sUserID2[50];
GetClientAuthId(client, AuthId_Steam2, sUserID2, sizeof(sUserID2));
if(StrEqual(sUserID, sUserID2))
{
char sTimeLeft[20];
KvGetString(hConfig, "ctban_time", sTimeLeft, sizeof(sTimeLeft));
if (!sTimeLeft[0])
ctban[client] = StringToInt(sTimeLeft);
else
ctban[client] = 0;
if(ctban[client] > 0)
{
if(ctbantimer[client] == null)
ctbantimer[client] = CreateTimer(1.0, Timer_CTBan, client, TIMER_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 (!( 1 <= client <= MaxClients ) || !IsClientInGame(client))
return false;
return true;
}
|