plugins/reservedslots.sp
author Nicholas Hastings <skamonkey@gmail.com>
Thu Feb 27 09:53:57 2014 -0500 (2014-02-27)
changeset 4255 dbfcf51478ee
parent 4102 0eb66c45a884
permissions -rw-r--r--
Remove TextMsg params padding on Dota.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Reserved Slots Plugin
     5  * Provides basic reserved slots.
     6  *
     7  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    10  * This program is free software; you can redistribute it and/or modify it under
    11  * the terms of the GNU General Public License, version 3.0, as published by the
    12  * Free Software Foundation.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but WITHOUT
    15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    17  * details.
    18  *
    19  * You should have received a copy of the GNU General Public License along with
    20  * this program.  If not, see <http://www.gnu.org/licenses/>.
    21  *
    22  * As a special exception, AlliedModders LLC gives you permission to link the
    23  * code of this program (as well as its derivative works) to "Half-Life 2," the
    24  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    25  * by the Valve Corporation.  You must obey the GNU General Public License in
    26  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    27  * this exception to all derivative works.  AlliedModders LLC defines further
    28  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    29  * or <http://www.sourcemod.net/license.php>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #pragma semicolon 1
    35 
    36 #include <sourcemod>
    37 
    38 public Plugin:myinfo = 
    39 {
    40 	name = "Reserved Slots",
    41 	author = "AlliedModders LLC",
    42 	description = "Provides basic reserved slots",
    43 	version = SOURCEMOD_VERSION,
    44 	url = "http://www.sourcemod.net/"
    45 };
    46 
    47 new g_adminCount = 0;
    48 new bool:g_isAdmin[MAXPLAYERS+1];
    49 
    50 /* Handles to convars used by plugin */
    51 new Handle:sm_reserved_slots;
    52 new Handle:sm_hide_slots;
    53 new Handle:sv_visiblemaxplayers;
    54 new Handle:sm_reserve_type;
    55 new Handle:sm_reserve_maxadmins;
    56 new Handle:sm_reserve_kicktype;
    57 
    58 enum KickType
    59 {
    60 	Kick_HighestPing,
    61 	Kick_HighestTime,
    62 	Kick_Random,	
    63 };
    64 
    65 public OnPluginStart()
    66 {
    67 	LoadTranslations("reservedslots.phrases");
    68 	
    69 	sm_reserved_slots = CreateConVar("sm_reserved_slots", "0", "Number of reserved player slots", 0, true, 0.0);
    70 	sm_hide_slots = CreateConVar("sm_hide_slots", "0", "If set to 1, reserved slots will hidden (subtracted from the max slot count)", 0, true, 0.0, true, 1.0);
    71 	sv_visiblemaxplayers = FindConVar("sv_visiblemaxplayers");
    72 	sm_reserve_type = CreateConVar("sm_reserve_type", "0", "Method of reserving slots", 0, true, 0.0, true, 2.0);
    73 	sm_reserve_maxadmins = CreateConVar("sm_reserve_maxadmins", "1", "Maximum amount of admins to let in the server with reserve type 2", 0, true, 0.0);
    74 	sm_reserve_kicktype = CreateConVar("sm_reserve_kicktype", "0", "How to select a client to kick (if appropriate)", 0, true, 0.0, true, 2.0);
    75 	
    76 	HookConVarChange(sm_reserved_slots, SlotCountChanged);
    77 	HookConVarChange(sm_hide_slots, SlotHideChanged);
    78 }
    79 
    80 public OnPluginEnd()
    81 {
    82 	/* 	If the plugin has been unloaded, reset visiblemaxplayers. In the case of the server shutting down this effect will not be visible */
    83 	ResetVisibleMax();
    84 }
    85 
    86 public OnMapStart()
    87 {
    88 	if (GetConVarBool(sm_hide_slots))
    89 	{		
    90 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
    91 	}
    92 }
    93 
    94 public OnConfigsExecuted()
    95 {
    96 	if (GetConVarBool(sm_hide_slots))
    97 	{
    98 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
    99 	}	
   100 }
   101 
   102 public Action:OnTimedKick(Handle:timer, any:client)
   103 {	
   104 	if (!client || !IsClientInGame(client))
   105 	{
   106 		return Plugin_Handled;
   107 	}
   108 	
   109 	KickClient(client, "%T", "Slot reserved", client);
   110 	
   111 	if (GetConVarBool(sm_hide_slots))
   112 	{				
   113 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
   114 	}
   115 	
   116 	return Plugin_Handled;
   117 }
   118 
   119 public OnClientPostAdminCheck(client)
   120 {
   121 	new reserved = GetConVarInt(sm_reserved_slots);
   122 
   123 	if (reserved > 0)
   124 	{
   125 		new clients = GetClientCount(false);
   126 		new limit = GetMaxHumanPlayers() - reserved;
   127 		new flags = GetUserFlagBits(client);
   128 		
   129 		new type = GetConVarInt(sm_reserve_type);
   130 		
   131 		if (type == 0)
   132 		{
   133 			if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
   134 			{
   135 				if (GetConVarBool(sm_hide_slots))
   136 				{
   137 					SetVisibleMaxSlots(clients, limit);
   138 				}
   139 				
   140 				return;
   141 			}
   142 			
   143 			/* Kick player because there are no public slots left */
   144 			CreateTimer(0.1, OnTimedKick, client);
   145 		}
   146 		else if (type == 1)
   147 		{	
   148 			if (clients > limit)
   149 			{
   150 				if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
   151 				{
   152 					new target = SelectKickClient();
   153 						
   154 					if (target)
   155 					{
   156 						/* Kick public player to free the reserved slot again */
   157 						CreateTimer(0.1, OnTimedKick, target);
   158 					}
   159 				}
   160 				else
   161 				{				
   162 					/* Kick player because there are no public slots left */
   163 					CreateTimer(0.1, OnTimedKick, client);
   164 				}
   165 			}
   166 		}
   167 		else if (type == 2)
   168 		{
   169 			if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
   170 			{
   171 				g_adminCount++;
   172 				g_isAdmin[client] = true;
   173 			}
   174 			
   175 			if (clients > limit && g_adminCount < GetConVarInt(sm_reserve_maxadmins))
   176 			{
   177 				/* Server is full, reserved slots aren't and client doesn't have reserved slots access */
   178 				
   179 				if (g_isAdmin[client])
   180 				{
   181 					new target = SelectKickClient();
   182 						
   183 					if (target)
   184 					{
   185 						/* Kick public player to free the reserved slot again */
   186 						CreateTimer(0.1, OnTimedKick, target);
   187 					}
   188 				}
   189 				else
   190 				{				
   191 					/* Kick player because there are no public slots left */
   192 					CreateTimer(0.1, OnTimedKick, client);
   193 				}		
   194 			}
   195 		}
   196 	}
   197 }
   198 
   199 public OnClientDisconnect_Post(client)
   200 {
   201 	if (GetConVarBool(sm_hide_slots))
   202 	{		
   203 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
   204 	}
   205 	
   206 	if (g_isAdmin[client])
   207 	{
   208 		g_adminCount--;
   209 		g_isAdmin[client] = false;	
   210 	}
   211 }
   212 
   213 public SlotCountChanged(Handle:convar, const String:oldValue[], const String:newValue[])
   214 {
   215 	/* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
   216 	new slotcount = GetConVarInt(convar);
   217 	if (slotcount == 0)
   218 	{
   219 		ResetVisibleMax();
   220 	}
   221 	else if (GetConVarBool(sm_hide_slots))
   222 	{
   223 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - slotcount);
   224 	}
   225 }
   226 
   227 public SlotHideChanged(Handle:convar, const String:oldValue[], const String:newValue[])
   228 {
   229 	/* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
   230 	if (!GetConVarBool(convar))
   231 	{
   232 		ResetVisibleMax();
   233 	}
   234 	else
   235 	{
   236 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
   237 	}
   238 }
   239 
   240 SetVisibleMaxSlots(clients, limit)
   241 {
   242 	new num = clients;
   243 	
   244 	if (clients == GetMaxHumanPlayers())
   245 	{
   246 		num = GetMaxHumanPlayers();
   247 	} else if (clients < limit) {
   248 		num = limit;
   249 	}
   250 	
   251 	SetConVarInt(sv_visiblemaxplayers, num);
   252 }
   253 
   254 ResetVisibleMax()
   255 {
   256 	SetConVarInt(sv_visiblemaxplayers, -1);
   257 }
   258 
   259 SelectKickClient()
   260 {
   261 	new KickType:type = KickType:GetConVarInt(sm_reserve_kicktype);
   262 	
   263 	new Float:highestValue;
   264 	new highestValueId;
   265 	
   266 	new Float:highestSpecValue;
   267 	new highestSpecValueId;
   268 	
   269 	new bool:specFound;
   270 	
   271 	new Float:value;
   272 	
   273 	for (new i=1; i<=MaxClients; i++)
   274 	{	
   275 		if (!IsClientConnected(i))
   276 		{
   277 			continue;
   278 		}
   279 	
   280 		new flags = GetUserFlagBits(i);
   281 		
   282 		if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
   283 		{
   284 			continue;
   285 		}
   286 		
   287 		value = 0.0;
   288 			
   289 		if (IsClientInGame(i))
   290 		{
   291 			if (type == Kick_HighestPing)
   292 			{
   293 				value = GetClientAvgLatency(i, NetFlow_Outgoing);
   294 			}
   295 			else if (type == Kick_HighestTime)
   296 			{
   297 				value = GetClientTime(i);
   298 			}
   299 			else
   300 			{
   301 				value = GetRandomFloat(0.0, 100.0);
   302 			}
   303 
   304 			if (IsClientObserver(i))
   305 			{			
   306 				specFound = true;
   307 				
   308 				if (value > highestSpecValue)
   309 				{
   310 					highestSpecValue = value;
   311 					highestSpecValueId = i;
   312 				}
   313 			}
   314 		}
   315 		
   316 		if (value >= highestValue)
   317 		{
   318 			highestValue = value;
   319 			highestValueId = i;
   320 		}
   321 	}
   322 	
   323 	if (specFound)
   324 	{
   325 		return highestSpecValueId;
   326 	}
   327 	
   328 	return highestValueId;
   329 }