Eccid

slots.lua

Jul 24th, 2013
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.30 KB | None | 0 0
  1. ulx.convar( "rslotsMode", "0", " - Sets the slots mode. See config for more information.", ULib.ACCESS_ADMIN )
  2. ulx.convar( "rslots", "2", " - Sets the number of reserved slots, only applicable for modes 1 and 2.", ULib.ACCESS_ADMIN )
  3. ulx.convar( "rslotsVisible", "1", " - Sets whether slots are visible. See config for more information.", ULib.ACCESS_ADMIN )
  4.  
  5. local access = "ulx reservedslots" -- Access string needed for reserved slots
  6. local access_groups = {"owner","admin","superadmin","donator"}
  7. ULib.ucl.registerAccess( access, access_groups, "Access to reserved slots", "Other" ) -- Give admins access to reserved slots by default
  8.  
  9. function calcSlots( disconnect )
  10.     local mode = GetConVarNumber( "ulx_rslotsMode" )
  11.     if mode == 3 then return 1 end -- Only one slot on this mode
  12.  
  13.     local slots = GetConVarNumber( "ulx_rslots" )
  14.     if mode == 2 then return slots end
  15.  
  16.     if mode == 1 then
  17.         local admins = 0 -- Keep track of how many people with access we have
  18.  
  19.         local players = player.GetAll()
  20.         for _, player in ipairs( players ) do
  21.             if player:IsConnected() and player:query( access ) then
  22.                 admins = admins + 1
  23.             end
  24.         end
  25.  
  26.         if disconnect then admins = admins - 1 end -- Otherwise we're counting the disconnecting admin
  27.         if admins < 0 then admins = 0 end -- Just to be safe!
  28.  
  29.         local rslots = slots - admins
  30.         if rslots < 0 then rslots = 0 end -- If we have more admins right now then slots for them, we don't want to return a negative number.
  31.         return rslots
  32.     end
  33.  
  34.     return 0 -- We're actually having an error if we get here, but let's handle it gracefully
  35. end
  36.  
  37. local function updateSlots( ply, disconnect )
  38.     local visible = util.tobool( GetConVarString( "ulx_rslotsVisible" ) )
  39.     if not visible then -- Make sure our visible slots is up to date
  40.         local slots = calcSlots( disconnect )
  41.         local max = game.MaxPlayers()
  42.         game.ConsoleCommand( "sv_visiblemaxplayers " .. max - slots .. "\n" )
  43.     end
  44. end
  45. hook.Add( "PlayerDisconnected", "ulxSlotsDisconnect", function( ply ) updateSlots( ply, ply:query( access ) ) end )
  46. hook.Add( ulx.HOOK_ULXDONELOADING, "ULXUpdateSlots", updateSlots )
  47.  
  48. local function playerAccess( ply )
  49.     local mode = GetConVarNumber( "ulx_rslotsMode" )
  50.     if mode == 0 then return end -- Off!
  51.  
  52.     local visible = util.tobool( GetConVarString( "ulx_rslotsVisible" ) )
  53.     local slots = calcSlots()
  54.     local cur = #player.GetAll()
  55.     local max = game.MaxPlayers()
  56.  
  57.     if ply:query( access ) then -- If they have access, handle this differently
  58.         if not visible then -- Make sure our visible slots is up to date
  59.             updateSlots()
  60.         end
  61.  
  62.         if mode == 3 and cur + slots > max then -- We've got some kicking to do!
  63.             local shortestply
  64.             local shortesttime = 0
  65.  
  66.             local players = player.GetAll()
  67.             for _, player in ipairs( players ) do
  68.                 if not ULib.ucl.query( player, access ) then
  69.                     if player:TimeConnected() > shortesttime then
  70.                         shortesttime = player:TimeConnected()
  71.                         shortestply = player
  72.                     end
  73.                 end
  74.             end
  75.  
  76.             if not shortestply then -- We've got a server filled to the brim with admins? Odd but okay
  77.                 return
  78.             end
  79.  
  80.             ULib.kick( shortestply, "[ULX] Freeing slot. Sorry, you had the shortest connection time." )
  81.         end
  82.  
  83.         return
  84.     end
  85.  
  86.     if cur + slots > max then
  87.         ULib.queueFunctionCall( ULib.kick, ply, "[ULX] Reserved slot, sorry!" ) -- Wait a frame so all access hooks can be called properly.
  88.     end
  89. end
  90. hook.Add( ULib.HOOK_UCLAUTH, "ULXReservedSlots", playerAccess, 20 ) -- Run at the end of auth
  91.  
  92. // http://www.facepunch.com/showthread.php?t=1253982&p=39923262
  93. // Made by Map in a Box and edited by others.
  94. local function tosteamid(cid)
  95.   local steam64=tonumber(cid:sub(2))
  96.   local a = steam64 % 2 == 0 and 0 or 1
  97.   local b = math.abs(6561197960265728 - steam64 - a) / 2
  98.   local sid = "STEAM_0:" .. a .. ":" .. (a == 1 and b -1 or b)
  99.   return sid
  100. end
  101.  
  102.  hook.Add("CheckPassword", "CheckPassword_reserve", function( comID, ipPort, serverPassword, userPassword, name )
  103.     local mode = GetConVarNumber( "ulx_rslotsMode" )
  104.     local conply = tosteamid(comID)
  105.     if not mode == 2 then
  106.         return true
  107.     end
  108.     local slots = GetConVarNumber( "ulx_rslots" )
  109.     local cur = #player.GetAll()
  110.     local max = game.MaxPlayers()  
  111.     if cur + slots > max then
  112.         if not conply:query( access ) then
  113.             return false, "The server is full."
  114.         end
  115.     else
  116.         return true
  117.     end
  118.     end)
Advertisement
Add Comment
Please, Sign In to add comment