include( "autorun/sh_config.lua" )
Radar = Radar or {} -- DO NOT CHANGE THESE OR IT WILL BREAK
Radar.Config = Radar.Config or {}
if not Radar.Config.Enable then return end -- No need to run this if Give Radar is disabled.
if not Radar.Enable then return end -- Just a double-check to make sure it doesn't run.
-- These are where the default chat commands are set up. Not currently integrated to ULX.
Radar.VoteCommands = {
"!giveradar",
"!voteradar",
"/giveradar",
"/voteradar"
}
Radar.RevokeCommands = {
"!revoke",
"/revoke",
"!revokeradar",
"/revokeradar"
}
Radar.TotalVotes = 0
Radar.Wait = 20 -- The time in seconds the player needs to wait before revoking his vote to prevent chat spam.
Radar._Wait = CurTime() + Radar.Wait
Radar.Given = false -- Global bool, basically every round the radar isn't given.
-- Set up config again just in-case.
Radar.MinPlys = Radar.Config.MinPlys or 5
Radar.UseVotesOverPercent = Radar.Config.UseVotesOverPercent or false
Radar.MinPercent = Radar.Config.MinPercent or 0.66
Radar.MinVotes = Radar.Config.MinVotes or 10
Radar.VoteMessage = Radar.Config.VoteMessage or " voted to give Traitors a Radar."
Radar.RevokeMessage = Radar.Config.RevokeMessage or " revoked their vote to give Traitors a Radar."
if not Radar.UseVotesOverPercent then
function Radar.ShouldGive()
if Radar.TotalVotes >= math.Round( #player.GetAll() * Radar.MinPercent ) then
return true
end
return false
end
function Radar.GiveRadar()
for k,v in pairs( player.GetAll() ) do
if v:IsActiveTraitor() then
v:Give( EQUIP_RADAR )
PrintMessage( HUD_PRINTTALK, "Vote passed: Traitors have been given a Radar." )
end
end
Radar.Given = true
end
function Radar.CanVote( ply )
local players = table.Count( player.GetAll() )
if ply.Voted then
return false, "You've already voted."
elseif Radar._Wait >= CurTime() then
return false, "You must wait a few more seconds before you can vote again"
elseif Radar.Given then
return false, "Traitors have already been given a Radar."
elseif players < RTV.MinPlys then
return false, "You need at least " .. RTV.MinPlys .. " players to vote."
end
return true
end
function Radar.AddVote( ply )
if Radar.CanVote( ply ) then
Radar.TotalVotes = Radar.TotalVotes + 1
ply.Voted = true
MsgN( ply:Nick() .. Radar.VoteMessage )
PrintMessage( HUD_PRINTTALK, ply:Nick() .. Radar.VoteMessage .. "(" .. Radar.TotalVotes .. "/" .. math.Round( #player.GetAll() * Radar.MinPercent ) .. ")" )
if Radar.ShouldGive() then
Radar.GiveRadar()
end
end
end
function Radar.RemoveVote( ply )
Radar.TotalVotes = Radar.TotalVoles - 1
ply.Voted = false
MsgN( ply:Nick() .. Radar.RevokeMessage )
PrintMessage( HUD_PRINTTALK, ply:Nick() .. Radar.RevokeMessage .. "(" .. Radar.TotalVotes .. "/" .. math.Round( #player.GetAll() * Radar.MinPercent ) .. ")" )
if Radar.ShouldGive() then
Radar.GiveRadar()
end
end
hook.Add( "PlayerDisconnected", "Radar.RemoveVote", Radar.RemoveVote )
function Radar.StartRemove( ply )
if not ply.Voted then
ply:PrintMessage( HUD_PRINTTALK, "You have not voted yet." )
else
Radar.RemoveVote( ply )
end
end
function Radar.StartVote( ply )
local can, err = Radar.CanVote( ply )
if not can then
ply:PrintMessage( HUD_PRINTTALK, err )
return
end
Radar.AddVote( ply )
end
concommand.Add( "give_radar", Radar.StartVote )
function ChatCommandsCheck( ply, text )
if table.HasValue( Radar.VoteCommands, string.lower( text ) ) then
Radar.StartVote( ply )
end
if table.HasValue( Radar.RevokeCommands, string.lower( text ) ) then
Radar.StartRemove( ply )
end
end
hook.Add( "PlayerSay", "ChatCommandsCheck", ChatCommandsCheck )
else
function Radar.ShouldGive()
if Radar.TotalVotes >= Radar.MinVotes then
return true
end
return false
end
function Radar.GiveRadar()
for k,v in pairs( player.GetAll() ) do
if v:IsActiveTraitor() then
v:Give( EQUIP_RADAR )
end
end
Radar.Given = true
end
function Radar.CanVote( ply )
local players = table.Count( player.GetAll() )
if ply.Voted then
return false, "You've already voted."
elseif Radar._Wait >= CurTime() then
return false, "You must wait a few more seconds before you can vote again"
elseif Radar.Given then
return false, "Traitors have already been given a Radar."
elseif players < RTV.MinPlys then
return false, "You need at least " .. RTV.MinPlys .. " players to vote."
end
return true
end
function Radar.AddVote( ply )
if Radar.CanVote( ply ) then
Radar.TotalVotes = Radar.TotalVotes + 1
ply.Voted = true
MsgN( ply:Nick() .. Radar.VoteMessage )
PrintMessage( HUD_PRINTTALK, ply:Nick() .. Radar.VoteMessage .. "(" .. Radar.TotalVotes .. "/" .. math.Round( #player.GetAll() * Radar.MinVotes ) .. ")" )
if Radar.ShouldGive() then
Radar.GiveRadar()
end
end
end
function Radar.RemoveVote( ply )
Radar.TotalVotes = math.Clamp( Radar.TotalVotes - 1, 0, math.huge )
ply.Voted = false
MsgN( ply:Nick() .. Radar.RevokeMessage )
PrintMessage( HUD_PRINTTALK, ply:Nick() .. Radar.RevokeMessage .. "(" .. Radar.TotalVotes .. "/" .. math.Round( #player.GetAll() * Radar.MinVotes ) .. ")" )
if Radar.ShouldGive() then
Radar.GiveRadar()
end
end
hook.Add( "PlayerDisconnected", "Radar.RemoveVote", Radar.RemoveVote )
function Radar.StartRemove( ply )
if not ply.Voted then
ply:PrintMessage( HUD_PRINTTALK, "You have not voted yet." )
else
Radar.RemoveVote( ply )
end
end
function Radar.StartVote( ply )
local can, err = Radar.CanVote( ply )
if not can then
ply:PrintMessage( HUD_PRINTTALK, err )
return
end
Radar.AddVote( ply )
end
concommand.Add( "vote_radar", Radar.StartVote )
function ChatCommandsCheck( ply, text )
if table.HasValue( Radar.VoteCommands, string.lower( text ) ) then
Radar.StartVote( ply )
end
if table.HasValue( Radar.RevokeCommands, string.lower( text ) ) then
Radar.StartRemove( ply )
end
end
hook.Add( "PlayerSay", "ChatCommandsCheck", ChatCommandsCheck )
end
function Radar.Restore()
Radar.Given = false
for k,v in pairs( player.GetAll() ) do
if v.Voted then
v.Voted = false
end
end
MsgN( "End of round detected. Give radar votes have been reset." )
end
hook.Add( "TTTEndRound", "Radar.Restore", Radar.Restore )