Skip to content

Commit

Permalink
V2.0
Browse files Browse the repository at this point in the history
1. Implement managing user flags
2. Cleanup code
  • Loading branch information
Kyli3Boi committed May 25, 2021
1 parent cbfa37f commit df3b5e3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 55 deletions.
Binary file modified addons/sourcemod/plugins/SurfTimer_AutoVip.smx
Binary file not shown.
138 changes: 83 additions & 55 deletions addons/sourcemod/scripting/SurfTimer_AutoVip.sp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public Plugin myinfo =
name = "SurfTimer_AutoVip",
author = "Kyli3_Boi",
description = "Automatically manage VIP for highest ranked players. For use on a surf server running SurfTimer",
version = "1.0",
version = "2.0",
url = "https://github.com/Kyli3Boi"
};

Expand All @@ -27,6 +27,7 @@ ConVar g_hAutoVipTitleLevel2 = null; // Title to give VIPs for VIP level 2
ConVar g_hAutoVipTitleLevel3 = null; // Title to give VIPs for VIP level 3
ConVar g_hAutoVipRemoveType = null; // On VIP removal do you want to just disable player or delete player
ConVar g_hAutoVipEnableDebug = null; // Enable/Disable debug messages
ConVar g_hAutoVipAdminFlag = null; // Flag to give temp admins

// Array Handles //
Handle g_szSteamID = null;
Expand All @@ -37,6 +38,9 @@ Handle g_szAutoVipList = null;
// Bool
bool g_bAutoVipDebug = false; // Bool for debug messages

// Int
int g_iAdminFlag;

// SQL //
Handle g_hDb = null; // Database Handle

Expand All @@ -50,7 +54,7 @@ char sql_InsertVip[] = "INSERT INTO ck_vipadmins (steamid, title, namecolour, te
char sql_UpdateVip[] = "UPDATE ck_vipadmins SET inuse = 1, vip = %i, active = 1 WHERE steamid = '%s'";
char sql_DisableVip[] = "UPDATE ck_vipadmins SET inuse = 0, vip = 0, active = 0 WHERE steamid = '%s'";
char sql_DeleteVip[] = "DELETE FROM ck_vipadmins where steam id = '%s'";
char sql_CreateTable[] = "CREATE TABLE IF NOT EXISTS `autovip` (`steamid` varchar(32) NOT NULL DEFAULT '', PRIMARY KEY (`steamid`)) DEFAULT CHARSET=utf8mb4;";
char sql_CreateAutoVipTable[] = "CREATE TABLE IF NOT EXISTS `autovip` (`steamid` varchar(32) NOT NULL DEFAULT '', PRIMARY KEY (`steamid`)) DEFAULT CHARSET=utf8mb4;";
char sql_TruncateAutoVip[] = "TRUNCATE TABLE autovip";
char sql_SaveTopPlayer[] = "INSERT INTO autovip (steamid) VALUES ('%s')";
char sql_GetAutoVipList[] = "SELECT steamid FROM autovip";
Expand All @@ -59,47 +63,88 @@ char sql_GetAutoVipList[] = "SELECT steamid FROM autovip";

