Improve AdminSlot plugin (#631)

* Adminslot: Trim spaces

* Adminslot: Use semicolon everywhere

* Adminslot: Use brackets everywhere

* Adminslot: Hook cvars change and make sure max visible players is set right away

* Adminslot: Rename g_sv_visiblemaxplayers

* Adminslot: Move free slot calculation in its own function

* Adminslot: Use create_cvar and define appropriate bounds

* Adminslot: Use get_playersnum_ex with flag for clarity

* Adminslot: Move all the logic inside setVisibleSlots

Looks like the commit should have been split for clarity.

The initial purpose is to avoid code duplication and regroup checks.
The logic is the same with a little improvement to handle situations where a cvar is changed on-the-fly and sv_visiblemaxplayers needs to be reset.

Ultimately, the logic is the following:

 - At player's connection:
 -- Do nothing if amx_reservation == 0 and sv_visiblemaxplayers <= 0. If sv_visiblemaxplayers is set, we reset it directly.
 -- Check if player needs to be kicked. If not, and amx_hideslots == 0, then do nothing.
 -- Otherwise we update sv_visiblemaxplayers

 - Others events:
 -- Do nothing if either amx_reservation == 0 or amx_hideslots == 0, and if sv_visiblemaxplayers <= 0. If sv_visiblemaxplayers is set, we reset it directly.
 -- Otherwise we update sv_visiblemaxplayers

* Adminslot: Rename variables and adjust constantness

* Adminslot: Add a description to the cvars

* Adjust CVAR_HIDESLOTS english sentence

* Add the Deutsch translations
This commit is contained in:
Vincent Herbet 2020-06-03 23:52:47 +02:00 committed by GitHub
parent b9b4001bf9
commit 6d6a41b894
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 39 deletions

View File

@ -14,64 +14,97 @@
#include <amxmodx>
#include <amxmisc>
new g_ResPtr
new g_HidePtr
new g_sv_visiblemaxplayers
new CvarReservation;
new CvarHideSlots;
new CvarHandleMaxVisiblePlayers;
public plugin_init()
{
register_plugin("Slots Reservation", AMXX_VERSION_STR, "AMXX Dev Team")
register_dictionary("adminslots.txt")
register_dictionary("common.txt")
g_ResPtr = register_cvar("amx_reservation", "0", FCVAR_PROTECTED)
g_HidePtr = register_cvar("amx_hideslots", "0")
g_sv_visiblemaxplayers = get_cvar_pointer("sv_visiblemaxplayers")
register_plugin("Slots Reservation", AMXX_VERSION_STR, "AMXX Dev Team");
register_dictionary("adminslots.txt");
register_dictionary("common.txt");
hook_cvar_change(create_cvar("amx_reservation", "0", FCVAR_PROTECTED, fmt("%L", LANG_SERVER, "CVAR_RESERVATION"), .has_min = true, .min_val = 0.0, .has_max = true, .max_val = float(MaxClients - 1)), "@OnReservationChange");
hook_cvar_change(create_cvar("amx_hideslots" , "0", FCVAR_NONE , fmt("%L", LANG_SERVER, "CVAR_HIDESLOTS") , .has_min = true, .min_val = 0.0, .has_max = true, .max_val = 1.0), "@OnHideSlotsChange");
CvarHandleMaxVisiblePlayers = get_cvar_pointer("sv_visiblemaxplayers");
}
public plugin_cfg()
@OnReservationChange(const handle, const oldValue[], const newValue[])
{
set_task(3.0, "MapLoaded")
CvarReservation = strtol(newValue);
setVisibleSlots();
}
public MapLoaded()
@OnHideSlotsChange(const handle, const oldValue[], const newValue[])
{
if (get_pcvar_num(g_HidePtr))
{
setVisibleSlots(get_playersnum(1), MaxClients - get_pcvar_num(g_ResPtr))
}
CvarHideSlots = strtol(newValue);
setVisibleSlots();
}
public client_authorized(id)
{
new players = get_playersnum(1)
new limit = MaxClients - get_pcvar_num(g_ResPtr)
if (access(id, ADMIN_RESERVATION) || (players <= limit))
{
if (get_pcvar_num(g_HidePtr))
setVisibleSlots(players, limit)
return
}
server_cmd("kick #%d ^"%L^"", get_user_userid(id), id, "DROPPED_RES")
setVisibleSlots(id);
}
public client_remove(id)
{
if (get_pcvar_num(g_HidePtr))
{
setVisibleSlots(get_playersnum(1), MaxClients - get_pcvar_num(g_ResPtr))
}
setVisibleSlots();
}
setVisibleSlots(players, limit)
setVisibleSlots(const playerId = 0)
{
new num = players + 1
if ((playerId == 0 && !CvarHideSlots) || !CvarReservation)
{
if (get_pcvar_num(CvarHandleMaxVisiblePlayers) > 0)
{
resetVisibleSlots(MaxClients);
}
if (players == MaxClients)
num = MaxClients
else if (players < limit)
num = limit
set_pcvar_num(g_sv_visiblemaxplayers, num)
return;
}
new const playersCount = get_playersnum_ex(GetPlayers_IncludeConnecting);
new const freeVisibleSlots = MaxClients - CvarReservation;
if (playerId != 0)
{
if (playersCount > freeVisibleSlots && !access(playerId, ADMIN_RESERVATION))
{
server_cmd("kick #%d ^"%L^"", get_user_userid(playerId), playerId, "DROPPED_RES");
return;
}
if (!CvarHideSlots)
{
return;
}
}
new maxVisiblePlayers = playersCount + 1;
if (playersCount == MaxClients)
{
maxVisiblePlayers = MaxClients;
}
else if (playersCount < freeVisibleSlots)
{
maxVisiblePlayers = freeVisibleSlots;
}
resetVisibleSlots(maxVisiblePlayers);
}
resetVisibleSlots(value)
{
if (value == MaxClients)
{
value = -1; // Default sv_visiblemaxplayers value.
}
set_pcvar_num(CvarHandleMaxVisiblePlayers, value);
}

View File

@ -1,8 +1,12 @@
[en]
DROPPED_RES = Dropped due to slot reservation
CVAR_RESERVATION = Amount of slots to reserve
CVAR_HIDESLOTS = If you set this to 1, you can hide slots on your server.^nIf the server's public and hidden slots are full, you must manually connect with the "connect" console command.
[de]
DROPPED_RES = Sorry, dieser Slot ist reserviert
CVAR_RESERVATION = Anzahl der zu reservierenden Slots
CVAR_HIDESLOTS = Wenn diese CVAR auf 1 gesetzt wird, können Slots auf Ihrem Server ausgeblendet werden.^nWenn die öffentlichen Slots auf dem Server "voll" sind und weitere freie Slots ausgeblendet sind, müssen Sie die Verbindung manuell über den Konsolenbefehl "connect" herstellen.
[sr]
DROPPED_RES = Server je pun, nemate pristup rezervisanim mestima