Skip to content
View in the app

A better way to browse. Learn more.

Learn more

dynamic-server-name-status/addons/amxmodx/scripting/olds_hoststatus.sma

olds_hoststatus.sma
243 lines · 6.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Dynamic Server Name Status (olds.ro workshop card LIB-1571)
 *
 * Puts the map, the minutes left and the round number in the server name
 * (hostname), so players browsing the server list see how far the map is.
 *
 * Cvars (saved with their descriptions in configs/plugins/olds_hoststatus.cfg
 * the first time the plugin starts):
 *   olds_hs_format    "olds.ro | %map% | %left% min | R%round%"
 *                     the server name; these words are replaced:
 *                       %map%    the map name
 *                       %left%   whole minutes left on the map, rounded up
 *                                ("--" without a time limit)
 *                       %round%  the round number on this map (1, 2, 3 ...)
 *                     Empty = the plugin leaves the hostname alone.
 *   olds_hs_interval  10   seconds between updates (1 - 300)
 *
 * The name is cut at 63 bytes (all the server list shows), never inside a
 * UTF-8 character, and is set only when it changed. It is also updated at
 * once when a round starts.
 *
 * The round number counts the round starts of the game (ReAPI
 * RG_CSGameRules_RestartRound). "Game Commencing" and sv_restart start
 * again at round 1; so does every new map.
 *
 * Server command:
 *   olds_hs_status   prints the format, the round, the time left and the
 *                    hostname now
 *
 * Requires AMX Mod X 1.10 or newer and ReAPI (ReGameDLL_CS).
 *
 * Copyright (C) 2026 alexit (olds.ro workshop)
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details: <https://www.gnu.org/licenses/>.
 */

#include <amxmodx>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Dynamic Server Name Status";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_hoststatus.txt";

// The server list shows at most 63 bytes of the hostname.
const MAX_HOSTNAME = 63;
const TASK_UPDATE = 15711;

new g_format[192];      // olds_hs_format
new g_interval;         // olds_hs_interval

new g_round = 1;
new g_updates;
new g_pcvarTimelimit;

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
	register_dictionary(DICTIONARY);

	bind_pcvar_string(create_cvar("olds_hs_format", "olds.ro | %map% | %left% min | R%round%", FCVAR_NONE,
		"Server name; %map% = map, %left% = minutes left, %round% = round number; empty = leave the hostname alone"),
		g_format, charsmax(g_format));

	new pcvar = create_cvar("olds_hs_interval", "10", FCVAR_NONE,
		"Seconds between hostname updates",
		true, 1.0, true, 300.0);
	bind_pcvar_num(pcvar, g_interval);
	hook_cvar_change(pcvar, "on_interval_change");

	AutoExecConfig(true, "olds_hoststatus");

	register_srvcmd("olds_hs_status", "cmd_status");

	RegisterHookChain(RG_CSGameRules_RestartRound, "fw_restart_round_pre", false);
	RegisterHookChain(RG_CSGameRules_RestartRound, "fw_restart_round_post", true);

	g_pcvarTimelimit = get_cvar_pointer("mp_timelimit");
}

public OnConfigsExecuted()
{
	task_update();
	schedule(g_interval);
}

public on_interval_change(pcvar, const oldValue[], const newValue[])
{
	schedule(str_to_num(newValue));
}

schedule(seconds)
{
	remove_task(TASK_UPDATE);
	set_task(float(clamp(seconds, 1, 300)), "task_update", TASK_UPDATE, _, _, "b");
}

/**
 * Before the round restarts: a complete reset (Game Commencing,
 * sv_restart) starts again at round 1, any other round start counts up.
 */
public fw_restart_round_pre()
{
	if (get_member_game(m_bCompleteReset))
		g_round = 1;
	else
		g_round++;
}

public fw_restart_round_post()
{
	task_update();
}

/**
 * Every olds_hs_interval seconds and at every round start.
 */
public task_update()
{
	if (!g_format[0])
		return;

	new text[MAX_HOSTNAME + 1];
	build_name(text, charsmax(text));

	new current[128];
	get_cvar_string("hostname", current, charsmax(current));

	if (equal(current, text))
		return;

	set_cvar_string("hostname", text);
	g_updates++;
}

build_name(text[], maxLength)
{
	new full[256], value[32];
	copy(full, charsmax(full), g_format);

	get_mapname(value, charsmax(value));
	replace_string(full, charsmax(full), "%map%", value);

	minutes_left(value, charsmax(value));
	replace_string(full, charsmax(full), "%left%", value);

	num_to_str(g_round, value, charsmax(value));
	replace_string(full, charsmax(full), "%round%", value);

	remove_control_characters(full);
	trim(full);
	utf8_cut(full, MAX_HOSTNAME);
	trim(full);

	copy(text, maxLength, full);
}

/**
 * Whole minutes left, rounded up so "0" shows only when the time is over;
 * "--" without a time limit.
 */
minutes_left(out[], maxLength)
{
	if (get_pcvar_float(g_pcvarTimelimit) <= 0.0)
	{
		copy(out, maxLength, "--");
		return;
	}

	new seconds = max(get_timeleft(), 0);
	num_to_str((seconds + 59) / 60, out, maxLength);
}

public cmd_status()
{
	new current[128], left[16];
	get_cvar_string("hostname", current, charsmax(current));
	minutes_left(left, charsmax(left));

	server_print("%L", LANG_SERVER, "OLDS_HS_STATUS", g_format, g_interval);
	server_print("%L", LANG_SERVER, "OLDS_HS_NOW", g_round, left, get_timeleft(), g_updates);
	server_print("%L", LANG_SERVER, "OLDS_HS_HOSTNAME", current, strlen(current));
	return PLUGIN_HANDLED;
}

/**
 * Drops bytes below 32 (line breaks and the like).
 */
remove_control_characters(text[])
{
	new to;

	for (new from = 0; text[from]; from++)
	{
		if ((text[from] & 0xFF) >= ' ')
			text[to++] = text[from];
	}

	text[to] = EOS;
}

/**
 * Shortens text to at most maxBytes bytes without leaving half of a UTF-8
 * character at the end.
 */
utf8_cut(text[], maxBytes)
{
	if (strlen(text) <= maxBytes)
		return;

	text[maxBytes] = EOS;

	// Find where the last character starts: continuation bytes are 10xxxxxx.
	new start = maxBytes - 1;

	while (start > 0 && (text[start] & 0xC0) == 0x80)
		start--;

	new lead = text[start] & 0xFF;
	new needed = 1;

	if (lead >= 0xF0)
		needed = 4;
	else if (lead >= 0xE0)
		needed = 3;
	else if (lead >= 0xC0)
		needed = 2;

	if (maxBytes - start < needed)
		text[start] = EOS;
}

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.