Skip to content
View in the app

A better way to browse. Learn more.

Learn more

admin-set-score/addons/amxmodx/scripting/olds_setscore.sma

olds_setscore.sma
391 lines · 11.6 KB · Pawn (AMX Mod X)
Raw
/*
 * Admin Set Score (olds.ro workshop card LIB-2086)
 *
 * Admins set a player's frags and deaths to exact values, to fix a mistake
 * or to hand out an event prize. The scoreboard of every player shows the
 * new score at once.
 *
 * Console command (admins with the ADMIN_RCON flag, and the server console):
 *   amx_setscore <name|#userid> <frags> <deaths>
 *     A plain number sets the value: amx_setscore Bob 20 0
 *     +n or -n changes the current value: amx_setscore Bob +3 +0
 *     The player can be named by a part of the name (any letter case) when
 *     only one player matches, or by #userid as shown by "status".
 *   Frags may end anywhere from -olds_setscore_max to olds_setscore_max
 *   (Counter-Strike frags go below 0 after suicides and team kills); deaths
 *   from 0 to olds_setscore_max. A request outside these limits is refused
 *   and the score stays as it was.
 *
 * Cvars (configs/plugins/olds_setscore.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_setscore_max 9999   Highest frags or deaths value the command may set
 *                            (0 - 32767, the scoreboard limit).
 *   olds_setscore_log 1      1 = write each change to the AMXX log (admin,
 *                            player, old and new score), 0 = do not log.
 *
 * Every text a player or admin reads comes from data/lang/olds_setscore.txt
 * (English). The AMXX log line stays in English for the server owner.
 *
 * Requires AMX Mod X 1.10 or newer with the cstrike and fun modules.
 * ReAPI is not used. Written and tested on AMX Mod X 1.10.0.5490.
 *
 * 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 <amxmisc>
#include <cstrike>
#include <fun>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Admin Set Score";
new const PLUGIN_VERSION[] = "1.1.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_setscore.txt";
new const COMMAND_NAME[] = "amx_setscore";
new const COMMAND_USAGE[] = "<name|#userid> <frags> <deaths>";

// The scoreboard sends frags and deaths as signed 16-bit numbers.
const SCORE_LIMIT = 32767;

// More digits than any allowed value has. Longer numbers are refused as
// too large without being converted, so they cannot overflow.
const MAX_DIGITS = 6;

// What parse_value() made of a frags or deaths argument.
enum ParseResult
{
	PARSE_INVALID,      // not a number, +n or -n
	PARSE_TOO_LARGE,    // more than MAX_DIGITS digits
	PARSE_OK
};

new g_maxValue;      // olds_setscore_max
new g_log;           // olds_setscore_log

new g_msgScoreInfo;

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

	g_msgScoreInfo = get_user_msgid("ScoreInfo");

	if (!g_msgScoreInfo)
		set_fail_state("The ScoreInfo message does not exist. This plugin needs Counter-Strike.");

	// The help line shown by amx_help comes from the dictionary.
	register_concmd(COMMAND_NAME, "cmd_setscore", ADMIN_RCON, "OLDS_SETSCORE_HELP", -1, true);

	bind_pcvar_num(create_cvar("olds_setscore_max", "9999", FCVAR_NONE,
		"Highest frags or deaths value amx_setscore may set (0 - 32767)",
		true, 0.0, true, float(SCORE_LIMIT)), g_maxValue);

	bind_pcvar_num(create_cvar("olds_setscore_log", "1", FCVAR_NONE,
		"1 = write each amx_setscore change to the AMXX log, 0 = do not log",
		true, 0.0, true, 1.0), g_log);

	AutoExecConfig(true, "olds_setscore");
}

/**
 * amx_setscore <name|#userid> <frags> <deaths>
 *
 * The replies come from this plugin's own dictionary. The amxmisc helpers
 * (cmd_access, cmd_target) were left out because they print through the
 * common.txt dictionary, which has texts in many other languages.
 */
