Advertisement
Guest User

Untitled

a guest
Feb 27th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. GM.Database = {}
  2.  
  3. GM.Database.address = "107.180.56.150"
  4. GM.Database.username = "bcbest"
  5. GM.Database.password = "bc7475bc"
  6. GM.Database.port = 3306
  7. GM.Database.database = "callofgmod"
  8. GM.Database.ReconnectTime = 30
  9. GM.Database.Queue = {}
  10.  
  11. local database_object
  12. local cfg = Gamemode.Config
  13. require("tmysql4")
  14.  
  15. function DatabasePrint(msg)
  16. if cfg.Debug then
  17. MsgN("[WarGame] Database: "..msg)
  18. end
  19. end
  20.  
  21. function GM.Database:connect(host, user, pass, db, port)
  22. local error
  23. database_object, error = tmysql.initialize( host, user, pass, db, port)
  24. if error then
  25. DatabasePrint("Error - "..error)
  26. DatabasePrint("Retrying connection in "..tostring(GM.Database.ReconnectTime).." seconds!")
  27.  
  28. timer.Simple(GM.Database.ReconnectTime, function()
  29. GM.Database:connect(host, user, pass, db, port)
  30. end)
  31. end
  32.  
  33. if database_object then
  34. print("\n[WarGame] Database: Connected Succesfully!\n")
  35. end
  36. end
  37.  
  38. function GM.Database:escape(str)
  39. -- Check if we have a connection?
  40. if (database_object) then
  41. return database_object:Escape(str)
  42. else
  43. return str
  44. end
  45. end
  46.  
  47. function Gamemodequery(query, callback)
  48. -- Check if we have a connection ;)
  49. if (not database_object) then
  50. GM.Database:connect(GM.Database.address, GM.Database.username, GM.Database.password, GM.Database.database, GM.Database.port)
  51. database_object:Query(query, function(cb)
  52. if (callback) then
  53. callback(cb[1].data)
  54. end
  55. end)
  56. end
  57.  
  58. -- Query
  59. return database_object:Query(query, function(cb)
  60. if (callback) then
  61. callback(cb[1].data)
  62. end
  63. end)
  64. end
  65.  
  66. GM.Database:connect(GM.Database.address, GM.Database.username, GM.Database.password, GM.Database.database, GM.Database.port)
  67.  
  68. Gamemodequery([[CREATE TABLE IF NOT EXISTS `players`
  69. (
  70. `id` INT AUTO_INCREMENT PRIMARY KEY,
  71. `uid` BIGINT(20),
  72. `name` TEXT,
  73. `level` INT,
  74. `xp` INT
  75. )
  76. COLLATE='latin1_swedish_ci'
  77. ENGINE=MyISAM;
  78. ]], function()
  79. DatabasePrint("Succesfully created table 'players'!")
  80. end)
  81.  
  82. Gamemodequery([[CREATE TABLE IF NOT EXISTS `sv_settings`
  83. (
  84. `gamemode` TEXT
  85. )
  86. COLLATE='latin1_swedish_ci'
  87. ENGINE=MyISAM;
  88. ]], function()
  89. DatabasePrint("Succesfully created table 'sv_settings'!")
  90. end)
  91.  
  92. Gamemodequery([[
  93. SELECT `gamemode` FROM `sv_settings`;
  94. ]], function(cb)
  95. if not cb then
  96. Gamemodequery([[
  97. INSERT INTO `sv_settings` (`gamemode`)
  98. VALUES ("tdm");
  99. ]])
  100. DatabasePrint("The gamemode was not set. The server set the gamemode to Team Death Match!")
  101. else
  102. DatabasePrint("The server has a gamemode. No need to rewrite it!")
  103. end
  104. end)
  105.  
  106. function GM:PlayerInitialSpawn(ply)
  107. local function CheckIfThere(tbl, val)
  108. for k, v in pairs(tbl) do
  109. if v.uid == val then
  110. return true
  111. end
  112. end
  113. return false
  114. end
  115. Gamemodequery([[
  116. SELECT `uid` FROM `players`;
  117. ]], function(cb)
  118. if not CheckIfThere(cb, ply:SteamID64()) then
  119. Gamemodequery([[
  120. INSERT INTO `players` (`uid`, `name`)
  121. VALUES ("]]..ply:SteamID64()..[[", "]]..ply:Nick()..[[");
  122. ]])
  123. DatabasePrint(ply:Nick().."'s' data was not found and we have just wrote it!")
  124. end
  125. DatabasePrint(ply:Nick().."'s' data was found!")
  126. end)
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement