sm-plugins/StatGroups/scripting/StatGroups.sp
zaCade 7f3ad69afa StatGroups: Grant SM groups to Top10, 20, 40.
Made for CSGO, altho can run anywhere.
2018-07-23 17:19:36 +02:00

221 lines
6.6 KiB
SourcePawn

#pragma newdecls required
#include <sourcemod>
#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, ...};
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
name = "StatGroups",
author = "zaCade (Original by Jenz)",
description = "Grant sourcemod groups based on HLStats ranks",
version = "1.2"
};
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnRebuildAdminCache(AdminCachePart part)
{
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;
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
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])
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);
}
}
//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock int RetrieveStatRank(int client)
{
char sError[512];
char sQuery[512];
if (!SQL_CheckConfig("stats"))
{
LogError("Could not find database config \"stats\".");
return -1;
}
Database database;
if ((database = SQL_Connect("stats", true, sError, sizeof(sError))) == null)
{
LogError("Connection error: \"%s\".", sError);
delete database;
return -1;
}
char sSteamID[32];
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);
DBResultSet results;
if ((results = SQL_Query(database, sQuery)) == null)
{
delete database;
delete results;
return -1;
}
if (!results.FetchRow())
{
delete database;
delete results;
return -1;
}
int rank;
if ((rank = results.FetchInt(0)) == 0)
{
delete database;
delete results;
return -1;
}
delete database;
delete results;
return rank;
}