public cmd_setscore(id, level, cid)
{
	if (!has_command_access(id, level))
	{
		console_print(id, "%L", id, "OLDS_SETSCORE_NO_ACCESS", COMMAND_NAME);
		print_usage(id);
		return PLUGIN_HANDLED;
	}

	if (read_argc() != 4)
	{
		print_usage(id);
		return PLUGIN_HANDLED;
	}

	// One cell more than the longest name (31 characters), so a full-length
	// name still fits without filling the buffer.
	new targetArg[MAX_NAME_LENGTH + 1], fragsArg[32], deathsArg[32];

	// An argument that fills its buffer was cut short and is treated as
	// invalid rather than read in part.
	new targetLen = read_argv(1, targetArg, charsmax(targetArg));
	new fragsLen = read_argv(2, fragsArg, charsmax(fragsArg));
	new deathsLen = read_argv(3, deathsArg, charsmax(deathsArg));

	new fragsValue, deathsValue;
	new bool:fragsRelative, bool:deathsRelative;

	new ParseResult:fragsResult = fragsLen >= charsmax(fragsArg)
		? PARSE_INVALID : parse_value(fragsArg, fragsValue, fragsRelative);

	if (fragsResult == PARSE_INVALID)
	{
		console_print(id, "%L", id, "OLDS_SETSCORE_BAD_FRAGS");
		print_usage(id);
		return PLUGIN_HANDLED;
	}

	new ParseResult:deathsResult = deathsLen >= charsmax(deathsArg)
		? PARSE_INVALID : parse_value(deathsArg, deathsValue, deathsRelative);

	if (deathsResult == PARSE_INVALID)
	{
		console_print(id, "%L", id, "OLDS_SETSCORE_BAD_DEATHS");
		print_usage(id);
		return PLUGIN_HANDLED;
	}

	new target = targetLen >= charsmax(targetArg) ? 0 : find_target(id, targetArg);

	if (!target)
	{
		if (targetLen >= charsmax(targetArg))
			console_print(id, "%L", id, "OLDS_SETSCORE_NAME_TOO_LONG");

		return PLUGIN_HANDLED;
	}

	new oldFrags = get_user_frags(target);
	new oldDeaths = cs_get_user_deaths(target);

	new name[MAX_NAME_LENGTH];
	get_user_name(target, name, charsmax(name));

	// A number with more digits than any allowed value is refused here
	// without being added, so it cannot overflow.
	new newFrags = fragsRelative ? oldFrags + fragsValue : fragsValue;

	if (fragsResult == PARSE_TOO_LARGE || newFrags > g_maxValue || newFrags < -g_maxValue)
	{
		console_print(id, "%L", id, "OLDS_SETSCORE_FRAGS_LIMIT", g_maxValue, g_maxValue, name, oldFrags, oldDeaths);
		return PLUGIN_HANDLED;
	}

	new newDeaths = deathsRelative ? oldDeaths + deathsValue : deathsValue;

	if (deathsResult == PARSE_TOO_LARGE || newDeaths > g_maxValue || newDeaths < 0)
	{
		console_print(id, "%L", id, "OLDS_SETSCORE_DEATHS_LIMIT", g_maxValue, name, oldFrags, oldDeaths);
		return PLUGIN_HANDLED;
	}

	set_user_frags(target, newFrags);

	// The module would send ScoreInfo by itself here. It is switched off so
	// the scoreboard gets one update with both values, sent below.
	cs_set_user_deaths(target, newDeaths, false);
	send_score_info(target);

	console_print(id, "%L", id, "OLDS_SETSCORE_CHANGED", name, oldFrags, oldDeaths, newFrags, newDeaths);

	// The colour codes stay in the code, so the dictionary holds plain text.
	new score[32];
	formatex(score, charsmax(score), "^4%d / %d^1", newFrags, newDeaths);
	client_print_color(target, print_team_default, "%L", target, "OLDS_SETSCORE_TOLD", score);

	if (g_log)
		log_change(id, target, oldFrags, oldDeaths, newFrags, newDeaths);

	return PLUGIN_HANDLED;
}

/**
 * The server console (id 0) always has access, and so has the host of a
 * listen server (id 1). Anyone else needs the flag in level: the one the
 * command was registered with, or the one set for it in cmdaccess.ini.
 */
bool:has_command_access(id, level)
{
	if (id == 0 || (id == 1 && !is_dedicated_server()))
		return true;

	if (level == ADMIN_ALL)
		return true;

	if (level == ADMIN_ADMIN)
		return is_user_admin(id) != 0;

	return (get_user_flags(id) & level) != 0;
}

print_usage(id)
{
	console_print(id, "%L", id, "OLDS_SETSCORE_USAGE", COMMAND_NAME, COMMAND_USAGE);
	console_print(id, "  %L", id, "OLDS_SETSCORE_USAGE_HINT");
}

/**
 * Reads "20" (set to 20), "+3" (add 3) or "-3" (take away 3). Anything else,
 * including an empty sign or a space, is PARSE_INVALID. A number with more
 * digits than MAX_DIGITS is PARSE_TOO_LARGE and value is left at 0.
 */
