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