Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. --[[
  2. MYSQL
  3. ]]
  4.  
  5. require("mysqloo")
  6.  
  7. Breach = (Breach or {})
  8. Breach.MySQL = {}
  9.  
  10. BREACH_MYSQL_DB = (BREACH_MYSQL_DB or nil)
  11.  
  12. BREACH_MYSQL_HOST = "web-panel01.gmchosting.com"
  13. BREACH_MYSQL_USER = "empirese_misfit"
  14. BREACH_MYSQL_PASS = "cuine123"
  15. BREACH_MYSQL_DBNM = "empirese_breachlevels"
  16. BREACH_MYSQL_PORT = 3306
  17.  
  18. function Breach.MySQL:Connect()
  19. if (BREACH_MYSQL_DB && BREACH_MYSQL_DB:status() != mysqloo.DATABASE_NOT_CONNECTED) then
  20. return true
  21. end
  22.  
  23. local db = mysqloo.connect( BREACH_MYSQL_HOST, BREACH_MYSQL_USER, BREACH_MYSQL_PASS, BREACH_MYSQL_DBNM, BREACH_MYSQL_PORT )
  24.  
  25. function db:onConnected()
  26. MsgC( "[Breach MySQL] ", Color(255,255,255), "Database has connected!\n" )
  27.  
  28. BREACH_MYSQL_DB = self
  29. Breach.MySQL:CreateTables()
  30. end
  31.  
  32. function db:onConnectionFailed( err )
  33. MsgC( "[Breach MySQL] ", Color(255,255,255), "Connection to database failed!\n" )
  34. MsgC( "[Breach MySQL] ", Color(255,255,255), "Error:", err )
  35. end
  36.  
  37. db:connect()
  38. end
  39.  
  40. function Breach.MySQL:CreateTables()
  41. local q = BREACH_MYSQL_DB:query( "SELECT 1 FROM `breach_pdata` LIMIT 1;" )
  42.  
  43. function q:onSuccess( data )
  44. MsgC("[Breach MySQL] ", Color(255,255,255), "breach_pdata table exists!\n")
  45. MsgC("[Breach MySQL] ", Color(255,255,255), "Successfully loaded!\n")
  46. end
  47.  
  48. function q:onError( err, sql )
  49. local ctbl = BREACH_MYSQL_DB:query( [[
  50. CREATE TABLE `breach_pdata` (
  51. stid VARCHAR (255),
  52. level INT,
  53. exp INT,
  54. PRIMARY KEY (stid)
  55. )]] )
  56.  
  57. function ctbl:onSuccess( data )
  58. MsgC("[Breach MySQL] ", Color(255,255,255), "Created breach_pdata table!\n")
  59. end
  60.  
  61. function ctbl:onError( err, sql )
  62.  
  63. MsgC("[Breach MySQL] ", Color(255,255,255), "Failed to create breach_pdata table!\n")
  64. MsgC("[Breach MySQL] ", Color(255,255,255), "SQL: " .. sql .. "\n")
  65. MsgC("[Breach MySQL] ", Color(255,255,255), "Error: " .. err .. "\n")
  66. end
  67.  
  68. ctbl:start()
  69.  
  70. end
  71.  
  72. q:start()
  73. end
  74.  
  75. Breach.MySQL:Connect()
  76.  
  77. -- [[PLAYER INIT]]
  78. hook.Add("PlayerSpawn", "Breach.MySQL.PInit",
  79. function(ply)
  80. local checkply = BREACH_MYSQL_DB:query( "SELECT * FROM `breach_pdata` WHERE stid = '" .. ply:SteamID() .. "'")
  81.  
  82. function checkply:onSuccess( data )
  83. if (#data <= 0) then
  84. local cp = BREACH_MYSQL_DB:query( "INSERT INTO `breach_pdata` (stid, level, exp) VALUES ('" .. ply:SteamID() .. "','1','0')" )
  85.  
  86. function cp:onSuccess( data )
  87. MsgC("[Breach MySQL] ", Color(255,255,255), "Created " .. ply:Nick() .. "'s data!\n")
  88. end
  89.  
  90. function cp:onError( err, sql )
  91. print( "Query errored!" )
  92. print( "Query:", sql )
  93. print( "Error:", err )
  94. end
  95.  
  96. cp:start()
  97. end
  98.  
  99. ply:SetNLevel( data[1] && data[1].level || 1 )
  100. ply:SetPData( "breach_level", data[1] && data[1].level || 1 )
  101.  
  102. ply:SetNEXP( data[1] && data[1].exp || 0 )
  103. ply:SetPData( "breach_exp", data[1] && data[1].exp || 1 )
  104. end
  105.  
  106. function checkply:onError( err, sql )
  107. print( "Query errored!" )
  108. print( "Query:", sql )
  109. print( "Error:", err )
  110. end
  111.  
  112. checkply:start()
  113. end)
  114.  
  115. -- [[LEVEL HOOKS]]
  116. hook.Add("Breach_EXP_UPD", "Breach.EXP.Upd_MySQL",
  117. function(ply, amt)
  118. local updexp = BREACH_MYSQL_DB:query( "UPDATE `breach_pdata` SET exp = '" .. amt .. "' WHERE stid = '" .. ply:SteamID() .. "'" )
  119.  
  120. function updexp:onSuccess( data )
  121. MsgC("[Breach MySQL] ", Color(255,255,255), "Updated " .. ply:Nick() .. "'s EXP!\n")
  122. end
  123.  
  124. function updexp:onError( err, sql )
  125. print( "Query errored!" )
  126. print( "Query:", sql )
  127. print( "Error:", err )
  128. end
  129.  
  130. updexp:start()
  131. end)
  132.  
  133. hook.Add("Breach_LVL_UPD", "Breach.LVL.Upd_MySQL",
  134. function(ply, amt)
  135. local updlvl = BREACH_MYSQL_DB:query( "UPDATE `breach_pdata` SET level = '" .. amt .. "' WHERE stid = '" .. ply:SteamID() .. "'" )
  136.  
  137. function updlvl:onSuccess( data )
  138. MsgC("[Breach MySQL] ", Color(255,255,255), "Updated " .. ply:Nick() .. "'s level!\n")
  139. end
  140.  
  141. function updlvl:onError( err, sql )
  142. print( "Query errored!" )
  143. print( "Query:", sql )
  144. print( "Error:", err )
  145. end
  146.  
  147. updlvl:start()
  148. end)
  149.  
  150. timer.Create("Breach_MYSQL_INFO_UPDATE", 30, 0,
  151. function()
  152. local plys = player.GetAll()
  153.  
  154. for i = 1,#plys do
  155.  
  156. local updlvl = BREACH_MYSQL_DB:query( "UPDATE `breach_pdata` SET level = '" .. plys[i]:GetPData( "breach_level" ) ..
  157. "', exp = '" .. plys[i]:GetPData( "breach_exp" ) .. "' WHERE stid = '" .. ply:SteamID() .. "'" )
  158.  
  159. function updlvl:onSuccess( data )
  160. MsgC("[Breach MySQL] ", Color(255,255,255), "Updated " .. ply:Nick() .. "'s level!\n")
  161. end
  162.  
  163. function updlvl:onError( err, sql )
  164. print( "Query errored!" )
  165. print( "Query:", sql )
  166. print( "Error:", err )
  167. end
  168.  
  169. updlvl:start()
  170.  
  171. end
  172. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement