Skip to content
View in the app

A better way to browse. Learn more.

Learn more

observer-catch-up/addons/amxmodx/scripting/olds_catchup.sma

olds_catchup.sma
212 lines · 6.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Observer Catch-up (olds.ro workshop card AMX-034)
 *
 * A player who becomes a spectator gets the results of the last completed
 * rounds in chat, once, newest last:
 *     Round 6: Terrorists won (the bomb exploded)
 *     Round 7: Counter-Terrorists won (all terrorists eliminated)
 * Anyone can print them again with /recap.
 *
 * Results come from RG_RoundEnd (winner, reason, round number). A round is
 * recorded once even if the round end fires twice; a round closed by another
 * plugin's rg_round_end() (which skips the hookchain) is caught by a check
 * every second, with the winner the game stored. A match restart (sv_restart,
 * game commencing) clears the history.
 *
 * Spectator entry comes from the TeamInfo message: a player whose team turns
 * into SPECTATOR from anything else. Moving spectator to spectator again
 * (/spec twice) prints nothing.
 *
 * Cvar: olds_catchup_rounds 2 (1-5). Commands: say /recap,
 * olds_catchup_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Observer Catch-up"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_HISTORY = 5
const TASK_WATCH = 8411
const REASON_PLUGIN = -1          // ended by rg_round_end(): the reason is not known

new g_round                        // rounds started since the match (re)started
new g_recordedRound                // last round put in the history
new g_historyRound[MAX_HISTORY]
new WinStatus:g_historyWinner[MAX_HISTORY]
new g_historyReason[MAX_HISTORY]
new g_historyCount                 // entries, oldest first

new g_lastTeam[MAX_PLAYERS + 1][16]
new Float:g_nextUse[MAX_PLAYERS + 1]

new g_rounds

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_dictionary("olds_catchup.txt")

    bind_pcvar_num(create_cvar("olds_catchup_rounds", "2", FCVAR_NONE,
        "Completed rounds shown to a new spectator (1-5).", true, 1.0, true, float(MAX_HISTORY)), g_rounds)
    AutoExecConfig(true, "olds_catchup")

    register_clcmd("say /recap", "cmd_recap")
    register_clcmd("say_team /recap", "cmd_recap")
    register_concmd("olds_catchup_status", "cmd_status", ADMIN_CFG, "- the stored round results")

    register_event("TeamInfo", "on_team_info", "a")
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    set_task(1.0, "task_watch_round_end", TASK_WATCH, _, _, "b")
}

/* ---------- rounds ---------- */

public on_restart_round_pre()
{
    // sv_restart / game commencing: a new match, a new history.
    if (get_member_game(m_bCompleteReset))
        clear_history()
    g_round++
    return HC_CONTINUE
}

clear_history()
{
    g_round = 0
    g_recordedRound = 0
    g_historyCount = 0
}

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    if (event == ROUND_GAME_COMMENCE || event == ROUND_GAME_RESTART)
    {
        clear_history()
        return HC_CONTINUE
    }
    // Only a round end that really happened (a blocked call leaves the round running).
    if (get_member_game(m_bRoundTerminating))
        record(status, _:event)
    return HC_CONTINUE
}

public task_watch_round_end()
{
    if (g_round && g_recordedRound != g_round && get_member_game(m_bRoundTerminating))
    {
        new WinStatus:winner
        switch (get_member_game(m_iRoundWinStatus))
        {
            case 1: winner = WINSTATUS_CTS
            case 2: winner = WINSTATUS_TERRORISTS
            case 3: winner = WINSTATUS_DRAW
            default: winner = WINSTATUS_NONE
        }
        record(winner, REASON_PLUGIN)
    }
}

record(WinStatus:winner, reason)
{
    // One entry per round: a second round end for the same round is ignored.
    if (!g_round || g_recordedRound == g_round)
        return
    g_recordedRound = g_round

    if (g_historyCount == MAX_HISTORY)
    {
        for (new i = 1; i < MAX_HISTORY; i++)
        {
            g_historyRound[i - 1] = g_historyRound[i]
            g_historyWinner[i - 1] = g_historyWinner[i]
            g_historyReason[i - 1] = g_historyReason[i]
        }
        g_historyCount--
    }
    g_historyRound[g_historyCount] = g_round
    g_historyWinner[g_historyCount] = winner
    g_historyReason[g_historyCount] = reason
    g_historyCount++
}

/* ---------- spectators ---------- */

public on_team_info()
{
    new id = read_data(1)
    if (!(1 <= id <= MaxClients) || !is_user_connected(id))
        return
    new team[16]
    read_data(2, team, charsmax(team))
    new bool:entered = equal(team, "SPECTATOR") && !equal(g_lastTeam[id], "SPECTATOR")
    copy(g_lastTeam[id], charsmax(g_lastTeam[]), team)
    if (entered && g_historyCount)
        print_history(id)
}

public client_putinserver(id)
{
    g_lastTeam[id][0] = EOS
}

public client_disconnected(id)
{
    g_lastTeam[id][0] = EOS
    g_nextUse[id] = 0.0
}

public cmd_recap(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED
    new Float:now = get_gametime()
    if (now < g_nextUse[id])
        return PLUGIN_HANDLED
    g_nextUse[id] = now + 1.0

    if (!g_historyCount)
        client_print_color(id, print_team_default, "^4[Recap]^1 %L", id, "CATCHUP_NONE")
    else
        print_history(id)
    return PLUGIN_HANDLED
}

print_history(id)
{
    new winner[48], reason[64], key[24]
    for (new i = max(0, g_historyCount - g_rounds); i < g_historyCount; i++)
    {
        switch (g_historyWinner[i])
        {
            case WINSTATUS_CTS: formatex(winner, charsmax(winner), "%L", id, "CATCHUP_WIN_CT")
            case WINSTATUS_TERRORISTS: formatex(winner, charsmax(winner), "%L", id, "CATCHUP_WIN_T")
            case WINSTATUS_DRAW: formatex(winner, charsmax(winner), "%L", id, "CATCHUP_DRAW")
            default: formatex(winner, charsmax(winner), "%L", id, "CATCHUP_NO_WINNER")
        }
        if (g_historyReason[i] == REASON_PLUGIN || !(0 <= g_historyReason[i] <= _:ROUND_GAME_OVER))
            formatex(key, charsmax(key), "CATCHUP_REASON_UNKNOWN")
        else
            formatex(key, charsmax(key), "CATCHUP_REASON_%d", g_historyReason[i])
        formatex(reason, charsmax(reason), "%L", id, key)
        client_print_color(id, print_team_default, "^4[Recap]^1 %L", id, "CATCHUP_LINE", g_historyRound[i], winner,
            reason)
    }
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[Recap] round %d; %d result(s) stored; showing the last %d", g_round, g_historyCount, g_rounds)
    for (new i = 0; i < g_historyCount; i++)
        console_print(id, "[Recap] round %d: winner %d, reason %d", g_historyRound[i], _:g_historyWinner[i],
            g_historyReason[i])
    return PLUGIN_HANDLED
}

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.