SHARE
TWEET

Untitled

a guest Dec 11th, 2019 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         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 amount and amount > 0 and account.money >= amount then
  114.         local howlong   = 5
  115.         local xtime     = 1 -- if 1 = then number above will be in SECONDS. if 60 = in MINUTES
  116.         local minorsec  = ''
  117.         if xtime == 1 then
  118.          minorsec = ' secondes'
  119.         elseif xtime == 60 then
  120.         minorsec = ' minutes'
  121.         end
  122.        
  123.         xPlayer.removeAccountMoney('black_money', amount)
  124.         TriggerClientEvent('esx:showNotification', xPlayer.source, ('You have ~g~$' .. ESX.Math.GroupDigits(amount) .. '~s~ waiting in ~y~money laundering~s~ (' .. howlong .. minorsec .. ').'))
  125.         Citizen.Wait(1000 * xtime * howlong)
  126.         xPlayer.addMoney(amount)
  127.         TriggerClientEvent('esx:showNotification', xPlayer.source, ('YAAY!! ~g~$' .. ESX.Math.GroupDigits(amount) .. '~s~ added to your ~y~society~s~'))
  128.         else
  129.             xPlayer.showNotification(_U('invalid_amount'))
  130.         end
  131.         print(('esx_society: %s attempted to call washMoney!'):format(xPlayer.identifier))
  132. end)
  133.  
  134. RegisterServerEvent('esx_society:putVehicleInGarage')
  135. AddEventHandler('esx_society:putVehicleInGarage', 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.         table.insert(garage, vehicle)
  142.         store.set('garage', garage)
  143.     end)
  144. end)
  145.  
  146. RegisterServerEvent('esx_society:removeVehicleFromGarage')
  147. AddEventHandler('esx_society:removeVehicleFromGarage', function(societyName, vehicle)
  148.     local society = GetSociety(societyName)
  149.  
  150.     TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  151.         local garage = store.get('garage') or {}
  152.  
  153.         for i=1, #garage, 1 do
  154.             if garage[i].plate == vehicle.plate then
  155.                 table.remove(garage, i)
  156.                 break
  157.             end
  158.         end
  159.  
  160.         store.set('garage', garage)
  161.     end)
  162. end)
  163.  
  164. ESX.RegisterServerCallback('esx_society:getSocietyMoney', function(source, cb, societyName)
  165.     local society = GetSociety(societyName)
  166.  
  167.     if society then
  168.         TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  169.             cb(account.money)
  170.         end)
  171.     else
  172.         cb(0)
  173.     end
  174. end)
  175.  
  176. ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, society)
  177.     if Config.EnableESXIdentity then
  178.  
  179.         MySQL.Async.fetchAll('SELECT firstname, lastname, identifier, job, job_grade FROM users WHERE job = @job ORDER BY job_grade DESC', {
  180.             ['@job'] = society
  181.         }, function (results)
  182.             local employees = {}
  183.  
  184.             for i=1, #results, 1 do
  185.                 table.insert(employees, {
  186.                     name       = results[i].firstname .. ' ' .. results[i].lastname,
  187.                     identifier = results[i].identifier,
  188.                     job = {
  189.                         name        = results[i].job,
  190.                         label       = Jobs[results[i].job].label,
  191.                         grade       = results[i].job_grade,
  192.                         grade_name  = Jobs[results[i].job].grades[tostring(results[i].job_grade)].name,
  193.                         grade_label = Jobs[results[i].job].grades[tostring(results[i].job_grade)].label
  194.                     }
  195.                 })
  196.             end
  197.  
  198.             cb(employees)
  199.         end)
  200.     else
  201.         MySQL.Async.fetchAll('SELECT name, identifier, job, job_grade FROM users WHERE job = @job ORDER BY job_grade DESC', {
  202.             ['@job'] = society
  203.         }, function (result)
  204.             local employees = {}
  205.  
  206.             for i=1, #result, 1 do
  207.                 table.insert(employees, {
  208.                     name       = result[i].name,
  209.                     identifier = result[i].identifier,
  210.                     job = {
  211.                         name        = result[i].job,
  212.                         label       = Jobs[result[i].job].label,
  213.                         grade       = result[i].job_grade,
  214.                         grade_name  = Jobs[result[i].job].grades[tostring(result[i].job_grade)].name,
  215.                         grade_label = Jobs[result[i].job].grades[tostring(result[i].job_grade)].label
  216.                     }
  217.                 })
  218.             end
  219.  
  220.             cb(employees)
  221.         end)
  222.     end
  223. end)
  224.  
  225. ESX.RegisterServerCallback('esx_society:getJob', function(source, cb, society)
  226.     local job    = json.decode(json.encode(Jobs[society]))
  227.     local grades = {}
  228.  
  229.     for k,v in pairs(job.grades) do
  230.         table.insert(grades, v)
  231.     end
  232.  
  233.     table.sort(grades, function(a, b)
  234.         return a.grade < b.grade
  235.     end)
  236.  
  237.     job.grades = grades
  238.  
  239.     cb(job)
  240. end)
  241.  
  242. ESX.RegisterServerCallback('esx_society:setJob', function(source, cb, identifier, job, grade, type)
  243.     local xPlayer = ESX.GetPlayerFromId(source)
  244.     local isBoss = xPlayer.job.grade_name == 'boss'
  245.  
  246.     if isBoss then
  247.         local xTarget = ESX.GetPlayerFromIdentifier(identifier)
  248.  
  249.         if xTarget then
  250.             xTarget.setJob(job, grade)
  251.  
  252.             if type == 'hire' then
  253.                 xTarget.showNotification(_U('you_have_been_hired', job))
  254.             elseif type == 'promote' then
  255.                 xTarget.showNotification(_U('you_have_been_promoted'))
  256.             elseif type == 'fire' then
  257.                 xTarget.showNotification(_U('you_have_been_fired', xTarget.getJob().label))
  258.             end
  259.  
  260.             cb()
  261.         else
  262.             MySQL.Async.execute('UPDATE users SET job = @job, job_grade = @job_grade WHERE identifier = @identifier', {
  263.                 ['@job']        = job,
  264.                 ['@job_grade']  = grade,
  265.                 ['@identifier'] = identifier
  266.             }, function(rowsChanged)
  267.                 cb()
  268.             end)
  269.         end
  270.     else
  271.         print(('esx_society: %s attempted to setJob'):format(xPlayer.identifier))
  272.         cb()
  273.     end
  274. end)
  275.  
  276. ESX.RegisterServerCallback('esx_society:setJobSalary', function(source, cb, job, grade, salary)
  277.     local isBoss = isPlayerBoss(source, job)
  278.     local identifier = GetPlayerIdentifier(source, 0)
  279.  
  280.     if isBoss then
  281.         if salary <= Config.MaxSalary then
  282.             MySQL.Async.execute('UPDATE job_grades SET salary = @salary WHERE job_name = @job_name AND grade = @grade', {
  283.                 ['@salary']   = salary,
  284.                 ['@job_name'] = job,
  285.                 ['@grade']    = grade
  286.             }, function(rowsChanged)
  287.                 Jobs[job].grades[tostring(grade)].salary = salary
  288.                 local xPlayers = ESX.GetPlayers()
  289.  
  290.                 for i=1, #xPlayers, 1 do
  291.                     local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  292.  
  293.                     if xPlayer.job.name == job and xPlayer.job.grade == grade then
  294.                         xPlayer.setJob(job, grade)
  295.                     end
  296.                 end
  297.  
  298.                 cb()
  299.             end)
  300.         else
  301.             print(('esx_society: %s attempted to setJobSalary over config limit!'):format(identifier))
  302.             cb()
  303.         end
  304.     else
  305.         print(('esx_society: %s attempted to setJobSalary'):format(identifier))
  306.         cb()
  307.     end
  308. end)
  309.  
  310. ESX.RegisterServerCallback('esx_society:getOnlinePlayers', function(source, cb)
  311.     local xPlayers = ESX.GetPlayers()
  312.     local players  = {}
  313.  
  314.     for i=1, #xPlayers, 1 do
  315.         local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  316.         table.insert(players, {
  317.             source     = xPlayer.source,
  318.             identifier = xPlayer.identifier,
  319.             name       = xPlayer.name,
  320.             job        = xPlayer.job
  321.         })
  322.     end
  323.  
  324.     cb(players)
  325. end)
  326.  
  327. ESX.RegisterServerCallback('esx_society:getVehiclesInGarage', function(source, cb, societyName)
  328.     local society = GetSociety(societyName)
  329.  
  330.     TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  331.         local garage = store.get('garage') or {}
  332.         cb(garage)
  333.     end)
  334. end)
  335.  
  336. ESX.RegisterServerCallback('esx_society:isBoss', function(source, cb, job)
  337.     cb(isPlayerBoss(source, job))
  338. end)
  339.  
  340. function isPlayerBoss(playerId, job)
  341.     local xPlayer = ESX.GetPlayerFromId(playerId)
  342.  
  343.     if xPlayer.job.name == job and xPlayer.job.grade_name == 'boss' then
  344.         return true
  345.     else
  346.         print(('esx_society: %s attempted open a society boss menu!'):format(xPlayer.identifier))
  347.         return false
  348.     end
  349. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top