public void OnPluginStart()
{
CreateConVars();
//Create ConVars
AutoExecConfig_SetCreateDirectory(true);
AutoExecConfig_SetCreateFile(true);
AutoExecConfig_SetFile("surftimer_AutoVip");

g_hAutoVipEnabled = AutoExecConfig_CreateConVar("sm_autovip_enabled", "1", "Give VIP to top players 0 - Disabled | 1 - Enabled", FCVAR_NOTIFY);
g_hAutoVipNumber = AutoExecConfig_CreateConVar("sm_autovip_number", "1", "Number of top players that should recieve VIP", FCVAR_NOTIFY, true, 1.0, true, 20.0);
g_hAutoVipLevel = AutoExecConfig_CreateConVar("sm_autovip_viplevel", "1", "The level of VIP top players should recieve");
g_hAutoVipTitleLevel1 = AutoExecConfig_CreateConVar("sm_autovip_title1", "[{lime}VIP{default}]", "Title to give when using VIP level 1");
g_hAutoVipTitleLevel2 = AutoExecConfig_CreateConVar("sm_autovip_title2", "[{pink}Super VIP{default}]", "Title to give when using VIP level 2");
g_hAutoVipTitleLevel3 = AutoExecConfig_CreateConVar("sm_autovip_title3", "[{darkred}Superior VIP{default}]", "Title to give when using VIP level 3");
g_hAutoVipRemoveType = AutoExecConfig_CreateConVar("sm_autovip_removetype", "0", "The way in which to remove VIP from a player 0 - Disable player VIP | 1 - Delete player from VIP table in database");
g_hAutoVipEnableDebug = AutoExecConfig_CreateConVar("sm_autovip_debug", "0", "Print debug messages to server 0 - Disabled | 1 - Enabled");
g_hAutoVipAdminFlag = AutoExecConfig_CreateConVar("sm_autovip_adminflag", "a", "The admin flag to give to VIPs (surftimer default \"a\")");

AutoExecConfig_ExecuteFile();
AutoExecConfig_CleanFile();

// Connect to DB
db_Connect();

// Set debug bool
g_bAutoVipDebug = GetConVarBool(g_hAutoVipEnableDebug);

// Make sure VIP flag is valid
char szAdminFlag[24];
AdminFlag bufferAdminFlag;
bool adminFlagValid;

GetConVarString(g_hAutoVipAdminFlag, szAdminFlag, sizeof(szAdminFlag));
adminFlagValid = FindFlagByChar(szAdminFlag[0], bufferAdminFlag);
if(!adminFlagValid)
{
PrintToServer("[surftimer_AutoVip] Invalid sm_autovip_adminflag, setting to default");
g_iAdminFlag = ADMFLAG_RESERVATION;
}
else
{
g_iAdminFlag = FlagToBit(bufferAdminFlag);
}
}

public void OnMapStart()
{
db_Connect();

// Create Arrays
g_szSteamID = CreateArray(32);
g_szName = CreateArray(64);
g_szCurrentVipList = CreateArray(32);
g_szAutoVipList = CreateArray(32);

g_bAutoVipDebug = GetConVarBool(g_hAutoVipEnableDebug);
db_GetTopPlayers();
}

public void OnMapEnd()
public void OnClientPostAdminFilter(int client)
{
CloseHandle(g_szSteamID);
CloseHandle(g_szName);
CloseHandle(g_szCurrentVipList);
CloneHandle(g_szAutoVipList);
CloseHandle(g_hDb);
if (!IsFakeClient(client))
{
CreateTempVip(client);
}
}

public void CreateConVars()
public void CreateTempVip(int client)
{
AutoExecConfig_SetCreateDirectory(true);
AutoExecConfig_SetCreateFile(true);
AutoExecConfig_SetFile("surftimer_AutoVip");
if (g_bAutoVipDebug)
{
PrintToServer("[surftimer_AutoVip] Checking if player should be assigned VIP flag");
}

if (!IsFakeClient(client))
{
char szSteamId[32];
int index;

g_hAutoVipEnabled = AutoExecConfig_CreateConVar("sm_autovip_enabled", "1", "Give VIP to top players 0 - Disabled | 1 - Enabled", FCVAR_NOTIFY);
g_hAutoVipNumber = AutoExecConfig_CreateConVar("sm_autovip_number", "1", "Number of top players that should recieve VIP", FCVAR_NOTIFY, true, 1.0, true, 20.0);
g_hAutoVipLevel = AutoExecConfig_CreateConVar("sm_autovip_viplevel", "1", "The level of VIP top players should recieve");
g_hAutoVipTitleLevel1 = AutoExecConfig_CreateConVar("sm_autovip_title1", "[{lime}VIP{default}]", "Title to give when using VIP level 1");
g_hAutoVipTitleLevel2 = AutoExecConfig_CreateConVar("sm_autovip_title2", "[{pink}Super VIP{default}]", "Title to give when using VIP level 2");
g_hAutoVipTitleLevel3 = AutoExecConfig_CreateConVar("sm_autovip_title3", "[{darkred}Superior VIP{default}]", "Title to give when using VIP level 3");
g_hAutoVipRemoveType = AutoExecConfig_CreateConVar("sm_autovip_removetype", "0", "The way in which to remove VIP from a player 0 - Disable player VIP | 1 - Delete player from VIP table in database");
g_hAutoVipEnableDebug = AutoExecConfig_CreateConVar("sm_autovip_debug", "0", "Print debug messages to server 0 - Disabled | 1 - Enabled");
GetClientAuthId(client, AuthId_Steam2, szSteamId, sizeof(szSteamId), true);

index = FindStringInArray(g_szSteamID, szSteamId);

AutoExecConfig_ExecuteFile();
AutoExecConfig_CleanFile();
if (index != -1)
{
SetUserFlagBits(client, g_iAdminFlag);
}
}
}

