Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.76 KB | None | 0 0
  1. ESX = nil
  2. Jobs = {}
  3. RegisteredSocieties = {}
  4.  
  5. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  6.  
  7. function GetSociety(name)
  8. for i=1, #RegisteredSocieties, 1 do
  9. if RegisteredSocieties[i].name == name then
  10. return RegisteredSocieties[i]
  11. end
  12. end
  13. end
  14.  
  15. MySQL.ready(function()
  16. local result = MySQL.Sync.fetchAll('SELECT * FROM jobs', {})
  17.  
  18. for i=1, #result, 1 do
  19. Jobs[result[i].name] = result[i]
  20. Jobs[result[i].name].grades = {}
  21. end
  22.  
  23. local result2 = MySQL.Sync.fetchAll('SELECT * FROM job_grades', {})
  24.  
  25. for i=1, #result2, 1 do
  26. Jobs[result2[i].job_name].grades[tostring(result2[i].grade)] = result2[i]
  27. end
  28. end)
  29.  
  30. AddEventHandler('esx_society:registerSociety', function(name, label, account, datastore, inventory, data)
  31. local found = false
  32.  
  33. local society = {
  34. name = name,
  35. label = label,
  36. account = account,
  37. datastore = datastore,
  38. inventory = inventory,
  39. data = data,
  40. }
  41.  
  42. for i=1, #RegisteredSocieties, 1 do
  43. if RegisteredSocieties[i].name == name then
  44. found = true
  45. RegisteredSocieties[i] = society
  46. break
  47. end
  48. end
  49.  
  50. if not found then
  51. table.insert(RegisteredSocieties, society)
  52. end
  53. end)
  54.  
  55. AddEventHandler('esx_society:getSocieties', function(cb)
  56. cb(RegisteredSocieties)
  57. end)
  58.  
  59. AddEventHandler('esx_society:getSociety', function(name, cb)
  60. cb(GetSociety(name))
  61. end)
  62.  
  63. RegisterServerEvent('esx_society:withdrawMoney')
  64. AddEventHandler('esx_society:withdrawMoney', function(society, amount)
  65. local xPlayer = ESX.GetPlayerFromId(source)
  66. local society = GetSociety(society)
  67.  
  68. TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  69. if amount > 0 and account.money >= amount then
  70. account.removeMoney(amount)
  71. xPlayer.addMoney(amount)
  72.  
  73. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('have_withdrawn', amount))
  74. else
  75. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  76. end
  77. end)
  78. end)
  79.  
  80. RegisterServerEvent('esx_society:depositMoney')
  81. AddEventHandler('esx_society:depositMoney', function(society, amount)
  82. local xPlayer = ESX.GetPlayerFromId(source)
  83. local society = GetSociety(society)
  84.  
  85. if amount > 0 and xPlayer.get('money') >= amount then
  86.  
  87. TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  88. xPlayer.removeMoney(amount)
  89. account.addMoney(amount)
  90. end)
  91.  
  92. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('have_deposited', amount))
  93.  
  94. else
  95. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  96. end
  97. end)
  98.  
  99. RegisterServerEvent('esx_society:washMoney')
  100. AddEventHandler('esx_society:washMoney', function(society, amount)
  101. local xPlayer = ESX.GetPlayerFromId(source)
  102. local account = xPlayer.getAccount('black_money')
  103. amount = tonumber(amount)
  104. if amount and amount > 0 and account.money >= amount then
  105.  
  106. xPlayer.removeAccountMoney('black_money', amount)
  107.  
  108. MySQL.Async.execute('INSERT INTO society_moneywash (identifier, society, amount) VALUES (@identifier, @society, @amount)',
  109. {
  110. ['@identifier'] = xPlayer.identifier,
  111. ['@society'] = society,
  112. ['@amount'] = amount
  113. }, function(rowsChanged)
  114. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have', amount))
  115. end)
  116.  
  117. else
  118. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  119. end
  120.  
  121. end)
  122.  
  123. RegisterServerEvent('esx_society:putVehicleInGarage')
  124. AddEventHandler('esx_society:putVehicleInGarage', function(societyName, vehicle)
  125. local society = GetSociety(societyName)
  126.  
  127. TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  128. local garage = store.get('garage') or {}
  129. table.insert(garage, vehicle)
  130. store.set('garage', garage)
  131. end)
  132. end)
  133.  
  134. RegisterServerEvent('esx_society:removeVehicleFromGarage')
  135. AddEventHandler('esx_society:removeVehicleFromGarage', function(societyName, vehicle)
  136. local society = GetSociety(societyName)
  137.  
  138. TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  139. local garage = store.get('garage') or {}
  140.  
  141. for i=1, #garage, 1 do
  142. if garage[i].plate == vehicle.plate then
  143. table.remove(garage, i)
  144. break
  145. end
  146. end
  147.  
  148. store.set('garage', garage)
  149. end)
  150. end)
  151.  
  152. ESX.RegisterServerCallback('esx_society:getSocietyMoney', function(source, cb, societyName)
  153. local society = GetSociety(societyName)
  154.  
  155. if society ~= nil then
  156.  
  157. TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  158. cb(account.money)
  159. end)
  160.  
  161. else
  162. cb(0)
  163. end
  164. end)
  165.  
  166. ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, society)
  167. if Config.EnableESXIdentity then
  168.  
  169. MySQL.Async.fetchAll('SELECT * FROM users WHERE job = @job ORDER BY job_grade DESC', {
  170. ['@job'] = society
  171. }, function (results)
  172. local employees = {}
  173.  
  174. for i=1, #results, 1 do
  175. table.insert(employees, {
  176. name = results[i].firstname .. ' ' .. results[i].lastname,
  177. identifier = results[i].identifier,
  178. job = {
  179. name = results[i].job,
  180. label = Jobs[results[i].job].label,
  181. grade = results[i].job_grade,
  182. grade_name = Jobs[results[i].job].grades[tostring(results[i].job_grade)].name,
  183. grade_label = Jobs[results[i].job].grades[tostring(results[i].job_grade)].label,
  184. }
  185. })
  186. end
  187.  
  188. cb(employees)
  189. end)
  190. else
  191. MySQL.Async.fetchAll('SELECT * FROM users WHERE job = @job ORDER BY job_grade DESC', {
  192. ['@job'] = society
  193. }, function (result)
  194. local employees = {}
  195.  
  196. for i=1, #result, 1 do
  197. table.insert(employees, {
  198. name = result[i].name,
  199. identifier = result[i].identifier,
  200. job = {
  201. name = result[i].job,
  202. label = Jobs[result[i].job].label,
  203. grade = result[i].job_grade,
  204. grade_name = Jobs[result[i].job].grades[tostring(result[i].job_grade)].name,
  205. grade_label = Jobs[result[i].job].grades[tostring(result[i].job_grade)].label,
  206. }
  207. })
  208. end
  209.  
  210. cb(employees)
  211. end)
  212. end
  213. end)
  214.  
  215. ESX.RegisterServerCallback('esx_society:getJob', function(source, cb, society)
  216. local job = json.decode(json.encode(Jobs[society]))
  217. local grades = {}
  218.  
  219. for k,v in pairs(job.grades) do
  220. table.insert(grades, v)
  221. end
  222.  
  223. table.sort(grades, function(a, b)
  224. return a.grade < b.grade
  225. end)
  226.  
  227. job.grades = grades
  228. cb(job)
  229. end)
  230.  
  231.  
  232. ESX.RegisterServerCallback('esx_society:setJob', function(source, cb, identifier, job, grade, type)
  233.  
  234. local xPlayer = ESX.GetPlayerFromId(source)
  235. local xTarget = ESX.GetPlayerFromIdentifier(identifier)
  236.  
  237. if xTarget ~= nil then
  238. xTarget.setJob(job, grade)
  239.  
  240. if type == 'hire' then
  241. TriggerClientEvent('esx:showNotification', xTarget.source, _U('you_have_been_hired', job))
  242. elseif type == 'promote' then
  243. TriggerClientEvent('esx:showNotification', xTarget.source, _U('you_have_been_promoted'))
  244. elseif type == 'fire' then
  245. TriggerClientEvent('esx:showNotification', xTarget.source, _U('you_have_been_fired', xTarget.getJob().label))
  246. end
  247.  
  248. cb()
  249. else
  250. MySQL.Async.execute('UPDATE users SET job = @job, job_grade = @job_grade WHERE identifier = @identifier',
  251. {
  252. ['@job'] = job,
  253. ['@job_grade'] = grade,
  254. ['@identifier'] = identifier
  255. }, function(rowsChanged)
  256. cb()
  257. end)
  258. end
  259.  
  260. end)
  261.  
  262. ESX.RegisterServerCallback('esx_society:setJobSalary', function(source, cb, job, grade, salary)
  263.  
  264. MySQL.Async.execute('UPDATE job_grades SET salary = @salary WHERE job_name = @job_name AND grade = @grade',
  265. {
  266. ['@salary'] = salary,
  267. ['@job_name'] = job,
  268. ['@grade'] = grade
  269. }, function(rowsChanged)
  270.  
  271. Jobs[job].grades[tostring(grade)].salary = salary
  272. local xPlayers = ESX.GetPlayers()
  273.  
  274. for i=1, #xPlayers, 1 do
  275.  
  276. local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  277. if xPlayer.job.name == job and xPlayer.job.grade == grade then
  278. xPlayer.setJob(job, grade)
  279. end
  280.  
  281. end
  282.  
  283. cb()
  284. end)
  285.  
  286. end)
  287.  
  288. ESX.RegisterServerCallback('esx_society:getOnlinePlayers', function(source, cb)
  289. local xPlayers = ESX.GetPlayers()
  290. local players = {}
  291.  
  292. for i=1, #xPlayers, 1 do
  293. local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  294. table.insert(players, {
  295. source = xPlayer.source,
  296. identifier = xPlayer.identifier,
  297. name = xPlayer.name,
  298. job = xPlayer.job
  299. })
  300. end
  301.  
  302. cb(players)
  303. end)
  304.  
  305. ESX.RegisterServerCallback('esx_society:getVehiclesInGarage', function(source, cb, societyName)
  306. local society = GetSociety(societyName)
  307. TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  308. local garage = store.get('garage') or {}
  309. cb(garage)
  310. end)
  311. end)
  312.  
  313. ESX.RegisterServerCallback('esx_society:isBoss', function(source, cb, jobName)
  314. local xPlayer = ESX.GetPlayerFromId(source)
  315.  
  316. if xPlayer.job.name == jobName and xPlayer.job.grade_name == 'boss' then
  317. cb(true)
  318. else
  319. print(('esx_society: %s attempted open a society boss menu!'):format(xPlayer.identifier))
  320. cb(false)
  321. end
  322. end)
  323.  
  324. function WashMoneyCRON(d, h, m)
  325. MySQL.Async.fetchAll('SELECT * FROM society_moneywash', {}, function(result)
  326. for i=1, #result, 1 do
  327.  
  328. -- add society money
  329. local society = GetSociety(result[i].society)
  330. TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  331. account.addMoney(result[i].amount)
  332. end)
  333.  
  334. -- send notification if player is online
  335. local xPlayer = ESX.GetPlayerFromIdentifier(result[i].identifier)
  336. if xPlayer ~= nil then
  337. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_laundered', result[i].amount))
  338. end
  339.  
  340. MySQL.Async.execute('DELETE FROM society_moneywash WHERE id = @id',
  341. {
  342. ['@id'] = result[i].id
  343. })
  344. end
  345. end)
  346. end
  347.  
  348. TriggerEvent('cron:runAt', 3, 0, WashMoneyCRON)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement