3 * =============================================================================
4 * SourceMod Reserved Slots Plugin
5 * Provides basic reserved slots.
7 * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
8 * =============================================================================
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, version 3.0, as published by the
12 * Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
22 * As a special exception, AlliedModders LLC gives you permission to link the
23 * code of this program (as well as its derivative works) to "Half-Life 2," the
24 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
25 * by the Valve Corporation. You must obey the GNU General Public License in
26 * all respects for all other code used. Additionally, AlliedModders LLC grants
27 * this exception to all derivative works. AlliedModders LLC defines further
28 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
29 * or <http://www.sourcemod.net/license.php>.
38 public Plugin:myinfo =
40 name = "Reserved Slots",
41 author = "AlliedModders LLC",
42 description = "Provides basic reserved slots",
43 version = SOURCEMOD_VERSION,
44 url = "http://www.sourcemod.net/"
47 /* Maximum number of clients that can connect to server */
50 new bool:g_isAdmin[MAXPLAYERS+1];
52 /* Handles to convars used by plugin */
53 new Handle:sm_reserved_slots;
54 new Handle:sm_hide_slots;
55 new Handle:sv_visiblemaxplayers;
56 new Handle:sm_reserve_type;
57 new Handle:sm_reserve_maxadmins;
58 new Handle:sm_reserve_kicktype;
67 public OnPluginStart()
69 LoadTranslations("reservedslots.phrases");
71 sm_reserved_slots = CreateConVar("sm_reserved_slots", "0", "Number of reserved player slots", 0, true, 0.0);
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);
73 sv_visiblemaxplayers = FindConVar("sv_visiblemaxplayers");
74 sm_reserve_type = CreateConVar("sm_reserve_type", "0", "Method of reserving slots", 0, true, 0.0, true, 2.0);
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);
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);
78 HookConVarChange(sm_reserved_slots, SlotsChanged);
79 HookConVarChange(sm_hide_slots, SlotsChanged);
84 /* If the plugin has been unloaded, reset visiblemaxplayers. In the case of the server shutting down this effect will not be visible */
85 SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
90 g_MaxClients = GetMaxClients();
92 if (GetConVarBool(sm_hide_slots))
94 SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
98 public OnConfigsExecuted()
100 if (GetConVarBool(sm_hide_slots))
102 SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
106 public Action:OnTimedKick(Handle:timer, any:client)
108 if (!client || !IsClientInGame(client))
110 return Plugin_Handled;
113 KickClient(client, "%T", "Slot reserved", client);
115 if (GetConVarBool(sm_hide_slots))
117 SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
120 return Plugin_Handled;
123 public OnClientPostAdminCheck(client)
125 new reserved = GetConVarInt(sm_reserved_slots);
129 new clients = GetClientCount(false);
130 new limit = g_MaxClients - reserved;
131 new flags = GetUserFlagBits(client);
133 new type = GetConVarInt(sm_reserve_type);
137 if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
139 if (GetConVarBool(sm_hide_slots))
141 SetVisibleMaxSlots(clients, limit);
147 /* Kick player because there are no public slots left */
148 CreateTimer(0.1, OnTimedKick, client);
154 if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
156 new target = SelectKickClient();
160 /* Kick public player to free the reserved slot again */
161 CreateTimer(0.1, OnTimedKick, target);
166 /* Kick player because there are no public slots left */
167 CreateTimer(0.1, OnTimedKick, client);
173 if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
176 g_isAdmin[client] = true;
179 if (clients > limit && g_adminCount < GetConVarInt(sm_reserve_maxadmins))
181 /* Server is full, reserved slots aren't and client doesn't have reserved slots access */
183 if (g_isAdmin[client])
185 new target = SelectKickClient();
189 /* Kick public player to free the reserved slot again */
190 CreateTimer(0.1, OnTimedKick, target);
195 /* Kick player because there are no public slots left */
196 CreateTimer(0.1, OnTimedKick, client);
203 public OnClientDisconnect_Post(client)
205 if (GetConVarBool(sm_hide_slots))
207 SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
210 if (g_isAdmin[client])
213 g_isAdmin[client] = false;
217 public SlotsChanged(Handle:convar, const String:oldValue[], const String:newValue[])
219 /* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
220 if (StringToInt(newValue) == 0)
222 SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
226 SetVisibleMaxSlots(clients, limit)
230 if (clients == g_MaxClients)
233 } else if (clients < limit) {
237 SetConVarInt(sv_visiblemaxplayers, num);
242 new KickType:type = KickType:GetConVarInt(sm_reserve_kicktype);
244 new Float:highestValue;
247 new Float:highestSpecValue;
248 new highestSpecValueId;
254 for (new i=1; i<=g_MaxClients; i++)
256 if (!IsClientConnected(i))
261 new flags = GetUserFlagBits(i);
263 if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
270 if (IsClientInGame(i))
272 if (type == Kick_HighestPing)
274 value = GetClientAvgLatency(i, NetFlow_Outgoing);
276 else if (type == Kick_HighestTime)
278 value = GetClientTime(i);
282 value = GetRandomFloat(0.0, 100.0);
285 if (IsClientObserver(i))
289 if (value > highestSpecValue)
291 highestSpecValue = value;
292 highestSpecValueId = i;
297 if (value >= highestValue)
299 highestValue = value;
306 return highestSpecValueId;
309 return highestValueId;