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