Promuj serwer
Black Friday Koniec za 50% taniej: miejsce na Master Serverze i lista Client, z PREMIUM DOZYWOTNIO
03 Dni 14 Godz 49 Min 26 Sek
Zobacz oferty
csb-match-system / addons / amxmodx / scripting / csb_match_system.sma
404 lines 10.2 KiB Pawn Raw Permalink History
/*
 * CSB Match System
 * Copyright (C) 2026 counter-strike-boost.com
 *
 * A clan-war / scrim manager. amx_match runs a full match: a knife round for
 * side selection, an LO3 (three live restarts), MR12/MR15 live halves with an
 * automatic side swap at half time, and an end-of-match MOTD summary with each
 * player's kills and deaths. The whole thing is a small finite-state machine
 * driven by the round-win audio events, and match cvars are pulled from a cfg.
 *
 * Inspired by the eSL / WarMod family of match plugins. Independent GPL
 * re-implementation.
 *
 * 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. Distributed WITHOUT ANY WARRANTY. See the GNU General
 * Public License for more details: <https://www.gnu.org/licenses/>.
 */

#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <cstrike>
#include <fun>

new const PLUGIN[] = "CSB Match System"
new const VERSION[] = "1.0.0"
new const AUTHOR[]  = "counter-strike-boost.com"

enum _:MatchState
{
    MS_OFF = 0,
    MS_KNIFE,
    MS_PICK,
    MS_LIVE,
    MS_END
}

new g_iState = MS_OFF
new g_iMaxRounds = 15          // rounds per half (MR)
new g_iRoundsPlayed
new g_iTeamScore[2]            // 0 = team that started as T, 1 = started as CT
new bool:g_bSwapped
new bool:g_bHalfDone
new g_iKnifeWinnerTeam         // 0 or 1
new g_iKills[33]
new g_iDeaths[33]

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)

    register_clcmd("amx_match", "cmdMatch", ADMIN_RCON, "<rounds per half> - start a match (default 15)")
    register_clcmd("amx_matchstop", "cmdMatchStop", ADMIN_RCON, "- abort the running match")

    register_clcmd("say /stay", "cmdStay")
    register_clcmd("say /switch", "cmdSwitch")
    register_clcmd("say /score", "cmdScore")

    RegisterHam(Ham_Spawn, "player", "fwSpawnPost", 1)

    register_event("DeathMsg", "evDeath", "a")
    register_event("SendAudio", "evTerWin", "a", "2=%!MRAD_terwin")
    register_event("SendAudio", "evCtWin", "a", "2=%!MRAD_ctwin")

    set_task(1.0, "taskHud", _, _, _, "b")
}

public cmdMatch(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED

    if (g_iState != MS_OFF)
    {
        console_print(id, "[Match] A match is already running. Use amx_matchstop first.")
        return PLUGIN_HANDLED
    }

    new arg[8]
    read_argv(1, arg, charsmax(arg))
    new mr = str_to_num(arg)
    if (mr < 1) mr = 15
    g_iMaxRounds = mr

    // Pull match rules from a cfg (create addons/amxmodx/configs/csb_match.cfg).
    server_cmd("exec csb_match.cfg")

    resetStats()
    g_iTeamScore[0] = 0
    g_iTeamScore[1] = 0
    g_iRoundsPlayed = 0
    g_bSwapped = false
    g_bHalfDone = false
    g_iState = MS_KNIFE

    announce("^x04[Match]^x01 MR%d match starting -- ^x04KNIFE ROUND^x01 for side choice!", mr)
    server_cmd("sv_restartround 3")
    return PLUGIN_HANDLED
}

public cmdMatchStop(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED

    g_iState = MS_OFF
    remove_task(0)
    announce("^x04[Match]^x01 The match was aborted by an admin.")
    return PLUGIN_HANDLED
}

public fwSpawnPost(id)
{
    if (g_iState != MS_KNIFE || !is_user_alive(id))
        return

    // Knife round: knife only, no money.
    strip_user_weapons(id)
    give_item(id, "weapon_knife")
    cs_set_user_money(id, 0, 1)
}

public evDeath()
{
    if (g_iState != MS_LIVE)
        return

    new killer = read_data(1)
    new victim = read_data(2)

    if (victim >= 1 && victim <= 32)
        g_iDeaths[victim]++

    if (killer >= 1 && killer <= 32 && killer != victim)
        g_iKills[killer]++
}

public evTerWin()
{
    onRoundWon(true)
}

public evCtWin()
{
    onRoundWon(false)
}

onRoundWon(bool:tWon)
{
    switch (g_iState)
    {
        case MS_KNIFE:
        {
            // Whoever won the knife round chooses the side.
            g_iKnifeWinnerTeam = tSideTeam(tWon)
            g_iState = MS_PICK
            new sideName[24]
            copy(sideName, charsmax(sideName), tWon ? "Terrorists" : "Counter-Terrorists")
            announce("^x04[Match]^x01 %s won the knife round! A winner: type ^x04/stay^x01 or ^x04/switch^x01.", sideName)
        }
        case MS_LIVE:
        {
            new teamWon = tSideTeam(tWon)
            g_iTeamScore[teamWon]++
            g_iRoundsPlayed++

            announce("^x04[Match]^x01 Score -- Team A: %d  Team B: %d  (round %d)", g_iTeamScore[0], g_iTeamScore[1], g_iRoundsPlayed)

            checkProgress()
        }
    }
}

// which match-team (0 or 1) is currently on the given side
tSideTeam(bool:isT)
{
    if (isT)
        return g_bSwapped ? 1 : 0
    return g_bSwapped ? 0 : 1
}

checkProgress()
{
    new winScore = g_iMaxRounds + 1

    if (g_iTeamScore[0] >= winScore || g_iTeamScore[1] >= winScore)
    {
        endMatch()
        return
    }

    if (!g_bHalfDone && g_iRoundsPlayed >= g_iMaxRounds)
    {
        halfTime()
        return
    }

    if (g_iRoundsPlayed >= g_iMaxRounds * 2)
    {
        endMatch()
        return
    }
}

halfTime()
{
    g_bHalfDone = true
    g_bSwapped = !g_bSwapped

    announce("^x04[Match]^x01 HALF TIME -- swapping sides.")

    new players[32], num
    get_players(players, num)
    for (new i = 0; i < num; i++)
    {
        new CsTeams:team = cs_get_user_team(players[i])
        if (team == CS_TEAM_T)
            cs_set_user_team(players[i], CS_TEAM_CT)
        else if (team == CS_TEAM_CT)
            cs_set_user_team(players[i], CS_TEAM_T)
    }

    startLive(false)
}

public cmdStay(id)
{
    if (g_iState != MS_PICK)
        return PLUGIN_HANDLED
    if (!onTeam(id, g_iKnifeWinnerTeam))
    {
        client_print(id, print_chat, "[Match] Only the knife-round winners choose the side.")
        return PLUGIN_HANDLED
    }
    announce("^x04[Match]^x01 Winners chose to STAY. Going live!")
    startLive(true)
    return PLUGIN_HANDLED
}

public cmdSwitch(id)
{
    if (g_iState != MS_PICK)
        return PLUGIN_HANDLED
    if (!onTeam(id, g_iKnifeWinnerTeam))
    {
        client_print(id, print_chat, "[Match] Only the knife-round winners choose the side.")
        return PLUGIN_HANDLED
    }

    new players[32], num
    get_players(players, num)
    for (new i = 0; i < num; i++)
    {
        new CsTeams:team = cs_get_user_team(players[i])
        if (team == CS_TEAM_T)
            cs_set_user_team(players[i], CS_TEAM_CT)
        else if (team == CS_TEAM_CT)
            cs_set_user_team(players[i], CS_TEAM_T)
    }
    g_bSwapped = !g_bSwapped

    announce("^x04[Match]^x01 Winners chose to SWITCH sides. Going live!")
    startLive(true)
    return PLUGIN_HANDLED
}

bool:onTeam(id, matchTeam)
{
    new CsTeams:team = cs_get_user_team(id)
    if (team != CS_TEAM_T && team != CS_TEAM_CT)
        return false
    return tSideTeam(team == CS_TEAM_T) == matchTeam
}