public void db_Connect()
Expand All @@ -125,12 +170,7 @@ public void db_Connect()
}
else
{
if (g_bAutoVipDebug)
{
PrintToServer("[surftimer_AutoVip] is not enabled");
}

return;
SetFailState("[surftimer_AutoVip] is not enabled");
}
}

Expand All @@ -155,30 +195,17 @@ public void db_CheckTables()

if (!SQL_FastQuery(g_hDb, sql_CheckAutoVip))
{
db_CreateAutoVipTable();
SQL_FastQuery(g_hDb, sql_CreateAutoVipTable);
}
else
{
if (g_bAutoVipDebug)
{
PrintToServer("[surftimer_AutoVip] Required autovip DB table found");
}

CreateTimer(5.0, GetTopPlayers, _, TIMER_FLAG_NO_MAPCHANGE);
}
}

public void db_CreateAutoVipTable()
{
SQL_FastQuery(g_hDb, sql_CreateTable);
CreateTimer(5.0, GetTopPlayers, _, TIMER_FLAG_NO_MAPCHANGE);
}

public Action GetTopPlayers(Handle timer)
{
db_GetTopPlayers();
return Plugin_Handled;
}

public void db_GetTopPlayers()
{
Expand Down Expand Up @@ -242,11 +269,7 @@ public void db_GetTopPlayersCallback(Handle owner, Handle hndl, const char[] err

public void db_GetCurrentAutoVipPlayers()
{
char szQuery[128];

Format(szQuery, 128, sql_GetAutoVipList);
SQL_TQuery(g_hDb, db_GetCurrentAutoVipPlayersCallback, szQuery, DBPrio_Low);

SQL_TQuery(g_hDb, db_GetCurrentAutoVipPlayersCallback, sql_GetAutoVipList, DBPrio_Low);
}

public void db_GetCurrentAutoVipPlayersCallback(Handle owner, Handle hndl, const char[] error, any data)
Expand Down Expand Up @@ -319,10 +342,7 @@ public void db_SaveTopPlayers()

public void db_GetCurrentVipList()
{
char szQuery[128];

Format(szQuery, 128, sql_GetCurrentVipList);
SQL_TQuery(g_hDb, db_GetCurrentVipListCallback, szQuery, DBPrio_Low);
SQL_TQuery(g_hDb, db_GetCurrentVipListCallback, sql_GetCurrentVipList, DBPrio_Low);
}

public void db_GetCurrentVipListCallback(Handle owner, Handle hndl, const char[] error, any data)
Expand Down Expand Up @@ -465,4 +485,12 @@ public void db_RemoveVipCallback(Handle owner, Handle hndl, const char[] error,
LogError("[surftimer_AutoVip] SQL Error (db_RemoveVipCallback): %s", error);
return;
}
}

public void OnMapEnd()
{
CloseHandle(g_szSteamID);
CloseHandle(g_szName);
CloseHandle(g_szCurrentVipList);
CloneHandle(g_szAutoVipList);
}

0 comments on commit df3b5e3

Please sign in to comment.