Advertisement
Guest User

Untitled

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