startLive(bool:doLo3)
{
    g_iState = MS_LIVE

    if (doLo3)
    {
        announce("^x04[Match]^x01 LIVE on three restarts...")
        server_cmd("sv_restartround 1")
        set_task(4.0, "taskLo3b")
        set_task(8.0, "taskLo3c")
    }
    else
    {
        server_cmd("sv_restartround 3")
    }
}

public taskLo3b()
{
    announce("^x04[Match]^x01 Restart 2 of 3...")
    server_cmd("sv_restartround 1")
}

public taskLo3c()
{
    announce("^x04[Match]^x01 Restart 3 of 3 -- ^x04LIVE! LIVE! LIVE!^x01")
    server_cmd("sv_restartround 1")
}

endMatch()
{
    g_iState = MS_END

    new winner[16]
    if (g_iTeamScore[0] > g_iTeamScore[1])
        copy(winner, charsmax(winner), "Team A")
    else if (g_iTeamScore[1] > g_iTeamScore[0])
        copy(winner, charsmax(winner), "Team B")
    else
        copy(winner, charsmax(winner), "Nobody (draw)")

    announce("^x04[Match]^x01 MATCH OVER -- %s wins %d : %d", winner, g_iTeamScore[0], g_iTeamScore[1])

    showSummary()

    g_iState = MS_OFF
}

showSummary()
{
    new body[1536], line[192]

    formatex(body, charsmax(body), "<body bgcolor=#000000><font color=#FFFFFF><h3>CSB Match Summary</h3>")
    format(body, charsmax(body), "%s<p>Final score -- Team A: %d &nbsp; Team B: %d</p><pre>", body, g_iTeamScore[0], g_iTeamScore[1])
    format(body, charsmax(body), "%s%-22s %5s %6s^n", body, "Player", "Kills", "Deaths")

    new players[32], num
    get_players(players, num)
    for (new i = 0; i < num; i++)
    {
        new pid = players[i]
        new name[24]
        get_user_name(pid, name, charsmax(name))
        formatex(line, charsmax(line), "%-22s %5d %6d^n", name, g_iKills[pid], g_iDeaths[pid])
        add(body, charsmax(body), line)
    }

    add(body, charsmax(body), "</pre></font></body>")

    for (new i = 0; i < num; i++)
        show_motd(players[i], body, "Match Summary")
}

public cmdScore(id)
{
    if (g_iState == MS_OFF)
    {
        client_print(id, print_chat, "[Match] No match is running.")
        return PLUGIN_HANDLED
    }
    client_print(id, print_chat, "[Match] Team A: %d  Team B: %d  (round %d, MR%d)", g_iTeamScore[0], g_iTeamScore[1], g_iRoundsPlayed, g_iMaxRounds)
    return PLUGIN_HANDLED
}

resetStats()
{
    for (new i = 1; i <= 32; i++)
    {
        g_iKills[i] = 0
        g_iDeaths[i] = 0
    }
}

public taskHud()
{
    if (g_iState == MS_OFF)
        return

    new stateName[24]
    switch (g_iState)
    {
        case MS_KNIFE: copy(stateName, charsmax(stateName), "KNIFE ROUND")
        case MS_PICK:  copy(stateName, charsmax(stateName), "SIDE PICK")
        case MS_LIVE:  copy(stateName, charsmax(stateName), "LIVE")
        case MS_END:   copy(stateName, charsmax(stateName), "FINISHED")
        default:       copy(stateName, charsmax(stateName), "MATCH")
    }

    set_hudmessage(255, 200, 0, 0.02, 0.02, 0, 0.0, 1.1, 0.0, 0.0, -1)
    show_hudmessage(0, "[%s]  A %d : %d B  (MR%d)", stateName, g_iTeamScore[0], g_iTeamScore[1], g_iMaxRounds)
}

announce(const fmt[], any:...)
{
    new msg[192]
    vformat(msg, charsmax(msg), fmt, 2)

    new players[32], num, msgid = get_user_msgid("SayText")
    get_players(players, num, "ch")

    for (new i = 0; i < num; i++)
    {
        message_begin(MSG_ONE, msgid, _, players[i])
        write_byte(players[i])
        write_string(msg)
        message_end()
    }
}