ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourceMod Reserved Slots Plugin
ds@1952: * Provides basic reserved slots.
ds@1952: *
ds@1952: * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #pragma semicolon 1
ds@1952:
ds@1952: #include
ds@1952:
ds@1952: public Plugin:myinfo =
ds@1952: {
ds@1952: name = "Reserved Slots",
ds@1952: author = "AlliedModders LLC",
ds@1952: description = "Provides basic reserved slots",
ds@1952: version = SOURCEMOD_VERSION,
ds@1952: url = "http://www.sourcemod.net/"
ds@1952: };
ds@1952:
ds@1952: /* Maximum number of clients that can connect to server */
ds@1952: new g_MaxClients;
pred@2006: new g_adminCount = 0;
pred@2006: new bool:g_isAdmin[MAXPLAYERS+1];
ds@1952:
ds@1952: /* Handles to convars used by plugin */
ds@1952: new Handle:sm_reserved_slots;
ds@1952: new Handle:sm_hide_slots;
ds@1952: new Handle:sv_visiblemaxplayers;
ds@1952: new Handle:sm_reserve_type;
pred@2006: new Handle:sm_reserve_maxadmins;
pred@2022: new Handle:sm_reserve_kicktype;
pred@2022:
pred@2022: enum KickType
pred@2022: {
pred@2022: Kick_HighestPing,
pred@2022: Kick_HighestTime,
pred@2022: Kick_Random,
pred@2022: };
ds@1952:
ds@1952: public OnPluginStart()
ds@1952: {
ds@1952: LoadTranslations("reservedslots.phrases");
ds@1952:
ds@1952: sm_reserved_slots = CreateConVar("sm_reserved_slots", "0", "Number of reserved player slots", 0, true, 0.0);
ds@1952: 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: sv_visiblemaxplayers = FindConVar("sv_visiblemaxplayers");
pred@2006: sm_reserve_type = CreateConVar("sm_reserve_type", "0", "Method of reserving slots", 0, true, 0.0, true, 2.0);
pred@2006: 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: 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:
ds@1952: HookConVarChange(sm_reserved_slots, SlotsChanged);
ds@1952: HookConVarChange(sm_hide_slots, SlotsChanged);
ds@1952: }
ds@1952:
ds@1952: public OnPluginEnd()
ds@1952: {
ds@1952: /* If the plugin has been unloaded, reset visiblemaxplayers. In the case of the server shutting down this effect will not be visible */
ds@1952: SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
ds@1952: }
ds@1952:
ds@1952: public OnMapStart()
ds@1952: {
ds@1952: g_MaxClients = GetMaxClients();
ds@1952:
ds@1952: if (GetConVarBool(sm_hide_slots))
ds@1952: {
ds@1952: SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: public OnConfigsExecuted()
ds@1952: {
ds@1952: if (GetConVarBool(sm_hide_slots))
ds@1952: {
ds@1952: SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: public Action:OnTimedKick(Handle:timer, any:client)
ds@1952: {
ds@1952: if (!client || !IsClientInGame(client))
ds@1952: {
ds@1952: return Plugin_Handled;
ds@1952: }
ds@1952:
ds@1952: KickClient(client, "%T", "Slot reserved", client);
ds@1952:
ds@1952: if (GetConVarBool(sm_hide_slots))
ds@1952: {
ds@1952: SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@1952: }
ds@1952:
ds@1952: return Plugin_Handled;
ds@1952: }
ds@1952:
ds@1952: public OnClientPostAdminCheck(client)
ds@1952: {
ds@1952: new reserved = GetConVarInt(sm_reserved_slots);
ds@1952:
ds@1952: if (reserved > 0)
ds@1952: {
ds@1952: new clients = GetClientCount(false);
ds@1952: new limit = g_MaxClients - reserved;
ds@1952: new flags = GetUserFlagBits(client);
ds@1952:
ds@1952: new type = GetConVarInt(sm_reserve_type);
ds@1952:
ds@1952: if (type == 0)
ds@1952: {
ds@1952: if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
ds@1952: {
ds@1952: if (GetConVarBool(sm_hide_slots))
ds@1952: {
ds@1952: SetVisibleMaxSlots(clients, limit);
ds@1952: }
ds@1952:
ds@1952: return;
ds@1952: }
ds@1952:
ds@1952: /* Kick player because there are no public slots left */
ds@1952: CreateTimer(0.1, OnTimedKick, client);
ds@1952: }
pred@2006: else if (type == 1)
pred@2006: {
ds@1952: if (clients > limit)
pred@2133: {
ds@1952: if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
ds@1952: {
ds@1952: new target = SelectKickClient();
ds@1952:
ds@1952: if (target)
ds@1952: {
ds@1952: /* Kick public player to free the reserved slot again */
ds@1952: CreateTimer(0.1, OnTimedKick, target);
ds@1952: }
ds@1952: }
ds@1952: else
ds@1952: {
ds@1952: /* Kick player because there are no public slots left */
ds@1952: CreateTimer(0.1, OnTimedKick, client);
ds@1952: }
ds@1952: }
ds@1952: }
pred@2006: else if (type == 2)
pred@2006: {
pred@2006: if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
pred@2006: {
pred@2006: g_adminCount++;
pred@2006: g_isAdmin[client] = true;
pred@2006: }
pred@2006:
pred@2006: if (clients > limit && g_adminCount < GetConVarInt(sm_reserve_maxadmins))
pred@2006: {
pred@2006: /* Server is full, reserved slots aren't and client doesn't have reserved slots access */
pred@2006:
pred@2006: if (g_isAdmin[client])
pred@2006: {
pred@2006: new target = SelectKickClient();
pred@2006:
pred@2006: if (target)
pred@2006: {
pred@2006: /* Kick public player to free the reserved slot again */
pred@2006: CreateTimer(0.1, OnTimedKick, target);
pred@2006: }
pred@2006: }
pred@2006: else
pred@2006: {
pred@2006: /* Kick player because there are no public slots left */
pred@2006: CreateTimer(0.1, OnTimedKick, client);
pred@2006: }
pred@2006: }
pred@2006: }
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: public OnClientDisconnect_Post(client)
ds@1952: {
ds@1952: if (GetConVarBool(sm_hide_slots))
ds@1952: {
ds@1952: SetVisibleMaxSlots(GetClientCount(false), g_MaxClients - GetConVarInt(sm_reserved_slots));
ds@1952: }
pred@2006:
pred@2006: if (g_isAdmin[client])
pred@2006: {
pred@2006: g_adminCount--;
pred@2006: g_isAdmin[client] = false;
pred@2006: }
ds@1952: }
ds@1952:
ds@1952: public SlotsChanged(Handle:convar, const String:oldValue[], const String:newValue[])
ds@1952: {
ds@1952: /* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
ds@1952: if (StringToInt(newValue) == 0)
ds@1952: {
ds@1952: SetConVarInt(sv_visiblemaxplayers, g_MaxClients);
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: SetVisibleMaxSlots(clients, limit)
ds@1952: {
ds@1952: new num = clients;
ds@1952:
ds@1952: if (clients == g_MaxClients)
ds@1952: {
ds@1952: num = g_MaxClients;
ds@1952: } else if (clients < limit) {
ds@1952: num = limit;
ds@1952: }
ds@1952:
ds@1952: SetConVarInt(sv_visiblemaxplayers, num);
ds@1952: }
ds@1952:
ds@1952: SelectKickClient()
ds@1952: {
pred@2022: new KickType:type = KickType:GetConVarInt(sm_reserve_kicktype);
ds@1952:
pred@2022: new Float:highestValue;
pred@2022: new highestValueId;
pred@2022:
pred@2022: new Float:highestSpecValue;
pred@2022: new highestSpecValueId;
ds@1952:
ds@1952: new bool:specFound;
ds@1952:
pred@2022: new Float:value;
ds@1952:
ds@1952: for (new i=1; i<=g_MaxClients; i++)
ds@1952: {
ds@1952: if (!IsClientConnected(i))
ds@1952: {
ds@1952: continue;
ds@1952: }
ds@1952:
ds@1952: new flags = GetUserFlagBits(i);
ds@1952:
ds@1952: if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
ds@1952: {
ds@1952: continue;
ds@1952: }
ds@1952:
pred@2022: value = 0.0;
ds@1952:
ds@1952: if (IsClientInGame(i))
pred@2022: {
pred@2022: if (type == Kick_HighestPing)
pred@2022: {
pred@2022: value = GetClientAvgLatency(i, NetFlow_Outgoing);
pred@2022: }
pred@2022: else if (type == Kick_HighestTime)
pred@2022: {
pred@2022: value = GetClientTime(i);
pred@2022: }
pred@2022: else
pred@2022: {
pred@2022: value = GetRandomFloat(0.0, 100.0);
pred@2022: }
pred@2022:
ds@1952: if (IsClientObserver(i))
ds@1952: {
ds@1952: specFound = true;
ds@1952:
pred@2022: if (value > highestSpecValue)
ds@1952: {
pred@2022: highestSpecValue = value;
pred@2022: highestSpecValueId = i;
ds@1952: }
ds@1952: }
ds@1952: }
ds@1952:
pred@2022: if (value >= highestValue)
ds@1952: {
pred@2022: highestValue = value;
pred@2022: highestValueId = i;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: if (specFound)
ds@1952: {
pred@2022: return highestSpecValueId;
ds@1952: }
ds@1952:
pred@2022: return highestValueId;
pred@2022: }