Skip to content
View in the app

A better way to browse. Learn more.

Learn more

afk-seat-offer/addons/amxmodx/scripting/olds_seat.sma

olds_seat.sma
331 lines · 9.7 KB · Pawn (AMX Mod X)
Raw
/*
 * AFK Seat Offer (olds.ro workshop card AMX-029)
 *
 * Spectators who want to play say /queue. When a player moves from a team to
 * spectator (by choice or moved there by an AFK manager), the seat they left
 * is offered to the first player in the queue with a yes/no menu. A yes puts
 * them on that team, after checking again that they are still here, still a
 * spectator, allowed, and that the team still has room. A no, or no answer in
 * olds_seat_timeout seconds, ends their offer and the seat goes to the next
 * player in the queue. Spectators who never said /queue are never asked.
 *
 *   say /queue          join the queue (again: see your place)
 *   say /queue leave    leave the queue
 *
 * Seat moves come from the TeamInfo message (team -> SPECTATOR). Admission is
 * by ReAPI rg_set_user_team; the player spawns at the next round.
 *
 * Room on a team: fewer players than the map's spawn points for it, within
 * mp_limitteams, and, when olds_seat_maxplayers is above 0, fewer than that
 * many players on both teams together.
 *
 * Cvars: olds_seat_maxplayers 0 (0 = the game's limits only),
 *        olds_seat_timeout 10 (5-60 seconds),
 *        olds_seat_flags "" (admin flags needed to take a seat; empty = everyone).
 * Command: olds_seat_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  "AFK Seat Offer"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

new g_queue[MAX_PLAYERS]              // player ids, first in line first
new g_queueLength

new TeamName:g_team[MAX_PLAYERS + 1]  // last team seen in TeamInfo
new TeamName:g_offerTeam[MAX_PLAYERS + 1]   // seat offered to this player, TEAM_UNASSIGNED = none
new g_offerMenu[MAX_PLAYERS + 1]
new Float:g_nextUse[MAX_PLAYERS + 1]

new g_maxPlayers
new g_timeout
new g_flags[24]

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

    bind_pcvar_num(create_cvar("olds_seat_maxplayers", "0", FCVAR_NONE,
        "Most players on both teams together; 0 = the game's own limits.", true, 0.0, true, float(MAX_PLAYERS)), g_maxPlayers)
    bind_pcvar_num(create_cvar("olds_seat_timeout", "10", FCVAR_NONE,
        "Seconds a seat offer stays open (5-60).", true, 5.0, true, 60.0), g_timeout)
    bind_pcvar_string(create_cvar("olds_seat_flags", "", FCVAR_NONE,
        "Admin flags needed to take a seat (empty = everyone)."), g_flags, charsmax(g_flags))
    AutoExecConfig(true, "olds_seat")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")
    register_concmd("olds_seat_status", "cmd_status", ADMIN_CFG, "- the queue and open offers")

    register_event("TeamInfo", "on_team_info", "a")
}

/* ---------- queue ---------- */

queue_place(id)
{
    for (new i = 0; i < g_queueLength; i++)
        if (g_queue[i] == id)
            return i
    return -1
}

queue_remove(id)
{
    new place = queue_place(id)
    if (place < 0)
        return
    for (new i = place + 1; i < g_queueLength; i++)
        g_queue[i - 1] = g_queue[i]
    g_queueLength--
}

bool:is_spectator(id)
{
    new TeamName:team = TeamName:get_member(id, m_iTeam)
    return team == TEAM_SPECTATOR || team == TEAM_UNASSIGNED
}

public on_say(id)
{
    if (!is_user_connected(id))
        return PLUGIN_CONTINUE

    new text[64]
    read_args(text, charsmax(text))
    remove_quotes(text)
    trim(text)
    if (!equali(text, "/queue", 6) || (text[6] && text[6] != ' '))
        return PLUGIN_CONTINUE

    new Float:now = get_gametime()
    if (now < g_nextUse[id])
        return PLUGIN_HANDLED
    g_nextUse[id] = now + 1.0

    new argument[16]
    copy(argument, charsmax(argument), text[6])
    trim(argument)

    if (equali(argument, "leave"))
    {
        if (queue_place(id) >= 0)
        {
            queue_remove(id)
            say_to(id, "SEAT_LEFT_QUEUE")
        }
        else
            say_to(id, "SEAT_NOT_QUEUED")
        return PLUGIN_HANDLED
    }

    if (!is_spectator(id))
    {
        say_to(id, "SEAT_ALREADY_PLAYING")
        return PLUGIN_HANDLED
    }
    new place = queue_place(id)
    if (place < 0)
    {
        g_queue[g_queueLength++] = id
        place = g_queueLength - 1
        say_to(id, "SEAT_QUEUED", place + 1)
    }
    else
        say_to(id, "SEAT_PLACE", place + 1, g_queueLength)
    return PLUGIN_HANDLED
}

/* ---------- seats ---------- */

public on_team_info()
{
    new id = read_data(1)
    if (!(1 <= id <= MaxClients) || !is_user_connected(id))
        return

    new name[16], TeamName:team
    read_data(2, name, charsmax(name))
    switch (name[0])
    {
        case 'T': team = TEAM_TERRORIST
        case 'C': team = TEAM_CT
        case 'S': team = TEAM_SPECTATOR
        default: team = TEAM_UNASSIGNED
    }
    new TeamName:previous = g_team[id]
    g_team[id] = team
    if (team == previous)
        return

    if (team == TEAM_TERRORIST || team == TEAM_CT)
    {
        // Took a seat by themselves: no longer waiting.
        queue_remove(id)
        return
    }
    if (team == TEAM_SPECTATOR && (previous == TEAM_TERRORIST || previous == TEAM_CT))
        offer_seat(previous)
}

