/*
* Comeback Card (olds.ro workshop card AMX-090)
* Copyright (C) 2026 daemon (olds.ro)
*
* 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/>.
*
* When a player is in the server with a valid identity, one read-only threaded SQLx query
* looks them up in the CMSStats player table and its ranks table, and the player alone
* sees: when they were last seen, their rank now and the points to the next rank. A new
* identity gets a "first visit" card. say /comeback shows the card again. At most
* olds_comeback_maxq lookups run at once; the rest wait in line. A reply for a player who
* left or changed identity is thrown away. Nothing is ever written to the database.
*/
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
#define PLUGIN "Comeback Card"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define KEY_LEN 64
#define CARD_LEN 192
#define IDENT_LEN 32
enum
{
ST_NONE = 0,
ST_WAITING,
ST_RUNNING,
ST_DONE,
ST_FAILED
}
new g_maxQ, g_keyMode, g_lastUnix
new g_table[IDENT_LEN], g_keyCol[IDENT_LEN], g_lastCol[IDENT_LEN], g_pointsCol[IDENT_LEN]
new g_ranks[IDENT_LEN], g_rankName[IDENT_LEN], g_rankPoints[IDENT_LEN]
new g_host[64], g_user[64], g_pass[64], g_db[64], g_driver[16]
new bool:g_authed[MAX_PLAYERS + 1], bool:g_inGame[MAX_PLAYERS + 1]
new g_state[MAX_PLAYERS + 1], g_serial[MAX_PLAYERS + 1]
new g_key[MAX_PLAYERS + 1][KEY_LEN]
new g_card[MAX_PLAYERS + 1][CARD_LEN]
new g_queue[MAX_PLAYERS], g_queued, g_running
new g_errors, g_lastErrorLog
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_comeback.txt")
bind_pcvar_num(create_cvar("olds_comeback_maxq", "3", FCVAR_NONE, "Lookups running at the same time", true, 1.0, true, 16.0), g_maxQ)
bind_pcvar_num(create_cvar("olds_comeback_key", "0", FCVAR_NONE, "Player key: 0 = SteamID, 1 = name, 2 = IP", true, 0.0, true, 2.0), g_keyMode)
bind_pcvar_num(create_cvar("olds_comeback_lastunix", "0", FCVAR_NONE, "1 = the last-seen column holds a unix time, 0 = a date-time", true, 0.0, true, 1.0), g_lastUnix)
bind_pcvar_string(create_cvar("olds_comeback_table", "csstats_players", FCVAR_NONE, "CMSStats player table"), g_table, charsmax(g_table))
bind_pcvar_string(create_cvar("olds_comeback_keycol", "steamid", FCVAR_NONE, "Column matched with the player key"), g_keyCol, charsmax(g_keyCol))
bind_pcvar_string(create_cvar("olds_comeback_lastcol", "last_join", FCVAR_NONE, "Last-seen column"), g_lastCol, charsmax(g_lastCol))
bind_pcvar_string(create_cvar("olds_comeback_pointscol", "skill", FCVAR_NONE, "Points column"), g_pointsCol, charsmax(g_pointsCol))
bind_pcvar_string(create_cvar("olds_comeback_ranks", "csstats_ranks", FCVAR_NONE, "Ranks table (empty = no ranks)"), g_ranks, charsmax(g_ranks))
bind_pcvar_string(create_cvar("olds_comeback_rankname", "name", FCVAR_NONE, "Rank name column"), g_rankName, charsmax(g_rankName))
bind_pcvar_string(create_cvar("olds_comeback_rankpoints", "points", FCVAR_NONE, "Column with the points a rank starts at"), g_rankPoints, charsmax(g_rankPoints))
bind_pcvar_string(create_cvar("olds_comeback_driver", "mysql", FCVAR_NONE, "SQLx driver: mysql or sqlite"), g_driver, charsmax(g_driver))
bind_pcvar_string(create_cvar("olds_comeback_host", "127.0.0.1", FCVAR_PROTECTED, "Database host"), g_host, charsmax(g_host))
bind_pcvar_string(create_cvar("olds_comeback_user", "", FCVAR_PROTECTED, "Database user (read-only rights are enough)"), g_user, charsmax(g_user))
bind_pcvar_string(create_cvar("olds_comeback_pass", "", FCVAR_PROTECTED, "Database password"), g_pass, charsmax(g_pass))
bind_pcvar_string(create_cvar("olds_comeback_db", "cmsstats", FCVAR_PROTECTED, "Database name (sqlite: the file in data/sqlite3)"), g_db, charsmax(g_db))
AutoExecConfig(true, "olds_comeback")
register_clcmd("say /comeback", "cmd_comeback")
register_clcmd("say_team /comeback", "cmd_comeback")
register_concmd("amx_comeback_status", "cmd_status", ADMIN_KICK, "- shows the comeback lookups")
}
/* ------------------------------------------------------------------ identity */
public client_authorized(id, const authid[])
{
g_authed[id] = true
try_start(id)
}
public client_putinserver(id)
{
g_inGame[id] = true
try_start(id)
}
public client_disconnected(id)
{
drop_player(id)
g_authed[id] = false
g_inGame[id] = false
g_card[id][0] = EOS
g_key[id][0] = EOS
}
/* A new name is a new identity when players are keyed by name. */
public client_infochanged(id)
{
if (g_keyMode != 1 || !g_inGame[id] || !g_key[id][0])
return
new name[KEY_LEN]
get_user_info(id, "name", name, charsmax(name))
if (equal(name, g_key[id]))
return
drop_player(id)
g_card[id][0] = EOS
g_key[id][0] = EOS
}
/* Forget the player's lookup: a reply still on its way will not match the serial. */
drop_player(id)
{
g_serial[id]++
for (new i = 0; i < g_queued; i++)
{
if (g_queue[i] != id)
continue
for (new k = i; k < g_queued - 1; k++)
g_queue[k] = g_queue[k + 1]
g_queued--
break
}
g_state[id] = ST_NONE
}
bool:player_key(id, out[], len)
{
if (g_keyMode == 1)
get_user_name(id, out, len)
else if (g_keyMode == 2)
get_user_ip(id, out, len, 1)
else
{
get_user_authid(id, out, len)
if (!equali(out, "STEAM_", 6) || contain(out, ":") < 0)
return false
}
return out[0] != EOS
}
try_start(id)
{
if (!g_authed[id] || !g_inGame[id] || g_state[id] != ST_NONE || is_user_hltv(id))
return
if (is_user_bot(id) && g_keyMode != 1)
return
if (!player_key(id, g_key[id], KEY_LEN - 1))
return
g_state[id] = ST_WAITING
g_queue[g_queued++] = id
pump()
}
/* ------------------------------------------------------------------ the queries */
bool:valid_ident(const text[])
{
if (!text[0])
return false
for (new i = 0; text[i]; i++)
if (!isalnum(text[i]) && text[i] != '_')
return false
return true
}
bool:config_ok()
{
if (!valid_ident(g_table) || !valid_ident(g_keyCol) || !valid_ident(g_lastCol) || !valid_ident(g_pointsCol))
return false
if (g_ranks[0] && (!valid_ident(g_ranks) || !valid_ident(g_rankName) || !valid_ident(g_rankPoints)))
return false
return true
}
pump()
{
while (g_queued > 0 && g_running < g_maxQ)
{
new id = g_queue[0]
for (new k = 0; k < g_queued - 1; k++)
g_queue[k] = g_queue[k + 1]
g_queued--
if (g_state[id] == ST_WAITING)
run_lookup(id)
}
}
run_lookup(id)
{
if (!config_ok())
{
g_state[id] = ST_FAILED
note_error("a table or column cvar holds something other than letters, digits and _")
return
}
if (!SQL_SetAffinity(g_driver))
{
g_state[id] = ST_FAILED
note_error("the SQLx driver in olds_comeback_driver is not loaded")
return
}
new quoted[KEY_LEN * 2], last[96], ranks[512], query[1024]
SQL_QuoteString(Empty_Handle, quoted, charsmax(quoted), g_key[id])
if (g_lastUnix)
formatex(last, charsmax(last), "p.%s", g_lastCol)
else if (equali(g_driver, "sqlite"))
formatex(last, charsmax(last), "CAST(strftime('%%s', p.%s) AS INTEGER)", g_lastCol)
else
formatex(last, charsmax(last), "UNIX_TIMESTAMP(p.%s)", g_lastCol)
if (g_ranks[0])
formatex(ranks, charsmax(ranks),
"(SELECT r.%s FROM %s r WHERE r.%s <= p.%s ORDER BY r.%s DESC LIMIT 1), (SELECT r.%s FROM %s r WHERE r.%s > p.%s ORDER BY r.%s ASC LIMIT 1), (SELECT r.%s FROM %s r WHERE r.%s > p.%s ORDER BY r.%s ASC LIMIT 1)",
g_rankName, g_ranks, g_rankPoints, g_pointsCol, g_rankPoints,
g_rankName, g_ranks, g_rankPoints, g_pointsCol, g_rankPoints,
g_rankPoints, g_ranks, g_rankPoints, g_pointsCol, g_rankPoints)
else
copy(ranks, charsmax(ranks), "NULL, NULL, NULL")
formatex(query, charsmax(query), "SELECT %s, p.%s, %s FROM %s p WHERE p.%s = '%s' LIMIT 1", last, g_pointsCol, ranks, g_table, g_keyCol, quoted)
new Handle:tuple = SQL_MakeDbTuple(g_host, g_user, g_pass, g_db, 4)
new data[2]
data[0] = id
data[1] = g_serial[id]
g_state[id] = ST_RUNNING
g_running++
SQL_ThreadQuery(tuple, "on_lookup", query, data, sizeof data)
SQL_FreeHandle(tuple)
}
note_error(const text[])
{
g_errors++
new now = get_systime()
if (g_lastErrorLog && now - g_lastErrorLog < 300)
return
g_lastErrorLog = now
log_amx("Comeback lookups fail: %s (%d failure(s) so far; logged at most every 5 minutes)", text, g_errors)
}
public on_lookup(failstate, Handle:query, error[], errnum, data[], size, Float:queuetime)
{
g_running = max(g_running - 1, 0)
new id = data[0]
new bool:current = id >= 1 && id <= MaxClients && g_inGame[id] && g_serial[id] == data[1] && g_state[id] == ST_RUNNING
if (failstate != TQUERY_SUCCESS)
{
note_error(error)
if (current)
g_state[id] = ST_FAILED
pump()
return
}
if (!current)
{
pump()
return
}
g_state[id] = ST_DONE
new name[MAX_NAME_LENGTH]
get_user_name(id, name, charsmax(name))
if (!SQL_NumResults(query))
{
formatex(g_card[id], CARD_LEN - 1, "%L", id, "CB_FIRST", name)
show_card(id)
pump()
return
}
new text[64]
SQL_ReadResult(query, 0, text, charsmax(text))
new seen = str_to_num(text)
SQL_ReadResult(query, 1, text, charsmax(text))
new points = floatround(str_to_float(text), floatround_floor)
new rank[48], next[48], nextPoints = -1
if (!SQL_IsNull(query, 2))
SQL_ReadResult(query, 2, rank, charsmax(rank))
if (!SQL_IsNull(query, 3))
SQL_ReadResult(query, 3, next, charsmax(next))
if (!SQL_IsNull(query, 4))
{
SQL_ReadResult(query, 4, text, charsmax(text))
nextPoints = floatround(str_to_float(text), floatround_ceil)
}
build_card(id, name, seen, points, rank, next, nextPoints)
show_card(id)
pump()
}
build_card(id, const name[], seen, points, const rank[], const next[], nextPoints)
{
new date[16], part1[96], part2[96]
if (seen > 0)
{
format_time(date, charsmax(date), "%Y-%m-%d", seen)
new days = max((get_systime() - seen) / 86400, 0)
formatex(part1, charsmax(part1), "%L", id, "CB_SEEN", name, date, days)
}
else
formatex(part1, charsmax(part1), "%L", id, "CB_SEEN_UNKNOWN", name)
if (!rank[0] && !next[0])
formatex(part2, charsmax(part2), "%L", id, "CB_NO_RANKS", points)
else if (!rank[0])
formatex(part2, charsmax(part2), "%L", id, "CB_NO_RANK", points, max(nextPoints - points, 0), next)
else if (!next[0] || nextPoints < 0)
formatex(part2, charsmax(part2), "%L", id, "CB_TOP_RANK", rank, points)
else
formatex(part2, charsmax(part2), "%L", id, "CB_RANK", rank, points, max(nextPoints - points, 0), next)
formatex(g_card[id], CARD_LEN - 1, "%s %s", part1, part2)
}
show_card(id)
{
if (is_user_connected(id) && g_card[id][0])
client_print(id, print_chat, "%s", g_card[id])
}
/* ------------------------------------------------------------------ commands */
public cmd_comeback(id)
{
if (g_card[id][0])
show_card(id)
else if (g_state[id] == ST_WAITING || g_state[id] == ST_RUNNING)
client_print(id, print_chat, "%L", id, "CB_LOADING")
else
{
g_state[id] = ST_NONE
client_print(id, print_chat, "%L", id, "CB_UNAVAILABLE")
try_start(id)
}
return PLUGIN_HANDLED
}
public cmd_status(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
console_print(id, "[Comeback] running %d of %d, waiting %d, failures %d, driver %s", g_running, g_maxQ, g_queued, g_errors, g_driver)
new name[MAX_NAME_LENGTH]
for (new p = 1; p <= MaxClients; p++)
{
if (!is_user_connected(p))
continue
get_user_name(p, name, charsmax(name))
console_print(id, "[Comeback] %s: state %d, key %s, card: %s", name, g_state[p], g_key[p], g_card[p])
}
return PLUGIN_HANDLED
}