plugins/reservedslots.sp
author Scott Ehlert <ds@alliedmods.net>
Sat Jul 12 04:08:41 2008 +0000 (2008-07-12)
changeset 2200 41f2dfdb67f2
parent 2133 cad0a5ea6742
child 2289 44045ead61cd
permissions -rw-r--r--
Fixed Windows build
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;
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@1952
    66
ds@1952
    67
public OnPluginStart()
ds@1952
    68
{
ds@1952
    69
	LoadTranslations("reservedslots.phrases");
ds@1952
    70
	
ds@1952
    71
	sm_reserved_slots = CreateConVar("sm_reserved_slots", "0", "Number of reserved player slots", 0, true, 0.0);
ds@1952
    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@1952
    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);
ds@1952
    77
	
ds@1952
    78
	HookConVarChange(sm_reserved_slots, SlotsChanged);
ds@1952
    79
	HookConVarChange(sm_hide_slots, SlotsChanged);
ds@1952
    80
}
ds@1952
    81
ds@1952
    82
public OnPluginEnd()
ds@1952
    83
{
ds@1952
    84
	/* 	If the plugin has been unloaded, reset visiblemaxplayers. In the case of the server shutting down this effect will not be visible */
ds@1952
    85
	SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
ds@1952
    86
}
ds@1952
    87
ds@1952
    88
public OnMapStart()
ds@1952
    89
{
ds@1952
    90
	g_MaxClients = GetMaxClients();
ds@1952
    91
	
ds@1952
    92
	if (GetConVarBool(sm_hide_slots))
ds@1952
    93
	{		
ds@1952
    94
		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@1952
    95
	}
ds@1952
    96
}
ds@1952
    97
ds@1952
    98
public OnConfigsExecuted()
ds@1952
    99
{
ds@1952
   100
	if (GetConVarBool(sm_hide_slots))
ds@1952
   101
	{
ds@1952
   102
		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@1952
   103
	}	
ds@1952
   104
}
ds@1952
   105
ds@1952
   106
public Action:OnTimedKick(Handle:timer, any:client)
ds@1952
   107
{	
ds@1952
   108
	if (!client || !IsClientInGame(client))
ds@1952
   109
	{
ds@1952
   110
		return Plugin_Handled;
ds@1952
   111
	}
ds@1952
   112
	
ds@1952
   113
	KickClient(client, "%T", "Slot reserved", client);
ds@1952
   114
	
ds@1952
   115
	if (GetConVarBool(sm_hide_slots))
ds@1952
   116
	{				
ds@1952
   117
		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@1952
   118
	}
ds@1952
   119
	
ds@1952
   120
	return Plugin_Handled;
ds@1952
   121
}
ds@1952
   122
ds@1952
   123