/* Offers a seat on `team` to the first queued player who has no open offer. */
offer_seat(TeamName:team)
{
    for (new i = 0; i < g_queueLength; i++)
    {
        new id = g_queue[i]
        if (g_offerTeam[id] != TEAM_UNASSIGNED || !is_user_connected(id) || !is_spectator(id))
            continue
        show_offer(id, team)
        return
    }
}

show_offer(id, TeamName:team)
{
    new title[128], yes[48], no[48], teamName[32]
    team_name(id, team, teamName, charsmax(teamName))
    formatex(title, charsmax(title), "%L", id, "SEAT_MENU_TITLE", teamName, g_timeout)
    formatex(yes, charsmax(yes), "%L", id, "SEAT_MENU_YES")
    formatex(no, charsmax(no), "%L", id, "SEAT_MENU_NO")

    new menu = menu_create(title, "on_offer_answer")
    menu_additem(menu, yes, "1")
    menu_additem(menu, no, "0")
    menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)

    g_offerTeam[id] = team
    g_offerMenu[id] = menu
    menu_display(id, menu, 0, g_timeout)
}

public on_offer_answer(id, menu, item)
{
    // Only the menu that is this player's open offer counts.
    new bool:current = 1 <= id <= MaxClients && g_offerMenu[id] == menu && g_offerTeam[id] != TEAM_UNASSIGNED
    new TeamName:team = current ? g_offerTeam[id] : TEAM_UNASSIGNED

    new bool:accepted
    if (current && item >= 0)
    {
        new info[4], access, callback
        menu_item_getinfo(menu, item, access, info, charsmax(info), _, _, callback)
        accepted = info[0] == '1'
    }
    menu_destroy(menu)
    if (!current)
        return PLUGIN_HANDLED

    g_offerTeam[id] = TEAM_UNASSIGNED
    g_offerMenu[id] = 0

    if (!accepted)
    {
        // Declined or expired: out of the queue, and the seat goes to the next player.
        queue_remove(id)
        if (is_user_connected(id))
            say_to(id, item == MENU_TIMEOUT ? "SEAT_EXPIRED" : "SEAT_DECLINED")
        offer_seat(team)
        return PLUGIN_HANDLED
    }

    admit(id, team)
    return PLUGIN_HANDLED
}

admit(id, TeamName:team)
{
    queue_remove(id)
    if (!is_user_connected(id))
        return
    if (!is_spectator(id))
    {
        say_to(id, "SEAT_ALREADY_PLAYING")
        return
    }
    if (g_flags[0] && !(get_user_flags(id) & read_flags(g_flags)))
    {
        say_to(id, "SEAT_NO_ACCESS")
        offer_seat(team)
        return
    }
    if (!team_has_room(team))
    {
        say_to(id, "SEAT_TEAM_FULL")
        return
    }
    rg_set_user_team(id, team, MODEL_AUTO, true)
    new teamName[32]
    team_name(id, team, teamName, charsmax(teamName))
    say_to(id, "SEAT_ADMITTED", teamName)
}

bool:team_has_room(TeamName:team)
{
    new terrorists = get_playersnum_ex(GetPlayers_MatchTeam, "TERRORIST")
    new cts = get_playersnum_ex(GetPlayers_MatchTeam, "CT")
    new mine = team == TEAM_CT ? cts : terrorists, other = team == TEAM_CT ? terrorists : cts

    new spawns = get_member_game(team == TEAM_CT ? m_iSpawnPointCount_CT : m_iSpawnPointCount_Terrorist)
    if (spawns > 0 && mine >= spawns)
        return false
    new limit = get_cvar_num("mp_limitteams")
    if (limit > 0 && mine + 1 - other > limit)
        return false
    if (g_maxPlayers > 0 && terrorists + cts >= g_maxPlayers)
        return false
    return true
}

public client_disconnected(id)
{
    queue_remove(id)
    g_team[id] = TEAM_UNASSIGNED
    g_nextUse[id] = 0.0
    new TeamName:team = g_offerTeam[id]
    g_offerTeam[id] = TEAM_UNASSIGNED
    g_offerMenu[id] = 0
    // An open offer to someone who left goes to the next player.
    if (team != TEAM_UNASSIGNED)
        offer_seat(team)
}

/* ---------- helpers ---------- */

team_name(id, TeamName:team, name[], len)
{
    formatex(name, len, "%L", id, team == TEAM_CT ? "SEAT_TEAM_CT" : "SEAT_TEAM_T")
}

say_to(id, const key[], any:...)
{
    new pattern[160], message[192]
    formatex(pattern, charsmax(pattern), "%L", id, key)
    vformat(message, charsmax(message), pattern, 3)
    client_print_color(id, print_team_default, "^4[Seat]^1 %s", message)
}

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

    console_print(id, "[Seat] %d queued; T room %d, CT room %d; max players %d; offer time %d s",
        g_queueLength, team_has_room(TEAM_TERRORIST), team_has_room(TEAM_CT), g_maxPlayers, g_timeout)
    new name[MAX_NAME_LENGTH]
    for (new i = 0; i < g_queueLength; i++)
    {
        new player = g_queue[i]
        get_user_name(player, name, charsmax(name))
        console_print(id, "[Seat] %d. %s%s", i + 1, name,
            g_offerTeam[player] == TEAM_CT ? " (offered CT)" : (g_offerTeam[player] == TEAM_TERRORIST ? " (offered T)" : ""))
    }
    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.