Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. ESX = nil
  2. local DataStoresIndex = {}
  3. local DataStores = {}
  4. local SharedDataStores = {}
  5.  
  6. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  7.  
  8. MySQL.ready(function()
  9. local result = MySQL.Sync.fetchAll('SELECT * FROM datastore')
  10.  
  11. for i=1, #result, 1 do
  12. local name = result[i].name
  13. local label = result[i].label
  14. local shared = result[i].shared
  15.  
  16. local result2 = MySQL.Sync.fetchAll('SELECT * FROM datastore_data WHERE name = @name', {
  17. ['@name'] = name
  18. })
  19.  
  20. if shared == 0 then
  21.  
  22. table.insert(DataStoresIndex, name)
  23. DataStores[name] = {}
  24.  
  25. for j=1, #result2, 1 do
  26. local storeName = result2[j].name
  27. local storeOwner = result2[j].owner
  28. local storeData = (result2[j].data == nil and {} or json.decode(result2[j].data))
  29. local dataStore = CreateDataStore(storeName, storeOwner, storeData)
  30.  
  31. table.insert(DataStores[name], dataStore)
  32. end
  33.  
  34. else
  35.  
  36. local data = nil
  37.  
  38. if #result2 == 0 then
  39. MySQL.Sync.execute('INSERT INTO datastore_data (name, owner, data) VALUES (@name, NULL, \'{}\')', {
  40. ['@name'] = name
  41. })
  42.  
  43. data = {}
  44. else
  45. data = json.decode(result2[1].data)
  46. end
  47.  
  48. local dataStore = CreateDataStore(name, nil, data)
  49. SharedDataStores[name] = dataStore
  50.  
  51. end
  52. end
  53. end)
  54.  
  55. function GetDataStore(name, owner)
  56. for i=1, #DataStores[name], 1 do
  57. if DataStores[name][i].owner == owner then
  58. return DataStores[name][i]
  59. end
  60. end
  61. end
  62.  
  63. function GetDataStoreOwners(name)
  64. local identifiers = {}
  65.  
  66. for i=1, #DataStores[name], 1 do
  67. table.insert(identifiers, DataStores[name][i].owner)
  68. end
  69.  
  70. return identifiers
  71. end
  72.  
  73. function GetSharedDataStore(name)
  74. return SharedDataStores[name]
  75. end
  76.  
  77. AddEventHandler('esx_datastore:getDataStore', function(name, owner, cb)
  78. cb(GetDataStore(name, owner))
  79. end)
  80.  
  81. AddEventHandler('esx_datastore:getDataStoreOwners', function(name, cb)
  82. cb(GetDataStoreOwners(name))
  83. end)
  84.  
  85. AddEventHandler('esx_datastore:getSharedDataStore', function(name, cb)
  86. cb(GetSharedDataStore(name))
  87. end)
  88.  
  89. --[[AddEventHandler('esx:playerLoaded', function(source)
  90. local _source = source
  91. local xPlayer = ESX.GetPlayerFromId(_source)
  92. local dataStores = {}
  93.  
  94. for i=1, #DataStoresIndex, 1 do
  95. local name = DataStoresIndex[i]
  96. local dataStore = GetDataStore(name, xPlayer.identifier)
  97.  
  98. if dataStore == nil then
  99. MySQL.Async.execute('INSERT INTO datastore_data (name, owner, data) VALUES (@name, @owner, @data)',
  100. {
  101. ['@name'] = name,
  102. ['@owner'] = xPlayer.identifier,
  103. ['@data'] = '{}'
  104. })
  105.  
  106. dataStore = CreateDataStore(name, xPlayer.identifier, {})
  107. table.insert(DataStores[name], dataStore)
  108. end
  109.  
  110. table.insert(dataStores, dataStore)
  111. end
  112.  
  113. xPlayer.set('dataStores', dataStores)
  114. end)--]]
  115.  
  116. --AddEventHandler('esx:playerLoaded', function(source)
  117. AddEventHandler('esx:playerLoaded', function(playerId, xPlayer)
  118.  
  119. local result = MySQL.Sync.fetchAll('SELECT * FROM datastore')
  120.  
  121. for i=1, #result, 1 do
  122. local name = result[i].name
  123. local label = result[i].label
  124. local shared = result[i].shared
  125.  
  126. local result2 = MySQL.Sync.fetchAll('SELECT * FROM datastore_data WHERE name = @name', {
  127. ['@name'] = name
  128. })
  129.  
  130. if shared == 0 then
  131.  
  132. table.insert(DataStoresIndex, name)
  133. DataStores[name] = {}
  134.  
  135. for j=1, #result2, 1 do
  136. local storeName = result2[j].name
  137. local storeOwner = result2[j].owner
  138. local storeData = (result2[j].data == nil and {} or json.decode(result2[j].data))
  139. local dataStore = CreateDataStore(storeName, storeOwner, storeData)
  140.  
  141. table.insert(DataStores[name], dataStore)
  142. end
  143. end
  144. end
  145.  
  146. local _source = source
  147. local xPlayer = ESX.GetPlayerFromId(_source)
  148. local dataStores = {}
  149.  
  150. for i=1, #DataStoresIndex, 1 do
  151. local name = DataStoresIndex[i]
  152. local dataStore = GetDataStore(name, xPlayer.identifier)
  153.  
  154. if dataStore == nil then
  155. MySQL.Async.execute('INSERT INTO datastore_data (name, owner, data) VALUES (@name, @owner, @data)',
  156. {
  157. ['@name'] = name,
  158. ['@owner'] = xPlayer.identifier,
  159. ['@data'] = '{}'
  160. })
  161.  
  162. dataStore = CreateDataStore(name, xPlayer.identifier, {})
  163. table.insert(DataStores[name], dataStore)
  164. end
  165.  
  166. table.insert(dataStores, dataStore)
  167. end
  168.  
  169. xPlayer.set('dataStores', dataStores)
  170. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement