Skip to content
View in the app

A better way to browse. Learn more.

Learn more

wardrobe-ballot/addons/amxmodx/scripting/olds_wardrobe.sma

olds_wardrobe.sma
337 lines · 9.9 KB · Pawn (AMX Mod X)
Raw
/*
 * Wardrobe Ballot (olds.ro workshop card AMX-069)
 *
 * Jailbreak: an inmate says /theme and the inmates vote, for
 * olds_wardrobe_secs seconds, on a cosmetic theme from the packs the server's
 * wardrobe core has installed. The theme with the most votes is applied to
 * every inmate through the core, and the core's default comes back at round
 * end.
 *
 *   say /theme       start the vote (inmates, once a round) or reopen the menu
 *   say /theme <n>   vote for theme n of the menu directly
 *
 * Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI, and a wardrobe core that
 * provides the natives in include/olds_wardrobe_core.inc (library
 * "wardrobe_core"): its registry of installed themes, a check that every file
 * of a theme is present and precached, a preview and an apply. Inmates are
 * the terrorists.
 *
 * Rules: only themes the core confirms complete are listed; an incomplete
 * one is left out and a server log line names the missing file (once per
 * map). Each inmate has one vote: voting again replaces it. Choosing a theme
 * in the menu also asks the core for a preview. Ties go to the theme listed
 * first. No votes: nothing changes.
 *
 * Cvar: olds_wardrobe_secs 20 (5-60). Command: olds_wardrobe_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

/* Wardrobe core API (include/olds_wardrobe_core.inc), declared here so the plugin compiles on its own. */
#pragma reqlib "wardrobe_core"
native wardrobe_theme_count()
native wardrobe_theme_check(theme, name[], nameLen, missing[], missingLen)
native wardrobe_preview(id, theme)
native wardrobe_apply(id, theme)
const WARDROBE_DEFAULT = -1

#define PLUGIN  "Wardrobe Ballot"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_THEMES = 32
const THEME_NAME = 32
const TASK_VOTE = 6911

new g_listed[MAX_THEMES]                 // menu position -> core theme index
new g_listedName[MAX_THEMES][THEME_NAME]
new g_listedCount
new bool:g_reported[MAX_THEMES]          // missing file already logged this map

new bool:g_voting
new bool:g_doneThisRound
new g_vote[MAX_PLAYERS + 1]              // menu position + 1, 0 = no vote
new bool:g_applied[MAX_PLAYERS + 1]
new g_winner = -1                        // core theme index applied this round
new Float:g_nextUse[MAX_PLAYERS + 1]

new g_secs

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

    bind_pcvar_num(create_cvar("olds_wardrobe_secs", "20", FCVAR_NONE,
        "Seconds the theme vote stays open (5-60).", true, 5.0, true, 60.0), g_secs)
    AutoExecConfig(true, "olds_wardrobe")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")
    register_concmd("olds_wardrobe_status", "cmd_status", ADMIN_CFG, "- themes, votes and the applied theme")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
}

bool:is_inmate(id)
{
    return is_user_connected(id) && TeamName:get_member(id, m_iTeam) == TEAM_TERRORIST
}

/* ---------- the core's registry ---------- */

/* Lists the complete themes; logs each incomplete one once per map. */
read_registry()
{
    g_listedCount = 0
    new total = wardrobe_theme_count()
    if (total > MAX_THEMES)
    {
        log_amx("the wardrobe core reports %d themes; only the first %d are offered", total, MAX_THEMES)
        total = MAX_THEMES
    }
    new name[THEME_NAME], missing[128]
    for (new theme = 0; theme < total; theme++)
    {
        name[0] = EOS
        missing[0] = EOS
        if (!wardrobe_theme_check(theme, name, charsmax(name), missing, charsmax(missing)) || !name[0])
        {
            if (!g_reported[theme])
            {
                g_reported[theme] = true
                log_amx("theme %d ^"%s^" is incomplete (%s missing or not precached): left out of the vote", theme,
                    name[0] ? name : "?", missing[0] ? missing : "a file")
            }
            continue
        }
        g_listed[g_listedCount] = theme
        copy(g_listedName[g_listedCount], charsmax(g_listedName[]), name)
        g_listedCount++
    }
}

/* ---------- /theme ---------- */

public on_say(id)
{
    if (!is_user_connected(id))
        return PLUGIN_CONTINUE
    new text[32]
    read_args(text, charsmax(text))
    remove_quotes(text)
    trim(text)
    if (!equali(text, "/theme", 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 + 0.5

    if (!is_inmate(id))
    {
        say_to(id, "WARDROBE_INMATES_ONLY")
        return PLUGIN_HANDLED
    }
    new arg[8]
    copy(arg, charsmax(arg), text[6])
    trim(arg)

    if (!g_voting)
    {
        if (g_doneThisRound)
        {
            say_to(id, "WARDROBE_DONE")
            return PLUGIN_HANDLED
        }
        if (!start_vote(id))
            return PLUGIN_HANDLED
    }
    if (arg[0])
    {
        new position = is_str_num(arg) ? str_to_num(arg) : 0
        if (!(1 <= position <= g_listedCount))
            say_to(id, "WARDROBE_NUMBER", g_listedCount)
        else
            cast_vote(id, position - 1)
    }
    else
        show_ballot(id)
    return PLUGIN_HANDLED
}

bool:start_vote(id)
{
    read_registry()
    if (!g_listedCount)
    {
        say_to(id, "WARDROBE_NO_THEMES")
        return false
    }
    arrayset(g_vote, 0, sizeof(g_vote))
    g_voting = true
    remove_task(TASK_VOTE)
    set_task(float(g_secs), "task_close_vote", TASK_VOTE)

    new starter[MAX_NAME_LENGTH], players[MAX_PLAYERS], count
    get_user_name(id, starter, charsmax(starter))
    get_players_ex(players, count, GetPlayers_ExcludeBots | GetPlayers_ExcludeHLTV | GetPlayers_MatchTeam, "TERRORIST")
    for (new i = 0; i < count; i++)
    {
        say_to(players[i], "WARDROBE_STARTED", starter, g_secs)
        if (players[i] != id)
            show_ballot(players[i])
    }
    return true
}

show_ballot(id)
{
    new menu = menu_create(fmt("%L", id, "WARDROBE_MENU_TITLE", g_secs), "on_ballot")
    for (new p = 0; p < g_listedCount; p++)
        menu_additem(menu, fmt("%s%s", g_listedName[p], g_vote[id] == p + 1 ? " *" : ""))
    menu_display(id, menu)
}

public on_ballot(id, menu, item)
{
    menu_destroy(menu)
    if (item < 0 || !g_voting || !is_inmate(id) || item >= g_listedCount)
        return PLUGIN_HANDLED
    cast_vote(id, item)
    wardrobe_preview(id, g_listed[item])
    return PLUGIN_HANDLED
}

/* One vote per inmate: a new one replaces it. */
cast_vote(id, position)
{
    new bool:changed = g_vote[id] && g_vote[id] != position + 1
    g_vote[id] = position + 1
    say_to(id, changed ? "WARDROBE_CHANGED" : "WARDROBE_VOTED", g_listedName[position])
}

public task_close_vote()
{
    if (!g_voting)
        return
    g_voting = false
    g_doneThisRound = true

    new tally[MAX_THEMES], votes
    for (new id = 1; id <= MaxClients; id++)
    {
        if (g_vote[id] && is_inmate(id))
        {
            tally[g_vote[id] - 1]++
            votes++
        }
        g_vote[id] = 0
    }
    if (!votes)
    {
        tell_inmates("WARDROBE_NO_VOTES", "", 0, 0)
        return
    }
    new best
    for (new p = 1; p < g_listedCount; p++)
        if (tally[p] > tally[best])
            best = p

    // The registry may have changed since the vote opened: check once more.
    new name[THEME_NAME], missing[128]
    if (!wardrobe_theme_check(g_listed[best], name, charsmax(name), missing, charsmax(missing)))
    {
        log_amx("theme %d ^"%s^" became incomplete (%s): not applied", g_listed[best], g_listedName[best],
            missing[0] ? missing : "a file")
        tell_inmates("WARDROBE_FAILED", g_listedName[best], 0, 0)
        return
    }
    g_winner = g_listed[best]
    new players[MAX_PLAYERS], count
    get_players_ex(players, count, GetPlayers_MatchTeam, "TERRORIST")
    for (new i = 0; i < count; i++)
    {
        wardrobe_apply(players[i], g_winner)
        g_applied[players[i]] = true
    }
    tell_inmates("WARDROBE_WON", g_listedName[best], tally[best], votes)
}

tell_inmates(const key[], const theme[], count, votes)
{
    new players[MAX_PLAYERS], total
    get_players_ex(players, total, GetPlayers_ExcludeBots | GetPlayers_ExcludeHLTV | GetPlayers_MatchTeam, "TERRORIST")
    for (new i = 0; i < total; i++)
        say_to(players[i], key, theme, count, votes)
}

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

restore_default()
{
    for (new id = 1; id <= MaxClients; id++)
    {
        if (g_applied[id] && is_user_connected(id))
            wardrobe_apply(id, WARDROBE_DEFAULT)
        g_applied[id] = false
        g_vote[id] = 0
    }
    g_winner = -1
    g_voting = false
    remove_task(TASK_VOTE)
}

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    restore_default()
    return HC_CONTINUE
}

public on_restart_round_pre()
{
    restore_default()
    g_doneThisRound = false
    return HC_CONTINUE
}

public client_disconnected(id)
{
    g_vote[id] = 0
    g_applied[id] = false
    g_nextUse[id] = 0.0
}

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

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[Wardrobe]^1 %s", message)
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    read_registry()
    console_print(id, "[Wardrobe] %d complete theme(s) of %d; vote %s; applied theme %d; vote time %d s",
        g_listedCount, wardrobe_theme_count(), g_voting ? "open" : "closed", g_winner, g_secs)
    for (new p = 0; p < g_listedCount; p++)
    {
        new votes
        for (new v = 1; v <= MaxClients; v++)
            if (g_vote[v] == p + 1)
                votes++
        console_print(id, "[Wardrobe]   %d. %s (core theme %d): %d vote(s)", p + 1, g_listedName[p], g_listed[p], votes)
    }
    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.