From bb82d7d8ccf7789715d261f78256f53e861ff24b Mon Sep 17 00:00:00 2001 From: zaCade Date: Mon, 23 Jul 2018 19:42:09 +0200 Subject: [PATCH] TokenGroups: Grant sourcemod groups. --- TokenGroups/scripting/TokenGroups.sp | 244 ++++++++++++++++++ TokenGroups/scripting/include/tokengroups.inc | 19 ++ 2 files changed, 263 insertions(+) create mode 100644 TokenGroups/scripting/TokenGroups.sp create mode 100644 TokenGroups/scripting/include/tokengroups.inc diff --git a/TokenGroups/scripting/TokenGroups.sp b/TokenGroups/scripting/TokenGroups.sp new file mode 100644 index 00000000..9d4d5199 --- /dev/null +++ b/TokenGroups/scripting/TokenGroups.sp @@ -0,0 +1,244 @@ +#pragma newdecls required + +#include +#include + +#define MaxTokens 9 + +/* BOOLEANS */ +bool g_bHasToken[MAXPLAYERS+1][MaxTokens]; +bool g_bPostFilter[MAXPLAYERS+1]; + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public Plugin myinfo = +{ + name = "TokenGroups", + author = "zaCade", + description = "Grant sourcemod groups", + version = "1.0" +}; + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int errorSize) +{ + CreateNative("CheckTokens", Native_CheckTokens); + CreateNative("GrantTokens", Native_GrantTokens); + + RegPluginLibrary("TokenGroups"); + return APLRes_Success; +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnRebuildAdminCache(AdminCachePart part) +{ + if (part == AdminCache_Overrides) + return; + + if (part == AdminCache_Groups) + { + CreateAdminGroup("Token-1"); + CreateAdminGroup("Token-1-VIP"); + CreateAdminGroup("Token-2"); + CreateAdminGroup("Token-2-VIP"); + CreateAdminGroup("Token-3"); + CreateAdminGroup("Token-3-VIP"); + CreateAdminGroup("Token-4"); + CreateAdminGroup("Token-4-VIP"); + CreateAdminGroup("Token-5"); + CreateAdminGroup("Token-5-VIP"); + CreateAdminGroup("Token-6"); + CreateAdminGroup("Token-6-VIP"); + CreateAdminGroup("Token-7"); + CreateAdminGroup("Token-7-VIP"); + CreateAdminGroup("Token-8"); + CreateAdminGroup("Token-8-VIP"); + CreateAdminGroup("Token-9"); + CreateAdminGroup("Token-9-VIP"); + } + + CreateTimer(1.0, OnRebuildAdminCachePost, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public Action OnRebuildAdminCachePost(Handle hTimer) +{ + for (int client = 1; client <= MaxClients; client++) + CheckClientTokens(client); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnClientConnected(int client) +{ + ResetClient(client); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnClientDisconnect(int client) +{ + ResetClient(client); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnClientPostAdminFilter(int client) +{ + g_bPostFilter[client] = true; + + CheckClientTokens(client); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +stock void ResetClient(int client) +{ + for (int token = 0; token < MaxTokens; token++) + g_bHasToken[client][token] = false; + + g_bPostFilter[client] = false; +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +stock void CheckClientTokens(int client) +{ + if (!g_bPostFilter[client]) + return; + + if (CheckCommandAccess(client, "sm_tokens_vip", ADMFLAG_CUSTOM1)) + { + if (g_bHasToken[client][0]) ApplyAdminGroup(client, "Token-1-VIP"); + if (g_bHasToken[client][1]) ApplyAdminGroup(client, "Token-2-VIP"); + if (g_bHasToken[client][2]) ApplyAdminGroup(client, "Token-3-VIP"); + if (g_bHasToken[client][3]) ApplyAdminGroup(client, "Token-4-VIP"); + if (g_bHasToken[client][4]) ApplyAdminGroup(client, "Token-5-VIP"); + if (g_bHasToken[client][5]) ApplyAdminGroup(client, "Token-6-VIP"); + if (g_bHasToken[client][6]) ApplyAdminGroup(client, "Token-7-VIP"); + if (g_bHasToken[client][7]) ApplyAdminGroup(client, "Token-8-VIP"); + if (g_bHasToken[client][8]) ApplyAdminGroup(client, "Token-9-VIP"); + } + else + { + if (g_bHasToken[client][0]) ApplyAdminGroup(client, "Token-1"); + if (g_bHasToken[client][1]) ApplyAdminGroup(client, "Token-2"); + if (g_bHasToken[client][2]) ApplyAdminGroup(client, "Token-3"); + if (g_bHasToken[client][3]) ApplyAdminGroup(client, "Token-4"); + if (g_bHasToken[client][4]) ApplyAdminGroup(client, "Token-5"); + if (g_bHasToken[client][5]) ApplyAdminGroup(client, "Token-6"); + if (g_bHasToken[client][6]) ApplyAdminGroup(client, "Token-7"); + if (g_bHasToken[client][7]) ApplyAdminGroup(client, "Token-8"); + if (g_bHasToken[client][8]) ApplyAdminGroup(client, "Token-9"); + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +stock void CreateAdminGroup(const char[] group) +{ + GroupId GrpID; + + if ((GrpID = FindAdmGroup(group)) == INVALID_GROUP_ID) + { + LogMessage("Creating new admin group %s", group); + + GrpID = CreateAdmGroup(group); + GrpID.ImmunityLevel = 0; + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +stock void ApplyAdminGroup(int client, const char[] group) +{ + AdminId AdmID; + GroupId GrpID; + + if ((AdmID = GetUserAdmin(client)) == INVALID_ADMIN_ID) + { + LogMessage("Creating new admin for %L", client); + + AdmID = CreateAdmin(); + SetUserAdmin(client, AdmID, true); + } + + if ((GrpID = FindAdmGroup(group)) != INVALID_GROUP_ID) + { + if (AdminInheritGroup(AdmID, GrpID)) + { + LogMessage("%L added to group %s", client, group); + } + } + else + { + LogMessage("%L group not found %s", client, group); + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int Native_CheckTokens(Handle hPlugin, int numParams) +{ + int client = GetNativeCell(1); + + if (client < 1 || client > MaxClients) + { + ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index. (%d)", client); + return; + } + + if (!IsClientConnected(client)) + { + ThrowNativeError(SP_ERROR_NATIVE, "Client (%d) is not connected.", client); + return; + } + + CheckClientTokens(client); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int Native_GrantTokens(Handle hPlugin, int numParams) +{ + int client = GetNativeCell(1); + int token = GetNativeCell(2); + + if (client < 1 || client > MaxClients) + { + ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index. (%d)", client); + return; + } + + if (token < 1 || token > MaxTokens) + { + ThrowNativeError(SP_ERROR_NATIVE, "Invalid token index. (%d)", token); + return; + } + + if (!IsClientConnected(client)) + { + ThrowNativeError(SP_ERROR_NATIVE, "Client (%d) is not connected.", client); + return; + } + + g_bHasToken[client][token -= 1] = true; + + CheckClientTokens(client); +} \ No newline at end of file diff --git a/TokenGroups/scripting/include/tokengroups.inc b/TokenGroups/scripting/include/tokengroups.inc new file mode 100644 index 00000000..6d28ff3e --- /dev/null +++ b/TokenGroups/scripting/include/tokengroups.inc @@ -0,0 +1,19 @@ +#if defined _tokengroups_included_ + #endinput +#endif +#define _tokengroups_included_ + +/** + * Check tokens + * + * @param client Client index + */ +native void CheckTokens(int client); + +/** + * Grant a token + * + * @param client Client index + * @param token Token index + */ +native void GrantTokens(int client, int token);