AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Problem with afk plugin (https://forums.alliedmods.net/showthread.php?t=163186)

FiFiX 07-27-2011 15:13

Problem with afk plugin
 
Code:

/* AMX Mod X
*  Full Server Idler Kicker (+last AFK to Spec)
*
* (c) Copyright 2006 by VEN
*
* This file is provided as is (no warranties)
*
*    DESCRIPTION
*      The concept of this plugin is "don't clean the full server from idlers unless
*      new connection try". By idlers i mean HLTV/unassigned/spectator/AFK users.
*      I created this plugin because i don't like the goal of all other plugins that
*      deal with idelrs using the conditions which is strictly based on the idle time.
*      This plugin isn't using the idle time as primary condition. So how it work?
*      Let's say the new user is trying to connect to the full server. Normally he
*      would be kicked unless he have a slot reservation access, but this plugin
*      able to clean the slot for this user if it will find the idler on the server.
*      Firstly it will try to find and kick HLTV (this can be disabled). Secondly -
*      the most idle unassigned/spectator user. Though the plugin will never kick
*      a client with immunity access (this can be configured) or the user who is just
*      connected and still unassigned - it will "immune" him for a few seconds
*      (this can be configured). And lastly the plugin will try to find and kick
*      the most idle alive AFK player (min. idle time that required to mark a player
*      as AFK can be configured). Also as a bonus feature the plugin able to detect
*      when all alive players in the same team are AFK. Usually it would be just
*      a single AFK player who is the only one alive player in a team. In this case
*      to avoid waiting/seaching AFK player(s) by other player(s) the plugin will move
*      him/them to the spectators. Later such AFK users can be kicked from Spectators
*      following the steeps that listed above. Requirement for full server kicker are
*      at least one free reserverved slot (controlled by amx_reservation AMXX's CVAR).
*/

#include <amxmodx>

#define PLUGIN_NAME "Full Server Idler Kicker"
#define PLUGIN_VERSION "0.1"
#define PLUGIN_AUTHOR "VEN"

// OPTIONS BELOW

// comment to disable access check on kick
#define IMMUNE_SPEC_ACCESS ADMIN_KICK

// connected user's unassigned kick immunity time
#define IMMUNE_TIME_AFTER_CONNECT 5

// AFK check interval (seconds)
#define AFK_CHECK_INTERVAL 5

// max. allowed AFK time (seconds)
#define MAX_ALLOWED_AFK_TIME 45

// comment to disable HLTV kick
//#define HLTV_KICK

#if defined HLTV_KICK
    new const g_hltv_kick_reason[] = "Sorry, HLTV isn't allowed on the full server"
#endif

// kick reasons
new const g_spec_kick_reason[] = "SPECTOWANIE na pelnym serwerze jest ZABRONIONE!"
new const g_afk_kick_reason[]  = "Zostales wykopany za SPECTOWANIE na pelnym serwerze!"

// AFK to Spectators transfer reason
new const g_afktospec_reason[] = "\yZostales przeniesiony na SPECT za bycie AFK"

// chat reasons
new const g_spec_kick_chat[] = "[AMXX] %s zostal wykopany za SPECTOWANIE na pelnym SERWERZE."
new const g_afk_kick_chat[]  = "[AMXX] %s zostal wykopany za bycie AFK na pelnym SERWERZE."
new const g_afktospec_chat[] = "[AMXX] %s zostal przeniesiony na SPECT za bycie AFK."

// OPTIONS ABOVE

new const g_teamname[2][] = {"TERRORIST", "CT"}

#define MAX_PLAYERS 32
new bool:g_connected[MAX_PLAYERS + 1]
new g_origin[MAX_PLAYERS + 1][3]
new g_afktime[MAX_PLAYERS + 1]
new g_specgametime[MAX_PLAYERS + 1]

new g_maxplayers
new g_pcvar_reservation

public plugin_init() {
    register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)

    register_event("TeamInfo", "event_spectate", "a", "2=UNASSIGNED", "2=SPECTATOR")
    register_event("TeamInfo", "event_playteam", "a", "2=TERRORIST", "2=CT")

    g_maxplayers = get_maxplayers()
    g_pcvar_reservation = get_cvar_pointer("amx_reservation")

    set_task(float(AFK_CHECK_INTERVAL), "task_afk_check", _, _, _, "b")
}

public client_connect(id) {
    g_connected[id] = true

    if (get_playersnum(1) <= g_maxplayers - get_pcvar_num(g_pcvar_reservation))
        return

#if defined HLTV_KICK
    if (is_user_hltv(id)) {
        client_kick(id, g_hltv_kick_reason)
        return
    }
#endif

    static players[32], num, i, tchar[2]
    new candidate, candidatetime
    get_players(players, num, "b")
    for (new x; x < num; ++x) {
        i = players[x]

#if defined IMMUNE_SPEC_ACCESS
        if (get_user_flags(i) & IMMUNE_SPEC_ACCESS)
            continue
#endif

        if (is_user_hltv(i)) {
#if defined HLTV_KICK
            client_kick(i, g_hltv_kick_reason)
            return
#else
            continue
#endif
        }

        get_user_team(i, tchar, 1)
        if (((tchar[0] == 'U' && get_user_time(i, 1) > IMMUNE_TIME_AFTER_CONNECT) || tchar[0] == 'S') && (!candidatetime || g_specgametime[i] < candidatetime)) {
            candidatetime = g_specgametime[i]
            candidate = i
        }
    }

    if (candidate) {
        chat_msg(candidate, g_spec_kick_chat)
        client_kick(candidate, g_spec_kick_reason)
        return
    }

    static origin[3], afktime
    get_players(players, num, "a")
    for (new x; x < num; ++x) {
        i = players[x]
        get_user_origin(i, origin)
        if (!is_user_afk(i, origin)) {
            g_afktime[i] = 0
            g_origin[i] = origin
            continue
        }

        afktime = g_afktime[i]
        if (afktime >= MAX_ALLOWED_AFK_TIME && afktime > candidatetime) {
            candidatetime = afktime
            candidate = i
        }
    }

    if (candidate) {
        chat_msg(candidate, g_afk_kick_chat)
        client_kick(candidate, g_afk_kick_reason)
    }
}

public task_afk_check() {
    static players[32], num, i, bool:allafk, origin[3]
    for (new a; a < 2; ++a) {
        get_players(players, num, "ae", g_teamname[a])
        allafk = true
        for (new x; x < num; ++x) {
            i = players[x]
            get_user_origin(i, origin)
            if (is_user_afk(i, origin)) {
                g_afktime[i] += AFK_CHECK_INTERVAL
                if (g_afktime[i] < MAX_ALLOWED_AFK_TIME)
                    allafk = false
            }
            else {
                g_afktime[i] = 0
                g_origin[i] = origin
                allafk = false
            }
        }

        if (!allafk)
            continue

        for (new x; x < num; ++x) {
            i = players[x]
            chat_msg(i, g_afktospec_chat)
            user_to_spec(i, g_afktospec_reason)
        }
    }
}

public event_spectate() {
    new id = read_data(1)
    if (g_connected[id] && !g_specgametime[id])
        g_specgametime[id] = floatround(get_gametime())
}

public event_playteam() {
    new id = read_data(1)
    if (g_connected[id])
        clear_vars(id)
}

public client_disconnect(id) {
    g_connected[id] = false
    clear_vars(id)
}

clear_vars(id) {
    g_origin[id][0] = 0
    g_origin[id][1] = 0
    g_origin[id][2] = 0
    g_afktime[id] = 0
    g_specgametime[id] = 0
}

bool:is_user_afk(id, const origin[3]) {
    return (origin[0] == g_origin[id][0] && origin[1] == g_origin[id][1])
}

chat_msg(id, const text[]) {
    static name[32]
    get_user_name(id, name, 31)
    client_print(0, print_chat, text, name)
}

stock client_kick(id, const reason[] = "") {
    server_cmd("kick #%d ^"%s^"", get_user_userid(id), reason)
    server_exec()
}

stock user_to_spec(id, const reason[] = "") {
    user_kill(id, 1)
    engclient_cmd(id, "jointeam", "6")
    show_menu(id, 1023, reason)
}

Tell me why this plugin kicks an admin with "c" flag? As I see, it shouldn't..


All times are GMT -4. The time now is 01:12.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.