diff --git a/AdminGroups/scripting/AdminGroups.sp b/AdminGroups/scripting/AdminGroups.sp new file mode 100644 index 00000000..ff3c58e1 --- /dev/null +++ b/AdminGroups/scripting/AdminGroups.sp @@ -0,0 +1,320 @@ +#pragma newdecls required + +#include +#include + +/* ARRAYS */ +ArrayList G_hArray_AdminGroups; +ArrayList G_hArray_ClientGroups[MAXPLAYERS+1]; + +/* BOOLEANS */ +bool G_bClientPostAdminFilter[MAXPLAYERS+1]; + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public Plugin myinfo = +{ + name = "AdminGroups", + author = "zaCade", + description = "Manage custom sourcemod admin groups with plugins", + version = "1.0" +}; + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int errorSize) +{ + CreateNative("AdminGroups_CreateAdminGroup", Native_CreateAdminGroup); + CreateNative("AdminGroups_DeleteAdminGroup", Native_DeleteAdminGroup); + + CreateNative("AdminGroups_GrantAdminGroup", Native_GrantAdminGroup); + CreateNative("AdminGroups_RevokeAdminGroup", Native_RevokeAdminGroup); + + RegPluginLibrary("AdminGroups"); + return APLRes_Success; +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnPluginStart() +{ + G_hArray_AdminGroups = new ArrayList(128); + + for (int client = 1; client <= MaxClients; client++) + { + G_hArray_ClientGroups[client] = new ArrayList(128); + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnRebuildAdminCache(AdminCachePart part) +{ + switch(part) + { + case(AdminCache_Groups): + { + CreateAdminGroups(); + } + case(AdminCache_Admins): + { + CreateTimer(1.0, OnRebuildAdminCachePost, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE); + } + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public Action OnRebuildAdminCachePost(Handle hTimer) +{ + for (int client = 1; client <= MaxClients; client++) + { + GrantAdminGroups(client); + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnClientConnected(int client) +{ + G_bClientPostAdminFilter[client] = false; + + G_hArray_ClientGroups[client].Clear(); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnClientDisconnect(int client) +{ + G_bClientPostAdminFilter[client] = false; + + G_hArray_ClientGroups[client].Clear(); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public void OnClientPostAdminFilter(int client) +{ + G_bClientPostAdminFilter[client] = true; + + GrantAdminGroups(client); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: Create all groups (Wrapper) +//---------------------------------------------------------------------------------------------------- +stock void CreateAdminGroups() +{ + if (G_hArray_AdminGroups.Length) + { + for (int adminGroupIndex; adminGroupIndex < G_hArray_AdminGroups.Length; adminGroupIndex++) + { + char group[128]; + G_hArray_AdminGroups.GetString(adminGroupIndex, group, sizeof(group)); + + CreateAdminGroup(group); + } + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: Create a specific group +//---------------------------------------------------------------------------------------------------- +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: Grant all groups (Wrapper) +//---------------------------------------------------------------------------------------------------- +stock void GrantAdminGroups(int client) +{ + if (!G_bClientPostAdminFilter[client]) + return; + + if (G_hArray_ClientGroups[client].Length) + { + for (int clientGroupIndex; clientGroupIndex < G_hArray_ClientGroups[client].Length; clientGroupIndex++) + { + char group[128]; + G_hArray_ClientGroups[client].GetString(clientGroupIndex, group, sizeof(group)); + + GrantAdminGroup(client, group); + } + } +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: Grant a specific group +//---------------------------------------------------------------------------------------------------- +stock void GrantAdminGroup(int client, const char[] group) +{ + if (!G_bClientPostAdminFilter[client]) + return; + + 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_CreateAdminGroup(Handle hPlugin, int numParams) +{ + char group[128]; + GetNativeString(1, group, sizeof(group)); + + int adminGroupIndex; + if ((adminGroupIndex = G_hArray_AdminGroups.FindString(group)) != -1) + { + ThrowNativeError(SP_ERROR_NATIVE, "Group already exists. (%s) [%d]", group, adminGroupIndex); + return; + } + + G_hArray_AdminGroups.PushString(group); + + SortADTArray(G_hArray_AdminGroups, Sort_Ascending, Sort_String); + + CreateAdminGroup(group); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int Native_DeleteAdminGroup(Handle hPlugin, int numParams) +{ + char group[128]; + GetNativeString(1, group, sizeof(group)); + + int adminGroupIndex; + if ((adminGroupIndex = G_hArray_AdminGroups.FindString(group)) == -1) + { + ThrowNativeError(SP_ERROR_NATIVE, "Group doesnt exist. (%s) [%d]", group, adminGroupIndex); + return; + } + + G_hArray_AdminGroups.Erase(adminGroupIndex); + + SortADTArray(G_hArray_AdminGroups, Sort_Ascending, Sort_String); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int Native_GrantAdminGroup(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; + } + + char group[128]; + GetNativeString(2, group, sizeof(group)); + + int adminGroupIndex + if ((adminGroupIndex = G_hArray_AdminGroups.FindString(group)) == -1) + { + ThrowNativeError(SP_ERROR_NATIVE, "Group doesnt exist. (%s) [%d]", group, adminGroupIndex); + return; + } + + int clientGroupIndex; + if ((clientGroupIndex = G_hArray_ClientGroups[client].FindString(group)) != -1) + { + ThrowNativeError(SP_ERROR_NATIVE, "Client already has group. (%s) [%d]", group, clientGroupIndex); + return; + } + + G_hArray_ClientGroups[client].PushString(group); + + SortADTArray(G_hArray_ClientGroups[client], Sort_Ascending, Sort_String); + + GrantAdminGroup(client, group); +} + +//---------------------------------------------------------------------------------------------------- +// Purpose: +//---------------------------------------------------------------------------------------------------- +public int Native_RevokeAdminGroup(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; + } + + char group[128]; + GetNativeString(2, group, sizeof(group)); + + int adminGroupIndex + if ((adminGroupIndex = G_hArray_AdminGroups.FindString(group)) == -1) + { + ThrowNativeError(SP_ERROR_NATIVE, "Group doesnt exist. (%s) [%d]", group, adminGroupIndex); + return; + } + + int clientGroupIndex; + if ((clientGroupIndex = G_hArray_ClientGroups[client].FindString(group)) == -1) + { + ThrowNativeError(SP_ERROR_NATIVE, "Client doesnt have group. (%s) [%d]", group, clientGroupIndex); + return; + } + + G_hArray_ClientGroups[client].Erase(clientGroupIndex); + + SortADTArray(G_hArray_ClientGroups[client], Sort_Ascending, Sort_String); +} diff --git a/StatGroups/scripting/StatGroups.sp b/StatGroups/scripting/StatGroups.sp index 1cc67d76..888bbc3a 100644 --- a/StatGroups/scripting/StatGroups.sp +++ b/StatGroups/scripting/StatGroups.sp @@ -1,16 +1,9 @@ #pragma newdecls required #include +#include -#define GAMEID "csgo-ze" - -/* BOOLS */ -bool G_bPreAdminChecked[MAXPLAYERS+1]; -bool G_bResponseFailed[MAXPLAYERS+1]; -bool G_bResponsePassed[MAXPLAYERS+1]; - -/* INTERGERS */ -int iRank[MAXPLAYERS+1] = {-1, ...}; +#define GameID "csgo-ze" //---------------------------------------------------------------------------------------------------- // Purpose: @@ -26,59 +19,11 @@ public Plugin myinfo = //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- -public void OnRebuildAdminCache(AdminCachePart part) +public void OnAllPluginsLoaded() { - if (part == AdminCache_Overrides) - return; - - if (part == AdminCache_Groups) - { - CreateAdminGroup("Ranking-Top10"); - CreateAdminGroup("Ranking-Top20"); - CreateAdminGroup("Ranking-Top40"); - } - - CreateTimer(1.0, OnRebuildAdminCachePost, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE); -} - -//---------------------------------------------------------------------------------------------------- -// Purpose: -//---------------------------------------------------------------------------------------------------- -public Action OnRebuildAdminCachePost(Handle hTimer) -{ - for (int client = 1; client <= MaxClients; client++) - { - if (!G_bResponsePassed[client] || !G_bPreAdminChecked[client]) - continue; - - if (iRank[client] <= 10) ApplyAdminGroup(client, "Ranking-Top10"); - if (iRank[client] <= 20) ApplyAdminGroup(client, "Ranking-Top20"); - if (iRank[client] <= 40) ApplyAdminGroup(client, "Ranking-Top40"); - } -} - -//---------------------------------------------------------------------------------------------------- -// Purpose: -//---------------------------------------------------------------------------------------------------- -public void OnClientConnected(int client) -{ - G_bPreAdminChecked[client] = false; - G_bResponseFailed[client] = false; - G_bResponsePassed[client] = false; - - iRank[client] = -1; -} - -//---------------------------------------------------------------------------------------------------- -// Purpose: -//---------------------------------------------------------------------------------------------------- -public void OnClientDisconnect(int client) -{ - G_bPreAdminChecked[client] = false; - G_bResponseFailed[client] = false; - G_bResponsePassed[client] = false; - - iRank[client] = -1; + AdminGroups_CreateAdminGroup("Ranking-Top10"); + AdminGroups_CreateAdminGroup("Ranking-Top20"); + AdminGroups_CreateAdminGroup("Ranking-Top40"); } //---------------------------------------------------------------------------------------------------- @@ -86,82 +31,13 @@ public void OnClientDisconnect(int client) //---------------------------------------------------------------------------------------------------- public void OnClientAuthorized(int client, const char[] sAuthID) { - if ((iRank[client] = RetrieveStatRank(client)) != -1) - G_bResponsePassed[client] = true; - else - G_bResponseFailed[client] = true; -} - -//---------------------------------------------------------------------------------------------------- -// Purpose: -//---------------------------------------------------------------------------------------------------- -public Action OnClientPreAdminCheck(int client) -{ - G_bPreAdminChecked[client] = true; - - if (G_bResponsePassed[client] || G_bResponseFailed[client]) - return Plugin_Continue; - - RunAdminCacheChecks(client); - return Plugin_Handled; -} - -//---------------------------------------------------------------------------------------------------- -// Purpose: -//---------------------------------------------------------------------------------------------------- -public void OnClientPostAdminFilter(int client) -{ - if (!G_bResponsePassed[client]) + int rank; + if ((rank = RetrieveStatRank(client)) == -1) return; - if (iRank[client] <= 10) ApplyAdminGroup(client, "Ranking-Top10"); - if (iRank[client] <= 20) ApplyAdminGroup(client, "Ranking-Top20"); - if (iRank[client] <= 40) ApplyAdminGroup(client, "Ranking-Top40"); -} - -//---------------------------------------------------------------------------------------------------- -// 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); - } + if (rank <= 10) AdminGroups_GrantAdminGroup(client, "Ranking-Top10"); + if (rank <= 20) AdminGroups_GrantAdminGroup(client, "Ranking-Top20"); + if (rank <= 40) AdminGroups_GrantAdminGroup(client, "Ranking-Top40"); } //---------------------------------------------------------------------------------------------------- @@ -190,7 +66,7 @@ stock int RetrieveStatRank(int client) if (GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID))) strcopy(sSteamID, sizeof(sSteamID), sSteamID[8]); - SQL_FormatQuery(database, sQuery, sizeof(sQuery), "SELECT COUNT(*) AS rank FROM hlstats_Players WHERE hlstats_Players.game = '%s' AND hideranking = 0 AND skill > (SELECT skill FROM hlstats_Players JOIN hlstats_PlayerUniqueIds ON hlstats_Players.playerId = hlstats_PlayerUniqueIds.playerId WHERE hlstats_PlayerUniqueIds.game = '%s' AND uniqueId = '%s')", GAMEID, GAMEID, sSteamID); + SQL_FormatQuery(database, sQuery, sizeof(sQuery), "SELECT COUNT(*) AS rank FROM hlstats_Players WHERE hlstats_Players.game = '%s' AND hideranking = 0 AND skill > (SELECT skill FROM hlstats_Players JOIN hlstats_PlayerUniqueIds ON hlstats_Players.playerId = hlstats_PlayerUniqueIds.playerId WHERE hlstats_PlayerUniqueIds.game = '%s' AND uniqueId = '%s')", GameID, GameID, sSteamID); DBResultSet results; if ((results = SQL_Query(database, sQuery)) == null) diff --git a/includes/admingroups.inc b/includes/admingroups.inc new file mode 100644 index 00000000..4092cf75 --- /dev/null +++ b/includes/admingroups.inc @@ -0,0 +1,34 @@ +#if defined _admingroups_included_ + #endinput +#endif +#define _admingroups_included_ + +/** + * Create a admin group. + * + * @param group Name of the group to create. + */ +native void AdminGroups_CreateAdminGroup(const char[] group); + +/** + * Delete a admin group. + * + * @param group Name of the group to delete. + */ +native void AdminGroups_DeleteAdminGroup(const char[] group); + +/** + * Grant a admin group. + * + * @param client Client to grant to. + * @param group Name of the group to grant. + */ +native void AdminGroups_GrantAdminGroup(int client, const char[] group); + +/** + * Revoke a admin group. + * + * @param client Client to revoke from. + * @param group Name of the group to revoke. + */ +native void AdminGroups_RevokeAdminGroup(int client, const char[] group);