ParseResult:parse_value(const arg[], &value, &bool:relative)
{
	new pos = 0;
	new sign = 1;

	value = 0;
	relative = false;

	if (arg[0] == '+' || arg[0] == '-')
	{
		relative = true;
		sign = arg[0] == '-' ? -1 : 1;
		pos = 1;
	}

	// At least one character must be a digit ("+" or "" are not numbers).
	if (arg[pos] == EOS)
		return PARSE_INVALID;

	new number = 0;
	new digits = 0;
	new bool:tooLarge = false;

	for (; arg[pos] != EOS; pos++)
	{
		if (!isdigit(arg[pos]))
			return PARSE_INVALID;

		// Leading zeros are not counted, so "0005" is read as 5.
		if (digits == 0 && arg[pos] == '0')
			continue;

		if (++digits > MAX_DIGITS)
			tooLarge = true;
		else
			number = number * 10 + (arg[pos] - '0');
	}

	if (tooLarge)
		return PARSE_TOO_LARGE;

	value = sign * number;
	return PARSE_OK;
}

/**
 * Finds the player an admin named: "#userid", an exact name, or a part of
 * one name (any letter case). HLTV proxies are skipped. When nobody, or more
 * than one player, matches, the reason goes to the admin and 0 is returned.
 */
find_target(id, const arg[])
{
	new players[MAX_PLAYERS], count;
	get_players_ex(players, count, GetPlayers_ExcludeHLTV);

	if (arg[0] == '#' && arg[1] != EOS)
	{
		if (!is_str_num(arg[1]))
		{
			console_print(id, "%L", id, "OLDS_SETSCORE_NOT_USERID", arg);
			return 0;
		}

		new userid = str_to_num(arg[1]);

		for (new i = 0; i < count; i++)
		{
			if (get_user_userid(players[i]) == userid)
				return players[i];
		}

		console_print(id, "%L", id, "OLDS_SETSCORE_NO_USERID", arg);
		return 0;
	}

	new name[MAX_NAME_LENGTH];
	new match = 0;
	new matches = 0;

	for (new i = 0; i < count; i++)
	{
		get_user_name(players[i], name, charsmax(name));

		// An exact name wins even when it is also part of other names.
		if (equali(name, arg))
			return players[i];

		if (containi(name, arg) != -1)
		{
			match = players[i];
			matches++;
		}
	}

	if (matches == 1)
		return match;

	if (matches == 0)
		console_print(id, "%L", id, "OLDS_SETSCORE_NO_MATCH", arg);
	else
		console_print(id, "%L", id, "OLDS_SETSCORE_MANY_MATCH", matches, arg);

	return 0;
}

/**
 * Sends the player's scoreboard line to every client, so the new score shows
 * at once instead of after the next kill.
 */
send_score_info(id)
{
	message_begin(MSG_ALL, g_msgScoreInfo);
	write_byte(id);
	write_short(get_user_frags(id));
	write_short(cs_get_user_deaths(id));
	write_short(0);                         // class id, not used by Counter-Strike
	write_short(get_user_team(id));
	message_end();
}

/**
 * One line in the AMXX log (logs/L<date>.log), in the same form as the
 * stock admin commands.
 */
log_change(admin, target, oldFrags, oldDeaths, newFrags, newDeaths)
{
	new targetName[MAX_NAME_LENGTH], targetAuth[MAX_AUTHID_LENGTH];
	get_user_name(target, targetName, charsmax(targetName));
	get_user_authid(target, targetAuth, charsmax(targetAuth));

	if (admin)
	{
		new adminName[MAX_NAME_LENGTH], adminAuth[MAX_AUTHID_LENGTH];
		get_user_name(admin, adminName, charsmax(adminName));
		get_user_authid(admin, adminAuth, charsmax(adminAuth));

		log_amx("Cmd: ^"%s<%d><%s><>^" set the score of ^"%s<%d><%s><>^" from %d / %d to %d / %d",
			adminName, get_user_userid(admin), adminAuth,
			targetName, get_user_userid(target), targetAuth,
			oldFrags, oldDeaths, newFrags, newDeaths);
	}
	else
	{
		log_amx("Cmd: Server set the score of ^"%s<%d><%s><>^" from %d / %d to %d / %d",
			targetName, get_user_userid(target), targetAuth,
			oldFrags, oldDeaths, newFrags, newDeaths);
	}
}

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.