Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. --[[
  2. PointShop tMySQL provider by Jackson Cannon
  3. For use with PointShop-Donation: https://github.com/jackson-c/PointShop-Donation
  4.  
  5. Adapted from the tMySQL provider by Spencer Sharkey
  6.  
  7. tmysql4 and libmysql dlls must be installed for this to work.
  8.  
  9. Be sure to setup and connect to the donation site once before using this, to set up the data tables.
  10. Once configured, change PS.Config.DataProvider = 'pdata' to PS.Config.DataProvider = 'psdonation' in pointshop's sh_config.lua.
  11. ]]--
  12.  
  13. -- config
  14.  
  15. local mysql_hostname = '127.0.0.1' -- Your MySQL server address.
  16. local mysql_username = 'username' -- Your MySQL username.
  17. local mysql_password = 'password' -- Your MySQL password.
  18. local mysql_database = 'mydatabase' -- Your MySQL database.
  19. local mysql_port = 3306 -- Your MySQL port. Most likely is 3306.
  20.  
  21. -- see below for setting up donator ranks
  22.  
  23. require('tmysql4')
  24.  
  25. PROVIDER.Fallback = "pdata"
  26.  
  27. local db, err = tmysql.initialize(mysql_hostname, mysql_username, mysql_password, mysql_database, mysql_port)
  28.  
  29. if (err) then
  30. print("Error connecting to MySQL:")
  31. ErrorNoHalt(err)
  32. else
  33. function PROVIDER:GetData(ply, callback)
  34.  
  35. qs = string.format("SELECT * FROM `users` WHERE id64='%s'", ply:SteamID64())
  36. db:Query(qs, function(res)
  37. res=res[1]
  38. if (not res.status) then ErrorNoHalt("[PSMySQL-GetData] "..res.error) return end
  39. res=res.data
  40. if (#res < 1) then callback(0, {}) return end
  41. local row = res[1]
  42. ply.donatedAmount = row.donation_total or 0
  43. --[[
  44.  
  45. --Example setup of donator rank:
  46. --if player has donated $5 or more, set them to donator rank, otherwise set them to user rank
  47. --but we don't want to override admin ranks
  48.  
  49. if !ply:IsAdmin() then
  50. if ply.donatedAmount >= 500 then
  51.  
  52. ply:SetUserGroup("donator")
  53.  
  54. else
  55.  
  56. ply:SetUserGroup("user")
  57.  
  58. end
  59. end
  60.  
  61. ]]--
  62. print("Pointshop loaded: "..row.points.." points for "..ply:Nick())
  63. callback(row.points or 0, util.JSONToTable(row.items or '{}'))
  64. end, QUERY_FLAG_ASSOC)
  65. end
  66.  
  67. function PROVIDER:SetPoints(ply, points)
  68. local qs = string.format("INSERT INTO `users` (id64, points, items) VALUES ('%s', '%s', '[]') ON DUPLICATE KEY UPDATE points = VALUES(points)", ply:SteamID64(), points or 0)
  69. db:Query(qs, function(res)
  70. res=res[1]
  71. if (not res.status) then ErrorNoHalt("[PSMySQL-SetPoints] "..res.error) return end
  72. end, QUERY_FLAG_ASSOC)
  73. end
  74.  
  75. function PROVIDER:GivePoints(ply, points)
  76. local qs = string.format("INSERT INTO `users` (id64, points, items) VALUES ('%s', '%s', '[]') ON DUPLICATE KEY UPDATE points = points + VALUES(points)", ply:SteamID64(), points or 0)
  77. db:Query(qs, function(res, pass, err)
  78. res=res[1]
  79. if (not res.status) then ErrorNoHalt("[PSMySQL-GivePoints] "..res.error) return end
  80. end, QUERY_FLAG_ASSOC)
  81. end
  82.  
  83. function PROVIDER:TakePoints(ply, points)
  84. local qs = string.format("INSERT INTO `users` (id64, points, items) VALUES ('%s', '%s', '[]') ON DUPLICATE KEY UPDATE points = points - VALUES(points)", ply:SteamID64(), points or 0)
  85. db:Query(qs, function(res, pass, err)
  86. res=res[1]
  87. if (not res.status) then ErrorNoHalt("[PSMySQL-TakePoints] "..res.error) return end
  88. end, QUERY_FLAG_ASSOC)
  89. end
  90.  
  91. function PROVIDER:SaveItem(ply, item_id, data)
  92. self:GiveItem(ply, item_id, data)
  93. end
  94.  
  95. function PROVIDER:GiveItem(ply, item_id, data)
  96. local tmp = table.Copy(ply.PS_Items)
  97. tmp[item_id] = data
  98. local qs = string.format("INSERT INTO `users` (id64, points, items) VALUES ('%s', '0', '%s') ON DUPLICATE KEY UPDATE items = VALUES(items)", ply:SteamID64(), db:Escape(util.TableToJSON(tmp)))
  99. db:Query(qs, function(res, pass, err)
  100. res=res[1]
  101. if (not res.status) then ErrorNoHalt("[PSMySQL-GiveItem] "..res.error) return end
  102. end, QUERY_FLAG_ASSOC)
  103. end
  104.  
  105. function PROVIDER:TakeItem(ply, item_id)
  106. local tmp = table.Copy(ply.PS_Items)
  107. tmp[item_id] = nil
  108. local qs = string.format("INSERT INTO `users` (id64, points, items) VALUES ('%s', '0', '%s') ON DUPLICATE KEY UPDATE items = VALUES(items)", ply:SteamID64(), db:Escape(util.TableToJSON(tmp)))
  109. db:Query(qs, function(res, pass, err)
  110. res=res[1]
  111. if (not res.status) then ErrorNoHalt("[PSMySQL-TakeItem] "..res.error) return end
  112. end, QUERY_FLAG_ASSOC)
  113. end
  114.  
  115. function PROVIDER:SetData(ply, points, items)
  116. local qs = string.format("INSERT INTO `users` (id64, points, items) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE points = VALUES(points), items = VALUES(items)", ply:SteamID64(), points or 0, db:Escape(util.TableToJSON(items)))
  117. db:Query(qs, function(res, pass, err)
  118. res=res[1]
  119. if (not res.status) then ErrorNoHalt("[PSMySQL-SetData] "..res.error) return end
  120. end, QUERY_FLAG_ASSOC)
  121. end
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement