Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.32 KB | None | 0 0
  1. ESX = nil
  2. local Jobs = {}
  3. local 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. amount = ESX.Math.Round(tonumber(amount))
  68.  
  69. if xPlayer.job.name ~= society.name then
  70. print(('esx_society: %s attempted to call withdrawMoney!'):format(xPlayer.identifier))
  71. return
  72. end
  73.  
  74. TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  75. if amount > 0 and account.money >= amount then
  76. account.removeMoney(amount)
  77. xPlayer.addMoney(amount)
  78.  
  79. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('have_withdrawn', ESX.Math.GroupDigits(amount)))
  80. else
  81. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  82. end
  83. end)
  84. end)
  85.  
  86. RegisterServerEvent('esx_society:depositMoney')
  87. AddEventHandler('esx_society:depositMoney', function(society, amount)
  88. local xPlayer = ESX.GetPlayerFromId(source)
  89. local society = GetSociety(society)
  90. amount = ESX.Math.Round(tonumber(amount))
  91.  
  92. if xPlayer.job.name ~= society.name then
  93. print(('esx_society: %s attempted to call depositMoney!'):format(xPlayer.identifier))
  94. return
  95. end
  96.  
  97. if amount > 0 and xPlayer.getMoney() >= amount then
  98. TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  99. xPlayer.removeMoney(amount)
  100. account.addMoney(amount)
  101. end)
  102.  
  103. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('have_deposited', ESX.Math.GroupDigits(amount)))
  104. else
  105. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  106. end
  107. end)
  108.  
  109. RegisterServerEvent('esx_society:washMoney')
  110. AddEventHandler('esx_society:washMoney', function(society, amount)
  111. local xPlayer = ESX.GetPlayerFromId(source)
  112. local account = xPlayer.getAccount('black_money')
  113. amount = ESX.Math.Round(tonumber(amount))
  114.  
  115. if xPlayer.job.name ~= society then
  116. print(('esx_society: %s attempted to call washMoney!'):format(xPlayer.identifier))
  117. return
  118. end
  119.  
  120. if amount and amount > 0 and account.money >= amount then
  121. local howlong = 5
  122. local xtime = 1 -- if 1 = then number above will be in SECONDS. if 60 = in MINUTES
  123. local minorsec = ''
  124. if xtime == 1 then
  125. minorsec = ' secondes'
  126. elseif xtime == 60 then
  127. minorsec = ' minutes'
  128. end
  129.  
  130. xPlayer.removeAccountMoney('black_money', amount)
  131. TriggerClientEvent('esx:showNotification', xPlayer.source, ('You have ~g~$' .. ESX.Math.GroupDigits(amount) .. '~s~ waiting in ~y~money laundering~s~ (' .. howlong .. minorsec .. ').'))
  132. Citizen.Wait(1000 * xtime * howlong)
  133. TriggerServerEvent('esx_society:depositMoney', 'police', amount)
  134. TriggerClientEvent('esx:showNotification', xPlayer.source, ('YAAY!! ~g~$' .. ESX.Math.GroupDigits(amount) .. '~s~ added to your ~y~society~s~'))
  135. else
  136. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  137. end
  138.  
  139. end)
  140.  
  141. RegisterServerEvent('esx_society:putVehicleInGarage')
  142. AddEventHandler('esx_society:putVehicleInGarage', function(societyName, vehicle)
  143. local society = GetSociety(societyName)
  144.  
  145. TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  146. local garage = store.get('garage') or {}
  147.  
  148. table.insert(garage, vehicle)
  149. store.set('garage', garage)
  150. end)
  151. end)
  152.  
  153. RegisterServerEvent('esx_society:removeVehicleFromGarage')
  154. AddEventHandler('esx_society:removeVehicleFromGarage', function(societyName, vehicle)
  155. local society = GetSociety(societyName)
  156.  
  157. TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  158. local garage = store.get('garage') or {}
  159.  
  160. for i=1, #garage, 1 do
  161. if garage[i].plate == vehicle.plate then
  162. table.remove(garage, i)
  163. break
  164. end
  165. end
  166.  
  167. store.set('garage', garage)
  168. end)
  169. end)
  170.  
  171. ESX.RegisterServerCallback('esx_society:getSocietyMoney', function(source, cb, societyName)
  172. local society = GetSociety(societyName)
  173.  
  174. if society then
  175. TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  176. cb(account.money)
  177. end)
  178. else
  179. cb(0)
  180. end
  181. end)
  182.  
  183. ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, society)
  184. if Config.EnableESXIdentity then
  185.  
  186. MySQL.Async.fetchAll('SELECT firstname, lastname, identifier, job, job_grade FROM users WHERE job = @job ORDER BY job_grade DESC', {
  187. ['@job'] = society
  188. }, function (results)
  189. local employees = {}
  190.  
  191. for i=1, #results, 1 do
  192. table.insert(employees, {
  193. name = results[i].firstname .. ' ' .. results[i].lastname,
  194. identifier = results[i].identifier,
  195. job = {
  196. name = results[i].job,
  197. label = Jobs[results[i].job].label,
  198. grade = results[i].job_grade,
  199. grade_name = Jobs[results[i].job].grades[tostring(results[i].job_grade)].name,
  200. grade_label = Jobs[results[i].job].grades[tostring(results[i].job_grade)].label
  201. }
  202. })
  203. end
  204.  
  205. cb(employees)
  206. end)
  207. else
  208. MySQL.Async.fetchAll('SELECT name, identifier, job, job_grade FROM users WHERE job = @job ORDER BY job_grade DESC', {
  209. ['@job'] = society
  210. }, function (result)
  211. local employees = {}
  212.  
  213. for i=1, #result, 1 do
  214. table.insert(employees, {
  215. name = result[i].name,
  216. identifier = result[i].identifier,
  217. job = {
  218. name = result[i].job,
  219. label = Jobs[result[i].job].label,
  220. grade = result[i].job_grade,
  221. grade_name = Jobs[result[i].job].grades[tostring(result[i].job_grade)].name,
  222. grade_label = Jobs[result[i].job].grades[tostring(result[i].job_grade)].label
  223. }
  224. })
  225. end
  226.  
  227. cb(employees)
  228. end)
  229. end
  230. end)
  231.  
  232. ESX.RegisterServerCallback('esx_society:getJob', function(source, cb, society)
  233. local job = json.decode(json.encode(Jobs[society]))
  234. local grades = {}
  235.  
  236. for k,v in pairs(job.grades) do
  237. table.insert(grades, v)
  238. end
  239.  
  240. table.sort(grades, function(a, b)
  241. return a.grade < b.grade
  242. end)
  243.  
  244. job.grades = grades
  245.  
  246. cb(job)
  247. end)
  248.  
  249.  
  250. ESX.RegisterServerCallback('esx_society:setJob', function(source, cb, identifier, job, grade, type)
  251. local xPlayer = ESX.GetPlayerFromId(source)
  252. local isBoss = xPlayer.job.grade_name == 'boss'
  253.  
  254. if isBoss then
  255. local xTarget = ESX.GetPlayerFromIdentifier(identifier)
  256.  
  257. if xTarget then
  258. xTarget.setJob(job, grade)
  259.  
  260. if type == 'hire' then
  261. TriggerClientEvent('esx:showNotification', xTarget.source, _U('you_have_been_hired', job))
  262. elseif type == 'promote' then
  263. TriggerClientEvent('esx:showNotification', xTarget.source, _U('you_have_been_promoted'))
  264. elseif type == 'fire' then
  265. TriggerClientEvent('esx:showNotification', xTarget.source, _U('you_have_been_fired', xTarget.getJob().label))
  266. end
  267.  
  268. cb()
  269. else
  270. MySQL.Async.execute('UPDATE users SET job = @job, job_grade = @job_grade WHERE identifier = @identifier', {
  271. ['@job'] = job,
  272. ['@job_grade'] = grade,
  273. ['@identifier'] = identifier
  274. }, function(rowsChanged)
  275. cb()
  276. end)
  277. end
  278. else
  279. print(('esx_society: %s attempted to setJob'):format(xPlayer.identifier))
  280. cb()
  281. end
  282. end)
  283.  
  284. ESX.RegisterServerCallback('esx_society:setJobSalary', function(source, cb, job, grade, salary)
  285. local isBoss = isPlayerBoss(source, job)
  286. local identifier = GetPlayerIdentifier(source, 0)
  287.  
  288. if isBoss then
  289. if salary <= Config.MaxSalary then
  290. MySQL.Async.execute('UPDATE job_grades SET salary = @salary WHERE job_name = @job_name AND grade = @grade', {
  291. ['@salary'] = salary,
  292. ['@job_name'] = job,
  293. ['@grade'] = grade
  294. }, function(rowsChanged)
  295. Jobs[job].grades[tostring(grade)].salary = salary
  296. local xPlayers = ESX.GetPlayers()
  297.  
  298. for i=1, #xPlayers, 1 do
  299. local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  300.  
  301. if xPlayer.job.name == job and xPlayer.job.grade == grade then
  302. xPlayer.setJob(job, grade)
  303. end
  304. end
  305.  
  306. cb()
  307. end)
  308. else
  309. print(('esx_society: %s attempted to setJobSalary over config limit!'):format(identifier))
  310. cb()
  311. end
  312. else
  313. print(('esx_society: %s attempted to setJobSalary'):format(identifier))
  314. cb()
  315. end
  316. end)
  317.  
  318. ESX.RegisterServerCallback('esx_society:getOnlinePlayers', function(source, cb)
  319. local xPlayers = ESX.GetPlayers()
  320. local players = {}
  321.  
  322. for i=1, #xPlayers, 1 do
  323. local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  324. table.insert(players, {
  325. source = xPlayer.source,
  326. identifier = xPlayer.identifier,
  327. name = xPlayer.name,
  328. job = xPlayer.job
  329. })
  330. end
  331.  
  332. cb(players)
  333. end)
  334.  
  335. ESX.RegisterServerCallback('esx_society:getVehiclesInGarage', function(source, cb, societyName)
  336. local society = GetSociety(societyName)
  337.  
  338. TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  339. local garage = store.get('garage') or {}
  340. cb(garage)
  341. end)
  342. end)
  343.  
  344. ESX.RegisterServerCallback('esx_society:isBoss', function(source, cb, job)
  345. cb(isPlayerBoss(source, job))
  346. end)
  347.  
  348. function isPlayerBoss(playerId, job)
  349. local xPlayer = ESX.GetPlayerFromId(playerId)
  350.  
  351. if xPlayer.job.name == job and xPlayer.job.grade_name == 'boss' then
  352. return true
  353. else
  354. print(('esx_society: %s attempted open a society boss menu!'):format(xPlayer.identifier))
  355. return false
  356. end
  357. end
  358.  
  359. function WashMoneyCRON(d, h, m)
  360. MySQL.Async.fetchAll('SELECT * FROM society_moneywash', {}, function(result)
  361. for i=1, #result, 1 do
  362. local society = GetSociety(result[i].society)
  363. local xPlayer = ESX.GetPlayerFromIdentifier(result[i].identifier)
  364.  
  365. -- add society money
  366. TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  367. account.addMoney(result[i].amount)
  368. end)
  369.  
  370. -- send notification if player is online
  371. if xPlayer then
  372. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_laundered', ESX.Math.GroupDigits(result[i].amount)))
  373. end
  374.  
  375. MySQL.Async.execute('DELETE FROM society_moneywash WHERE id = @id', {
  376. ['@id'] = result[i].id
  377. })
  378. end
  379. end)
  380. end
  381.  
  382. TriggerEvent('cron:runAt', 3, 0, WashMoneyCRON)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement