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