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