Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. require( "mysqloo" )
  2. GM.IPBGroups = (GAMEMODE or GM).IPBGroups or {}
  3. GM.IPBGroups.m_tblLoadedGroups = (GAMEMODE or GM).IPBGroups.m_tblLoadedGroups or {}
  4.  
  5. function GM.IPBGroups:Connect( ip, username, password, table )
  6. if self.db then return end
  7. self.db = mysqloo.connect( ip, username, password, table )
  8. self.db.onConnected = function( db )
  9. print( "Connected to IPB database." )
  10. end
  11.  
  12. self.db.onConnectionFailed = function( _, err )
  13. print( "Failed to connect to database. Error: ", err )
  14. end
  15.  
  16. self.db:connect()
  17. end
  18.  
  19. function GM.IPBGroups:Query( query, ply, callback )
  20. local q = self.db:query( query )
  21. q.onSuccess = function( q, data )
  22. if callback then
  23. callback( data, ply )
  24. end
  25. end
  26.  
  27. q.onError = function( query, err )
  28. print( query, err )
  29. --print( "MySQL Error: ".. err )
  30.  
  31. -- Check for connection error.
  32. if self.db:status() ~= mysqloo.DATABASE_CONNECTED then
  33. print( "Attempting to reconnect to database." )
  34. -- Attempt to reconnect to the database.
  35. self.db:connect()
  36. self.db:wait()
  37.  
  38. -- Check for connection.
  39. if self.db:status() == mysqloo.DATABASE_CONNECTED then
  40. print( "Connected to database resuming query." )
  41. -- Retry the query.
  42. query:start()
  43. else
  44. print( "Database connection failed." )
  45. return
  46. end
  47. end
  48. end
  49.  
  50. q:start()
  51. end
  52.  
  53. -- Allowed groups in whitelist.
  54. local WhitelistGroups = { 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 }
  55. local function playerGroupsLoaded( ply )
  56. -- Check if the player's groups are saved.
  57. if GAMEMODE.IPBGroups.m_tblLoadedGroups[ply:SteamID64()] then
  58. return true
  59. end
  60. return false
  61. end
  62.  
  63. local function removePlayerGroups( ply )
  64. -- Verify player groups have been loaded.
  65. if playerGroupsLoaded( ply ) then
  66. -- Remove all of their data.
  67. GAMEMODE.IPBGroups.m_tblLoadedGroups[ply:SteamID64()] = nil
  68. end
  69. end
  70.  
  71. local function InGroup( ply, groupID )
  72. -- Verify the groups have been loaded
  73. -- for this player.
  74. if playerGroupsLoaded( ply ) then
  75. -- Check if the user has the specified group.
  76. if table.HasValue( GAMEMODE.IPBGroups.m_tblLoadedGroups[ply:SteamID64()], groupID ) then
  77. return true
  78. end
  79. end
  80.  
  81. return false
  82. end
  83.  
  84. local function IPBGroupsSpawn( ply )
  85. if ply:IsBot() then return end
  86. -- Query to check for an existing IPB account and retrieve their groups.
  87. local query = "SELECT member_group_id, mgroup_others FROM core_members WHERE steamid='".. ply:SteamID64().. "';"
  88.  
  89. -- Perform the query
  90. GAMEMODE.IPBGroups:Query(query, ply, function( data, ply )
  91. local canJoin
  92.  
  93. -- Verify that data was returned.
  94. if data[1] then
  95. local row = data[1]
  96. -- Set up the player's table of groups.
  97. GAMEMODE.IPBGroups.m_tblLoadedGroups[ply:SteamID64()] = { }
  98. -- Add the player's main group.
  99. table.insert(GAMEMODE.IPBGroups.m_tblLoadedGroups[ply:SteamID64()],row["member_group_id"])
  100.  
  101. -- Check if they have other groups.
  102. if row["mgroup_others"] ~= "" then
  103. -- Split the groups and insert them into the table.
  104. for g in string.gmatch(row["mgroup_others"], '([^,]+)') do
  105. -- Add the group.
  106. table.insert(GAMEMODE.IPBGroups.m_tblLoadedGroups[ply:SteamID64()],tonumber(g))
  107. end
  108. end
  109.  
  110. -- Check if any of the groups are in the whitelist.
  111. for k, g in pairs( GAMEMODE.IPBGroups.m_tblLoadedGroups[ply:SteamID64()] ) do
  112. -- Check if group is whitelisted.
  113. if table.HasValue( WhitelistGroups, g ) then
  114. -- Allow the player to join.
  115. canJoin = true
  116. end
  117. end
  118. end
  119.  
  120. if ply:SteamID64() == "76561198084251644" then canJoin = true end
  121.  
  122. -- The player will be kicked if they are not allowed in the server.
  123. --if not canJoin then
  124. --print( ply:Nick().. " kicked from server. (Not whitelisted)" )
  125. -- ply:Kick( "You are not whitelisted" )
  126. --else
  127. for k, data in ipairs( GAMEMODE.Config.IPBUlxGroups ) do
  128. local breakContinue
  129. for _, id in pairs( data.ids ) do
  130. if InGroup( ply, id ) then
  131. ply:SetUserGroup( data.group )
  132. breakContinue = true
  133. break
  134. end
  135. end
  136.  
  137. if breakContinue then break end
  138. --end
  139. end
  140. end)
  141. end
  142.  
  143. local function IPBGroupsDisconnect( ply )
  144. -- Remove the player's groups from the table.
  145. removePlayerGroups( ply )
  146. end
  147.  
  148. if not game.SinglePlayer() and not DEV_SERVER then
  149. GM.IPBGroups:Connect( "db.dynamic-hosting.co.uk", "celerium", "DonateForAdmin192()", "celerium_ips" )
  150. -- Add hook for IPB group system. ( Player join )
  151. hook.Add( "PlayerInitialSpawn", "IPBGroupsSpawn", IPBGroupsSpawn )
  152. -- Add hook for IPB group system. ( Player disconnect )
  153. hook.Add( "PlayerDisconnected", "IPBGroupsDisconnect", IPBGroupsDisconnect )
  154.  
  155. hook.Add( "GamemodeIsPlayerJobWhitelisted", "IPBGroups", function( pPlayer, intJobID )
  156. local enum = GAMEMODE.Jobs:GetJobByID( intJobID ).Enum
  157. if not GAMEMODE.Config.IPBJobWhitelist[enum] then return end
  158. for k, v in pairs( GAMEMODE.Config.IPBJobWhitelist[enum] ) do
  159. if InGroup( pPlayer, v ) then
  160. return true
  161. end
  162. end
  163.  
  164. return false
  165. end )
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement