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