plugins/reservedslots.sp
author David Anderson <dvander@alliedmods.net>
Fri Nov 16 14:17:56 2007 +0000 (2007-11-16)
changeset 1684 c4e72bd36591
parent 1676 df6b4e53cca0
child 1694 833909112af3
permissions -rw-r--r--
fixed amb1161 - voteban didn't actually ban
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@1676
   102
	SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
pred@1676
   103
	
dvander@1351
   104
	return Plugin_Handled;
dvander@1351
   105
}
dvander@1351
   106
dvander@1273
   107
public OnClientPostAdminCheck(client)
ds@988
   108
{
ds@988
   109
	new reserved = GetConVarInt(sm_reserved_slots);
pred@1617
   110
ds@988
   111
	if (reserved > 0)
ds@988
   112
	{
ds@988
   113
		new clients = GetClientCount(false);
ds@988
   114
		new limit = g_MaxClients - reserved;
ds@988
   115
		new flags = GetUserFlagBits(client);
pred@1676
   116
		
pred@1676
   117
		new type = GetConVarInt(sm_reserve_type);
pred@1676
   118
		
pred@1676
   119
		if (type == 0)
ds@988
   120
		{
pred@1676
   121
			if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
ds@988
   122
			{
pred@1676
   123
				if (GetConVarBool(sm_hide_slots))
pred@1676
   124
				{
pred@1676
   125
					SetVisibleMaxSlots(clients, limit);
pred@1676
   126
				}
pred@1676
   127
				
pred@1676
   128
				return;
ds@988
   129
			}
ds@988
   130
			
pred@1676
   131
			/* Kick player because there are no public slots left */
pred@1676
   132
			CreateTimer(0.1, OnTimedKick, client);
pred@1676
   133
		}
pred@1676
   134
		else
pred@1676
   135
		{		
pred@1676
   136
			if (clients > limit)
pred@1676
   137
			{	
pred@1676
   138
				if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
pred@1617
   139
				{
pred@1676
   140
					new target = SelectKickClient();
pred@1676
   141
						
pred@1676
   142
					if (target)
pred@1676
   143
					{
pred@1676
   144
						/* Kick public player to free the reserved slot again */
pred@1676
   145
						CreateTimer(0.1, OnTimedKick, target);
pred@1676
   146
					}
pred@1676
   147
				}
pred@1676
   148
				else
pred@1676
   149
				{				
pred@1676
   150
					/* Kick player because there are no public slots left */
pred@1676
   151
					CreateTimer(0.1, OnTimedKick, client);
pred@1617
   152
				}
pred@1617
   153
			}
ds@988
   154
		}
ds@988
   155
	}
ds@988
   156
}
ds@988
   157
ds@998
   158
public OnClientDisconnect_Post(client)
ds@988
   159
{
ds@988
   160
	if (GetConVarBool(sm_hide_slots))
ds@998
   161
	{		
ds@998
   162
		SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@988
   163
	}
ds@988
   164
}
ds@998
   165
pred@1665
   166
public SlotsChanged(Handle:convar, const String:oldValue[], const String:newValue[])
pred@1665
   167
{
pred@1665
   168
	/* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
pred@1665
   169
	if (StringToInt(newValue) == 0)
pred@1665
   170
	{
pred@1665
   171
		SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
pred@1665
   172
	}
pred@1665
   173
}
pred@1665
   174
ds@998
   175
SetVisibleMaxSlots(clients, limit)
ds@998
   176
{
pred@1676
   177
	new num = clients;
ds@998
   178
	
ds@999
   179
	if (clients == g_MaxClients)
ds@998
   180
	{
ds@998
   181
		num = g_MaxClients;
ds@998
   182
	} else if (clients < limit) {
ds@998
   183
		num = limit;
ds@998
   184
	}
ds@998
   185
	
ds@998
   186
	SetConVarInt(sv_visiblemaxplayers, num);
ds@998
   187
}
pred@1617
   188
pred@1617
   189
SelectKickClient()
pred@1617
   190
{
pred@1617
   191
	new Float:highestLatency;
pred@1617
   192
	new highestLatencyId;
pred@1617
   193
	
pred@1617
   194
	new Float:highestSpecLatency;
pred@1617
   195
	new highestSpecLatencyId;
pred@1617
   196
	
pred@1617
   197
	new bool:specFound;
pred@1617
   198
	
pred@1617
   199
	new Float:latency;
pred@1617
   200
	
pred@1617
   201
	for (new i=1; i<=g_MaxClients; i++)
pred@1676
   202
	{	
pred@1617
   203
		if (!IsClientConnected(i))
pred@1617
   204
		{
pred@1617
   205
			continue;
pred@1617
   206
		}
pred@1617
   207
	
pred@1617
   208
		new flags = GetUserFlagBits(i);
pred@1617
   209
		
pred@1666
   210
		if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
pred@1617
   211
		{
pred@1617
   212
			continue;
pred@1617
   213
		}
pred@1676
   214
		
pred@1676
   215
		latency = 0.0;
pred@1617
   216
			
pred@1617
   217
		if (IsClientInGame(i))
pred@1676
   218
		{		
pred@1676
   219
			latency = GetClientAvgLatency(i, NetFlow_Outgoing);
pred@1676
   220
			
pred@1676
   221
			LogMessage("Latency : %f",latency);
pred@1617
   222
			
pred@1618
   223
			if (IsClientObserver(i))
pred@1676
   224
			{			
pred@1617
   225
				specFound = true;
pred@1617
   226
				
pred@1617
   227
				if (latency > highestSpecLatency)
pred@1617
   228
				{
pred@1617
   229
					highestSpecLatency = latency;
pred@1617
   230
					highestSpecLatencyId = i;
pred@1617
   231
				}
pred@1617
   232
			}
pred@1617
   233
		}
pred@1617
   234
		
pred@1676
   235
		if (latency >= highestLatency)
pred@1617
   236
		{
pred@1617
   237
			highestLatency = latency;
pred@1617
   238
			highestLatencyId = i;
pred@1617
   239
		}
pred@1617
   240
	}
pred@1617
   241
	
pred@1617
   242
	if (specFound)
pred@1617
   243
	{
pred@1617
   244
		return highestSpecLatencyId;
pred@1617
   245
	}
pred@1617
   246
	
pred@1617
   247
	return highestLatencyId;
pred@1617
   248
}