public OnClientPostAdminCheck(client)
ds@1952
   124
{
ds@1952
   125
	new reserved = GetConVarInt(sm_reserved_slots);
ds@1952
   126
ds@1952
   127
	if (reserved > 0)
ds@1952
   128
	{
ds@1952
   129
		new clients = GetClientCount(false);
ds@1952
   130
		new limit = g_MaxClients - reserved;
ds@1952
   131
		new flags = GetUserFlagBits(client);
ds@1952
   132
		
ds@1952
   133
		new type = GetConVarInt(sm_reserve_type);
ds@1952
   134
		
ds@1952
   135
		if (type == 0)
ds@1952
   136
		{
ds@1952
   137
			if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
ds@1952
   138
			{
ds@1952
   139
				if (GetConVarBool(sm_hide_slots))
ds@1952
   140
				{
ds@1952
   141
					SetVisibleMaxSlots(clients, limit);
ds@1952
   142
				}
ds@1952
   143
				
ds@1952
   144
				return;
ds@1952
   145
			}
ds@1952
   146
			
ds@1952
   147
			/* Kick player because there are no public slots left */
ds@1952
   148
			CreateTimer(0.1, OnTimedKick, client);
ds@1952
   149
		}
pred@2006
   150
		else if (type == 1)
pred@2006
   151
		{	
ds@1952
   152
			if (clients > limit)
pred@2133
   153
			{
ds@1952
   154
				if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
ds@1952
   155
				{
ds@1952
   156
					new target = SelectKickClient();
ds@1952
   157
						
ds@1952
   158
					if (target)
ds@1952
   159
					{
ds@1952
   160
						/* Kick public player to free the reserved slot again */
ds@1952
   161
						CreateTimer(0.1, OnTimedKick, target);
ds@1952
   162
					}
ds@1952
   163
				}
ds@1952
   164
				else
ds@1952
   165
				{				
ds@1952
   166
					/* Kick player because there are no public slots left */
ds@1952
   167
					CreateTimer(0.1, OnTimedKick, client);
ds@1952
   168
				}
ds@1952
   169
			}
ds@1952
   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@1952
   200
	}
ds@1952
   201
}
ds@1952
   202
ds@1952
   203
public OnClientDisconnect_Post(client)
ds@1952
   204
{
ds@1952
   205
	if (GetConVarBool(sm_hide_slots))
ds@1952
   206
	{		
ds@1952
   207
		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@1952
   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@1952
   215
}
ds@1952
   216
ds@1952
   217
public SlotsChanged(Handle:convar, const String:oldValue[], const String:newValue[])
ds@1952
   218
{
ds@1952
   219
	/* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
ds@1952
   220
	if (StringToInt(newValue) == 0)
ds@1952
   221
	{
ds@1952
   222
		SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
ds@1952
   223
	}
ds@1952
   224
}
ds@1952
   225
ds@1952
   226
SetVisibleMaxSlots(clients, limit)
ds@1952
   227
{
ds@1952
   228
	new num = clients;
ds@1952
   229
	
ds@1952
   230
	if (clients == g_MaxClients)
ds@1952
   231
	{
ds@1952
   232
		num = g_MaxClients;
ds@1952
   233
	} else if (clients < limit) {
ds@1952
   234
		num = limit;
ds@1952
   235
	}
ds@1952
   236
	
ds@1952
   237
	SetConVarInt(sv_visiblemaxplayers, num);
ds@1952
   238
}
ds@1952
   239
ds@1952
   240
SelectKickClient()
ds@1952
   241
{
pred@2022
   242
	new KickType:type = KickType:GetConVarInt(sm_reserve_kicktype);
ds@1952
   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;
ds@1952
   249
	
ds@1952
   250
	new bool:specFound;
ds@1952
   251
	
pred@2022
   252
	new Float:value;
ds@1952
   253
	
ds@1952
   254
	for (new i=1; i<=g_MaxClients; i++)
ds@1952
   255
	{	
ds@1952
   256
		if (!IsClientConnected(i))
ds@1952
   257
		{
ds@1952
   258
			continue;
ds@1952
   259
		}
ds@1952
   260
	
ds@1952
   261
		new flags = GetUserFlagBits(i);
ds@1952
   262
		
ds@1952
   263
		if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
ds@1952
   264
		{
ds@1952
   265
			continue;
ds@1952
   266
		}
ds@1952
   267
		
pred@2022
   268
		value = 0.0;
ds@1952
   269
			
ds@1952
   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
ds@1952
   285
			if (IsClientObserver(i))
ds@1952
   286
			{			
ds@1952
   287
				specFound = true;
ds@1952
   288
				
pred@2022
   289
				if (value > highestSpecValue)
ds@1952
   290
				{
pred@2022
   291
					highestSpecValue = value;
pred@2022
   292
					highestSpecValueId = i;
ds@1952
   293
				}
ds@1952
   294
			}
ds@1952
   295
		}
ds@1952
   296
		
pred@2022
   297
		if (value >= highestValue)
ds@1952
   298
		{
pred@2022
   299
			highestValue = value;
pred@2022
   300
			highestValueId = i;
ds@1952
   301
		}
ds@1952
   302
	}
ds@1952
   303
	
ds@1952
   304
	if (specFound)
ds@1952
   305
	{
pred@2022
   306
		return highestSpecValueId;
ds@1952
   307
	}
ds@1952
   308
	
pred@2022
   309
	return highestValueId;
pred@2022
   310
}