plugins/reservedslots.sp
author David Anderson <dvander@alliedmods.net>
Wed Dec 12 22:45:23 2007 +0000 (2007-12-12)
changeset 1788 6b5331ae47c4
parent 1694 833909112af3
child 1947 e73a1e51833b
permissions -rw-r--r--
fixed for amb1279 from ferret
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@1330
     7
 * SourceMod (C)2004-2007 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
ds@988
    47
/* Maximum number of clients that can connect to server */
ds@988
    48
new g_MaxClients;
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;
ds@988
    55
ds@988
    56
public OnPluginStart()
ds@988
    57
{
ferret99gt@1187
    58
	LoadTranslations("reservedslots.phrases");
ds@988
    59
	
ds@988
    60
	sm_reserved_slots = CreateConVar("sm_reserved_slots", "0", "Number of reserved player slots", 0, true, 0.0);
ds@988
    61
	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
    62
	sv_visiblemaxplayers = FindConVar("sv_visiblemaxplayers");
pred@1617
    63
	sm_reserve_type = CreateConVar("sm_reserve_type", "0", "Method of reserving slots", 0, true, 0.0, true, 1.0);
pred@1665
    64
	
pred@1665
    65
	HookConVarChange(sm_reserved_slots, SlotsChanged);
pred@1665
    66
	HookConVarChange(sm_hide_slots, SlotsChanged);
pred@1665
    67
}
pred@1665
    68
pred@1665
    69
public OnPluginEnd()
pred@1665
    70
{
pred@1665
    71
	/* 	If the plugin has been unloaded, reset visiblemaxplayers. In the case of the server shutting down this effect will not be visible */
pred@1665
    72
	SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
ds@988
    73
}
ds@988
    74
ds@988
    75
public OnMapStart()
ds@988
    76
{
ds@988
    77
	g_MaxClients = GetMaxClients();
pred@1665
    78
	
pred@1665
    79
	if (GetConVarBool(sm_hide_slots))
pred@1665
    80
	{		
pred@1665
    81
		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
pred@1665
    82
	}
ds@988
    83
}
ds@988
    84
ds@988
    85
public OnConfigsExecuted()
ds@988
    86
{
ds@988
    87
	if (GetConVarBool(sm_hide_slots))
ds@988
    88
	{
ds@998
    89
		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@988
    90
	}	
ds@988
    91
}
ds@988
    92
pred@1676
    93
public Action:OnTimedKick(Handle:timer, any:client)
pred@1676
    94
{	
dvander@1351
    95
	if (!client || !IsClientInGame(client))
dvander@1351
    96
	{
dvander@1351
    97
		return Plugin_Handled;
dvander@1351
    98
	}
dvander@1351
    99
	
dvander@1351
   100
	KickClient(client, "%T", "Slot reserved", client);
dvander@1351
   101
	
pred@1694
   102
	if (GetConVarBool(sm_hide_slots))
pred@1694
   103
	{				
pred@1694
   104
		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
pred@1694
   105
	}
pred@1676
   106
	
dvander@1351
   107
	return Plugin_Handled;
dvander@1351
   108
}
dvander@1351
   109
dvander@1273
   110
public OnClientPostAdminCheck(client)
ds@988
   111
{
ds@988
   112
	new reserved = GetConVarInt(sm_reserved_slots);
pred@1617
   113
ds@988
   114
	if (reserved > 0)
ds@988
   115
	{
ds@988
   116
		new clients = GetClientCount(false);
ds@988
   117
		new limit = g_MaxClients - reserved;
ds@988
   118
		new flags = GetUserFlagBits(client);
pred@1676
   119
		
pred@1676
   120
		new type = GetConVarInt(sm_reserve_type);
pred@1676
   121
		
pred@1676
   122
		if (type == 0)
ds@988
   123
		{
pred@1676
   124
			if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
ds@988
   125
			{
pred@1676
   126
				if (GetConVarBool(sm_hide_slots))
pred@1676
   127
				{
pred@1676
   128
					SetVisibleMaxSlots(clients, limit);
pred@1676
   129
				}
pred@1676
   130
				
pred@1676
   131
				return;
ds@988
   132
			}
ds@988
   133
			
pred@1676
   134
			/* Kick player because there are no public slots left */
pred@1676
   135
			CreateTimer(0.1, OnTimedKick, client);
pred@1676
   136
		}
pred@1676
   137
		else
pred@1676
   138
		{		
pred@1676
   139
			if (clients > limit)
pred@1676
   140
			{	
pred@1676
   141
				if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
pred@1617
   142
				{
pred@1676
   143
					new target = SelectKickClient();
pred@1676
   144
						
pred@1676
   145
					if (target)
pred@1676
   146
					{
pred@1676
   147
						/* Kick public player to free the reserved slot again */
pred@1676
   148
						CreateTimer(0.1, OnTimedKick, target);
pred@1676
   149
					}
pred@1676
   150
				}
pred@1676
   151
				else
pred@1676
   152
				{				
pred@1676
   153
					/* Kick player because there are no public slots left */
pred@1676
   154
					CreateTimer(0.1, OnTimedKick, client);
pred@1617
   155
				}
pred@1617
   156
			}
ds@988
   157
		}
ds@988
   158
	}
ds@988
   159
}
ds@988
   160
ds@998
   161
public OnClientDisconnect_Post(client)
ds@988
   162
{
ds@988
   163
	if (GetConVarBool(sm_hide_slots))
ds@998
   164
	{		
ds@998
   165
		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@988
   166
	}
ds@988
   167
}
ds@998
   168
pred@1665
   169
public SlotsChanged(Handle:convar, const String:oldValue[], const String:newValue[])
pred@1665
   170
{
pred@1665
   171
	/* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
pred@1665
   172
	if (StringToInt(newValue) == 0)
pred@1665
   173
	{
pred@1665
   174
		SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
pred@1665
   175
	}
pred@1665
   176
}
pred@1665
   177
ds@998
   178
SetVisibleMaxSlots(clients, limit)
ds@998
   179
{
pred@1676
   180
	new num = clients;
ds@998
   181
	
ds@999
   182
	if (clients == g_MaxClients)
ds@998
   183
	{
ds@998
   184
		num = g_MaxClients;
ds@998
   185
	} else if (clients < limit) {
ds@998
   186
		num = limit;
ds@998
   187
	}
ds@998
   188
	
ds@998
   189
	SetConVarInt(sv_visiblemaxplayers, num);
ds@998
   190
}
pred@1617
   191
pred@1617
   192
SelectKickClient()
pred@1617
   193
{
pred@1617
   194
	new Float:highestLatency;
pred@1617
   195
	new highestLatencyId;
pred@1617
   196
	
pred@1617
   197
	new Float:highestSpecLatency;
pred@1617
   198
	new highestSpecLatencyId;
pred@1617
   199
	
pred@1617
   200
	new bool:specFound;
pred@1617
   201
	
pred@1617
   202
	new Float:latency;
pred@1617
   203
	
pred@1617
   204
	for (new i=1; i<=g_MaxClients; i++)
pred@1676
   205
	{	
pred@1617
   206
		if (!IsClientConnected(i))
pred@1617
   207
		{
pred@1617
   208
			continue;
pred@1617
   209
		}
pred@1617
   210
	
pred@1617
   211
		new flags = GetUserFlagBits(i);
pred@1617
   212
		
pred@1666
   213
		if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
pred@1617
   214
		{
pred@1617
   215
			continue;
pred@1617
   216
		}
pred@1676
   217
		
pred@1676
   218
		latency = 0.0;
pred@1617
   219
			
pred@1617
   220
		if (IsClientInGame(i))
pred@1676
   221
		{		
pred@1676
   222
			latency = GetClientAvgLatency(i, NetFlow_Outgoing);
pred@1676
   223
			
pred@1676
   224
			LogMessage("Latency : %f",latency);
pred@1617
   225
			
pred@1618
   226
			if (IsClientObserver(i))
pred@1676
   227
			{			
pred@1617
   228
				specFound = true;
pred@1617
   229
				
pred@1617
   230
				if (latency > highestSpecLatency)
pred@1617
   231
				{
pred@1617
   232
					highestSpecLatency = latency;
pred@1617
   233
					highestSpecLatencyId = i;
pred@1617
   234
				}
pred@1617
   235
			}
pred@1617
   236
		}
pred@1617
   237
		
pred@1676
   238
		if (latency >= highestLatency)
pred@1617
   239
		{
pred@1617
   240
			highestLatency = latency;
pred@1617
   241
			highestLatencyId = i;
pred@1617
   242
		}
pred@1617
   243
	}
pred@1617
   244
	
pred@1617
   245
	if (specFound)
pred@1617
   246
	{
pred@1617
   247
		return highestSpecLatencyId;
pred@1617
   248
	}
pred@1617
   249
	
pred@1617
   250
	return highestLatencyId;
pred@1617
   251
}