• Print

Author Topic: Global ULib  (Read 7045 times)

0 Members and 1 Guest are viewing this topic.

Offline coolwilliam

  • Newbie
  • *
  • Posts: 11
  • Karma: 0
Global ULib
« on: May 16, 2011, 09:20:26 am »
Hello, i own a bunch of servers for the same community and i was wondering if it would be possible to make it so if you add an admin on a server it will add them as admin on all my servers through a mysql database. So if this could be coded or someone could help me make it that would be great because i am TERRIBLE at lua.
« Last Edit: May 16, 2011, 09:33:42 am by coolwilliam »

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Global ULib
« Reply #1 on: May 16, 2011, 12:47:54 pm »
Forgive the sloppy code.. This is what I use.

You would need to plug in your own SQL data and have the SQL  plugin from facepunch. Also, some of the functionality of this script would be useless to you, so just dig through it and pick what you want.


Code: Lua
  1.  
  2. --------------------
  3. --     Config     --
  4. --------------------
  5. local host = "127.0.0.1"
  6. local username = "root"
  7. local password = ""
  8. local database = "gmodtest"
  9. local port = 3306
  10. local table = "test"
  11.  
  12. local persistent = true -- Use a persistent MySQL connection?
  13.  
  14. require( "mysql" )
  15. local db
  16.  
  17.  
  18. function DoQuery( query, type )
  19.         local result, isok, err = mysql.query( db, query, type or mysql.QUERY_NUMERIC )
  20.  
  21.         if not isok and err == "" then isok = true end -- False positive
  22.  
  23.         if not isok then
  24.                 error( tostring( err ), 2 )
  25.                 return nil
  26.         end
  27.  
  28.         if result then
  29.                 -- print( query ) -- For debug
  30.                 -- PrintTable( result )
  31.         end
  32.  
  33.         return result
  34. end
  35.  
  36. function Connect()
  37.         if db then return db end -- Still connected
  38.  
  39.         db, err = mysql.connect( host, username, password, database, port )
  40.         if db == 0 then
  41.                 db = nil
  42.                 error( tostring( err ), 1 )
  43.                 return
  44.         end
  45.  
  46.         return db
  47. end
  48.  
  49. function Disconnect( force )
  50.         if not db then return end -- Already disconnected
  51.         if persistent and not force then return end -- Don't disconnect, persistent
  52.  
  53.         local succ, err = mysql.disconnect( db )
  54.         if not succ then
  55.                 error( tostring( err ), 2 )
  56.         end
  57.        
  58.         db = nil
  59. end
  60. hook.Add( "ShutDown", "G4P_SQL", function() Disconnect( true ) end ) -- Force closed on shutdown.
  61.  
  62. function Escape( str )
  63.         if not db then
  64.                 Msg( "Not connected to DB.\n" )
  65.                 return
  66.         end
  67.        
  68.         if not str then return end
  69.  
  70.         local esc, err = mysql.escape( db, str )
  71.         if not esc then
  72.                 error( tostring( err ), 2 )
  73.                 return nil
  74.         end
  75.  
  76.         -- print( "esc=" .. esc ) -- For debug
  77.         return esc
  78. end
  79.  
  80. -- Because we use this a lot
  81. function Format( str )
  82.         if not str then return "NULL" end
  83.         return string.format( "%q", str )
  84. end
  85.  
  86. function G4PSQL_Auth( ply, sid, uid )
  87.         Connect()
  88.         local results = DoQuery( "SELECT * FROM " .. table .. " WHERE steamid = '" .. sid .. "'" )
  89.         local newip = string.Explode( ":", ply:IPAddress() )
  90.  
  91.         if not results[1] then
  92.                 ServerLog("No Results Found ... Creating Entry")
  93.                 local result = DoQuery( "INSERT INTO " .. table .. " ( steamid, pname, pgroup, banned, pmodel, lastip ) VALUES( " ..Format( Escape( sid ) ) .. ", " ..Format( Escape( ply:Nick() ) ) .. ", " ..Format( Escape( "user" ) ) .. ", " ..Format( Escape( "FALSE" ) ).. ", " ..Format( Escape( "NONESET" ) ).. ", " ..Format( Escape( newip[1] ) ).. " )" )
  94.         else
  95.                 ServerLog("Results Found ... Loading Results")
  96.                 ply.steamid = results[1][1]
  97.                 ply.name = results[1][2]
  98.                 ply.group = results[1][3]
  99.                 ply.banned = results[1][4]
  100.                 ply.pmodel = results[1][5]
  101.                 ply.lplayed = results[1][6]
  102.                 ply.lastip = results[1][7]
  103.                
  104.                 ServerLog("Name: " .. ply.name)
  105.                 ServerLog("SteamID: " .. ply.steamid)
  106.                 ServerLog("Group: " .. ply.group)
  107.                 ServerLog("Banned?: " .. ply.banned)
  108.                 ServerLog("Player Model: " .. ply.pmodel)
  109.                 ServerLog("Last Played: " .. ply.lplayed)
  110.                 ServerLog("Last Known IP: " .. ply.lastip)
  111.                
  112.                
  113.                 if ply.name != ply:Nick() then
  114.                         ServerLog( "Updating name for player: " ..ply:Nick() )
  115.                         local result3 = DoQuery( "UPDATE " .. table .. " SET pname="..Format( Escape( ply:Nick() ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
  116.        
  117.                 end
  118.                 if ply.lastip != newip[1] then
  119.                         ServerLog( "Updating IP for player: " ..ply:Nick() )
  120.                         local result3 = DoQuery( "UPDATE " .. table .. " SET lastip="..Format( Escape( newip[1] ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
  121.        
  122.                 end
  123.                 local result4 = DoQuery( "UPDATE " .. table .. " SET lplayed="..Format( Escape( os.time() ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
  124.                 if ply.banned == "TRUE" then
  125.                         ULib.kick( ply, "BANNED! Visit our website at www.g4p.org to dispute this ban" )
  126.                 else
  127.                         ServerLog( "AUTHING PLAYER: " ..ply:Nick().. " in group (" ..ply.group..")." )
  128.                         if ply.group != "user" then
  129.                                 ULib.ucl.addUser( ply:SteamID(), _, _, ply.group )
  130.                         end
  131.                 end
  132.        
  133.         end
  134.         ply.newauth = 1
  135.         Disconnect()
  136. end
  137. hook.Add( "PlayerAuthed", "G4PSQL_Auth", G4PSQL_Auth )
  138.  
  139. function G4PSQL_PlayerSpawn(ply)
  140.  
  141.         local sid = ply:SteamID()
  142.  
  143.         if ply.newauth == 3 then
  144.                 --ServerLog( "CHECKING PLAYER MODEL FOR PLAYER: " ..ply:Nick() )
  145.                 local model1 = string.Explode( "/", ply:GetModel() )
  146.                 local model2 = string.Explode( ".", model1[#model1] )          
  147.                 local playermodel = model2[1]
  148.                 --ServerLog( "CURRENT PLAYER MODEL: " ..playermodel )
  149.                 --ServerLog( "SAVED PLAYER MODEL: " ..ply.pmodel )
  150.                
  151.                 if playermodel != ply.pmodel then
  152.                
  153.                         ServerLog( "Updating model for player: " ..ply:Nick() )
  154.                         --print("UPDATE " .. table .. " SET pname="..Format( Escape( ply:Nick() ) ) .. " WHERE sid=" ..Format( Escape( sid ) ) )
  155.                         Connect()
  156.                         local result3 = DoQuery( "UPDATE " .. table .. " SET pmodel="..Format( Escape( playermodel ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
  157.                         Disconnect()
  158.                 end
  159.                
  160.                 ply.newauth = 0
  161.         end
  162.        
  163.         if ply.newauth == 2 then
  164.                 ply.newauth = 3
  165.         end
  166.        
  167.         if ply.newauth == 1 then
  168.                 ply.newauth = 2
  169.         end
  170.  
  171. end
  172. hook.Add( "PlayerSpawn", "G4PSQL_PlayerSpawn", G4PSQL_PlayerSpawn )
  173.  
  174.  
  175.  
  176. function G4PSQL_ChangeGroup( ply, cmd, args )
  177.         local sid = args[1]
  178.         local group = string.lower(args[2])
  179.        
  180.        
  181.         if !IsValid( ply ) or ply:IsAdmin() then
  182.  
  183.                
  184.                 if #args != 2 then
  185.                         if ply:IsPlayer() then
  186.                                 ULib.tsay(ply, "Not enough arguements!", true)
  187.                         else
  188.                                 ServerLog("Not enough arguements!")
  189.                         end
  190.                 end
  191.        
  192.                 Connect()
  193.                 local results = DoQuery( "SELECT * FROM " .. table .. " WHERE steamid = '" .. sid .. "'" )
  194.                 Disconnect()
  195.                
  196.                 if not results[1] then
  197.                         Connect()
  198.                         local result = DoQuery( "INSERT INTO " .. table .. " ( steamid, pname, pgroup, banned ) VALUES( " ..Format( Escape( sid ) ) .. ", " ..Format( Escape( "SET BY CONSOLE" ) ) .. ", " ..Format( Escape( group ) ) .. ", " ..Format( Escape( "FALSE" ) ).. " )" )
  199.                         Disconnect()
  200.                         if ply:IsPlayer() then
  201.                                 ULib.tsay(ply, "created player: (" ..sid.. ") with group (" ..group.. ")", true)
  202.                         end
  203.                         ServerLog("created player: (" ..sid.. ") with group (" ..group.. ")")
  204.                 else
  205.                         Connect()
  206.                         --print( "UPDATE " .. table .. " SET group="..Format( Escape( group ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
  207.                         local result = DoQuery( "UPDATE " .. table .. " SET pgroup="..Format( Escape( group ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
  208.                         Disconnect()
  209.                         if ply:IsPlayer() then
  210.                                 ULib.tsay(ply, "set player: (" ..sid.. ") to group (" ..group.. ")", true)
  211.                         end
  212.                         ServerLog("set player: (" ..sid.. ") to group (" ..group.. ")")
  213.                 end
  214.                 ULib.ucl.addUser( sid, _, _, group )
  215.                
  216.         else
  217.                 ULib.tsay(ply, "This command is reserved for administrators only!", true)
  218.         end
  219. end
  220. concommand.Add( "g4p_setgroup", G4PSQL_ChangeGroup)
  221.  
  222. function G4PSQL_Ban( ply, cmd, args )
  223.         local sid = args[1]
  224.         local reason = args[2]
  225.         local override = args[3] or "WAT?"
  226.        
  227.         local asid = "CONSOLE"
  228.        
  229.         if ply:IsPlayer() then
  230.                 asid = ply:SteamID()
  231.         end
  232.        
  233.        
  234.         if ULib.getUser(sid) then
  235.                 sid = ULib.getUser(sid):SteamID()
  236.         end
  237.        
  238.         if !IsValid( ply ) or ply:IsAdmin() or ply:IsUserGroup("moderator") then
  239.  
  240.                
  241.                 if !reason then
  242.                         ServerLog("Not enough arguements! g4p_gban <SteamID or Connected Player Name> <Reason> Both arguements need to be in quotes!")
  243.                 else
  244.        
  245.                         Connect()
  246.                         local results = DoQuery( "SELECT * FROM " .. table .. " WHERE steamid = '" .. sid .. "'" )
  247.                         Disconnect()
  248.                        
  249.                         if not results[1] then
  250.                                 if override == "1" then
  251.                                         Connect()
  252.                                         local noreply = DoQuery( "INSERT INTO " .. table .. " ( steamid, pname, pgroup, banned, adminid, reason, bantime ) VALUES( " ..Format( Escape( sid ) ) .. ", " ..Format( Escape( "SET BY CONSOLE" ) ) .. ", " ..Format( Escape( "user" ) ) .. ", " ..Format( Escape( "TRUE" ) ).. ", " ..Format( Escape( asid ) ).. ", " ..Format( Escape( reason ) ).. ", " ..Format( Escape( os.time() ) ).. " )" )
  253.                                         Disconnect()
  254.                                         if ply:IsPlayer() then
  255.                                                 ULib.tsay(ply, "Player [SET BY CONSOLE] was added to the global ban list for [" ..reason.. "] (Override)", true)
  256.                                         end
  257.                                         ServerLog("Player [SET BY CONSOLE] was added to the global ban list for [" ..reason.. "] (Override)")
  258.                                
  259.                                         ULib.tsay(_, "Player [SET BY CONSOLE] was added to the global ban list for [" ..reason.. "] (Override)", true)
  260.                                 else
  261.                                         if ply:IsPlayer() then
  262.                                                 ULib.tsay(ply, "Player with SteamID (" ..sid..") does not exist! To override this add \"OR\" to the end of the concommand.", true)
  263.                                         end
  264.                                         ServerLog("Player with SteamID (" ..sid..") does not exist! To override this add 1 to the end of the concommand.")
  265.                                 end
  266.                                
  267.                         else
  268.                                 Connect()
  269.                                 local result = DoQuery( "UPDATE " .. table .. " SET banned=" .. Format( Escape( "TRUE" ) ) .. ", adminid=" .. Format( Escape( asid ) ) .. ", reason=" ..Format( Escape( reason ) ).. ", bantime=" .. Format( Escape( os.time() ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
  270.                                 Disconnect()
  271.                                 if ply:IsPlayer() then
  272.                                         ULib.tsay(ply, "Player [" ..results[1][2].. "] was added to the global ban list for [" ..reason.. "]", true)
  273.                                 end
  274.                                 ServerLog("Player [" ..results[1][2].. "] was added to the global ban list for [" ..reason.. "]")
  275.                                
  276.                                 ULib.tsay(_, "Player [" ..results[1][2].. "] was added to the global ban list for [" ..reason.. "]", true)
  277.                                 for _, v in pairs( player.GetAll() ) do
  278.                                         if v:SteamID() == sid then
  279.                                                 ULib.kick( v, "BANNED for [" .. reason.. "]! Visit our website at www.g4p.org to dispute this ban." )
  280.                                         end
  281.                                 end
  282.                         end
  283.                 end
  284.                
  285.         else
  286.                 ULib.tsay(ply, "This command is reserved for administrators only!", true)
  287.         end
  288. end
  289. concommand.Add( "gban", G4PSQL_Ban)
  290.  
  291. function G4PSQL_UnBan( ply, cmd, args )
  292.         local sid = args[1]
  293.        
  294.         if !IsValid( ply ) or ply:IsAdmin() or ply:IsUserGroup("moderator") then
  295.  
  296.                
  297.                 if #args != 1 then
  298.                         if ply:IsPlayer() then
  299.                                 ULib.tsay(ply, "Not enough arguements!", true)
  300.                         else
  301.                                 ServerLog("Not enough arguements!")
  302.                         end
  303.                 end
  304.        
  305.                 Connect()
  306.                 local results = DoQuery( "SELECT * FROM " .. table .. " WHERE steamid = '" .. sid .. "'" )
  307.                 Disconnect()
  308.                
  309.                 if not results[1] then
  310.                         if ply:IsPlayer() then
  311.                                 ULib.tsay(ply, "Player with SteamID (" ..sid..") does not exist!", true)
  312.                                 ServerLog("Player with SteamID (" ..sid..") does not exist!")
  313.                         else
  314.                                 ServerLog("Player with SteamID (" ..sid..") does not exist!")
  315.                         end
  316.                 else
  317.                         Connect()
  318.                         local result = DoQuery( "UPDATE " .. table .. " SET banned="..Format( Escape( "FALSE" ) ) .. " WHERE steamid=" ..Format( Escape( sid ) ) )
  319.                         Disconnect()
  320.                         if ply:IsPlayer() then
  321.                                 ULib.tsay(ply, "Unbanned player [" ..results[1][2].. "]", true)
  322.                         end
  323.                         ServerLog("Unbanned player [" ..results[1][2].. "]")
  324.                         ULib.tsay(_, "Unbanned player [" ..results[1][2].. "]", true)
  325.                 end
  326.                
  327.                
  328.         else
  329.                 ULib.tsay(ply, "This command is reserved for administrators only!", true)
  330.         end
  331. end
  332. concommand.Add( "gunban", G4PSQL_UnBan)
  333.  

Offline coolwilliam

  • Newbie
  • *
  • Posts: 11
  • Karma: 0
Re: Global ULib
« Reply #2 on: May 17, 2011, 04:24:19 pm »
Sorry, But um i dont get that at all and yes i have gm_mysql plugin already because i use uban.
How about you add me on steam and we can talk easier?
heyholetsgo210
« Last Edit: May 17, 2011, 04:47:23 pm by coolwilliam »

  • Print