plugins/reservedslots.sp
author Nicholas Hastings <skamonkey@gmail.com>
Sun Jul 21 11:53:56 2013 -0400 (2013-07-21)
changeset 3961 0dc477c29792
parent 3697 735dc2e19807
child 4102 0eb66c45a884
permissions -rw-r--r--
Split TF2, DoD:S, HL2:DM, and ND to separate binaries (bug 5813, r=asherkin).
     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 new bool:g_BotsSubtractFromMax;
    59 new g_SourceTV = -1;
    60 new g_Replay = -1;
    61 
    62 enum KickType
    63 {
    64 	Kick_HighestPing,
    65 	Kick_HighestTime,
    66 	Kick_Random,	
    67 };
    68 
    69 public OnPluginStart()
    70 {
    71 	LoadTranslations("reservedslots.phrases");
    72 	
    73 	sm_reserved_slots = CreateConVar("sm_reserved_slots", "0", "Number of reserved player slots", 0, true, 0.0);
    74 	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);
    75 	sv_visiblemaxplayers = FindConVar("sv_visiblemaxplayers");
    76 	sm_reserve_type = CreateConVar("sm_reserve_type", "0", "Method of reserving slots", 0, true, 0.0, true, 2.0);
    77 	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);
    78 	sm_reserve_kicktype = CreateConVar("sm_reserve_kicktype", "0", "How to select a client to kick (if appropriate)", 0, true, 0.0, true, 2.0);
    79 	
    80 	HookConVarChange(sm_reserved_slots, SlotCountChanged);
    81 	HookConVarChange(sm_hide_slots, SlotHideChanged);
    82 	
    83 	new EngineVersion:engineVersion = GetEngineVersion();
    84 	g_BotsSubtractFromMax = engineVersion == Engine_TF2 || engineVersion == Engine_CSS || engineVersion == Engine_DODS || engineVersion == Engine_HL2DM;
    85 	
    86 	if (g_BotsSubtractFromMax)
    87 	{
    88 		for (new i = 1; i <= MaxClients; i++)
    89 		{
    90 			if (!IsClientConnected(i))
    91 				continue;
    92 			
    93 			if (IsClientSourceTV(i))
    94 			{
    95 				g_SourceTV = i;
    96 			}
    97 			else if (IsClientReplay(i))
    98 			{
    99 				g_Replay = i;
   100 			}
   101 		}
   102 	}
   103 }
   104 
   105 public OnPluginEnd()
   106 {
   107 	/* 	If the plugin has been unloaded, reset visiblemaxplayers. In the case of the server shutting down this effect will not be visible */
   108 	ResetVisibleMax();
   109 }
   110 
   111 public OnMapStart()
   112 {
   113 	if (GetConVarBool(sm_hide_slots))
   114 	{		
   115 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
   116 	}
   117 }
   118 
   119 public OnConfigsExecuted()
   120 {
   121 	if (GetConVarBool(sm_hide_slots))
   122 	{
   123 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
   124 	}	
   125 }
   126 
   127 public Action:OnTimedKick(Handle:timer, any:client)
   128 {	
   129 	if (!client || !IsClientInGame(client))
   130 	{
   131 		return Plugin_Handled;
   132 	}
   133 	
   134 	KickClient(client, "%T", "Slot reserved", client);
   135 	
   136 	if (GetConVarBool(sm_hide_slots))
   137 	{				
   138 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
   139 	}
   140 	
   141 	return Plugin_Handled;
   142 }
   143 
   144 public OnClientPostAdminCheck(client)
   145 {
   146 	if (g_BotsSubtractFromMax)
   147 	{
   148 		if (IsClientSourceTV(client))
   149 		{
   150 			g_SourceTV = client;
   151 		}
   152 		else if (IsClientReplay(client))
   153 		{
   154 			g_Replay = client;
   155 		}
   156 	}
   157 	
   158 	new reserved = GetConVarInt(sm_reserved_slots);
   159 
   160 	if (reserved > 0)
   161 	{
   162 		new clients = GetClientCount(false);
   163 		new limit = GetMaxHumanPlayers() - reserved;
   164 		new flags = GetUserFlagBits(client);
   165 		
   166 		new type = GetConVarInt(sm_reserve_type);
   167 		
   168 		if (type == 0)
   169 		{
   170 			if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
   171 			{
   172 				if (GetConVarBool(sm_hide_slots))
   173 				{
   174 					SetVisibleMaxSlots(clients, limit);
   175 				}
   176 				
   177 				return;
   178 			}
   179 			
   180 			/* Kick player because there are no public slots left */
   181 			CreateTimer(0.1, OnTimedKick, client);
   182 		}
   183 		else if (type == 1)
   184 		{	
   185 			if (clients > limit)
   186 			{
   187 				if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
   188 				{
   189 					new target = SelectKickClient();
   190 						
   191 					if (target)
   192 					{
   193 						/* Kick public player to free the reserved slot again */
   194 						CreateTimer(0.1, OnTimedKick, target);
   195 					}
   196 				}
   197 				else
   198 				{				
   199 					/* Kick player because there are no public slots left */
   200 					CreateTimer(0.1, OnTimedKick, client);
   201 				}
   202 			}
   203 		}
   204 		else if (type == 2)
   205 		{
   206 			if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
   207 			{
   208 				g_adminCount++;
   209 				g_isAdmin[client] = true;
   210 			}
   211 			
   212 			if (clients > limit && g_adminCount < GetConVarInt(sm_reserve_maxadmins))
   213 			{
   214 				/* Server is full, reserved slots aren't and client doesn't have reserved slots access */
   215 				
   216 				if (g_isAdmin[client])
   217 				{
   218 					new target = SelectKickClient();
   219 						
   220 					if (target)
   221 					{
   222 						/* Kick public player to free the reserved slot again */
   223 						CreateTimer(0.1, OnTimedKick, target);
   224 					}
   225 				}
   226 				else
   227 				{				
   228 					/* Kick player because there are no public slots left */
   229 					CreateTimer(0.1, OnTimedKick, client);
   230 				}		
   231 			}
   232 		}
   233 	}
   234 }
   235 
   236 public OnClientDisconnect_Post(client)
   237 {
   238 	if (g_BotsSubtractFromMax)
   239 	{
   240 		if (client == g_SourceTV)
   241 		{
   242 			g_SourceTV = -1;
   243 		}
   244 		else if (client == g_Replay)
   245 		{
   246 			g_Replay = -1;
   247 		}
   248 	}
   249 	
   250 	if (GetConVarBool(sm_hide_slots))
   251 	{		
   252 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
   253 	}
   254 	
   255 	if (g_isAdmin[client])
   256 	{
   257 		g_adminCount--;
   258 		g_isAdmin[client] = false;	
   259 	}
   260 }
   261 
   262 public SlotCountChanged(Handle:convar, const String:oldValue[], const String:newValue[])
   263 {
   264 	/* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
   265 	new slotcount = GetConVarInt(convar);
   266 	if (slotcount == 0)
   267 	{
   268 		ResetVisibleMax();
   269 	}
   270 	else if (GetConVarBool(sm_hide_slots))
   271 	{
   272 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - slotcount);
   273 	}
   274 }
   275 
   276 public SlotHideChanged(Handle:convar, const String:oldValue[], const String:newValue[])
   277 {
   278 	/* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
   279 	if (!GetConVarBool(convar))
   280 	{
   281 		ResetVisibleMax();
   282 	}
   283 	else
   284 	{
   285 		SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
   286 	}
   287 }
   288 
   289 SetVisibleMaxSlots(clients, limit)
   290 {
   291 	new num = clients;
   292 	
   293 	if (clients == GetMaxHumanPlayers())
   294 	{
   295 		num = GetMaxHumanPlayers();
   296 	} else if (clients < limit) {
   297 		num = limit;
   298 	}
   299 	
   300 	if (g_BotsSubtractFromMax)
   301 	{
   302 		if (g_SourceTV > -1)
   303 		{
   304 			--num;
   305 		}
   306 		
   307 		if (g_Replay > -1)
   308 		{
   309 			--num;
   310 		}
   311 	}
   312 	
   313 	SetConVarInt(sv_visiblemaxplayers, num);
   314 }
   315 
   316 ResetVisibleMax()
   317 {
   318 	SetConVarInt(sv_visiblemaxplayers, -1);
   319 }
   320 
   321 SelectKickClient()
   322 {
   323 	new KickType:type = KickType:GetConVarInt(sm_reserve_kicktype);
   324 	
   325 	new Float:highestValue;
   326 	new highestValueId;
   327 	
   328 	new Float:highestSpecValue;
   329 	new highestSpecValueId;
   330 	
   331 	new bool:specFound;
   332 	
   333 	new Float:value;
   334 	
   335 	for (new i=1; i<=MaxClients; i++)
   336 	{	
   337 		if (!IsClientConnected(i))
   338 		{
   339 			continue;
   340 		}
   341 	
   342 		new flags = GetUserFlagBits(i);
   343 		
   344 		if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
   345 		{
   346 			continue;
   347 		}
   348 		
   349 		value = 0.0;
   350 			
   351 		if (IsClientInGame(i))
   352 		{
   353 			if (type == Kick_HighestPing)
   354 			{
   355 				value = GetClientAvgLatency(i, NetFlow_Outgoing);
   356 			}
   357 			else if (type == Kick_HighestTime)
   358 			{
   359 				value = GetClientTime(i);
   360 			}
   361 			else
   362 			{
   363 				value = GetRandomFloat(0.0, 100.0);
   364 			}
   365 
   366 			if (IsClientObserver(i))
   367 			{			
   368 				specFound = true;
   369 				
   370 				if (value > highestSpecValue)
   371 				{
   372 					highestSpecValue = value;
   373 					highestSpecValueId = i;
   374 				}
   375 			}
   376 		}
   377 		
   378 		if (value >= highestValue)
   379 		{
   380 			highestValue = value;
   381 			highestValueId = i;
   382 		}
   383 	}
   384 	
   385 	if (specFound)
   386 	{
   387 		return highestSpecValueId;
   388 	}
   389 	
   390 	return highestValueId;
   391 }