plugins/reservedslots.sp
author Fyren <fyrenmoo@gmail.com>
Wed Apr 01 05:56:51 2009 -0700 (2009-04-01)
changeset 2692 126e2d41c2b3
parent 2289 44045ead61cd
child 2920 1d5645ab7ae0
permissions -rw-r--r--
Disable nextmap in Synergy and Dystopia (bug 3687, bug 3741; r=liam)
     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 /* Maximum number of clients that can connect to server */
    48 new g_MaxClients;
    49 new g_adminCount = 0;
    50 new bool:g_isAdmin[MAXPLAYERS+1];
    51 
    52 /* Handles to convars used by plugin */
    53 new Handle:sm_reserved_slots;
    54 new Handle:sm_hide_slots;
    55 new Handle:sv_visiblemaxplayers;
    56 new Handle:sm_reserve_type;
    57 new Handle:sm_reserve_maxadmins;
    58 new Handle:sm_reserve_kicktype;
    59 
    60 enum KickType
    61 {
    62 	Kick_HighestPing,
    63 	Kick_HighestTime,
    64 	Kick_Random,	
    65 };
    66 
    67 public OnPluginStart()
    68 {
    69 	LoadTranslations("reservedslots.phrases");
    70 	
    71 	sm_reserved_slots = CreateConVar("sm_reserved_slots", "0", "Number of reserved player slots", 0, true, 0.0);
    72 	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);
    73 	sv_visiblemaxplayers = FindConVar("sv_visiblemaxplayers");
    74 	sm_reserve_type = CreateConVar("sm_reserve_type", "0", "Method of reserving slots", 0, true, 0.0, true, 2.0);
    75 	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);
    76 	sm_reserve_kicktype = CreateConVar("sm_reserve_kicktype", "0", "How to select a client to kick (if appropriate)", 0, true, 0.0, true, 2.0);
    77 	
    78 	HookConVarChange(sm_reserved_slots, SlotsChanged);
    79 	HookConVarChange(sm_hide_slots, SlotsChanged);
    80 }
    81 
    82 public OnPluginEnd()
    83 {
    84 	/* 	If the plugin has been unloaded, reset visiblemaxplayers. In the case of the server shutting down this effect will not be visible */
    85 	SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
    86 }
    87 
    88 public OnMapStart()
    89 {
    90 	g_MaxClients = GetMaxClients();
    91 	
    92 	if (GetConVarBool(sm_hide_slots))
    93 	{		
    94 		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
    95 	}
    96 }
    97 
    98 public OnConfigsExecuted()
    99 {
   100 	if (GetConVarBool(sm_hide_slots))
   101 	{
   102 		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
   103 	}	
   104 }
   105 
   106 public Action:OnTimedKick(Handle:timer, any:client)
   107 {	
   108 	if (!client || !IsClientInGame(client))
   109 	{
   110 		return Plugin_Handled;
   111 	}
   112 	
   113 	KickClient(client, "%T", "Slot reserved", client);
   114 	
   115 	if (GetConVarBool(sm_hide_slots))
   116 	{				
   117 		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
   118 	}
   119 	
   120 	return Plugin_Handled;
   121 }
   122 
   123 public OnClientPostAdminCheck(client)
   124 {
   125 	new reserved = GetConVarInt(sm_reserved_slots);
   126 
   127 	if (reserved > 0)
   128 	{
   129 		new clients = GetClientCount(false);
   130 		new limit = g_MaxClients - reserved;
   131 		new flags = GetUserFlagBits(client);
   132 		
   133 		new type = GetConVarInt(sm_reserve_type);
   134 		
   135 		if (type == 0)
   136 		{
   137 			if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
   138 			{
   139 				if (GetConVarBool(sm_hide_slots))
   140 				{
   141 					SetVisibleMaxSlots(clients, limit);
   142 				}
   143 				
   144 				return;
   145 			}
   146 			
   147 			/* Kick player because there are no public slots left */
   148 			CreateTimer(0.1, OnTimedKick, client);
   149 		}
   150 		else if (type == 1)
   151 		{	
   152 			if (clients > limit)
   153 			{
   154 				if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
   155 				{
   156 					new target = SelectKickClient();
   157 						
   158 					if (target)
   159 					{
   160 						/* Kick public player to free the reserved slot again */
   161 						CreateTimer(0.1, OnTimedKick, target);
   162 					}
   163 				}
   164 				else
   165 				{				
   166 					/* Kick player because there are no public slots left */
   167 					CreateTimer(0.1, OnTimedKick, client);
   168 				}
   169 			}
   170 		}
   171 		else if (type == 2)
   172 		{
   173 			if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
   174 			{
   175 				g_adminCount++;
   176 				g_isAdmin[client] = true;
   177 			}
   178 			
   179 			if (clients > limit && g_adminCount < GetConVarInt(sm_reserve_maxadmins))
   180 			{
   181 				/* Server is full, reserved slots aren't and client doesn't have reserved slots access */
   182 				
   183 				if (g_isAdmin[client])
   184 				{
   185 					new target = SelectKickClient();
   186 						
   187 					if (target)
   188 					{
   189 						/* Kick public player to free the reserved slot again */
   190 						CreateTimer(0.1, OnTimedKick, target);
   191 					}
   192 				}
   193 				else
   194 				{				
   195 					/* Kick player because there are no public slots left */
   196 					CreateTimer(0.1, OnTimedKick, client);
   197 				}		
   198 			}
   199 		}
   200 	}
   201 }
   202 
   203 public OnClientDisconnect_Post(client)
   204 {
   205 	if (GetConVarBool(sm_hide_slots))
   206 	{		
   207 		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
   208 	}
   209 	
   210 	if (g_isAdmin[client])
   211 	{
   212 		g_adminCount--;
   213 		g_isAdmin[client] = false;	
   214 	}
   215 }
   216 
   217 public SlotsChanged(Handle:convar, const String:oldValue[], const String:newValue[])
   218 {
   219 	/* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
   220 	if (StringToInt(newValue) == 0)
   221 	{
   222 		SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
   223 	}
   224 }
   225 
   226 SetVisibleMaxSlots(clients, limit)
   227 {
   228 	new num = clients;
   229 	
   230 	if (clients == g_MaxClients)
   231 	{
   232 		num = g_MaxClients;
   233 	} else if (clients < limit) {
   234 		num = limit;
   235 	}
   236 	
   237 	SetConVarInt(sv_visiblemaxplayers, num);
   238 }
   239 
   240 SelectKickClient()
   241 {
   242 	new KickType:type = KickType:GetConVarInt(sm_reserve_kicktype);
   243 	
   244 	new Float:highestValue;
   245 	new highestValueId;
   246 	
   247 	new Float:highestSpecValue;
   248 	new highestSpecValueId;
   249 	
   250 	new bool:specFound;
   251 	
   252 	new Float:value;
   253 	
   254 	for (new i=1; i<=g_MaxClients; i++)
   255 	{	
   256 		if (!IsClientConnected(i))
   257 		{
   258 			continue;
   259 		}
   260 	
   261 		new flags = GetUserFlagBits(i);
   262 		
   263 		if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
   264 		{
   265 			continue;
   266 		}
   267 		
   268 		value = 0.0;
   269 			
   270 		if (IsClientInGame(i))
   271 		{
   272 			if (type == Kick_HighestPing)
   273 			{
   274 				value = GetClientAvgLatency(i, NetFlow_Outgoing);
   275 			}
   276 			else if (type == Kick_HighestTime)
   277 			{
   278 				value = GetClientTime(i);
   279 			}
   280 			else
   281 			{
   282 				value = GetRandomFloat(0.0, 100.0);
   283 			}
   284 
   285 			if (IsClientObserver(i))
   286 			{			
   287 				specFound = true;
   288 				
   289 				if (value > highestSpecValue)
   290 				{
   291 					highestSpecValue = value;
   292 					highestSpecValueId = i;
   293 				}
   294 			}
   295 		}
   296 		
   297 		if (value >= highestValue)
   298 		{
   299 			highestValue = value;
   300 			highestValueId = i;
   301 		}
   302 	}
   303 	
   304 	if (specFound)
   305 	{
   306 		return highestSpecValueId;
   307 	}
   308 	
   309 	return highestValueId;
   310 }