
/* AMX Mod script. 
* 
* (c) Copyright 2002, OLO 
* This file is provided as is (no warranties). 
* 
*/ 


#include <amxmod> /* read this file to learn the functions */ 

/* 
* vote for changing the level: 
* amx_votemap < map > [ map ] 
* examples: 
* amx_votemap "de_dust" 
* amx_votemap "de_dust" "de_dust2" "cs_italy" 
* 
* vote for kicking the player: 
* amx_votekick < part of nick > 
* examples: 
* amx_votekick "Man" 
* 
* vote for banning and kicking the player for 30 minutes: 
* amx_voteban < part of nick > 
* examples: 
* amx_voteban "Man" 
* 
* custom voting: 
* amx_vote < question > < answer1 > < answer2 > 
* examples: 
* amx_vote "sv_restart" "5" "0" 
* amx_vote "mp_timelimit" "20" "45" 
* amx_vote "are you hungry?" "yes" "no" 
* 
*/ 
 
new bool:cstrike_running

new answer[129] 
new option_name[4][33] 
new option[4] 
new valid_maps = 0 
new Float:vote_ratio 
new Float:voting 

new gVoteMap 
new gVoteMaps 
new gVoteKick 
new gVoteBan 
new gVoteCustom 

public delay_action(cmd[]) 
   server_cmd(cmd)

public check_votes() 
{ 
   new players[32], inum 
   get_players(players,inum,"c") 
   new best = 0 
   for(new a = 0; a < 4; ++a) { 
      if (option[a] > best) 
         best = a 
   } 
   new Float:result_v = inum ? (float(option[best]) / float(inum)) : 0.0 
   if (result_v >= vote_ratio) { 
      new execute[129] 
      new len = format(execute,128,answer,option_name[best]) 
      set_task(2.0,"delay_action",0,execute,len + 1) 
      client_print(0,print_chat,"[AMX] Voting successful. The result: %s",execute) 
      log_to_file("addons/amx/admin.log","World triggered ^"voting_success^" (needed ^"%.2f^") (ratio ^"%.2f^") (result ^"%s^")", 
         vote_ratio,result_v,execute) 
   } 
   else { 
      client_print(0,print_chat,"[AMX] Voting failed...") 
      log_to_file("addons/amx/admin.log","World triggered ^"voting_failure^" (needed ^"%.2f^") (ratio ^"%.2f^")", 
         vote_ratio,result_v) 
   } 
   return PLUGIN_HANDLED 
} 

public vote_count(id,key) 
{ 
   if (get_cvar_float("amx_vote_answers")) { 
      new name[33] 
      get_user_name(id,name,32) 
      client_print(id,print_chat,"[AMX] %s voted for option # %d",name,key+1) 
   } 
   option[key] += 1 
   return PLUGIN_HANDLED 
} 

public admin_votemap(id) 
{ 
   if (!(get_user_flags(id)&ADMIN_VOTE)){ 
      client_print(id,print_console,"[AMX] You have no access to that command") 
      return PLUGIN_HANDLED 
   } 
   if (voting > get_gametime()){ 
      client_print(id,print_console,"[AMX] There is already one voting...") 
      return PLUGIN_HANDLED 
   } 
   if (voting > 0.0 && voting + get_cvar_float("amx_vote_delay") > get_gametime()) { 
      client_print(id,print_console,"[AMX] Voting not allowed at this time") 
      return PLUGIN_HANDLED 
   } 
   new argc = read_argc() 
   if (argc > 5) 
      argc = 5 
   if (argc < 2){ 
      client_print(id,print_console,"[AMX] Usage: amx_votemap < map > [ map ] [ map ] [ map ]") 
      return PLUGIN_HANDLED 
   } 
   new menu_msg[257] 
   new keys = (1<<9) 
   valid_maps = 0 
   for(new i = 1; i < argc; ++i){ 
      read_argv(i,option_name[valid_maps],32) 
      if (is_map_valid(option_name[valid_maps])) 
         valid_maps++; 
   } 
   if (valid_maps == 0) { 
      client_print(id,print_console,"[AMX] Given %s not valid",(argc==2)?"map is":"maps are") 
      return PLUGIN_HANDLED 
   } 
   else if (valid_maps > 1){ 
      if(cstrike_running) 
         copy(menu_msg,256,"\yChoose map \w^n^n") 
      else 
         copy(menu_msg,256,"Choose map ^n^n") 

      new temp[129] 
      for(new a = 0; a < valid_maps; ++a){ 
         format(temp,128,"%d.  %s^n",a+1,option_name[a]) 
         add(menu_msg,256,temp) 
         keys |= (1<<a) 
      } 
      add(menu_msg,256,"^n0.  None") 
   } 
   else{ 
      if(cstrike_running) 
         format(menu_msg,256,"\yChange map to %s?\w^n^n1.  Yes^n0.  No",option_name[0]) 
      else 
         format(menu_msg,256,"Change map to %s?^n^n1.  Yes^n0.  No",option_name[0]) 

      keys |= (1<<0) 
   } 

   new authid[17],name[33] 
   get_user_authid(id,authid,16) 
   get_user_name(id,name,32) 
   log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" vote map(s)",name,get_user_userid(id),authid) 

   new Float:vote_time = get_cvar_float("amx_vote_time") + 2.0 
   voting = get_gametime() + vote_time 
   vote_ratio = get_cvar_float("amx_votemap_ratio") 
   copy(answer,128,"changelevel %s") 
   show_menu(0,keys,menu_msg,floatround(vote_time)) 
   set_task(vote_time,"check_votes") 
   client_print(id,print_console,"[AMX] Voting has started...") 
   for(new j = 0; j < 4;++j) option[j] = 0 
   return PLUGIN_HANDLED 
} 

public admin_vote(id) 
{ 
   if (!(get_user_flags(id)&ADMIN_VOTE)){ 
      client_print(id,print_console,"[AMX] You have no access to that command") 
      return PLUGIN_HANDLED 
   } 
   if (voting > get_gametime()){ 
      client_print(id,print_console,"[AMX] There is already one voting...") 
      return PLUGIN_HANDLED 
   } 
   if (voting > 0.0 && voting + get_cvar_float("amx_vote_delay") > get_gametime()) { 
      client_print(id,print_console,"[AMX] Voting not allowed at this time") 
      return PLUGIN_HANDLED 
   } 
   if (read_argc() < 4){ 
      client_print(id,print_console,"[AMX] amx_vote < question > < answer1 > < anwser2 >") 
      return PLUGIN_HANDLED 
   } 
   new quest[129] 
   read_argv(1,quest,128) 
   if ((contain(quest,"sv_password")!=-1)||(contain(quest,"rcon_password")!=-1)){ 
      client_print(id,print_console,"[AMX] Voting for this has been forbidden") 
      return PLUGIN_HANDLED 
   } 
   read_argv(2,option_name[0],32) 
   read_argv(3,option_name[1],32) 

   new authid[17],name[33] 
   get_user_authid(id,authid,16) 
   get_user_name(id,name,32) 
   log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" vote custom (question ^"%s^") (option1 ^"%s^") (option2 ^"%s^")", 
      name,get_user_userid(id),authid,quest,option_name[0],option_name[1]) 

   new menu_msg[257] 
   new keys = (1<<0)|(1<<1) 
   if(cstrike_running) 
      format(menu_msg,256,"\yVote: %s?\w^n^n1.  %s^n2.  %s",quest,option_name[0],option_name[1]) 
   else 
      format(menu_msg,256,"Vote: %s?^n^n1.  %s^n2.  %s",quest,option_name[0],option_name[1]) 
   new Float:vote_time = get_cvar_float("amx_vote_time") + 2.0 
   voting = get_gametime() + vote_time 
   vote_ratio = get_cvar_float("amx_vote_ratio") 
   format(answer,128,"%s %%s",quest) 
   show_menu(0,keys,menu_msg,floatround(vote_time)) 
   set_task(vote_time,"check_votes") 
   client_print(id,print_console,"[AMX] Voting has started...") 
   for(new j = 0; j < 4;++j) option[j] = 0 
   return PLUGIN_HANDLED 
} 

public admin_votekickban(id) 
{ 
   if (!(get_user_flags(id)&ADMIN_VOTE)){ 
      client_print(id,print_console,"[AMX] You have no access to that command") 
      return PLUGIN_HANDLED 
   } 
   if (voting > get_gametime()){ 
      client_print(id,print_console,"[AMX] There is already one voting...") 
      return PLUGIN_HANDLED 
   } 
   if (voting > 0.0 && voting + get_cvar_float("amx_vote_delay") > get_gametime()) { 
      client_print(id,print_console,"[AMX] Voting not allowed at this time") 
      return PLUGIN_HANDLED 
   } 
   new cmd[33] 
   read_argv(0,cmd,32) 
   if (read_argc() < 2){ 
      client_print(id,print_console,"[AMX] Usage: %s < authid or part of nick >",cmd) 
      return PLUGIN_HANDLED 
   } 
   new voteban = equal(cmd,"amx_voteban") ? 1 : 0 
   new arg[33] 
   read_argv(1,arg,32) 
   new player = find_player("c",arg) 
   if (!player) 
      player = find_player("bl",arg) 
   new menu_msg[257] 
   new keys = (1<<9) 
   if (player == 0) { 
      client_print(id,print_console,"[AMX] Client with that authid or part of nick not found") 
      return PLUGIN_HANDLED 
   } 
   else{ 
      if (get_user_flags(player)&ADMIN_IMMUNITY){ 
         client_print(id,print_console,"[AMX] The player has immunity") 
         return PLUGIN_HANDLED 
      } else if (voteban && is_user_bot(player))   { 
         client_print(id,print_console,"[AMX] Bot can't be banned") 
         return PLUGIN_HANDLED 
      } 
      get_user_name(player,arg,32) 
      format(menu_msg,256,"\y%s %s?\w^n^n1.  Yes^n0.  No", voteban ? "Ban" : "Kick", arg) 
      if (voteban) 
         get_user_authid(player,option_name[0],32) 
      else 
         num_to_str(get_user_userid(player),option_name[0],32) 
      keys |= (1<<0) 
   } 

   new authid[17],name[33] 
   get_user_authid(id,authid,16) 
   get_user_name(id,name,32) 
   log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" vote %s (target ^"%s^")", 
      name,get_user_userid(id),authid,voteban ? "ban" : "kick",arg) 

   new Float:vote_time = get_cvar_float("amx_vote_time") + 2.0 
   voting = get_gametime() + vote_time 
   vote_ratio = get_cvar_float(voteban ? "amx_voteban_ratio" : "amx_votekick_ratio") 
   copy(answer,128, voteban ? "banid 30.0 %s kick" : "kick #%s") 
   show_menu(0,keys,menu_msg,floatround(vote_time)) 
   set_task(vote_time,"check_votes") 
   client_print(id,print_console,"[AMX] Voting has started...") 
   for(new j = 0; j < 4;++j) option[j] = 0 
   return PLUGIN_HANDLED 
} 

public plugin_init() 
{ 
   register_plugin("Admin Votes","0.5","default") 
   gVoteMap = register_menuid("Change map to ") 
   gVoteMaps = register_menuid("Choose map ") 
   gVoteKick = register_menuid("Kick ") 
   gVoteBan = register_menuid("Ban ") 
   gVoteCustom = register_menuid("Vote: ") 
   register_menucmd(gVoteMap ,(1<<0)|(1<<1)|(1<<2)|(1<<3),"vote_count") 
   register_menucmd(gVoteMaps ,(1<<0)|(1<<1)|(1<<2)|(1<<3),"vote_count") 
   register_menucmd(gVoteKick ,(1<<0)|(1<<1)|(1<<2)|(1<<3),"vote_count") 
   register_menucmd(gVoteBan ,(1<<0)|(1<<1)|(1<<2)|(1<<3),"vote_count") 
   register_menucmd(gVoteCustom ,(1<<0)|(1<<1)|(1<<2)|(1<<3),"vote_count") 
   register_clcmd("amx_votemap","admin_votemap",ADMIN_VOTE,"amx_votemap < map > [ map ] [ map ] [ map ]") 
   register_clcmd("amx_votekick","admin_votekickban",ADMIN_VOTE,"amx_votekick < authid or part of nick >") 
   register_clcmd("amx_voteban","admin_votekickban",ADMIN_VOTE,"amx_voteban < authid or part of nick >") 
   register_clcmd("amx_vote","admin_vote",ADMIN_VOTE,"amx_vote < question > < answer1 > < anwser2 >") 
   register_cvar("amx_votekick_ratio","0.40") 
   register_cvar("amx_voteban_ratio","0.40") 
   register_cvar("amx_votemap_ratio","0.40") 
   register_cvar("amx_vote_ratio","0.40") 
   register_cvar("amx_vote_time","10") 
   register_cvar("amx_vote_answers","1") 
   register_cvar("amx_vote_delay","60") 
   new mod_name[33]
   get_modname(mod_name,32)
   cstrike_running = equal(mod_name,"cstrike") ? true : false
   return PLUGIN_CONTINUE 
} 

