#include <amxmodx>
#include <cstrike>
#include <fakemeta>
//#include <map_manager>
/* Основные настройки */
#define MAX_CHAR 64
#define ADMIN_LEVEL ADMIN_BAN
#define TIME_VOTE 15 /* (min: 5 | max: 30) */
#define TASK_VOTE_NEW 1488
#define MIN_PLAYERS 1
//#define COUNTDOWN // интервал между опросами
#define COLOR_MODE // поддержка цветов в меню: !y | !r | !w | !d (измените сами если хотите ниже)
new totalplayers;
new Percents[3][6];
const PDATA_SAFE = 2
const OFFSET_LINUX = 5
const OFFSET_CSMENUCODE = 205
new g_vote_name[33][MAX_CHAR]
new g_vote_it[33][3][MAX_CHAR]
new g_vote_current_it[33]
new g_it_votes[3]
new g_unlock[33]
new g_total_name[MAX_CHAR]
new g_total_it[3][MAX_CHAR]
new g_total_votes
#if defined COUNTDOWN
#define COUNTDOWN_TIME 200.0 // интервал между голосованиями (в секундах)
new Float:g_delay_vote_fix
#endif
public plugin_init()
{
register_plugin("[AMXX] Vote", "0.1", "Impossible")
register_clcmd("adminvote", "clcmd_vote_menu")
register_clcmd("vote_name", "function_vote_name")
register_clcmd("vote_item", "function_vote_item")
}
public client_putinserver(id)
{
g_unlock[id] = true
}
public client_disconnected(id)
{
g_unlock[id] = false
clear_vote_data(id)
}
public plugin_end()
{
remove_task(TASK_VOTE_NEW)
}
public clcmd_vote_menu(id)
{
if (!is_user_connected(id))
{
return PLUGIN_HANDLED;
}
vote_menu(id)
// todo
return PLUGIN_CONTINUE;
}
vote_menu(id)
{
static menu[128]
new menuid
menuid = menu_create("\wСоздать опрос", "vote_manager_handler")
if (g_vote_name[id][0])
{
formatex(menu, charsmax(menu), "Титул\d: \y%s^n", g_vote_name[id])
}
else
{
formatex(menu, charsmax(menu), "Титул\d: ---^n")
}
menu_additem(menuid, menu, "1", get_user_flags(id) & ADMIN_LEVEL ? 0 : (1 << 31))
if (g_vote_it[id][0][0])
{
formatex(menu, charsmax(menu), "\d[#] \y%s", g_vote_it[id][0])
}
else
{
formatex(menu, charsmax(menu), "добавить..")
}
menu_additem(menuid, menu, "2", get_user_flags(id) & ADMIN_LEVEL ? 0 : (1 << 31))
if (g_vote_it[id][1][0])
{
formatex(menu, charsmax(menu), "\d[#] \y%s", g_vote_it[id][1])
}
else
{
formatex(menu, charsmax(menu), "%sдобавить..", g_vote_it[id][0][0] ? "\w" : "\d")
}
menu_additem(menuid, menu, "3", get_user_flags(id) & ADMIN_LEVEL ? 0 : (1 << 31))
if (g_vote_it[id][2][0])
{
formatex(menu, charsmax(menu), "\d[#] \y%s^n", g_vote_it[id][2])
}
else
{
formatex(menu, charsmax(menu), "%sдобавить..^n", g_vote_it[id][1][0] ? "\w" : "\d")
}
menu_additem(menuid, menu, "4", get_user_flags(id) & ADMIN_LEVEL ? 0 : (1 << 31))
formatex(menu, charsmax(menu), "%sОчистить^n", (g_vote_name[id][0] || g_vote_it[id][0][0]) ? "\w" : "\d")
menu_additem(menuid, menu, "5", get_user_flags(id) & ADMIN_LEVEL ? (g_vote_name[id][0] || g_vote_it[id][0][0]) ? 0 : (1 << 31) : (1 << 31))
formatex(menu, charsmax(menu), "%sЗапустить опрос", (GetPlayingCount() < MIN_PLAYERS || !g_vote_name[id][0] || !g_vote_it[id][1][0] || task_exists(TASK_VOTE_NEW) /*|| is_vote_started()*/) ? "\d" : "\w")
menu_additem(menuid, menu, "6", get_user_flags(id) & ADMIN_LEVEL ? 0 : (1 << 31))
menu_setprop(menuid, MPROP_EXITNAME, "Выход")
if (pev_valid(id) == PDATA_SAFE)
{
set_pdata_int(id, OFFSET_CSMENUCODE, 0, OFFSET_LINUX)
}
menu_display(id, menuid, 0)
}
public vote_manager_handler(id, menuid, item)
{
if (item == MENU_EXIT)
{
menu_destroy(menuid)
// todo
return PLUGIN_HANDLED;
}
if (!(get_user_flags(id) & ADMIN_LEVEL))
{
return PLUGIN_HANDLED; // oops (something went wrong)
}
switch (item)
{
case 0:
{
client_cmd(id, "messagemode ^"vote_name^"")
// todo
g_unlock[id] = false
}
case 1, 2, 3:
{
client_cmd(id, "messagemode ^"vote_item^"")
g_vote_current_it[id] = item - 1
g_unlock[id] = false
}
case 4:
{
if (g_vote_name[id][0] || g_vote_it[id][0][0])
{
clear_vote_data(id)
}
vote_menu(id)
}
case 5:
{
if (GetPlayingCount() < MIN_PLAYERS)
{
menu_destroy(menuid)
UTIL_SayText(id, "!g[Голосование] !tКак минимум !g%d !tигроков..", MIN_PLAYERS)
return PLUGIN_HANDLED;
}
if (task_exists(TASK_VOTE_NEW))
{
menu_destroy(menuid)
UTIL_SayText(id, "!g[Голосование] !tНа данный момент уже запущен опрос")
vote_menu(id)
return PLUGIN_HANDLED;
}
/*if (is_vote_started())
{
menu_destroy(menuid)
UTIL_SayText(id, "!g[Голосование] !tНельзя запускать во время голосования за карту..")
vote_menu(id)
return PLUGIN_HANDLED;
} */
#if defined COUNTDOWN
static Float:current_time
// todo
current_time = get_gametime()
if (current_time - g_delay_vote_fix < COUNTDOWN_TIME)
{
menu_destroy(menuid)
new time = floatround(COUNTDOWN_TIME - (current_time - g_delay_vote_fix), floatround_ceil)
UTIL_SayText(id, "!g[Голосование] !tПодождите!g %d:%02d !tпосле предыдущего опроса", time / 60, time % 60)
vote_menu(id)
return PLUGIN_HANDLED;
}
#endif
if (!g_vote_name[id][0])
{
menu_destroy(menuid)
UTIL_SayText(id, "!g[Голосование] !tВы должны указать название")
vote_menu(id)
return PLUGIN_HANDLED;
}
if (!g_vote_it[id][1][0])
{
menu_destroy(menuid)
UTIL_SayText(id, "!g[Голосование] !tВы должны указать как минимум!g 2 пункта")
vote_menu(id)
return PLUGIN_HANDLED;
}
g_total_name = g_vote_name[id]
for (new b = 0; b <= 2; b++)
{
copy(g_total_it[b][0], MAX_CHAR - 1, g_vote_it[id][b])
}
g_total_votes = 0
for (new index = 1; index <= MaxClients; index++)
{
if (!is_user_connected(index) || is_user_bot(index) || is_user_hltv(index))
{
continue;
}
// Если хотите чтобы инициатор учавствовал, то убрерите эту проверку
/*if (index == id)
{
UTIL_SayText(id, "!g[Голосование] !tВы запустили голосование..")
// todo
continue;
}*/
show_vote_menu(index)
}
UTIL_SayText(0, "!g[Голосование] !tАдминистратор !g%n !tсоздал опрос..", id)
clear_vote_data(id)
#if defined COUNTDOWN
g_delay_vote_fix = current_time
#endif
set_task(float(clamp(TIME_VOTE, 5, 30)), "task_vote_end", TASK_VOTE_NEW)
}
// default: return PLUGIN_HANDLED;
}
menu_destroy(menuid)
// todo
return PLUGIN_HANDLED;
}
public function_vote_name(id)
{
if (!is_user_connected(id) || is_user_hltv(id))
{
return PLUGIN_HANDLED;
}
if (!(get_user_flags(id) & ADMIN_LEVEL))
{
return PLUGIN_HANDLED;
}
if (g_unlock[id])
{
return PLUGIN_HANDLED;
}
new args[MAX_CHAR]
read_args(args, charsmax(args))
remove_quotes(args)
trim(args)
// todo
while (replace(args, charsmax(args), "\r", "")) {}
while (replace(args, charsmax(args), "\y", "")) {}
while (replace(args, charsmax(args), "\w", "")) {}
while (replace(args, charsmax(args), "\d", "")) {}
g_unlock[id] = true
if (is_empty_str(args))
{
vote_menu(id)
// todo
return PLUGIN_HANDLED;
}
if (strlen(args) > 60)
{
client_cmd(id, "messagemode ^"vote_name^"")
UTIL_SayText(id, "!g[Голосование] !tВведите покороче название")
g_unlock[id] = false
return PLUGIN_HANDLED;
}
if (strlen(args) < 6)
{
client_cmd(id, "messagemode ^"vote_name^"")
UTIL_SayText(id, "!g[Голосование] !tНазвание не может быть таким коротким")
g_unlock[id] = false
return PLUGIN_HANDLED;
}
#if defined COLOR_MODE
while (replace(args, charsmax(args), "!d", "\d")) {}
while (replace(args, charsmax(args), "!w", "\w")) {}
while (replace(args, charsmax(args), "!r", "\r")) {}
while (replace(args, charsmax(args), "!y", "\y")) {}
#endif
g_vote_name[id] = args
vote_menu(id)
return PLUGIN_HANDLED;
}
public function_vote_item(id)
{
if (!is_user_connected(id) || is_user_hltv(id))
{
return PLUGIN_HANDLED;
}
if (!(get_user_flags(id) & ADMIN_LEVEL))
{
return PLUGIN_HANDLED;
}
if (g_unlock[id])
{
return PLUGIN_HANDLED;
}
new args[MAX_CHAR]
read_args(args, charsmax(args))
remove_quotes(args)
trim(args)
// todo
while (replace(args, charsmax(args), "\r", "")) {}
while (replace(args, charsmax(args), "\y", "")) {}
while (replace(args, charsmax(args), "\w", "")) {}
while (replace(args, charsmax(args), "\d", "")) {}
g_unlock[id] = true
if (is_empty_str(args))
{
vote_menu(id)
// todo
return PLUGIN_HANDLED;
}
if (strlen(args) > 40)
{
client_cmd(id, "messagemode ^"vote_item^"")
UTIL_SayText(id, "!g[Голосование] !tВведите покороче вариант")
g_unlock[id] = false
return PLUGIN_HANDLED;
}
#if defined COLOR_MODE
while (replace(args, charsmax(args), "!d", "\d")) {}
while (replace(args, charsmax(args), "!w", "\w")) {}
while (replace(args, charsmax(args), "!r", "\r")) {}
while (replace(args, charsmax(args), "!y", "\y")) {}
#endif
// Заполняем пустые слоты..
if (!g_vote_it[id][0][0] && g_vote_current_it[id] >= 0)
g_vote_it[id][0] = args
else if (!g_vote_it[id][1][0] && g_vote_current_it[id] >= 1)
g_vote_it[id][1] = args
else if (!g_vote_it[id][2][0] && g_vote_current_it[id] >= 2)
g_vote_it[id][2] = args
else
g_vote_it[id][g_vote_current_it[id]] = args
vote_menu(id)
// todo
return PLUGIN_HANDLED;
}
show_vote_menu(id)
{
static menu[128]
new menuid
formatex(menu, charsmax(menu), "\wОпрос: \y%s", g_total_name)
menuid = menu_create(menu, "vote_menu_handler")
formatex(menu, charsmax(menu), "\w%s", g_total_it[0])
menu_additem(menuid, menu, "1", 0)
formatex(menu, charsmax(menu), "\w%s", g_total_it[1])
menu_additem(menuid, menu, "2", 0)
if (!is_empty_str(g_total_it[2][0]))
{
formatex(menu, charsmax(menu), "\w%s", g_total_it[2])
// todo
menu_additem(menuid, menu, "3", 0)
}
menu_setprop(menuid, MPROP_EXITNAME, "Не голосовать")
if (pev_valid(id) == PDATA_SAFE)
{
set_pdata_int(id, OFFSET_CSMENUCODE, 0, OFFSET_LINUX)
}
menu_display(id, menuid, 0)
}
public vote_menu_handler(id, menuid, item)
{
if (item == MENU_EXIT)
{
menu_destroy(menuid)
// todo
return PLUGIN_HANDLED;
}
if (!task_exists(TASK_VOTE_NEW))
{
menu_destroy(menuid)
UTIL_SayText(id, "!g[Голосование] !tОпрос уже окончен")
return PLUGIN_HANDLED;
}
new name[32], it_name[MAX_CHAR]
get_user_name(id, name, charsmax(name))
copy(it_name, MAX_CHAR - 1, g_total_it[item])
// Избавалемся от символов в чате (актуально если включен #COLOR_MODE)
while (replace(it_name, charsmax(it_name), "\r", "")) {}
while (replace(it_name, charsmax(it_name), "\y", "")) {}
while (replace(it_name, charsmax(it_name), "\w", "")) {}
while (replace(it_name, charsmax(it_name), "\d", "")) {}
menu_destroy(menuid)
g_it_votes[item]++
g_total_votes++
float_to_str( (float( g_it_votes[item] ) / float( totalplayers )) * 100.0, Percents[item], 5 )
UTIL_SayText(0, "!g[Голосование] !tИгрок !g%s !tпроголосовал за !g%s (%d голоса или %s%%)", name, it_name, g_it_votes[item], Percents[item])
return PLUGIN_HANDLED;
}
public task_vote_end()
{
new best = 0
for (new a = 0; a <= 2; a++)
{
switch (random_num(0, 1))
{
case 0: if (g_it_votes[best] < g_it_votes[a]) best = a
case 1: if (g_it_votes[best] <= g_it_votes[a]) best = a
}
}
if (g_total_votes)
{
if (!is_empty_str(g_total_it[2][0]))
{
UTIL_SayText(0, "!tИтого: !g%s !t- %s%%", g_total_it[0], g_it_votes[0] == 0 ? "0" : Percents[0])
UTIL_SayText(0, "!tИтого: !g%s !t- %s%%", g_total_it[1], g_it_votes[1] == 0 ? "0" : Percents[1])
UTIL_SayText(0, "!tИтого: !g%s !t- %s%%", g_total_it[2], g_it_votes[2] == 0 ? "0" : Percents[2])
}else UTIL_SayText(0, "!tИтого: !g%s !t- %s%%, !g%s !t- %s%%", g_total_it[0], g_it_votes[0] == 0 ? "0" : Percents[0], g_total_it[1], g_it_votes[1] == 0 ? "0" : Percents[1])
UTIL_SayText(0, "!g[Голосование] !tГолосование завершено, победил вариант: !g%s - (%d голоса или %s%%)", g_total_it[best], g_it_votes[best], Percents[best])
}
else
{
UTIL_SayText(0, "!g[Голосование] !tНикто не проголосовал. Голосование провалено..")
}
destroy()
}
clear_vote_data(id)
{
arrayset(g_vote_name[id], 0, sizeof(g_vote_name))
for (new i = 0; i <= 2; i++) arrayset(g_vote_it[id][i], 0, sizeof(g_vote_it))
g_vote_current_it[id] = 0
}
destroy()
{
if (task_exists(TASK_VOTE_NEW)) remove_task(TASK_VOTE_NEW)
for (new i = 0; i <= 2; i++)
{
g_it_votes[i] = 0
// todo
arrayset(g_total_it[i], 0, sizeof(g_total_it))
}
arrayset(g_total_name, 0, sizeof(g_total_name))
g_total_votes = 0
}
stock is_empty_str(str[])
{
if (equal(str, "") || !strlen(str))
{
return true;
}
return false;
}
GetPlayingCount()
{
new pl, id, CsTeams:team
totalplayers = 0;
for (id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id) || is_user_bot(id) || is_user_hltv(id))
{
continue;
}
team = cs_get_user_team(id)
if (team != CS_TEAM_UNASSIGNED)
{
pl++
}
}
totalplayers = pl;
return pl;
}
#define MsgId_SayText 76
stock UTIL_SayText(pPlayer, const szMessage[], any:...)
{
new szBuffer[190];
if(numargs() > 2) vformat(szBuffer, charsmax(szBuffer), szMessage, 3);
else copy(szBuffer, charsmax(szBuffer), szMessage);
while(replace(szBuffer, charsmax(szBuffer), "#", "")) {}
while(replace(szBuffer, charsmax(szBuffer), "!y", "^1")) {}
while(replace(szBuffer, charsmax(szBuffer), "!t", "^3")) {}
while(replace(szBuffer, charsmax(szBuffer), "!g", "^4")) {}
while(replace(szBuffer, charsmax(szBuffer), "!b", "^0")) {}
switch(pPlayer)
{
case 0:
{
for(new iPlayer = 1; iPlayer <= MaxClients; iPlayer++)
{
if(!is_user_connected(iPlayer)) continue;
engfunc(EngFunc_MessageBegin, MSG_ONE_UNRELIABLE, MsgId_SayText, {0.0, 0.0, 0.0}, iPlayer);
write_byte(iPlayer);
write_string(szBuffer);
message_end();
}
}
default:
{
engfunc(EngFunc_MessageBegin, MSG_ONE_UNRELIABLE, MsgId_SayText, {0.0, 0.0, 0.0}, pPlayer);
write_byte(pPlayer);
write_string(szBuffer);
message_end();
}
}
}