Skip to content
View in the app

A better way to browse. Learn more.

Learn more

rematch-envelope/addons/amxmodx/scripting/olds_rematch.sma

olds_rematch.sma
332 lines · 9.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Rematch Envelope (olds.ro workshop card AMX-094)
 *
 * Knife Arena: when a duel ends, its two finishers get a private yes/no
 * invitation to a rematch. Only if both accept within olds_rematch_secs
 * seconds is the rematch handed to the duel manager for its next duel phase.
 *
 *   say /rematch         reopen your invitation menu (if you closed it)
 *   say /rematch yes|no  answer without the menu
 *
 * Needs: AMX Mod X 1.10 and a duel manager with the API in
 * include/olds_duel_manager.inc (library "duel_manager"): the forwards
 * duel_end(playerA, playerB, winner) and duel_phase_start(phase), and the
 * natives duel_is_eligible(id) and duel_enqueue_rematch(playerA, playerB).
 *
 * Rules: an invitation lasts olds_rematch_secs seconds and never past the
 * next duel phase start. Declining, or letting it run out, cancels it and
 * a finisher who had accepted is told it was declined. A finisher leaving
 * cancels it. Just before enqueueing, both players are checked again: same
 * people (user ids), still connected, and eligible for the manager. One
 * invitation gives at most one rematch entry.
 *
 * Cvar: olds_rematch_secs 12 (5-60). Command: olds_rematch_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>

#define PLUGIN  "Rematch Envelope"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

/* Duel manager API (include/olds_duel_manager.inc), declared here so the plugin compiles on its own. */
#pragma reqlib "duel_manager"
native duel_is_eligible(id)
native duel_enqueue_rematch(playerA, playerB)

const MAX_INVITES = 16
const TASK_EXPIRE = 9411

enum
{
    ANSWER_NONE = 0,
    ANSWER_YES,
    ANSWER_NO
}

new g_player[MAX_INVITES][2]             // 0 = free invitation
new g_userId[MAX_INVITES][2]
new g_answer[MAX_INVITES][2]
new g_inviteOf[MAX_PLAYERS + 1]          // invitation + 1, 0 = none
new Float:g_nextUse[MAX_PLAYERS + 1]
new g_enqueued

new g_secs

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

    bind_pcvar_num(create_cvar("olds_rematch_secs", "12", FCVAR_NONE,
        "Seconds both finishers have to accept a rematch (5-60).", true, 5.0, true, 60.0), g_secs)
    AutoExecConfig(true, "olds_rematch")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")
    register_concmd("olds_rematch_status", "cmd_status", ADMIN_CFG, "- open rematch invitations")
}

/* ---------- the duel manager ---------- */

/* A duel ended: invite both finishers. */
public duel_end(playerA, playerB, winner)
{
    if (!valid(playerA) || !valid(playerB) || playerA == playerB)
        return
    // Each player holds one invitation: a newer duel replaces the older one.
    cancel(playerA, "REMATCH_REPLACED")
    cancel(playerB, "REMATCH_REPLACED")

    new invite = -1
    for (new i = 0; i < MAX_INVITES && invite < 0; i++)
        if (!g_player[i][0])
            invite = i
    if (invite < 0)
    {
        log_amx("more than %d open rematch invitations: this duel gets none", MAX_INVITES)
        return
    }
    new players[2]
    players[0] = playerA
    players[1] = playerB
    for (new side = 0; side < 2; side++)
    {
        g_player[invite][side] = players[side]
        g_userId[invite][side] = get_user_userid(players[side])
        g_answer[invite][side] = ANSWER_NONE
        g_inviteOf[players[side]] = invite + 1
    }
    set_task(float(g_secs), "task_expire", TASK_EXPIRE + invite)
    for (new side = 0; side < 2; side++)
        show_invite(players[side])
}

/* A new duel phase: invitations do not carry past it. */
public duel_phase_start(phase)
{
    for (new invite = 0; invite < MAX_INVITES; invite++)
        if (g_player[invite][0])
            close_invite(invite, "REMATCH_EXPIRED", true)
}

bool:valid(id)
{
    return 1 <= id <= MaxClients && is_user_connected(id)
}

/* ---------- answers ---------- */

show_invite(id)
{
    new invite = g_inviteOf[id] - 1, side = (g_player[invite][1] == id) ? 1 : 0
    new other[MAX_NAME_LENGTH]
    get_user_name(g_player[invite][1 - side], other, charsmax(other))
    new menu = menu_create(fmt("%L", id, "REMATCH_TITLE", other, g_secs), "on_menu")
    menu_additem(menu, fmt("%L", id, "REMATCH_YES"))
    menu_additem(menu, fmt("%L", id, "REMATCH_NO"))
    menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)
    menu_display(id, menu, 0, g_secs)
}

public on_menu(id, menu, item)
{
    menu_destroy(menu)
    if (item < 0 || !g_inviteOf[id])
        return PLUGIN_HANDLED
    answer(id, item == 0)
    return PLUGIN_HANDLED
}

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, "/rematch", 8) || (text[8] && text[8] != ' '))
        return PLUGIN_CONTINUE

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

    if (!g_inviteOf[id])
    {
        say_to(id, "REMATCH_NONE")
        return PLUGIN_HANDLED
    }
    new word[8]
    copy(word, charsmax(word), text[8])
    trim(word)
    if (equali(word, "yes"))
        answer(id, true)
    else if (equali(word, "no"))
        answer(id, false)
    else
    {
        new invite = g_inviteOf[id] - 1, side = (g_player[invite][1] == id) ? 1 : 0
        if (g_answer[invite][side] == ANSWER_NONE)
            show_invite(id)
        else
            say_to(id, "REMATCH_ANSWERED")
    }
    return PLUGIN_HANDLED
}

answer(id, bool:yes)
{
    new invite = g_inviteOf[id] - 1, side = (g_player[invite][1] == id) ? 1 : 0
    if (g_answer[invite][side] != ANSWER_NONE)
    {
        say_to(id, "REMATCH_ANSWERED")
        return
    }
    if (!yes)
    {
        g_answer[invite][side] = ANSWER_NO
        say_to(id, "REMATCH_YOU_DECLINED")
        close_invite(invite, "REMATCH_DECLINED", false)
        return
    }
    g_answer[invite][side] = ANSWER_YES
    if (g_answer[invite][1 - side] != ANSWER_YES)
    {
        say_to(id, "REMATCH_WAITING")
        return
    }
    enqueue(invite)
}

/* Both accepted: check them again, then hand the rematch to the manager. */
enqueue(invite)
{
    new a = g_player[invite][0], b = g_player[invite][1]
    for (new side = 0; side < 2; side++)
    {
        new id = g_player[invite][side]
        if (!valid(id) || get_user_userid(id) != g_userId[invite][side])
        {
            close_invite(invite, "REMATCH_CANCELLED", true)
            return
        }
        if (!duel_is_eligible(id))
        {
            close_invite(invite, "REMATCH_NOT_ELIGIBLE", true)
            return
        }
    }
    free_invite(invite)
    if (!duel_enqueue_rematch(a, b))
    {
        say_to(a, "REMATCH_REFUSED")
        say_to(b, "REMATCH_REFUSED")
        return
    }
    g_enqueued++
    new nameA[MAX_NAME_LENGTH], nameB[MAX_NAME_LENGTH]
    get_user_name(a, nameA, charsmax(nameA))
    get_user_name(b, nameB, charsmax(nameB))
    say_to(a, "REMATCH_QUEUED", nameB)
    say_to(b, "REMATCH_QUEUED", nameA)
}

/* ---------- closing ---------- */

/* Tells who should know, then frees the invitation. everyone = both, else only who accepted. */
close_invite(invite, const key[], bool:everyone)
{
    for (new side = 0; side < 2; side++)
    {
        new id = g_player[invite][side]
        if (!valid(id) || get_user_userid(id) != g_userId[invite][side])
            continue
        if (everyone || g_answer[invite][side] == ANSWER_YES)
            say_to(id, key)
    }
    free_invite(invite)
}

free_invite(invite)
{
    remove_task(TASK_EXPIRE + invite)
    for (new side = 0; side < 2; side++)
    {
        new id = g_player[invite][side]
        if (1 <= id <= MaxClients && g_inviteOf[id] == invite + 1)
            g_inviteOf[id] = 0
        g_player[invite][side] = 0
        g_userId[invite][side] = 0
        g_answer[invite][side] = ANSWER_NONE
    }
}

public task_expire(taskId)
{
    new invite = taskId - TASK_EXPIRE
    if (0 <= invite < MAX_INVITES && g_player[invite][0])
        close_invite(invite, "REMATCH_EXPIRED", true)
}

cancel(id, const key[])
{
    if (g_inviteOf[id])
        close_invite(g_inviteOf[id] - 1, key, true)
}

public client_disconnected(id)
{
    g_nextUse[id] = 0.0
    if (!g_inviteOf[id])
        return
    new invite = g_inviteOf[id] - 1
    g_inviteOf[id] = 0
    // The one who stays is told; the one who left is gone.
    for (new side = 0; side < 2; side++)
        if (g_player[invite][side] == id)
            g_player[invite][side] = -1
    for (new side = 0; side < 2; side++)
    {
        new other = g_player[invite][side]
        if (valid(other) && other != id)
            say_to(other, "REMATCH_LEFT")
    }
    free_invite(invite)
}

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

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new const answers[][] = { "waiting", "yes", "no" }
    new open, nameA[MAX_NAME_LENGTH], nameB[MAX_NAME_LENGTH]
    for (new invite = 0; invite < MAX_INVITES; invite++)
    {
        if (!g_player[invite][0])
            continue
        open++
        get_user_name(g_player[invite][0], nameA, charsmax(nameA))
        get_user_name(g_player[invite][1], nameB, charsmax(nameB))
        console_print(id, "[Rematch]   %s (%s) vs %s (%s)", nameA, answers[g_answer[invite][0]], nameB,
            answers[g_answer[invite][1]])
    }
    console_print(id, "[Rematch] %d open invitation(s); %d rematch(es) enqueued this map; %d s to answer", open,
        g_enqueued, g_secs)
    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.