Skip to content
View in the app

A better way to browse. Learn more.

Learn more

alltalk-vote/addons/amxmodx/scripting/olds_atvote.sma

olds_atvote.sma
252 lines · 7.5 KB · Pawn (AMX Mod X)
Raw
/*
 * Alltalk Vote (olds.ro workshop card LIB-0978)
 *
 * Map voting: olds_atv_delay seconds after the map starts, players vote for
 * alltalk on or off for this map. The side with more than olds_atv_ratio of
 * the votes sets sv_alltalk; a tie or no votes leaves it as it was.
 *   menu                   opens for everyone, and for players who join while
 *                          the vote runs
 *   olds_atv_vote <1|0>    the same vote from the console
 *   amx_voteat             starts a vote now (ADMIN_VOTE)
 *   amx_atv_status         the vote's state (ADMIN_VOTE)
 *
 * Cvars
 *   olds_atv_delay 30    seconds after the map start before the vote (0 = no
 *                        vote at map start)
 *   olds_atv_time 15     seconds the vote runs
 *   olds_atv_ratio 0.5   share of the votes a side needs to exceed
 *   olds_atv_once 1      one vote per map (amx_voteat is refused after it)
 *
 * The vote start plays the stock buttons/blip1.wav; text and result go to chat.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>

#define PLUGIN  "Alltalk Vote"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const TASK_START = 9781
const TASK_END = 9782

new Float:g_delay, g_time, Float:g_ratio, g_once

new bool:g_running
new g_menu = -1
new g_yes, g_no
new g_votesRun
new Float:g_endsAt
new bool:g_voted[MAX_PLAYERS + 1]
new g_alltalk

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_dictionary("olds_atvote.txt")
    bind_pcvar_float(create_cvar("olds_atv_delay", "30", FCVAR_NONE,
        "Seconds after the map start before the alltalk vote (0 = none at map start).", true, 0.0, true, 600.0), g_delay)
    bind_pcvar_num(create_cvar("olds_atv_time", "15", FCVAR_NONE,
        "Seconds the alltalk vote runs.", true, 5.0, true, 120.0), g_time)
    bind_pcvar_float(create_cvar("olds_atv_ratio", "0.5", FCVAR_NONE,
        "Share of the votes a side must exceed to win.", true, 0.0, true, 0.99), g_ratio)
    bind_pcvar_num(create_cvar("olds_atv_once", "1", FCVAR_NONE,
        "1 = only one alltalk vote per map.", true, 0.0, true, 1.0), g_once)
    AutoExecConfig(true, "olds_atvote")

    g_alltalk = get_cvar_pointer("sv_alltalk")
    register_concmd("amx_voteat", "cmd_voteat", ADMIN_VOTE, "- starts an alltalk vote")
    register_concmd("amx_atv_status", "cmd_status", ADMIN_VOTE, "- the alltalk vote's state")
    register_clcmd("olds_atv_vote", "cmd_vote")
}

public plugin_cfg()
{
    if (g_delay > 0.0)
        set_task(g_delay, "task_start", TASK_START)
}

public plugin_end()
{
    if (g_menu != -1)
        menu_destroy(g_menu)
}

public client_putinserver(id)
{
    g_voted[id] = false
    if (g_running && !is_user_bot(id))
        set_task(1.0, "task_show_late", id)
}

public client_disconnected(id)
{
    remove_task(id)
    g_voted[id] = false
}

/* ---------- the vote ---------- */

public task_start()
{
    start_vote(0)
}

// 0 started, 1 already running, 2 refused by olds_atv_once.
start_vote(by)
{
    if (g_running)
        return 1
    if (g_once && g_votesRun > 0)
        return 2
    g_running = true
    g_votesRun++
    g_yes = 0
    g_no = 0
    for (new p = 0; p <= MaxClients; p++)
        g_voted[p] = false
    g_endsAt = get_gametime() + float(g_time)

    g_menu = menu_create("\yAlltalk", "on_menu")
    menu_additem(g_menu, "On")
    menu_additem(g_menu, "Off")
    menu_setprop(g_menu, MPROP_EXIT, MEXIT_NEVER)

    new players[MAX_PLAYERS], count
    get_players(players, count, "ch")
    for (new i = 0; i < count; i++)
        show_vote(players[i])
    client_cmd(0, "spk buttons/blip1")
    client_print_color(0, print_team_default, "^4[Alltalk]^1 %L", LANG_PLAYER, "ATV_STARTED", g_time,
        get_pcvar_num(g_alltalk))
    if (by)
    {
        new name[MAX_NAME_LENGTH]
        get_user_name(by, name, charsmax(name))
        log_amx("alltalk vote started by %s", name)
    }
    set_task(float(g_time), "task_end", TASK_END)
    return 0
}

show_vote(id)
{
    if (!g_running || g_menu == -1 || !is_user_connected(id) || g_voted[id])
        return
    // Localised title and items for this player.
    menu_setprop(g_menu, MPROP_TITLE, fmt("\y%L", id, "ATV_MENU_TITLE"))
    menu_item_setname(g_menu, 0, fmt("%L", id, "ATV_MENU_ON"))
    menu_item_setname(g_menu, 1, fmt("%L", id, "ATV_MENU_OFF"))
    new left = floatround(g_endsAt - get_gametime(), floatround_ceil)
    if (left > 0)
        menu_display(id, g_menu, 0, left)
}

public task_show_late(id)
{
    show_vote(id)
}

public on_menu(id, menu, item)
{
    if (item >= 0 && menu == g_menu)
        cast(id, item == 0)
    return PLUGIN_HANDLED
}

public cmd_vote(id)
{
    new arg[4]
    read_argv(1, arg, charsmax(arg))
    if (arg[0] != '1' && arg[0] != '0')
        console_print(id, "[Alltalk] usage: olds_atv_vote <1|0>")
    else if (!g_running)
        client_print_color(id, print_team_default, "^4[Alltalk]^1 %L", id, "ATV_NO_VOTE")
    else
        cast(id, arg[0] == '1')
    return PLUGIN_HANDLED
}

cast(id, bool:on)
{
    if (!g_running || !is_user_connected(id))
        return
    if (g_voted[id])
    {
        client_print_color(id, print_team_default, "^4[Alltalk]^1 %L", id, "ATV_ALREADY")
        return
    }
    g_voted[id] = true
    if (on)
        g_yes++
    else
        g_no++
    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    if (on)
        client_print_color(0, id, "^4[Alltalk]^1 %L", LANG_PLAYER, "ATV_VOTED_ON", name)
    else
        client_print_color(0, id, "^4[Alltalk]^1 %L", LANG_PLAYER, "ATV_VOTED_OFF", name)
}

public task_end()
{
    if (!g_running)
        return
    g_running = false
    // Close the menu where it is still open.
    new oldMenu, newMenu
    for (new id = 1; id <= MaxClients; id++)
    {
        remove_task(id)
        if (is_user_connected(id) && player_menu_info(id, oldMenu, newMenu) && newMenu == g_menu)
            show_menu(id, 0, "^n", 1)
    }
    menu_destroy(g_menu)
    g_menu = -1

    new total = g_yes + g_no, before = get_pcvar_num(g_alltalk)
    if (total && float(g_yes) / float(total) > g_ratio)
    {
        set_cvar_num("sv_alltalk", 1)
        client_print_color(0, print_team_default, "^4[Alltalk]^1 %L", LANG_PLAYER, "ATV_RESULT_ON", g_yes, total)
    }
    else if (total && float(g_no) / float(total) > g_ratio)
    {
        set_cvar_num("sv_alltalk", 0)
        client_print_color(0, print_team_default, "^4[Alltalk]^1 %L", LANG_PLAYER, "ATV_RESULT_OFF", g_no, total)
    }
    else
        client_print_color(0, print_team_default, "^4[Alltalk]^1 %L", LANG_PLAYER, "ATV_RESULT_SAME", g_yes, g_no, before)
    log_amx("alltalk vote: %d on, %d off; sv_alltalk %d -> %d", g_yes, g_no, before, get_pcvar_num(g_alltalk))
}

/* ---------- admin ---------- */

public cmd_voteat(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    remove_task(TASK_START)
    switch (start_vote(id))
    {
        case 0: console_print(id, "[Alltalk] vote started (%d s)", g_time)
        case 1: console_print(id, "[Alltalk] a vote is already running")
        case 2: console_print(id, "[Alltalk] alltalk was already voted on this map (olds_atv_once 1)")
    }
    return PLUGIN_HANDLED
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new Float:left = 0.0
    if (g_running)
        left = g_endsAt - get_gametime()
    console_print(id, "[Alltalk] running %d, on %d, off %d, %.1f s left, votes this map %d, sv_alltalk %d, once %d, ratio %.2f",
        g_running, g_yes, g_no, left, g_votesRun, get_pcvar_num(g_alltalk), g_once, g_ratio)
    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.