CapsAdmin

Untitled

Aug 15th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. if CLIENT then
  2.     function ShowVoteFrame(desc, time)
  3.         if VOTE_FRAME and VOTE_FRAME:IsValid() then VOTE_FRAME:Remove() end
  4.         local frame = vgui.Create("DPanel")
  5.         VOTE_FRAME = frame
  6.  
  7.         frame:SetSize(200, 200)
  8.         frame:SetPos(30, 30)
  9.         frame:SetBackgroundColor(Color(30, 30, 30, 255))
  10.         frame:DockPadding(4,4,4,4)
  11.  
  12.         local lbl = vgui.Create("DLabel", frame)
  13.         lbl:Dock(TOP)
  14.         lbl:SetFont("Default")
  15.         lbl:SetText(desc or "")
  16.         lbl:SizeToContents()
  17.         lbl:DockMargin(1,1,4,4)
  18.  
  19.         local info = vgui.Create("DPanel", frame)
  20.         info:Dock(TOP)
  21.         info:SetTall(frame:GetTall() / 2)
  22.  
  23.         local yes = vgui.Create("DLabel", info)
  24.         yes:Dock(TOP)
  25.         yes:SetFont("Trebuchet22")
  26.         yes:SetText("Press F11 to vote YES")
  27.         yes:SizeToContents()
  28.         yes:DockMargin(1,1,4,4)
  29.  
  30.         local no = vgui.Create("DLabel", info)
  31.         no:Dock(TOP)
  32.         no:SetFont("Trebuchet22")
  33.         no:SetText("Press F12 to vote NO")
  34.         no:SizeToContents()
  35.         no:DockMargin(1,1,4,4)
  36.  
  37.         info:SetTall(yes:GetTall() + no:GetTall() + 8)
  38.         info:DockMargin(0,0,4,4)
  39.  
  40.         local list = vgui.Create("DPanelList", frame)
  41.         list:EnableHorizontal(true)
  42.         list:Dock(TOP)
  43.         list:SetSpacing(2)
  44.         list:SetPadding(4)
  45.  
  46.         local votes = {}
  47.        
  48.         for key, ply in pairs(player.GetHumans()) do
  49.             local pnl = vgui.Create("DCheckBox")
  50.             pnl:SetSize(16, 16)
  51.             list:AddItem(pnl)
  52.             votes[ply:UniqueID()] = pnl
  53.         end
  54.  
  55.         list:SizeToContents()
  56.         list:DockMargin(1,1,4,4)
  57.  
  58.         local _, y = list:LocalToScreen()
  59.         y = y - list:GetTall()
  60.         local _, _y = frame:GetPos()
  61.         epoe.Print(_y - y)
  62.         frame:SetTall(frame:GetTall() - (_y - y) - 8)      
  63.  
  64.         local last = 0
  65.         hook.Add( "Think", "VoteThink", function()
  66.             for id, pnl in pairs(votes) do
  67.                 if pnl:IsValid() then
  68.                     local ply = player.GetByUniqueID(id) or NULL
  69.                     if ply:IsValid() then
  70.                         pnl:SetChecked(ply:GetNWBool("vote_answer") == 1)
  71.                         pnl:Toggle()
  72.                         pnl:Toggle()
  73.                     else
  74.                         votes[id]:Remove()
  75.                         list:InvalidateLayout()
  76.                     end
  77.                 else
  78.                     votes[id] = nil
  79.                     list:InvalidateLayout()
  80.                 end
  81.             end
  82.             if RealTime() > last + 1 then
  83.                 if input.IsKeyDown(KEY_F11) then
  84.                     RunConsoleCommand("voteyes")
  85.                     last = RealTime()
  86.                 elseif input.IsKeyDown(KEY_F12) then
  87.                     RunConsoleCommand("voteno")
  88.                     last = RealTime()
  89.                 end
  90.             end
  91.         end)
  92.        
  93.         timer.Simple(time or 10, function()
  94.             if VOTE_FRAME and VOTE_FRAME:IsValid() then
  95.                 VOTE_FRAME:Remove()
  96.             end
  97.             hook.Remove("Think", "VoteThink")
  98.         end)
  99.     end
  100.    
  101.     usermessage.Hook("ShowVoteFrame", function(umr)
  102.         local desc = umr:ReadString()
  103.         local time = umr:ReadShort()
  104.         ShowVoteFrame(desc, time)
  105.     end)
  106. end
  107.  
  108. if SERVER then
  109.     local function start_vote(cmd, line)   
  110.         local tag = "vote" .. cmd .. line
  111.        
  112.         umsg.Start("ShowVoteFrame")
  113.             umsg.String(cmd .. " " .. line)
  114.             umsg.Short(10)
  115.         umsg.End()
  116.        
  117.         timer.Simple(10, function()
  118.             local count = 0
  119.             local total = #player.GetHumans()
  120.            
  121.             for key, ply in pairs(player.GetHumans()) do
  122.                 local val = ply:GetNWBool("vote_answer")
  123.                 if val == -1 then
  124.                     --total = total - 1
  125.                 elseif val == 1 then
  126.                     count = count + 1
  127.                 end
  128.                 ply:SetNWBool("vote_answer", -1)
  129.             end
  130.            
  131.             if count > 0 and total > 0 then
  132.                 count = count / total
  133.                 local percent = math.Round(count*100)
  134.                
  135.                 Say(percent .. "% voted yes")
  136.            
  137.                 if percent > 51 then
  138.                     vote_func()
  139.                 end
  140.             end
  141.         end)
  142.     end
  143.  
  144.     hook.Add("PlayerSay", "vote", function(ply, str)       
  145.         local cmd, line = str:match("(vote)(.+)")
  146.         if cmd == "vote" then
  147.             cmd = line:match("(.-) ") or str:match("(.+)") or ""
  148.             line = line:match(".- (.+)")
  149.                    
  150.             if aowl.cmds[cmd] then
  151.                 vote_func = function()
  152.                     _G.CHAT = true
  153.                         aowl.CallCommand(ply, cmd, line, line and line:Split(",") or {}, true)
  154.                     _G.CHAT = nil
  155.                 end
  156.                 start_vote(cmd, line)
  157.             else
  158.                
  159.             end
  160.         end
  161.     end)
  162.    
  163.     concommand.Add("voteyes", function(ply)
  164.         print(ply, " ", " voted yes")
  165.         ply:SetNWBool("vote_answer", 1)
  166.     end)
  167.    
  168.     concommand.Add("voteno", function(ply) 
  169.         print(ply, " ", " voted no")
  170.         ply:SetNWBool("vote_answer", 0)
  171.     end)
  172. end
Advertisement
Add Comment
Please, Sign In to add comment