Guest User

voteskip.lua

a guest
Dec 13th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.44 KB | None | 0 0
  1. -- Script by giraffe
  2.  
  3. api_version = "1.6.0.0"
  4.  
  5. -- In seconds the time required of time played before you can vote to skip
  6. required_time = 300
  7.  
  8. -- In seconds the amount of time of inactivity required to be considered afk
  9. -- While afk, time is not added to time played and player is not included in the total player count
  10. afk_time = 90
  11.  
  12. -- In seconds the amount of time of inactivity required to be considered idle
  13. -- While idle, time is not added to time played
  14. idle_time = 10
  15.  
  16. -- Percent value between 0 through 100 of the non-afk players required to vote skip before a map skips
  17. required_percent = 50
  18.  
  19. -- In seconds how often to check if map should be skipped
  20. skip_check_time = 1.5
  21.  
  22. -- DO NOT EDIT THESE VARIABLES --
  23. has_voted_to_skip = {}
  24. time_waited = {}
  25. map_skipped = false
  26.  
  27. function OnScriptLoad()
  28.     timer(1000, "AddTime")
  29.     timer(skip_check_time * 1000, "SkipCheck")
  30.     register_callback(cb['EVENT_GAME_START'], "OnGameStart")
  31.     register_callback(cb['EVENT_CHAT'], "OnChatMessage")
  32.     register_callback(cb['EVENT_LEAVE'], "OnPlayerLeave")
  33.     register_callback(cb['EVENT_GAME_END'], "OnGameEnd")
  34.  
  35. end
  36.  
  37. function OnScriptUnload() end
  38.  
  39. function OnGameStart()
  40.     map_skipped = false
  41.     has_voted_to_skip = {}
  42.     time_waited = {}
  43. end
  44.  
  45. function OnChatMessage(PlayerIndex, Message)
  46.     if(Message:lower() == "skip") then
  47.         if(map_skipped) then
  48.             say(PlayerIndex, "Map is already changing.")
  49.         elseif(has_voted_to_skip[PlayerIndex] == true) then
  50.             say(PlayerIndex, "You have already voted to skip")
  51.         elseif(get_var(PlayerIndex, "$afk") ~= nil and tonumber(get_var(PlayerIndex, "$afk")) > idle_time) then
  52.             say(PlayerIndex, "You cannot vote to skip while idle/afk.")
  53.         elseif((required_time - time_waited[PlayerIndex]) <= 0) then
  54.             has_voted_to_skip[PlayerIndex] = true
  55.             players_needed = playersNeeded()
  56.             say_all(get_var(PlayerIndex, "$name") .. " has voted to skip this map.")
  57.             if(players_needed == 0) then
  58.  
  59.             elseif(players_needed == 1) then
  60.                 say_all("1 more player vote is needed to skip the map.")
  61.             else
  62.                 say_all(players_needed .. " more player votes are needed to skip the map.")
  63.             end
  64.         else
  65.             say(PlayerIndex, "You must wait " .. (required_time - time_waited[PlayerIndex]) .. " more seconds before voting to skip.")
  66.         end
  67.         return false
  68.     end
  69.     return true
  70. end
  71.  
  72. function OnPlayerLeave(PlayerIndex)
  73.     time_waited[PlayerIndex] = 0
  74.     has_voted_to_skip[PlayerIndex] = false
  75. end
  76.  
  77. function OnGameEnd()
  78.     map_skipped = true
  79. end
  80.  
  81. function AddTime()
  82.     for i=1,16 do
  83.         if(player_present(i) and (get_var(i, "$afk") == nil or tonumber(get_var(i, "$afk")) <= idle_time) and (time_waited[i] == nil or time_waited[i] < required_time)) then
  84.             if(time_waited[i] == nil) then
  85.                 time_waited[i] = 1
  86.             else
  87.                 time_waited[i] = time_waited[i] + 1
  88.             end
  89.         end
  90.     end
  91.     return true
  92. end
  93.  
  94. function playersNeeded()
  95.     active_players = 0
  96.     players_voted_skip = 0
  97.     for i=1,16 do
  98.         if(get_var(i, "$afk") == nil or tonumber(get_var(i, "$afk")) <= afk_time) then
  99.             if(player_present(i)) then
  100.                 active_players = active_players + 1
  101.             end
  102.             if(has_voted_to_skip[i] ~= nil and has_voted_to_skip[i] == true) then
  103.                 players_voted_skip = players_voted_skip + 1
  104.             end
  105.         end
  106.     end
  107.     return math.ceil((active_players) * (required_percent / 100)) - players_voted_skip
  108. end
  109.  
  110. function SkipCheck()
  111.     if(not map_skipped) then
  112.         active_players = 0
  113.         players_voted_skip = 0
  114.         for i=1,16 do
  115.             if(get_var(i, "$afk") == nil or tonumber(get_var(i, "$afk")) <= afk_time) then
  116.                 if(player_present(i)) then
  117.                     active_players = active_players + 1
  118.                 end
  119.                 if(has_voted_to_skip[i] ~= nil and has_voted_to_skip[i] == true) then
  120.                     players_voted_skip = players_voted_skip + 1
  121.                 end
  122.             end
  123.         end
  124.         if((players_voted_skip / active_players) >= (required_percent / 100)) then
  125.             say_all("Enough players have voted to skip. Map is changing.")
  126.             execute_command("sv_map_next")
  127.             map_skipped = true
  128.        end
  129.     end
  130.     return true
  131. end
Advertisement
Add Comment
Please, Sign In to add comment