Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.38 KB | None | 0 0
  1. ulx.convar( "rslotsMode", "1", " - Sets the slots mode. See config for more information.", ULib.ACCESS_ADMIN )
  2. ulx.convar( "rslots", "6", " - 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. ULib.ucl.registerAccess( access, ULib.ACCESS_ADMIN, "Access to reserved slots", "Other" ) -- Give admins access to reserved slots by default
  7.  
  8. function calcSlots( disconnect )
  9.     local mode = GetConVarNumber( "ulx_rslotsMode" )
  10.     if mode == 3 then return 1 end -- Only one slot on this mode
  11.  
  12.     local slots = GetConVarNumber( "ulx_rslots" )
  13.     if mode == 2 then return slots end
  14.  
  15.     if mode == 1 then
  16.         local admins = 0 -- Keep track of how many people with access we have
  17.  
  18.         local players = player.GetAll()
  19.         for _, player in ipairs( players ) do
  20.             if player:IsConnected() and player:query( access ) then
  21.                 admins = admins + 1
  22.             end
  23.         end
  24.  
  25.         if disconnect then admins = admins - 1 end -- Otherwise we're counting the disconnecting admin
  26.         if admins < 0 then admins = 0 end -- Just to be safe!
  27.  
  28.         local rslots = slots - admins
  29.         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.
  30.         return rslots
  31.     end
  32.  
  33.     return 0 -- We're actually having an error if we get here, but let's handle it gracefully
  34. end
  35.  
  36. local function updateSlots( ply, disconnect )
  37.     local visible = util.tobool( GetConVarString( "ulx_rslotsVisible" ) )
  38.     if not visible then -- Make sure our visible slots is up to date
  39.         local slots = calcSlots( disconnect )
  40.         local max = game.MaxPlayers()
  41.         game.ConsoleCommand( "sv_visiblemaxplayers " .. max - slots .. "\n" )
  42.     end
  43. end
  44. hook.Add( "PlayerDisconnected", "ulxSlotsDisconnect", function( ply ) updateSlots( ply, ply:query( access ) ) end )
  45. hook.Add( ulx.HOOK_ULXDONELOADING, "ULXUpdateSlots", updateSlots )
  46.  
  47. local function playerAccess( ply )
  48.     local mode = GetConVarNumber( "ulx_rslotsMode" )
  49.     if mode == 0 then return end -- Off!
  50.  
  51.     local visible = util.tobool( GetConVarString( "ulx_rslotsVisible" ) )
  52.     local slots = calcSlots()
  53.     local cur = #player.GetAll()
  54.     local max = game.MaxPlayers()
  55.  
  56.     if ply:query( access ) then -- If they have access, handle this differently
  57.         if not visible then -- Make sure our visible slots is up to date
  58.             updateSlots()
  59.         end
  60.  
  61.         if mode == 3 and cur + slots > max then -- We've got some kicking to do!
  62.             local shortestply
  63.             local shortesttime = math.huge
  64.  
  65.             local players = player.GetAll()
  66.             for _, player in ipairs( players ) do
  67.                 if not ULib.ucl.query( player, access ) then
  68.                     if player:TimeConnected() < shortesttime then
  69.                         shortesttime = player:TimeConnected()
  70.                         shortestply = player
  71.                     end
  72.                 end
  73.             end
  74.  
  75.             if not shortestply then -- We've got a server filled to the brim with admins? Odd but okay
  76.                 return
  77.             end
  78.  
  79.             ULib.kick( shortestply, "[ULX] Freeing slot. Sorry, you had the shortest connection time." )
  80.         end
  81.  
  82.         return
  83.     end
  84.  
  85.     if cur + slots > max then
  86.         ULib.queueFunctionCall( ULib.kick, ply, "Le serveur est Full!" ) -- Wait a frame so all access hooks can be called properly.
  87.     end
  88. end
  89. hook.Add( ULib.HOOK_UCLAUTH, "ULXReservedSlots", playerAccess, 20 ) -- Run at the end of auth
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement