CATEGORY_NAME = "Jailbreak"
local GUARD_BANS_FILE = "data/ulib/guardbans.txt"
//Ban Guard
function ulx.guardban( calling_ply, target_ply, minutes, reason, should_unguardban )
if not should_unguardban then
if !ULib.guardbans[ target_ply:SteamID() ] then
ULib.guardBan( target_ply, minutes, reason, calling_ply )
local time = "for #i minute(s)"
if minutes == 0 then time = "permanently" end
local str = "#A banned #T " .. time
if reason and reason ~= "" then str = str .. " (#s)" end
GAMEMODE:Notify("You have been banned from the Guard team.", target_ply)
end
elseif ULib.guardbans[ target_ply:SteamID() ] then
steamid = target_ply:SteamID()
if not ULib.isValidSteamID( steamid ) then
ULib.tsayError( calling_ply, "Invalid steamid." )
return
end
name = ULib.guardbans[ steamid ] and ULib.guardbans[ steamid ].name
ULib.unGuardBan( steamid )
GAMEMODE:Notify("You have been unbanned from the Guard team.", target_ply)
end
end
local guardban = ulx.command( CATEGORY_NAME, "ulx guardban", ulx.guardban, "!guardban" )
guardban:addParam{ type=ULib.cmds.PlayerArg }
guardban:addParam{ type=ULib.cmds.NumArg, hint="minutes, 0 for perma", ULib.cmds.optional, ULib.cmds.allowTimeString, min=0, invisible=true }
guardban:addParam{ type=ULib.cmds.StringArg, hint="reason", ULib.cmds.optional, ULib.cmds.takeRestOfLine, completes=ulx.common_kick_reasons, invisible=true }
guardban:addParam{ type=ULib.cmds.BoolArg, invisible=true }
guardban:defaultAccess( ULib.ACCESS_ADMIN )
guardban:help( "Bans targeted player from the Guard team." )
guardban:setOpposite( "ulx unguardban", {_, _, _, _, true}, "!unguardban" )
function ULib.guardBan( ply, time, reason, admin )
if not time or type( time ) ~= "number" then
time = 0
end
ULib.addGuardBan( ply:SteamID(), time, reason, ply:Name(), admin )
-- Load our currently banned users so we don't overwrite them
if ULib.fileExists( "cfg/guard_banned_user.cfg" ) then
ULib.execFile( "cfg/guard_banned_user.cfg" )
end
end
function ULib.addGuardBan( steamid, time, reason, name, admin )
local strTime = time ~= 0 and string.format( "for %s minute(s)", time ) or "permanently"
local showReason = string.format( "Banned %s: %s", strTime, reason )
local players = player.GetAll()
for i=1, #players do
if players[ i ]:SteamID() == steamid then
if (players[ i ]:Team() == TEAM_GUARD || players[ i ]:Team() == TEAM_GUARD_DEAD) then
forcechangeteam( admin, players[ i ] )
end
end
end
local admin_name
if admin then
admin_name = "(Console)"
if admin:IsValid() then
admin_name = string.format( "%s(%s)", admin:Name(), admin:SteamID() )
end
end
local t = {}
if ULib.guardbans[ steamid ] then
t = ULib.guardbans[ steamid ]
t.modified_admin = admin_name
t.modified_time = os.time()
else
t.admin = admin_name
end
t.time = t.time or os.time()
if time > 0 then
t.unban = ( ( time * 60 ) + os.time() )
else
t.unban = 0
end
if reason then
t.reason = reason
end
if name then
t.name = name
end
ULib.guardbans[ steamid ] = t
ULib.fileWrite( GUARD_BANS_FILE, ULib.makeKeyValues( ULib.guardbans ) )
end
function ULib.unGuardBan( steamid )
if ULib.fileExists( "cfg/guard_banned_user.cfg" ) then
ULib.execFile( "cfg/guard_banned_user.cfg" )
end
ULib.queueFunctionCall( game.ConsoleCommand, "removeid " .. steamid .. ";writeid\n" ) -- Execute after done loading bans
ULib.guardbans[ steamid ] = nil
ULib.fileWrite( GUARD_BANS_FILE, ULib.makeKeyValues( ULib.guardbans ) )
end
//Refresh Guard Bans
function ULib.refreshGuardBans()
local err
if not ULib.fileExists( GUARD_BANS_FILE ) then
ULib.guardbans = {}
else
ULib.guardbans, err = ULib.parseKeyValues( ULib.fileRead( GUARD_BANS_FILE ) )
end
if err then
Msg( "Bans file was not formatted correctly. Attempting to fix and backing up original\n" )
if err then
Msg( "Error while reading bans file was: " .. err .. "\n" )
end
Msg( "Original file was backed up to " .. ULib.backupFile( GUARD_BANS_FILE ) .. "\n" )
ULib.guardbans = {}
end
local default_bans = ""
if ULib.fileExists( "cfg/guard_banned_user.cfg" ) then
ULib.execFile( "cfg/guard_banned_user.cfg" )
ULib.queueFunctionCall( game.ConsoleCommand, "writeid\n" )
default_bans = ULib.fileRead( "cfg/guard_banned_user.cfg" )
end
--default_bans = ULib.makePatternSafe( default_bans )
default_bans = string.gsub( default_bans, "banid %d+ ", "" )
default_bans = string.Explode( "\n", default_bans:gsub( "\r", "" ) )
local ban_set = {}
for _, v in pairs( default_bans ) do
if v ~= "" then
ban_set[ v ] = true
if not ULib.guardbans[ v ] then
ULib.guardbans[ v ] = { unban = 0 }
end
end
end
for k, v in pairs( ULib.guardbans ) do
if type( v ) == "table" and type( k ) == "string" then
local time = ( v.unban - os.time() ) / 60
if time > 0 then
//game.ConsoleCommand( string.format( "banid %f %s\n", time, k ) )
elseif math.floor( v.unban ) == 0 then -- We floor it because GM10 has floating point errors that might make it be 0.1e-20 or something dumb.
if not ban_set[ k ] then
ULib.guardbans[ k ] = nil
end
else
ULib.guardbans[ k ] = nil
end
else
Msg( "Warning: Bad ban data is being ignored, key = " .. tostring( k ) .. "\n" )
ULib.guardbans[ k ] = nil
end
end
-- We're queueing this because it will split the load out for VERY large ban files
ULib.queueFunctionCall( function() ULib.fileWrite( GUARD_BANS_FILE, ULib.makeKeyValues( ULib.guardbans ) ) end )
end
ULib.pcallError( ULib.refreshGuardBans )
function forcechangeteam( calling_ply, target_ply )
if ulx.getExclusive( target_ply, calling_ply ) then
ULib.tsayError( calling_ply, ulx.getExclusive( target_ply, calling_ply ), true );
else
if (target_ply:Team() == TEAM_PRISONER) then
result = TEAM_GUARD;
elseif (target_ply:Team() == TEAM_PRISONER_DEAD) then
result = TEAM_GUARD_DEAD;
elseif (target_ply:Team() == TEAM_GUARD) then
result = TEAM_PRISONER;
elseif (target_ply:Team() == TEAM_GUARD_DEAD) then
result = TEAM_PRISONER_DEAD;
end
if (result) then
if (result == TEAM_GUARD or result == TEAM_PRISONER) then
target_ply.jb_ForceAlive = true;
end
target_ply:SetTeam(result);
target_ply:Spawn();
GAMEMODE:Notify(calling_ply:Name().." has force-swapped "..target_ply:Name()..".");
else
GAMEMODE:Notify("Couldn't find a team for "..target_ply:Name()..".", target_ply);
end
end
end
function ulx.outputguardban(calling_ply)
t = ULib.fileRead( GUARD_BANS_FILE )
p = ULib.parseKeyValues( ULib.fileRead( GUARD_BANS_FILE ) )
calling_ply:PrintMessage(HUD_PRINTCONSOLE, "\nList of Banned Guards:")
//calling_ply:PrintMessage(HUD_PRINTCONSOLE, t)
for k, v in pairs(p) do
calling_ply:PrintMessage(HUD_PRINTCONSOLE, k)
end
end
local outputguardban = ulx.command( CATEGORY_NAME, "ulx outputguardban", ulx.outputguardban, "!outputguardban" )
outputguardban:defaultAccess( ULib.ACCESS_ADMIN )
function checkBanList(target_ply)
isNotInTable = true
for k, v in pairs (ULib.guardbans) do
if(k == target_ply:SteamID()) then
print(k)
isNotInTable = false
end
end
print(isNotInTable)
return isNotInTable
end