Advertisement
Guest User

adminvote

a guest
May 28th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.39 KB | None | 0 0
  1.  
  2. #include <amxmodx>
  3. #include <amxmisc>
  4. #include <colorchat>
  5. new g_Answer[128]
  6. new g_optionName[4][64]
  7. new g_voteCount[4]
  8. new g_validMaps
  9. new g_yesNoVote
  10. new g_coloredMenus
  11. new g_voteCaller
  12. new g_Execute[256]
  13. new g_execLen
  14.  
  15. new bool:g_execResult
  16. new Float:g_voteRatio
  17.  
  18. public plugin_init()
  19. {
  20.     register_plugin("Admin Votes", AMXX_VERSION_STR, "AMXX Dev Team")
  21.     register_dictionary("adminvote.txt")
  22.     register_dictionary("common.txt")
  23.     register_dictionary("mapsmenu.txt")
  24.     register_menucmd(register_menuid("Change map to "), MENU_KEY_1|MENU_KEY_2, "voteCount")
  25.     register_menucmd(register_menuid("Choose map: "), MENU_KEY_1|MENU_KEY_2|MENU_KEY_3|MENU_KEY_4, "voteCount")
  26.     register_menucmd(register_menuid("Kick "), MENU_KEY_1|MENU_KEY_2, "voteCount")
  27.     register_menucmd(register_menuid("Ban "), MENU_KEY_1|MENU_KEY_2, "voteCount")
  28.     register_menucmd(register_menuid("Vote: "), MENU_KEY_1|MENU_KEY_2|MENU_KEY_3|MENU_KEY_4, "voteCount")
  29.     register_menucmd(register_menuid("The result: "), MENU_KEY_1|MENU_KEY_2, "actionResult")
  30.     register_concmd("amx_votemap", "cmdVoteMap", ADMIN_VOTE, "[map] [map]")
  31.     g_coloredMenus = colored_menus()
  32. }
  33.  
  34. public cmdCancelVote(id, level, cid)
  35. {
  36.     if (!cmd_access(id, level, cid, 0))
  37.         return PLUGIN_HANDLED
  38.  
  39.     if (task_exists(99889988, 1))
  40.     {
  41.         new authid[32], name[32]
  42.        
  43.         get_user_authid(id, authid, 31)
  44.         get_user_name(id, name, 31)
  45.         log_amx("Vote: ^"%s<%d><%s><>^" cancel vote session", name, get_user_userid(id), authid)
  46.    
  47.  
  48.         new maxpl=get_maxplayers();
  49.         new msg[256];
  50.         for (new i = 1; i <= maxpl; i++)
  51.         {
  52.             if (is_user_connected(i) && !is_user_bot(i))
  53.             {
  54.                 // HACK: ADMIN_CANC_VOTE_{1,2} keys were designed very poorly.  Remove all : and %s in it.
  55.                 LookupLangKey(msg, charsmax(msg), "ADMIN_CANC_VOTE_1", i);
  56.                 replace_all(msg, charsmax(msg), "%s", "");
  57.                 replace_all(msg, charsmax(msg), ":", "");
  58.                 trim(msg);
  59.                 show_activity_id(i, id, name, msg);
  60.             }
  61.         }
  62.        
  63.         console_print(id, "%L", id, "VOTING_CANC")
  64.         client_print(0,print_chat,"%L",LANG_PLAYER,"VOTING_CANC")
  65.         remove_task(99889988, 1)
  66.         set_cvar_float("amx_last_voting", get_gametime())
  67.     }
  68.     else
  69.         console_print(id, "%L", id, "NO_VOTE_CANC")
  70.  
  71.     return PLUGIN_HANDLED
  72. }
  73.  
  74. public delayedExec(cmd[])
  75.     server_cmd("%s", cmd)
  76.  
  77. public autoRefuse()
  78. {
  79.     log_amx("Vote: %L", "en", "RES_REF")
  80.     client_print(0, print_chat, "%L", LANG_PLAYER, "RES_REF")
  81. }
  82.  
  83. public actionResult(id, key)
  84. {
  85.     remove_task(4545454)
  86.    
  87.     switch (key)
  88.     {
  89.         case 0:
  90.         {
  91.             set_task(2.0, "delayedExec", 0, g_Execute, g_execLen)
  92.             log_amx("Vote: %L", "en", "RES_ACCEPTED")
  93.             client_print(0, print_chat, "%L", LANG_PLAYER, "RES_ACCEPTED")
  94.         }
  95.         case 1: autoRefuse()
  96.     }
  97.    
  98.     return PLUGIN_HANDLED
  99. }
  100.  
  101. public checkVotes()
  102. {
  103.     new best = 0
  104.    
  105.     if (!g_yesNoVote)
  106.     {
  107.         for (new a = 0; a < 4; ++a)
  108.             if (g_voteCount[a] > g_voteCount[best])
  109.        
  110.         best = a
  111.     }
  112.  
  113.     new votesNum = g_voteCount[0] + g_voteCount[1] + g_voteCount[2] + g_voteCount[3]
  114.     new iRatio = votesNum ? floatround(g_voteRatio * float(votesNum), floatround_ceil) : 1
  115.     new iResult = g_voteCount[best]
  116.     new players[32], pnum, i
  117.    
  118.     get_players(players, pnum, "c")
  119.    
  120.     if (iResult < iRatio)
  121.     {
  122.         new lVotingFailed[64]
  123.        
  124.         for (i = 0; i < pnum; i++)
  125.         {
  126.             format(lVotingFailed, 63, "%L", players[i], "VOTING_FAILED")
  127.             if (g_yesNoVote)
  128.                 client_print(players[i], print_chat, "%L", players[i], "VOTING_RES_1", lVotingFailed, g_voteCount[0], g_voteCount[1], iRatio)
  129.             else
  130.                 client_print(players[i], print_chat, "%L", players[i], "VOTING_RES_2", lVotingFailed, iResult, iRatio)
  131.         }
  132.        
  133.         format(lVotingFailed, 63, "%L", "en", "VOTING_FAILED")
  134.         log_amx("Vote: %s (got ^"%d^") (needed ^"%d^")", lVotingFailed, iResult, iRatio)
  135.        
  136.         return PLUGIN_CONTINUE
  137.     }
  138.  
  139.     g_execLen = format(g_Execute, 255, g_Answer, g_optionName[best]) + 1
  140.    
  141.     if (g_execResult)
  142.     {
  143.         g_execResult = false
  144.        
  145.         if (is_user_connected(g_voteCaller))
  146.         {
  147.             new menuBody[512], lTheResult[32], lYes[16], lNo[16]
  148.            
  149.             format(lTheResult, 31, "%L", g_voteCaller, "THE_RESULT")
  150.             format(lYes, 15, "%L", g_voteCaller, "YES")
  151.             format(lNo, 15, "%L", g_voteCaller, "NO")
  152.            
  153.             new len = format(menuBody, 511, g_coloredMenus ? "\y%s: \w%s^n^n" : "%s: %s^n^n", lTheResult, g_Execute)
  154.            
  155.             len += format(menuBody[len], 511 - len, g_coloredMenus ? "\y%L^n\w" : "%L^n", g_voteCaller, "WANT_CONTINUE")
  156.             format(menuBody[len], 511 - len, "^n1. %s^n2. %s", lYes, lNo)
  157.             show_menu(g_voteCaller, 0x03, menuBody, 10, "The result: ")
  158.             set_task(10.0, "autoRefuse", 4545454)
  159.         }
  160.         else
  161.             set_task(2.0, "delayedExec", 0, g_Execute, g_execLen)
  162.     }
  163.    
  164.     new lVotingSuccess[32]
  165.    
  166.     for (i = 0; i < pnum; i++)
  167.     {
  168.         format(lVotingSuccess, 31, "%L", players[i], "VOTING_SUCCESS")
  169.         client_print(players[i], print_chat, "%L", players[i], "VOTING_RES_3", lVotingSuccess, iResult, iRatio, g_Execute)
  170.     }
  171.    
  172.     format(lVotingSuccess, 31, "%L", "en", "VOTING_SUCCESS")
  173.     log_amx("Vote: %s (got ^"%d^") (needed ^"%d^") (result ^"%s^")", lVotingSuccess, iResult, iRatio, g_Execute)
  174.    
  175.     return PLUGIN_CONTINUE
  176. }
  177.  
  178. public voteCount(id, key)
  179. {
  180.     if (get_cvar_num("amx_vote_answers"))
  181.     {
  182.         new name[32]
  183.         get_user_name(id, name, 31)
  184.        
  185.         if (g_yesNoVote)
  186.             client_print(0, print_chat, "%L", LANG_PLAYER, key ? "VOTED_AGAINST" : "VOTED_FOR", name)
  187.         else
  188.             client_print(0, print_chat, "%L", LANG_PLAYER, "VOTED_FOR_OPT", name, key + 1)
  189.             ColorChat(0, GREY, "^4%s^1 vote^4%s", name, key + 1)
  190.             ColorChat(0, GREY, "^4%s^1 vote^4%s", name, g_optionName[g_validMaps])
  191.             ColorChat(0, GREY, "^4%s^1 vote^4%d", name, lChangeMap)
  192.     }
  193.     ++g_voteCount[key]
  194.    
  195.     return PLUGIN_HANDLED
  196. }
  197.  
  198. public cmdVoteMap(id, level, cid)
  199. {
  200.     if (!cmd_access(id, level, cid, 2))
  201.         return PLUGIN_HANDLED
  202.    
  203.     new Float:voting = get_cvar_float("amx_last_voting")
  204.     if (voting > get_gametime())
  205.     {
  206.         console_print(id, "%L", id, "ALREADY_VOTING")
  207.         return PLUGIN_HANDLED
  208.     }
  209.    
  210.     if (voting && voting + get_cvar_float("amx_vote_delay") > get_gametime())
  211.     {
  212.         console_print(id, "%L", id, "VOTING_NOT_ALLOW")
  213.         return PLUGIN_HANDLED
  214.     }
  215.  
  216.     new argc = read_argc()
  217.     if (argc > 5) argc = 5
  218.    
  219.     g_validMaps = 0
  220.     g_optionName[0][0] = 0
  221.     g_optionName[1][0] = 0
  222.     g_optionName[2][0] = 0
  223.     g_optionName[3][0] = 0
  224.    
  225.     for (new i = 1; i < argc; ++i)
  226.     {
  227.         read_argv(i, g_optionName[g_validMaps], 31)
  228.        
  229.         if (is_map_valid(g_optionName[g_validMaps]))
  230.             g_validMaps++
  231.     }
  232.    
  233.     if (g_validMaps == 0)
  234.     {
  235.         new lMaps[16]
  236.        
  237.         format(lMaps, 15, "%L", id, (argc == 2) ? "MAP_IS" : "MAPS_ARE")
  238.         console_print(id, "%L", id, "GIVEN_NOT_VALID", lMaps)
  239.         return PLUGIN_HANDLED
  240.     }
  241.  
  242.     new menu_msg[256], len = 0
  243.     new keys = 0
  244.    
  245.     if (g_validMaps > 1)
  246.     {
  247.         keys = MENU_KEY_0
  248.         len = format(menu_msg, 255, g_coloredMenus ? "\y%L: \w^n^n" : "%L: ^n^n", LANG_SERVER, "CHOOSE_MAP")
  249.         new temp[128]
  250.        
  251.         for (new a = 0; a < g_validMaps; ++a)
  252.         {
  253.             format(temp, 127, "%d.  %s^n", a+1, g_optionName[a])
  254.             len += copy(menu_msg[len], 255-len, temp)
  255.             keys |= (1<<a)
  256.         }
  257.        
  258.         format(menu_msg[len], 255-len, "^n0.  %L", LANG_SERVER, "NONE")
  259.         g_yesNoVote = 0
  260.     } else {
  261.         new lChangeMap[32], lYes[16], lNo[16]
  262.        
  263.         format(lChangeMap, 31, "%L", LANG_SERVER, "CHANGE_MAP_TO")
  264.         format(lYes, 15, "%L", LANG_SERVER, "YES")
  265.         format(lNo, 15, "%L", LANG_SERVER, "NO")
  266.         format(menu_msg, 255, g_coloredMenus ? "\y%s %s?\w^n^n1.  %s^n2.  %s" : "%s %s?^n^n1.  %s^n2.  %s", lChangeMap, g_optionName[0], lYes, lNo)
  267.         keys = MENU_KEY_1|MENU_KEY_2
  268.         g_yesNoVote = 1
  269.     }
  270.    
  271.     new authid[32], name[32]
  272.    
  273.     get_user_authid(id, authid, 31)
  274.     get_user_name(id, name, 31)
  275.    
  276.     if (argc == 2)
  277.         log_amx("Vote: ^"%s<%d><%s><>^" vote map (map ^"%s^")", name, get_user_userid(id), authid, g_optionName[0])
  278.     else
  279.         log_amx("Vote: ^"%s<%d><%s><>^" vote maps (map#1 ^"%s^") (map#2 ^"%s^") (map#3 ^"%s^") (map#4 ^"%s^")", name, get_user_userid(id), authid, g_optionName[0], g_optionName[1], g_optionName[2], g_optionName[3])
  280.  
  281.     new maxpl=get_maxplayers();
  282.     new msg[256];
  283.     for (new i = 1; i <= maxpl; i++)
  284.     {
  285.         if (is_user_connected(i) && !is_user_bot(i))
  286.         {
  287.             // HACK: ADMIN_VOTE_MAP_{1,2} keys were designed very poorly.  Remove all : and %s in it.
  288.             LookupLangKey(msg, charsmax(msg), "ADMIN_VOTE_MAP_1", i);
  289.             replace_all(msg, charsmax(msg), "%s", "");
  290.             replace_all(msg, charsmax(msg), ":", "");
  291.             trim(msg);
  292.             show_activity_id(i, id, name, msg);
  293.         }
  294.     }
  295.  
  296.     g_execResult = true
  297.     new Float:vote_time = get_cvar_float("amx_vote_time") + 2.0
  298.    
  299.     set_cvar_float("amx_last_voting", get_gametime() + vote_time)
  300.     g_voteRatio = get_cvar_float("amx_votemap_ratio")
  301.     g_Answer = "changelevel %s"
  302.     show_menu(0, keys, menu_msg, floatround(vote_time), (g_validMaps > 1) ? "Choose map: " : "Change map to ")
  303.     set_task(vote_time, "checkVotes", 99889988)
  304.     g_voteCaller = id
  305.     console_print(id, "%L", id, "VOTING_STARTED")
  306.     g_voteCount = {0, 0, 0, 0}
  307.    
  308.     return PLUGIN_HANDLED
  309. }
  310.  
  311. public cmdVote(id, level, cid)
  312. {
  313.     if (!cmd_access(id, level, cid, 4))
  314.         return PLUGIN_HANDLED
  315.    
  316.     new Float:voting = get_cvar_float("amx_last_voting")
  317.     if (voting > get_gametime())
  318.     {
  319.         console_print(id, "%L", id, "ALREADY_VOTING")
  320.         return PLUGIN_HANDLED
  321.     }
  322.    
  323.     if (voting && voting + get_cvar_float("amx_vote_delay") > get_gametime())
  324.     {
  325.         console_print(id, "%L", id, "VOTING_NOT_ALLOW")
  326.         return PLUGIN_HANDLED
  327.     }
  328.  
  329.     new quest[48]
  330.     read_argv(1, quest, 47)
  331.    
  332.     if ((contain(quest, "sv_password") != -1) || (contain(quest, "rcon_password") != -1) || (contain(quest, "kick") != -1) ||
  333.         (contain(quest, "addip") != -1) || (contain(quest, "ban") != -1))
  334.     {
  335.         console_print(id, "%L", id, "VOTING_FORBIDDEN")
  336.         return PLUGIN_HANDLED
  337.     }
  338.    
  339.     new count=read_argc();
  340.  
  341.     for (new i=0;i<4 && (i+2)<count;i++)
  342.     {
  343.         read_argv(i+2, g_optionName[i], sizeof(g_optionName[])-1);
  344.     }
  345.  
  346.     new authid[32], name[32]
  347.    
  348.     get_user_authid(id, authid, 31)
  349.     get_user_name(id, name, 31)
  350.     log_amx("Vote: ^"%s<%d><%s><>^" vote custom (question ^"%s^") (option#1 ^"%s^") (option#2 ^"%s^")", name, get_user_userid(id), authid, quest, g_optionName[0], g_optionName[1])
  351.  
  352.     new maxpl=get_maxplayers();
  353.     new msg[256];
  354.     for (new i = 1; i <= maxpl; i++)
  355.     {
  356.         if (is_user_connected(i) && !is_user_bot(i))
  357.         {
  358.             // HACK: ADMIN_VOTE_CUS_{1,2} keys were designed very poorly.  Remove all : and %s in it.
  359.             LookupLangKey(msg, charsmax(msg), "ADMIN_VOTE_CUS_1", i);
  360.             replace_all(msg, charsmax(msg), "%s", "");
  361.             replace_all(msg, charsmax(msg), ":", "");
  362.             trim(msg);
  363.             show_activity_id(i, id, name, msg);
  364.         }
  365.     }
  366.  
  367.     new menu_msg[512], lVote[16]
  368.    
  369.     format(lVote, 15, "%L", LANG_SERVER, "VOTE")
  370.    
  371.     count-=2;
  372.     if (count>4)
  373.     {
  374.         count=4;
  375.     }
  376.     // count now shows how many options were listed
  377.     new keys=0;
  378.     for (new i=0;i<count;i++)
  379.     {
  380.         keys |= (1<<i);
  381.     }
  382.    
  383.     new len=formatex(menu_msg, sizeof(menu_msg)-1, g_coloredMenus ? "\y%s: %s\w^n^n" : "%s: %s^n^n", lVote, quest);
  384.    
  385.     for (new i=0;i<count;i++)
  386.     {
  387.         len+=formatex(menu_msg[len], sizeof(menu_msg) - 1 - len ,"%d.  %s^n",i+1,g_optionName[i]);
  388.     }
  389.     g_execResult = false
  390.    
  391.     new Float:vote_time = get_cvar_float("amx_vote_time") + 2.0
  392.    
  393.     set_cvar_float("amx_last_voting", get_gametime() + vote_time)
  394.     g_voteRatio = get_cvar_float("amx_vote_ratio")
  395.     replace_all(quest,sizeof(quest)-1,"%","");
  396.     format(g_Answer, 127, "%s - %%s", quest)
  397.     show_menu(0, keys, menu_msg, floatround(vote_time), "Vote: ")
  398.     set_task(vote_time, "checkVotes", 99889988)
  399.     g_voteCaller = id
  400.     console_print(id, "%L", id, "VOTING_STARTED")
  401.     g_voteCount = {0, 0, 0, 0}
  402.     g_yesNoVote = 0
  403.    
  404.     return PLUGIN_HANDLED
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement