sm-plugins/NoAdminTools/scripting/NoAdminTools.sp
2019-06-15 23:29:56 +02:00

237 lines
5.7 KiB
SourcePawn

#pragma semicolon 1
#include <sourcemod>
#include <cstrike>
#include <AFKManager>
#include <mapchooser_extended>
int g_iAdminAFKTime;
int g_iSelfMaxExtendsAmount;
int g_iSelfExtendsTime;
float g_fSelfExtendsRatio;
float g_fSelfExtendsDelay;
bool g_bSelfExtends[MAXPLAYERS + 1] = { false, ...};
bool g_bSelfExtendsAllowed;
int g_iSelfExtends;
ConVar g_cvarTimeLimit;
public Plugin myinfo =
{
name = "No Admin Tools",
author = "Dogan",
description = "Make it possible for the server to do several things when there is no active admin online",
version = "1.0.0",
url = ""
};
public void OnPluginStart()
{
g_iSelfExtends = 0;
g_bSelfExtendsAllowed = false;
RegAdminCmd("sm_checkadmins", Command_DisplayActiveAdmins, ADMFLAG_GENERIC, "Check if there are any active Admins online or not.");
RegConsoleCmd("sm_selfextend", Command_SelfExtend, "Is available when all regular extends are depleted and there is no active admin online. Makes it possible for players to extend the map themselves");
ConVar cvar;
HookConVarChange((cvar = CreateConVar("sm_admin_afk_time", "30", "Time in seconds until an admin is considered as AFK.")), Cvar_AdminAFKTime);
g_iAdminAFKTime = cvar.IntValue;
HookConVarChange((cvar = CreateConVar("sm_selfextend_amount", "2", "Amount of sm_selfextend's allowed.")), Cvar_SelfExtendsAmount);
g_iSelfMaxExtendsAmount = cvar.IntValue;
HookConVarChange((cvar = CreateConVar("sm_selfextend_time", "10", "How long to extend in minutes when sm_selfextend passes through.")), Cvar_SelfExtendsTime);
g_iSelfExtendsTime = cvar.IntValue;
HookConVarChange((cvar = CreateConVar("sm_selfextend_ratio", "0.6", "Ratio needed for sm_selfextend to pass through.")), Cvar_SelfExtendsRatio);
g_fSelfExtendsRatio = cvar.FloatValue;
HookConVarChange((cvar = CreateConVar("sm_selfextend_delay", "60.0", "Time to pass until sm_selfextend can be used")), Cvar_SelfExtendsDelay);
g_fSelfExtendsDelay = cvar.FloatValue;
CloseHandle(cvar);
g_cvarTimeLimit = FindConVar("mp_timelimit");
AutoExecConfig(true, "plugin.NoAdminTools");
}
public void Cvar_AdminAFKTime(ConVar convar, const char[] oldValue, const char[] newValue)
{
g_iAdminAFKTime = convar.IntValue;
}
public void Cvar_SelfExtendsAmount(ConVar convar, const char[] oldValue, const char[] newValue)
{
g_iSelfMaxExtendsAmount = convar.IntValue;
}
public void Cvar_SelfExtendsTime(ConVar convar, const char[] oldValue, const char[] newValue)
{
g_iSelfExtendsTime = convar.IntValue;
}
public void Cvar_SelfExtendsRatio(ConVar convar, const char[] oldValue, const char[] newValue)
{
g_fSelfExtendsRatio = convar.FloatValue;
}
public void Cvar_SelfExtendsDelay(ConVar convar, const char[] oldValue, const char[] newValue)
{
g_fSelfExtendsDelay = convar.FloatValue;
}
public bool ActiveAdminPresent()
{
for(int i = 1; i <= MaxClients; i++)
{
if(IsValidClient(i) && CheckCommandAccess(i, "", ADMFLAG_GENERIC) && (GetClientIdleTime(i) < g_iAdminAFKTime))
{
return true;
}
}
return false;
}
public void OnClientDisconnect(int client)
{
if(g_bSelfExtends[client])
g_bSelfExtends[client] = false;
CheckRatio();
}
public void OnClientConnected(int client)
{
CheckRatio();
}
public void OnMapStart()
{
g_iSelfExtends = 0;
g_bSelfExtendsAllowed = false;
CreateTimer(g_fSelfExtendsDelay, Timer_DelaySelfExtend, _, TIMER_FLAG_NO_MAPCHANGE);
for(int i; i <= MaxClients; i++)
{
if(g_bSelfExtends[i])
g_bSelfExtends[i] = false;
}
}
public void OnMapEnd()
{
g_bSelfExtendsAllowed = false;
}
public Action Timer_DelaySelfExtend(Handle timer)
{
g_bSelfExtendsAllowed = true;
}
public Action Command_DisplayActiveAdmins(int client, int args)
{
if(ActiveAdminPresent())
{
ReplyToCommand(client, "[SM] There are active Admins online.");
}
else
{
ReplyToCommand(client, "[SM] There are no active Admins online.");
}
return Plugin_Handled;
}
public Action Command_SelfExtend(int client, int args)
{
if(g_iSelfMaxExtendsAmount <= g_iSelfExtends)
{
ReplyToCommand(client, "[SM] Not available because this map was already self-extended %d times.", g_iSelfMaxExtendsAmount);
return Plugin_Handled;
}
else if(GetExtendsLeft() > 0)
{
ReplyToCommand(client, "[SM] Not available because not all regular extends have been depleted.");
return Plugin_Handled;
}
else if(ActiveAdminPresent())
{
ReplyToCommand(client, "[SM] Not available because there is atleast one active Admin who can extend. Please ask the Admins.");
return Plugin_Handled;
}
else if(!g_bSelfExtendsAllowed)
{
ReplyToCommand(client, "[SM] Not available because it's still too early in the map.");
return Plugin_Handled;
}
else
{
char sName[32];
GetClientName(client, sName, sizeof(sName));
if(!g_bSelfExtends[client])
{
g_bSelfExtends[client] = true;
PrintToChatAll("[SM] %s wants to self-extend the map.", sName);
}
else
{
ReplyToCommand(client, "[SM] You have already voted to self-extend the map.");
}
CheckRatio();
}
return Plugin_Handled;
}
public Action CheckRatio()
{
if(!g_bSelfExtendsAllowed)
return Plugin_Handled;
float Players;
float SelfExtendsPlayers;
float PlayersNeeded;
for(int i = 1; i <= MaxClients; i++)
{
if(IsValidClient(i))
{
Players++;
}
if(g_bSelfExtends[i])
{
SelfExtendsPlayers++;
}
}
PlayersNeeded = Players * g_fSelfExtendsRatio;
if(SelfExtendsPlayers >= PlayersNeeded)
{
PrintToChatAll("[SM] Enough Players voted to self-extend the map. Adding %d minutes to the timelimit.", g_iSelfExtendsTime);
g_iSelfExtends++;
g_cvarTimeLimit.IntValue += g_iSelfExtendsTime;
for(int j; j <= MaxClients; j++)
{
if(g_bSelfExtends[j])
g_bSelfExtends[j] = false;
}
}
return Plugin_Handled;
}
static stock bool IsValidClient(int client)
{
if (client > 0 && client <= MaxClients && IsClientInGame(client))
{
return true;
}
else
{
return false;
}
}